tor-browser

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

basic.js (3259B)


      1 var g1 = newGlobal({sameCompartmentAs: this});
      2 var g2 = newGlobal({sameCompartmentAs: g1});
      3 g2.x = g1;
      4 gc();
      5 assertEq(objectGlobal(Math), this);
      6 assertEq(objectGlobal(g1.print), g1);
      7 assertEq(objectGlobal(g2.x), g1);
      8 
      9 assertEq(isSameCompartment(g1, g2), true);
     10 assertEq(isSameCompartment(g1, Math), true);
     11 
     12 // Different-compartment realms have wrappers.
     13 assertEq(objectGlobal(newGlobal({newCompartment: true}).Math), null);
     14 
     15 function testCrossRealmProto() {
     16    var g = newGlobal({sameCompartmentAs:this});
     17 
     18    for (var i = 0; i < 20; i++) {
     19        var o = Object.create(g.Math);
     20        assertEq(objectGlobal(o), this);
     21        assertEq(o.__proto__, g.Math);
     22 
     23        var a = Reflect.construct(Array, [], g.Object);
     24        assertEq(Array.isArray(a), true);
     25        assertEq(objectGlobal(a), this);
     26        assertEq(a.__proto__, g.Object.prototype);
     27    }
     28 }
     29 testCrossRealmProto();
     30 
     31 function testSystemNonSystemRealms() {
     32    var systemRealm = newGlobal({newCompartment: true, systemPrincipal: true});
     33    var ex;
     34    try {
     35        var nonSystemRealm = newGlobal({sameCompartmentAs: systemRealm, principal: 10});
     36    } catch(e) {
     37        ex = e;
     38    }
     39    assertEq(ex.toString().includes("non-system realms"), true);
     40    ex = null;
     41    try {
     42        systemRealm = newGlobal({systemPrincipal: true, sameCompartmentAs: this});
     43    } catch(e) {
     44        ex = e;
     45    }
     46    assertEq(ex.toString().includes("non-system realms"), true);
     47 }
     48 testSystemNonSystemRealms();
     49 
     50 function testNewObjectCache() {
     51    // NewObjectCache lookup based on the proto should not return a cross-realm
     52    // object.
     53    var g = newGlobal({sameCompartmentAs: this});
     54    var o1 = g.evaluate("Object.create(Math)");
     55    var o2 = Object.create(g.Math);
     56    assertEq(objectGlobal(o1), g);
     57    assertEq(objectGlobal(o2), this);
     58 }
     59 testNewObjectCache();
     60 
     61 function testCCWs() {
     62    // CCWs are allocated in the first realm.
     63    var g1 = newGlobal({newCompartment: true});
     64    var g2 = newGlobal({sameCompartmentAs: g1});
     65    g1.o1 = {x: 1};
     66    g2.o2 = {x: 2};
     67    g1 = null;
     68    gc();
     69    g2.o3 = {x: 3};
     70    assertEq(g2.o2.x, 2);
     71    assertEq(g2.o3.x, 3);
     72 }
     73 testCCWs();
     74 
     75 function testTypedArrayLazyBuffer(global) {
     76    var arr1 = new global.Int32Array(1);
     77    var arr2 = new Int32Array(arr1);
     78    assertEq(objectGlobal(arr2.buffer), this);
     79    global.buf = arr1.buffer;
     80    global.eval("assertEq(objectGlobal(buf), this);");
     81 }
     82 testTypedArrayLazyBuffer(newGlobal());
     83 testTypedArrayLazyBuffer(newGlobal({sameCompartmentAs: this}));
     84 
     85 function testEvalcx() {
     86    var g = newGlobal();
     87    evalcx("this.x = 7", g);
     88    assertEq(g.x, 7);
     89 
     90    g = newGlobal({newCompartment: true, invisibleToDebugger: true});
     91    var ex, sb;
     92    try {
     93        sb = g.eval("evalcx('')");
     94    } catch(e) {
     95        ex = e;
     96    }
     97    // Check for either an exception or CCW (with --more-compartments).
     98    assertEq((sb && objectGlobal(sb) === null) ||
     99             ex.toString().includes("visibility"), true);
    100 
    101    // Bug 1524707.
    102    var lazysb = evalcx("lazy");
    103    Object.setPrototypeOf(lazysb, Math);
    104    assertEq(lazysb.__proto__, Math);
    105 }
    106 testEvalcx();
    107 
    108 function testSetProto() {
    109    var o = {};
    110    o.__proto__ = newGlobal();
    111    o.__proto__ = newGlobal();
    112    assertEq(objectGlobal(o), this);
    113 }
    114 testSetProto();