tor-browser

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

throwOnCallConstructor.js (1857B)


      1 // Count constructor calls
      2 var cnt = 0;
      3 class Base { constructor() { ++cnt; } }
      4 
      5 // Force |JSFunction->hasScript()|
      6 new Base();
      7 assertEq(cnt, 1);
      8 
      9 // Calling a ClassConstructor must throw
     10 (function() {
     11    function f() { Base(); }
     12    try { f() } catch (e) {}
     13    try { f() } catch (e) {}
     14    assertEq(cnt, 1);
     15 })();
     16 
     17 // Spread-calling a ClassConstructor must throw
     18 (function() {
     19    function f() { Base(...[]); }
     20    try { f() } catch (e) {}
     21    try { f() } catch (e) {}
     22    assertEq(cnt, 1);
     23 })();
     24 
     25 // Function.prototype.call must throw on ClassConstructor
     26 (function() {
     27    function f() { Base.call(null); }
     28    try { f() } catch (e) {}
     29    try { f() } catch (e) {}
     30    assertEq(cnt, 1);
     31 })();
     32 
     33 // Function.prototype.apply must throw on ClassConstructor
     34 (function() {
     35    function f() { Base.apply(null, []); }
     36    try { f() } catch (e) {}
     37    try { f() } catch (e) {}
     38    assertEq(cnt, 1);
     39 })();
     40 
     41 // Getter must throw if it is a ClassConstructor
     42 (function() {
     43    var o = {};
     44    Object.defineProperty(o, "prop", { get: Base });
     45    function f() { o.prop };
     46    try { f() } catch (e) {}
     47    try { f() } catch (e) {}
     48    assertEq(cnt, 1);
     49 })();
     50 
     51 // Setter must throw if it is a ClassConstructor
     52 (function() {
     53    var o = {};
     54    Object.defineProperty(o, "prop", { set: Base });
     55    function f() { o.prop = 1 };
     56    try { f() } catch (e) {}
     57    try { f() } catch (e) {}
     58    assertEq(cnt, 1);
     59 })();
     60 
     61 // Proxy apply must throw if it is a ClassConstructor
     62 (function() {
     63    var o = new Proxy(function() { }, { apply: Base });
     64    function f() { o() };
     65    try { f() } catch (e) {}
     66    try { f() } catch (e) {}
     67    assertEq(cnt, 1);
     68 })();
     69 
     70 // Proxy get must throw if it is a ClassConstructor
     71 (function() {
     72    var o = new Proxy({}, { get: Base });
     73    function f() { o.x }
     74    try { f() } catch (e) {}
     75    try { f() } catch (e) {}
     76    assertEq(cnt, 1);
     77 })();