michael@0: // tests calling native functions via Debugger.Object.prototype.apply/call michael@0: michael@0: load(libdir + "asserts.js"); michael@0: michael@0: var g = newGlobal(); michael@0: g.eval("function f() { debugger; }"); michael@0: var dbg = new Debugger(g); michael@0: michael@0: function test(usingApply) { michael@0: dbg.onDebuggerStatement = function (frame) { michael@0: var max = frame.arguments[0]; michael@0: var cv = usingApply ? max.apply(null, [9, 16]) : max.call(null, 9, 16); michael@0: assertEq(cv.return, 16); michael@0: michael@0: cv = usingApply ? max.apply() : max.call(); michael@0: assertEq(cv.return, -1/0); michael@0: michael@0: cv = usingApply ? max.apply(null, [2, 5, 3, 8, 1, 9, 4, 6, 7]) michael@0: : max.call(null, 2, 5, 3, 8, 1, 9, 4, 6, 7); michael@0: assertEq(cv.return, 9); michael@0: michael@0: // second argument to apply must be an array michael@0: assertThrowsInstanceOf(function () { max.apply(null, 12); }, TypeError); michael@0: }; michael@0: g.eval("f(Math.max);"); michael@0: michael@0: dbg.onDebuggerStatement = function (frame) { michael@0: var push = frame.arguments[0]; michael@0: var arr = frame.arguments[1]; michael@0: var cv; michael@0: michael@0: cv = usingApply ? push.apply(arr, [0, 1, 2]) : push.call(arr, 0, 1, 2); michael@0: assertEq(cv.return, 3); michael@0: michael@0: cv = usingApply ? push.apply(arr, [arr]) : push.call(arr, arr); michael@0: assertEq(cv.return, 4); michael@0: michael@0: cv = usingApply ? push.apply(arr) : push.call(arr); michael@0: assertEq(cv.return, 4); michael@0: michael@0: // You can apply Array.prototype.push to a string; it does ToObject on michael@0: // it. But as the length property on String objects is non-writable, michael@0: // attempting to increase the length will throw a TypeError. michael@0: cv = usingApply michael@0: ? push.apply("hello", ["world"]) michael@0: : push.call("hello", "world"); michael@0: assertEq("throw" in cv, true); michael@0: var ex = cv.throw; michael@0: assertEq(frame.evalWithBindings("ex instanceof TypeError", { ex: ex }).return, true); michael@0: }; michael@0: g.eval("var a = []; f(Array.prototype.push, a);"); michael@0: assertEq(g.a.length, 4); michael@0: assertEq(g.a.slice(0, 3).join(","), "0,1,2"); michael@0: assertEq(g.a[3], g.a); michael@0: } michael@0: michael@0: test(true); michael@0: test(false);