tor-browser

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

exception-handling.js (3567B)


      1 // Test if we can handle WebAssembly.Exception on suspendable stack,
      2 // and in suspending function.
      3 
      4 function wasmException(i) {
      5  if (i == 0) {
      6    // no promise return
      7    throw new WebAssembly.Exception(ins1.exports.tag, [i]);
      8  }
      9  return Promise.reject(new WebAssembly.Exception(ins1.exports.tag, [i]));
     10 }
     11 
     12 var ins1 = wasmEvalText(`
     13 (module
     14  (import "" "t" (func $t (param i32)))
     15  (tag $t1 (param i32))
     16 
     17  (func (export "t0")
     18    i32.const 0
     19    call $t
     20  )
     21  (func (export "t1")
     22    i32.const 1
     23    call $t
     24  )
     25  (func (export "t2")
     26    try
     27      i32.const 2
     28      call $t
     29    catch $t1
     30      drop
     31    end
     32  )
     33  (func (export "t3")
     34    try
     35      i32.const 0
     36      call $t
     37    catch $t1
     38      drop
     39    end
     40  )
     41  (export "tag" (tag $t1))
     42 )
     43 `, {"": {
     44  t: new WebAssembly.Suspending(wasmException),
     45 }});
     46 
     47 async function testWasmException() {
     48  var test0 = WebAssembly.promising(ins1.exports.t0);
     49  var test1 = WebAssembly.promising(ins1.exports.t1);
     50  var test2 = WebAssembly.promising(ins1.exports.t2);
     51  var test3 = WebAssembly.promising(ins1.exports.t3);
     52  try {
     53    await test0();
     54    assertEq(true, false);
     55  } catch (ex) {
     56    assertEq(ex instanceof WebAssembly.Exception && ex.is(ins1.exports.tag), true);
     57    assertEq(ex.getArg(ins1.exports.tag, 0), 0);
     58  }
     59  try {
     60    await test1();
     61    assertEq(true, false);
     62  } catch (ex) {
     63    assertEq(ex instanceof WebAssembly.Exception && ex.is(ins1.exports.tag), true);
     64    assertEq(ex.getArg(ins1.exports.tag, 0), 1);
     65  }
     66  await test2();
     67  await test3();
     68 }
     69 
     70 // run test asynchronously
     71 var p = testWasmException();
     72 
     73 // Test if we can handle JS exception/rejection on suspendable stack,
     74 // and in suspending function.
     75 
     76 function jsException(i) {
     77  if (i == 0) {
     78    // No promise return.
     79    throw new Error("test" + i);
     80  }
     81  if (i == 42) {
     82    // Reject with non-Error object.
     83    return Promise.reject(3.14);
     84  }
     85  return Promise.reject(new Error("test" + i));
     86 }
     87 
     88 var ins2 = wasmEvalText(`
     89 (module
     90  (import "" "t" (func $t (param i32)))
     91  (import "" "tag" (tag $t1 (param externref)))
     92 
     93  (func (export "t0")
     94    i32.const 0
     95    call $t
     96  )
     97  (func (export "t1")
     98    i32.const 1
     99    call $t
    100  )
    101  (func (export "t2") (result externref)
    102    try
    103      i32.const 2
    104      call $t
    105    catch $t1
    106      return
    107    end
    108    unreachable
    109  )
    110  (func (export "t3") (result externref)
    111    try
    112      i32.const 0
    113      call $t
    114    catch $t1
    115      return
    116    end
    117    unreachable
    118  )
    119  (func (export "t42")
    120    i32.const 42
    121    call $t
    122  )
    123 )
    124 `, {"": {
    125  t: new WebAssembly.Suspending(jsException),
    126  tag: WebAssembly.JSTag,
    127 }});
    128 
    129 assertEq(WebAssembly.JSTag instanceof WebAssembly.Tag, true);
    130 async function testJSException() {
    131  var test0 = WebAssembly.promising(ins2.exports.t0);
    132  var test1 = WebAssembly.promising(ins2.exports.t1);
    133  var test2 = WebAssembly.promising(ins2.exports.t2);
    134  var test3 = WebAssembly.promising(ins2.exports.t3);
    135  var test42 = WebAssembly.promising(ins2.exports.t42);
    136  try {
    137    await test0();
    138    assertEq(true, false);
    139  } catch (ex) {
    140    assertEq(ex instanceof Error && ex.message == "test0", true);
    141  }
    142  try {
    143    await test1();
    144    assertEq(true, false);
    145  } catch (ex) {
    146    assertEq(ex instanceof Error && ex.message == "test1", true);
    147  }
    148  var ex2 = await test2();
    149  assertEq(ex2 instanceof Error && ex2.message == "test2", true);
    150  var ex3 = await test3();
    151  assertEq(ex3 instanceof Error && ex3.message == "test0", true);
    152  try {
    153    await test42();
    154    assertEq(true, false);
    155  } catch (ex) {
    156    assertEq(ex, 3.14);
    157  }
    158 }
    159 
    160 // run test asynchronously
    161 p = p.then(testJSException);