Object-apply-02.js (2025B)
1 // tests calling native functions via Debugger.Object.prototype.apply/call 2 3 load(libdir + "asserts.js"); 4 5 var g = newGlobal({newCompartment: true}); 6 g.eval("function f() { debugger; }"); 7 var dbg = new Debugger(g); 8 9 function test(usingApply) { 10 dbg.onDebuggerStatement = function (frame) { 11 var max = frame.arguments[0]; 12 var cv = usingApply ? max.apply(null, [9, 16]) : max.call(null, 9, 16); 13 assertEq(cv.return, 16); 14 15 cv = usingApply ? max.apply() : max.call(); 16 assertEq(cv.return, -1/0); 17 18 cv = usingApply ? max.apply(null, [2, 5, 3, 8, 1, 9, 4, 6, 7]) 19 : max.call(null, 2, 5, 3, 8, 1, 9, 4, 6, 7); 20 assertEq(cv.return, 9); 21 22 // second argument to apply must be an array 23 assertThrowsInstanceOf(function () { max.apply(null, 12); }, TypeError); 24 }; 25 g.eval("f(Math.max);"); 26 27 dbg.onDebuggerStatement = function (frame) { 28 var push = frame.arguments[0]; 29 var arr = frame.arguments[1]; 30 var cv; 31 32 cv = usingApply ? push.apply(arr, [0, 1, 2]) : push.call(arr, 0, 1, 2); 33 assertEq(cv.return, 3); 34 35 cv = usingApply ? push.apply(arr, [arr]) : push.call(arr, arr); 36 assertEq(cv.return, 4); 37 38 cv = usingApply ? push.apply(arr) : push.call(arr); 39 assertEq(cv.return, 4); 40 41 // You can apply Array.prototype.push to a string; it does ToObject on 42 // it. But as the length property on String objects is non-writable, 43 // attempting to increase the length will throw a TypeError. 44 cv = usingApply 45 ? push.apply("hello", ["world"]) 46 : push.call("hello", "world"); 47 assertEq("throw" in cv, true); 48 var ex = cv.throw; 49 assertEq(frame.evalWithBindings("ex instanceof TypeError", { ex: ex }).return, true); 50 }; 51 g.eval("var a = []; f(Array.prototype.push, a);"); 52 assertEq(g.a.length, 4); 53 assertEq(g.a.slice(0, 3).join(","), "0,1,2"); 54 assertEq(g.a[3], g.a); 55 } 56 57 test(true); 58 test(false);