tor-browser

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

debugger-frames.js (2530B)


      1 // Test debugger access to frames and environments work as expected inside a module.
      2 
      3 load(libdir + "asserts.js");
      4 
      5 function assertArrayEq(actual, expected)
      6 {
      7    var eq = actual.length == expected.length;
      8    if (eq) {
      9        for (var i = 0; i < actual.length; i++) {
     10            if (actual[i] !== expected[i]) {
     11                eq = false;
     12                break;
     13            }
     14        }
     15    }
     16    if (!eq) {
     17        print("Assertion failed: got " + JSON.stringify(actual) +
     18              ", expected " + JSON.stringify(expected));
     19        quit(3);
     20    }
     21 }
     22 
     23 var g2 = newGlobal({newCompartment: true});
     24 
     25 var dbg = Debugger(g2);
     26 dbg.onDebuggerStatement = function (frame) {
     27    // The current frame is a module frame.
     28    assertEq(frame.type, 'module');
     29    assertEq(frame.this, undefined);
     30 
     31    // The frame's environement is a module environment.
     32    let env = frame.environment;
     33    assertEq(env.type, 'declarative');
     34    assertEq(env.calleeScript, null);
     35 
     36    // Top level module definitions and imports are visible.
     37    assertArrayEq(env.names().sort(), ['a', 'b', 'c', 'x', 'y', 'z']);
     38    assertArrayEq(['a', 'b', 'c'].map(env.getVariable, env), [1, 2, 3]);
     39    assertArrayEq(['x', 'y', 'z'].map(env.getVariable, env), [4, 5, 6]);
     40 
     41    // Cannot set imports or const bindings.
     42    assertThrowsInstanceOf(() => env.setVariable('a', 10), TypeError);
     43    assertThrowsInstanceOf(() => env.setVariable('b', 11), TypeError);
     44    assertThrowsInstanceOf(() => env.setVariable('c', 12), TypeError);
     45    env.setVariable('x', 7);
     46    env.setVariable('y', 8);
     47    assertThrowsInstanceOf(() => env.setVariable('z', 9), TypeError);
     48    assertArrayEq(['a', 'b', 'c'].map(env.getVariable, env), [1, 2, 3]);
     49    assertArrayEq(['x', 'y', 'z'].map(env.getVariable, env), [7, 8, 6]);
     50 
     51    // The global lexical is the next thing after the module on the scope chain,
     52    // followed by the global.
     53    assertEq(env.parent.type, 'declarative');
     54    assertEq(env.parent.parent.type, 'object');
     55    assertEq(env.parent.parent.parent, null);
     56 };
     57 
     58 f = g2.eval(
     59 `
     60    // Set up a module to import from.
     61    a = registerModule('a', parseModule(
     62    \`
     63        export var a = 1;
     64        export let b = 2;
     65        export const c = 3;
     66        export function f(x) { return x + 1; }
     67    \`));
     68    moduleLink(a);
     69    moduleEvaluate(a);
     70 
     71    let m = parseModule(
     72    \`
     73        import { a, b, c } from 'a';
     74        var x = 4;
     75        let y = 5;
     76        const z = 6;
     77 
     78        eval("");
     79        debugger;
     80    \`);
     81    moduleLink(m);
     82    moduleEvaluate(m);
     83 `);