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