tor-browser

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

asm.js (3557B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 load(libdir + "asserts.js");
      6 
      7 const USE_ASM = '"use asm";';
      8 const HEAP_IMPORTS = "const i8=new glob.Int8Array(b);var u8=new glob.Uint8Array(b);"+
      9                     "const i16=new glob.Int16Array(b);var u16=new glob.Uint16Array(b);"+
     10                     "const i32=new glob.Int32Array(b);var u32=new glob.Uint32Array(b);"+
     11                     "const f32=new glob.Float32Array(b);var f64=new glob.Float64Array(b);";
     12 const BUF_MIN = 64 * 1024;
     13 const BUF_CHANGE_MIN = 16 * 1024 * 1024;
     14 const BUF_64KB = new ArrayBuffer(BUF_MIN);
     15 
     16 // Don't warn about asm.js deprecation, that will get in the way of testing
     17 // for the presence or absence of other asm.js warnings.
     18 setPrefValue("warn_asmjs_deprecation", false);
     19 
     20 function asmCompile()
     21 {
     22    var f = Function.apply(null, arguments);
     23    assertEq(!isAsmJSCompilationAvailable() || isAsmJSModule(f), true);
     24    return f;
     25 }
     26 
     27 function asmCompileCached()
     28 {
     29    if (!isAsmJSCompilationAvailable())
     30        return Function.apply(null, arguments);
     31 
     32    var f = Function.apply(null, arguments);
     33    assertEq(isAsmJSModule(f), true);
     34    return f;
     35 }
     36 
     37 function assertAsmDirectiveFail(str)
     38 {
     39    if (!isAsmJSCompilationAvailable())
     40        return;
     41 
     42    assertWarning(() => {
     43        eval(str)
     44    }, /meaningful in the Directive Prologue/);
     45 }
     46 
     47 function assertAsmTypeFail()
     48 {
     49    if (!isAsmJSCompilationAvailable())
     50        return;
     51 
     52    // Verify no error is thrown with warnings off
     53    Function.apply(null, arguments);
     54 
     55    // Turn on throwing on validation errors
     56    var oldOpts = options("throw_on_asmjs_validation_failure");
     57    assertEq(oldOpts.indexOf("throw_on_asmjs_validation_failure"), -1);
     58 
     59    var caught = false;
     60    try {
     61        Function.apply(null, arguments);
     62    } catch (e) {
     63        if (!e.message.includes("asm.js type error:"))
     64            throw new Error("Didn't catch the expected type failure error; instead caught: " + e + "\nStack: " + new Error().stack);
     65        caught = true;
     66    }
     67    if (!caught)
     68        throw new Error("Didn't catch the type failure error");
     69 
     70    // Turn warnings-as-errors back off
     71    options("throw_on_asmjs_validation_failure");
     72 }
     73 
     74 function assertAsmLinkFail(f, ...args)
     75 {
     76    if (!isAsmJSCompilationAvailable())
     77        return;
     78 
     79    assertEq(isAsmJSModule(f), true);
     80 
     81    // Verify no error is thrown with warnings off
     82    var ret = f.apply(null, args);
     83 
     84    assertEq(isAsmJSFunction(ret), false);
     85    if (typeof ret === 'object') {
     86        for (var i in ret) {
     87            assertEq(isAsmJSFunction(ret[i]), false);
     88        }
     89    }
     90 
     91    assertWarning(() => {
     92        f.apply(null, args);
     93    }, /disabled by linker/);
     94 }
     95 
     96 // Linking should throw an exception even without warnings-as-errors
     97 function assertAsmLinkAlwaysFail(f, ...args)
     98 {
     99    var caught = false;
    100    try {
    101        f.apply(null, args);
    102    } catch (e) {
    103        caught = true;
    104    }
    105    if (!caught)
    106        throw new Error("Didn't catch the link failure error");
    107 }
    108 
    109 function assertAsmLinkDeprecated(f, ...args)
    110 {
    111    if (!isAsmJSCompilationAvailable())
    112        return;
    113 
    114    assertWarning(() => {
    115        f.apply(null, args);
    116    }, /asm.js type error:/)
    117 }
    118 
    119 function asmLink(f, ...args)
    120 {
    121    if (!isAsmJSCompilationAvailable())
    122        return f.apply(null, args);
    123 
    124    var ret;
    125    assertNoWarning(() => {
    126        ret = f.apply(null, args);
    127    }, "No warning for asmLink")
    128 
    129    return ret;
    130 }