tor-browser

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

ion-args.js (3463B)


      1 let { exports } = wasmEvalText(`(module
      2    (func (export "i32") (param i32) (result i32)
      3     local.get 0
      4    )
      5 
      6    (func (export "f32") (param f32) (result f32)
      7     local.get 0
      8    )
      9 
     10    (func (export "f64") (param f64) (result f64)
     11     local.get 0
     12    )
     13 
     14    (func (export "mixed_args")
     15        (param i32) (param i32) (param i32) (param i32) (param i32) ;; 5 i32
     16        (param $f64 f64) ;; 1 f64
     17        (param i32)
     18        (result f64)
     19     local.get $f64
     20    )
     21 )`);
     22 
     23 const options = getJitCompilerOptions();
     24 const jitThreshold = options['ion.warmup.trigger'] * 2 + 2;
     25 
     26 let coercions = {
     27    i32(x) { return x|0; },
     28    f32(x) { return Math.fround(x); },
     29    f64(x) { return +x; }
     30 }
     31 
     32 function call(func, coercion, arg) {
     33    let expected;
     34    try {
     35        expected = coercion(arg);
     36    } catch(e) {
     37        expected = e.message;
     38    }
     39 
     40    for (var i = jitThreshold; i --> 0;) {
     41        try {
     42            assertEq(func(arg), expected);
     43        } catch(e) {
     44            assertEq(e.message, expected);
     45        }
     46    }
     47 }
     48 
     49 // Test misc kinds of arguments.
     50 (function() {
     51    const inputs = [
     52        42,
     53        3.5,
     54        -0,
     55        -Infinity,
     56        2**32,
     57        true,
     58        Symbol(),
     59        undefined,
     60        null,
     61        {},
     62        { valueOf() { return 13.37; } },
     63        "bonjour"
     64    ];
     65 
     66    for (let arg of inputs) {
     67        for (let func of ['i32', 'f32', 'f64']) {
     68            call(exports[func], coercions[func], arg);
     69        }
     70    }
     71 })();
     72 
     73 // Test mixup of float and int arguments.
     74 (function() {
     75    for (let i = 0; i < 10; i++) {
     76        assertEq(exports.mixed_args(i, i+1, i+2, i+3, i+4, i+0.5, i+5), i+0.5);
     77    }
     78 })();
     79 
     80 // Test high number of arguments.
     81 // All integers.
     82 let {func} = wasmEvalText(`(module
     83    (func (export "func")
     84        ${Array(32).join('(param i32)')}
     85        (param $last i32)
     86        (result i32)
     87     local.get $last
     88    )
     89 )`).exports;
     90 
     91 (function() {
     92    for (let i = 0; i < 10; i++) {
     93        assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
     94                      i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
     95                 ), i+31);
     96    }
     97 })();
     98 
     99 // All floats.
    100 func = wasmEvalText(`(module
    101    (func (export "func")
    102        ${Array(32).join('(param f64)')}
    103        (param $last i32)
    104        (result i32)
    105     local.get $last
    106    )
    107 )`).exports.func;
    108 
    109 (function() {
    110    for (let i = 0; i < 10; i++) {
    111        assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
    112                      i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
    113                 ), i+31);
    114    }
    115 })();
    116 
    117 // Mix em up! 1 i32, then 1 f32, then 1 f64, and again up to 32 args.
    118 let params = [];
    119 for (let i = 0; i < 32; i++) {
    120    params.push((i % 3 == 0) ? 'i32' :
    121                (i % 3 == 1) ? 'f32' :
    122                'f64'
    123               );
    124 }
    125 
    126 func = wasmEvalText(`(module
    127    (func (export "func")
    128        ${Array(32).join('(param f64)')}
    129        (param $last i32)
    130        (result i32)
    131     local.get $last
    132    )
    133 )`).exports.func;
    134 
    135 (function() {
    136    for (let i = 0; i < 10; i++) {
    137        assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
    138                      i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
    139                 ), i+31);
    140    }
    141 })();