tor-browser

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

bug-1431353.js (4875B)


      1 // |jit-test| skip-if: helperThreadCount() === 0
      2 
      3 // Test multiple concurrent off-thread parse jobs.
      4 
      5 function assertFails(f) {
      6    let failed = false;
      7    try {
      8        f();
      9    } catch (e) {
     10        failed = true;
     11    }
     12    assertEq(failed, true);
     13 }
     14 
     15 function encodeScript(source)
     16 {
     17    let entry = cacheEntry(source);
     18    let global = newGlobal({ cloneSingletons: true });
     19    evaluate(entry, { global: global, saveBytecodeWithDelazifications: true });
     20    return entry;
     21 }
     22 
     23 let a, b, c;
     24 let stencil, stencilA, stencilB, stencilC;
     25 
     26 // Calling run functions without arguments assumes a single off-thread job.
     27 
     28 // Test run functions fail when no jobs exist.
     29 
     30 assertFails(() => finishOffThreadStencil());
     31 
     32 assertFails(() => finishOffThreadStencil());
     33 
     34 assertFails(() => finishOffThreadStencil());
     35 
     36 // Test run functions fail when multiple jobs exist and no ID specified.
     37 
     38 a = offThreadCompileToStencil("");
     39 b = offThreadCompileToStencil("");
     40 assertFails(() => finishOffThreadStencil());
     41 stencilA = finishOffThreadStencil(a);
     42 stencilB = finishOffThreadStencil(b);
     43 evalStencil(stencilA);
     44 evalStencil(stencilB);
     45 
     46 a = offThreadCompileModuleToStencil("");
     47 b = offThreadCompileModuleToStencil("");
     48 assertFails(() => finishOffThreadStencil());
     49 stencilA = finishOffThreadStencil(a);
     50 stencilB = finishOffThreadStencil(b);
     51 instantiateModuleStencil(stencilA);
     52 instantiateModuleStencil(stencilB);
     53 
     54 a = offThreadDecodeStencil(encodeScript(""));
     55 b = offThreadDecodeStencil(encodeScript(""));
     56 assertFails(() => finishOffThreadStencil());
     57 stencilA = finishOffThreadStencil(a);
     58 stencilB = finishOffThreadStencil(b);
     59 evalStencil(stencilA);
     60 evalStencil(stencilB);
     61 
     62 // Test fun functions succeed when a single job exist and no ID specified.
     63 
     64 offThreadCompileToStencil("42");
     65 stencil = finishOffThreadStencil();
     66 assertEq(evalStencil(stencil), 42);
     67 
     68 offThreadCompileModuleToStencil("");
     69 stencil = finishOffThreadStencil();
     70 assertEq(typeof instantiateModuleStencil(stencil), "object");
     71 
     72 offThreadDecodeStencil(encodeScript("23"));
     73 stencil = finishOffThreadStencil();
     74 assertEq(evalStencil(stencil), 23);
     75 
     76 // Run functions take an ID argument returned from the compile function.
     77 
     78 // Test bad ID type and unknown ID.
     79 
     80 offThreadCompileToStencil("");
     81 assertFails(() => finishOffThreadStencil("foo"));
     82 assertFails(() => finishOffThreadStencil(42));
     83 stencil = finishOffThreadStencil();
     84 evalStencil(stencil);
     85 
     86 offThreadCompileModuleToStencil("");
     87 assertFails(() => finishOffThreadStencil("foo"));
     88 assertFails(() => finishOffThreadStencil(42));
     89 stencil = finishOffThreadStencil();
     90 instantiateModuleStencil(stencil);
     91 
     92 offThreadDecodeStencil(encodeScript(""));
     93 assertFails(() => finishOffThreadStencil("foo"));
     94 assertFails(() => finishOffThreadStencil(42));
     95 stencil = finishOffThreadStencil();
     96 evalStencil(stencil);
     97 
     98 // Test stale ID.
     99 
    100 a = offThreadCompileToStencil("");
    101 stencilA = finishOffThreadStencil(a);
    102 evalStencil(stencilA);
    103 assertFails(() => finishOffThreadStencil(a));
    104 
    105 a = offThreadCompileModuleToStencil("");
    106 stencilA = finishOffThreadStencil(a);
    107 assertFails(() => finishOffThreadStencil(a));
    108 instantiateModuleStencil(stencilA);
    109 
    110 a = offThreadDecodeStencil(encodeScript(""));
    111 stencilA = finishOffThreadStencil(a);
    112 evalStencil(stencilA);
    113 assertFails(() => finishOffThreadStencil(a));
    114 
    115 // Test running multiple jobs.
    116 
    117 a = offThreadCompileToStencil("1");
    118 b = offThreadCompileToStencil("2");
    119 stencilA = finishOffThreadStencil(a);
    120 stencilB = finishOffThreadStencil(b);
    121 assertEq(evalStencil(stencilA), 1);
    122 assertEq(evalStencil(stencilB), 2);
    123 
    124 a = offThreadCompileModuleToStencil("");
    125 b = offThreadCompileModuleToStencil("");
    126 stencilA = finishOffThreadStencil(a);
    127 stencilB = finishOffThreadStencil(b);
    128 assertEq(typeof instantiateModuleStencil(stencilA), "object");
    129 assertEq(typeof instantiateModuleStencil(stencilB), "object");
    130 
    131 a = offThreadDecodeStencil(encodeScript("3"));
    132 b = offThreadDecodeStencil(encodeScript("4"));
    133 stencilA = finishOffThreadStencil(a);
    134 stencilB = finishOffThreadStencil(b);
    135 assertEq(evalStencil(stencilA), 3);
    136 assertEq(evalStencil(stencilB), 4);
    137 
    138 // Test many jobs.
    139 
    140 const count = 100;
    141 let jobs;
    142 
    143 jobs = new Array(count);
    144 for (let i = 0; i < jobs.length; i++)
    145    jobs[i] = offThreadCompileToStencil(`${i} * ${i}`);
    146 for (let i = 0; i < jobs.length; i++) {
    147    stencil = finishOffThreadStencil(jobs[i]);
    148    assertEq(evalStencil(stencil), i * i);
    149 }
    150 
    151 jobs = new Array(count);
    152 for (let i = 0; i < jobs.length; i++)
    153    jobs[i] = offThreadCompileModuleToStencil("");
    154 for (let i = 0; i < jobs.length; i++) {
    155    stencil = finishOffThreadStencil(jobs[i]);
    156    assertEq(typeof instantiateModuleStencil(stencil), "object");
    157 }
    158 
    159 jobs = new Array(count);
    160 for (let i = 0; i < jobs.length; i++)
    161    jobs[i] = offThreadDecodeStencil(encodeScript(`${i} * ${i}`));
    162 for (let i = 0; i < jobs.length; i++) {
    163    stencil = finishOffThreadStencil(jobs[i]);
    164    assertEq(evalStencil(stencil), i * i);
    165 }