tor-browser

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

onNewScript-02.js (1954B)


      1 // Creating a new script with any number of subscripts triggers the newScript hook exactly once.
      2 
      3 var g = newGlobal({newCompartment: true});
      4 var dbg = Debugger(g);
      5 var seen = new WeakMap();
      6 var hits;
      7 dbg.onNewScript = function (s) {
      8    assertEq(s instanceof Debugger.Script, true);
      9    assertEq(!seen.has(s), true);
     10    seen.set(s, true);
     11    hits++;
     12 };
     13 
     14 dbg.uncaughtExceptionHook = function () { hits = -999; };
     15 
     16 function test(f) {
     17    hits = 0;
     18    f();
     19    assertEq(hits, 1);
     20 }
     21 
     22 // eval declaring a function
     23 test(function () { g.eval("function A(m, n) { return m===0?n+1:n===0?A(m-1,1):A(m-1,A(m,n-1)); }"); });
     24 
     25 // evaluate declaring a function
     26 test(function () { g.eval("function g(a, b) { return b===0?a:g(b,a%b); }"); });
     27 
     28 // eval declaring multiple functions
     29 test(function () {
     30    g.eval("function e(i) { return i===0||o(i-1); }\n" +
     31           "function o(i) { return i!==0&&e(i-1); }\n");
     32 });
     33 
     34 // eval declaring nested functions
     35 test(function () { g.eval("function plus(x) { return function plusx(y) { return x + y; }; }"); });
     36 
     37 // eval with a function-expression
     38 test(function () { g.eval("[3].map(function (i) { return -i; });"); });
     39 
     40 // eval with getters and setters
     41 test(function () { g.eval("var obj = {get x() { return 1; }, set x(v) { print(v); }};"); });
     42 
     43 // Function with nested functions
     44 test(function () { return g.Function("a", "b", "return b - a;"); });
     45 
     46 // eval declaring a star generator
     47 test(function () { g.eval("function* sg(n) { for (var i=0;i<n;i++) yield i; }"); });
     48 
     49 // eval creating several instances of a closure
     50 test(function () { g.eval("for (var i = 0; i < 7; i++)\n" +
     51                          "    obj = function () { return obj; };\n"); });
     52 
     53 // non-strict-mode direct eval
     54 g.eval("function e(s) { eval(s); }");
     55 test(function () { g.e("function f(x) { return -x; }"); });
     56 
     57 // strict-mode direct eval
     58 g.eval("function E(s) { 'use strict'; eval(s); }");
     59 test(function () { g.E("function g(x) { return -x; }"); });