tor-browser

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

construct-newtarget.js (2799B)


      1 // |reftest| skip-if(!this.hasOwnProperty("Intl"))
      2 
      3 // Test subclassing %Intl.PluralRules% works correctly.
      4 class MyPluralRules extends Intl.PluralRules {}
      5 
      6 var obj = new MyPluralRules();
      7 assertEq(obj instanceof MyPluralRules, true);
      8 assertEq(obj instanceof Intl.PluralRules, true);
      9 assertEq(Object.getPrototypeOf(obj), MyPluralRules.prototype);
     10 
     11 obj = Reflect.construct(MyPluralRules, []);
     12 assertEq(obj instanceof MyPluralRules, true);
     13 assertEq(obj instanceof Intl.PluralRules, true);
     14 assertEq(Object.getPrototypeOf(obj), MyPluralRules.prototype);
     15 
     16 obj = Reflect.construct(MyPluralRules, [], MyPluralRules);
     17 assertEq(obj instanceof MyPluralRules, true);
     18 assertEq(obj instanceof Intl.PluralRules, true);
     19 assertEq(Object.getPrototypeOf(obj), MyPluralRules.prototype);
     20 
     21 obj = Reflect.construct(MyPluralRules, [], Intl.PluralRules);
     22 assertEq(obj instanceof MyPluralRules, false);
     23 assertEq(obj instanceof Intl.PluralRules, true);
     24 assertEq(Object.getPrototypeOf(obj), Intl.PluralRules.prototype);
     25 
     26 
     27 // Set a different constructor as NewTarget.
     28 obj = Reflect.construct(MyPluralRules, [], Array);
     29 assertEq(obj instanceof MyPluralRules, false);
     30 assertEq(obj instanceof Intl.PluralRules, false);
     31 assertEq(obj instanceof Array, true);
     32 assertEq(Object.getPrototypeOf(obj), Array.prototype);
     33 
     34 obj = Reflect.construct(Intl.PluralRules, [], Array);
     35 assertEq(obj instanceof Intl.PluralRules, false);
     36 assertEq(obj instanceof Array, true);
     37 assertEq(Object.getPrototypeOf(obj), Array.prototype);
     38 
     39 
     40 // The prototype defaults to %PluralRulesPrototype% if null.
     41 function NewTargetNullPrototype() {}
     42 NewTargetNullPrototype.prototype = null;
     43 
     44 obj = Reflect.construct(Intl.PluralRules, [], NewTargetNullPrototype);
     45 assertEq(obj instanceof Intl.PluralRules, true);
     46 assertEq(Object.getPrototypeOf(obj), Intl.PluralRules.prototype);
     47 
     48 obj = Reflect.construct(MyPluralRules, [], NewTargetNullPrototype);
     49 assertEq(obj instanceof MyPluralRules, false);
     50 assertEq(obj instanceof Intl.PluralRules, true);
     51 assertEq(Object.getPrototypeOf(obj), Intl.PluralRules.prototype);
     52 
     53 
     54 // "prototype" property is retrieved exactly once.
     55 var trapLog = [], getLog = [];
     56 var ProxiedConstructor = new Proxy(Intl.PluralRules, new Proxy({
     57    get(target, propertyKey, receiver) {
     58        getLog.push(propertyKey);
     59        return Reflect.get(target, propertyKey, receiver);
     60    }
     61 }, {
     62    get(target, propertyKey, receiver) {
     63        trapLog.push(propertyKey);
     64        return Reflect.get(target, propertyKey, receiver);
     65    }
     66 }));
     67 
     68 obj = Reflect.construct(Intl.PluralRules, [], ProxiedConstructor);
     69 assertEqArray(trapLog, ["get"]);
     70 assertEqArray(getLog, ["prototype"]);
     71 assertEq(obj instanceof Intl.PluralRules, true);
     72 assertEq(Object.getPrototypeOf(obj), Intl.PluralRules.prototype);
     73 
     74 
     75 if (typeof reportCompare === "function")
     76    reportCompare(0, 0);