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 script 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 | var hits = 0; |
michael@0 | 10 | function test(usingApply) { |
michael@0 | 11 | dbg.onDebuggerStatement = function (frame) { |
michael@0 | 12 | var fn = frame.arguments[0]; |
michael@0 | 13 | var cv = usingApply ? fn.apply(null, [9, 16]) : fn.call(null, 9, 16); |
michael@0 | 14 | assertEq(Object.keys(cv).join(","), "return"); |
michael@0 | 15 | assertEq(Object.getPrototypeOf(cv), Object.prototype); |
michael@0 | 16 | assertEq(cv.return, 25); |
michael@0 | 17 | |
michael@0 | 18 | cv = usingApply ? fn.apply(null, ["hello ", "world"]) : fn.call(null, "hello ", "world"); |
michael@0 | 19 | assertEq(Object.keys(cv).join(","), "return"); |
michael@0 | 20 | assertEq(cv.return, "hello world"); |
michael@0 | 21 | |
michael@0 | 22 | // Handle more or less arguments. |
michael@0 | 23 | assertEq((usingApply ? fn.apply(null, [1, 5, 100]) : fn.call(null, 1, 5, 100)).return, 6); |
michael@0 | 24 | assertEq((usingApply ? fn.apply(null, []) : fn.call(null)).return, NaN); |
michael@0 | 25 | assertEq((usingApply ? fn.apply() : fn.call()).return, NaN); |
michael@0 | 26 | |
michael@0 | 27 | // Throw if a this-value or argument is an object but not a Debugger.Object. |
michael@0 | 28 | assertThrowsInstanceOf(function () { usingApply ? fn.apply({}, []) : fn.call({}); }, |
michael@0 | 29 | TypeError); |
michael@0 | 30 | assertThrowsInstanceOf(function () { usingApply ? fn.apply(null, [{}]) : fn.call(null, {}); }, |
michael@0 | 31 | TypeError); |
michael@0 | 32 | hits++; |
michael@0 | 33 | }; |
michael@0 | 34 | g.eval("f(function (a, b) { return a + b; });"); |
michael@0 | 35 | |
michael@0 | 36 | // The callee receives the right arguments even if more arguments are provided |
michael@0 | 37 | // than the callee's .length. |
michael@0 | 38 | dbg.onDebuggerStatement = function (frame) { |
michael@0 | 39 | assertEq((usingApply ? frame.arguments[0].apply(null, ['one', 'two']) |
michael@0 | 40 | : frame.arguments[0].call(null, 'one', 'two')).return, |
michael@0 | 41 | 2); |
michael@0 | 42 | hits++; |
michael@0 | 43 | }; |
michael@0 | 44 | g.eval("f(function () { return arguments.length; });"); |
michael@0 | 45 | |
michael@0 | 46 | // Exceptions are reported as {throw:} completion values. |
michael@0 | 47 | dbg.onDebuggerStatement = function (frame) { |
michael@0 | 48 | var lose = frame.arguments[0]; |
michael@0 | 49 | var cv = usingApply ? lose.apply(null, []) : lose.call(null); |
michael@0 | 50 | assertEq(Object.keys(cv).join(","), "throw"); |
michael@0 | 51 | assertEq(cv.throw, frame.callee); |
michael@0 | 52 | hits++; |
michael@0 | 53 | }; |
michael@0 | 54 | g.eval("f(function lose() { throw f; });"); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | test(true); |
michael@0 | 58 | test(false); |
michael@0 | 59 | assertEq(hits, 6); |