tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Object-apply-01.js (2385B)


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