tor-browser

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

casting.js (4748B)


      1 // Test exnref
      2 {
      3  const {
      4    refCast,
      5    refTest,
      6    branch,
      7    branchFail,
      8    refCastNullable,
      9    refTestNullable,
     10    branchNullable,
     11    branchFailNullable,
     12  } = wasmEvalText(`(module
     13    (tag $a)
     14    (func $make (param $null i32) (result exnref)
     15      (if (local.get $null)
     16        (then
     17          (return (ref.null exn))
     18        )
     19      )
     20 
     21      try_table (catch_all_ref 0)
     22        throw $a
     23      end
     24      unreachable
     25    )
     26 
     27    (func (export "refCast") (param $null i32)
     28      (call $make (local.get $null))
     29      ref.cast (ref exn)
     30      drop
     31    )
     32    (func (export "refTest") (param $null i32) (result i32)
     33      (call $make (local.get $null))
     34      ref.test (ref exn)
     35    )
     36    (func (export "branch") (param $null i32) (result i32)
     37      (block (result (ref exn))
     38        (call $make (local.get $null))
     39        br_on_cast 0 exnref (ref exn)
     40        drop
     41        (return (i32.const 0))
     42      )
     43      drop
     44      (return (i32.const 1))
     45    )
     46    (func (export "branchFail") (param $null i32) (result i32)
     47      (block (result exnref)
     48        (call $make (local.get $null))
     49        br_on_cast_fail 0 exnref (ref exn)
     50        drop
     51        (return (i32.const 1))
     52      )
     53      drop
     54      (return (i32.const 0))
     55    )
     56 
     57    (func (export "refCastNullable") (param $null i32)
     58      (call $make (local.get $null))
     59      ref.cast exnref
     60      drop
     61    )
     62    (func (export "refTestNullable") (param $null i32) (result i32)
     63      (call $make (local.get $null))
     64      ref.test exnref
     65    )
     66    (func (export "branchNullable") (param $null i32) (result i32)
     67      (block (result exnref)
     68        (call $make (local.get $null))
     69        br_on_cast 0 exnref exnref
     70        drop
     71        (return (i32.const 0))
     72      )
     73      drop
     74      (return (i32.const 1))
     75    )
     76    (func (export "branchFailNullable") (param $null i32) (result i32)
     77      (block (result exnref)
     78        (call $make (local.get $null))
     79        br_on_cast_fail 0 exnref exnref
     80        drop
     81        (return (i32.const 1))
     82      )
     83      drop
     84      (return (i32.const 0))
     85    )
     86  )`).exports;
     87 
     88  // cast non-null exnref -> (ref exn)
     89  refCast(0);
     90  assertEq(refTest(0), 1);
     91  assertEq(branch(0), 1);
     92  assertEq(branchFail(0), 1);
     93 
     94  // cast non-null exnref -> exnref
     95  refCastNullable(0);
     96  assertEq(refTestNullable(0), 1);
     97  assertEq(branchNullable(0), 1);
     98  assertEq(branchFailNullable(0), 1);
     99 
    100  // cast null exnref -> (ref exn)
    101  assertErrorMessage(() => refCast(1), WebAssembly.RuntimeError, /bad cast/);
    102  assertEq(refTest(1), 0);
    103  assertEq(branch(1), 0);
    104  assertEq(branchFail(1), 0);
    105 
    106  // cast null exnref -> exnref
    107  refCastNullable(1);
    108  assertEq(refTestNullable(1), 1);
    109  assertEq(branchNullable(1), 1);
    110  assertEq(branchFailNullable(1), 1);
    111 }
    112 
    113 
    114 // Test nullexnref
    115 {
    116  const {
    117    refCastNull,
    118    refCastNonNull,
    119    refTestNull,
    120    refTestNonNull,
    121    branchNull,
    122    branchNonNull,
    123    branchFailNull,
    124    branchFailNonNull,
    125  } = wasmEvalText(`(module
    126    (func (export "refCastNull")
    127      ref.null noexn
    128      ref.cast nullexnref
    129      drop
    130    )
    131    (func (export "refCastNonNull")
    132      ref.null noexn
    133      ref.cast (ref noexn)
    134      drop
    135    )
    136    (func (export "refTestNull") (result i32)
    137      ref.null noexn
    138      ref.test nullexnref
    139    )
    140    (func (export "refTestNonNull") (result i32)
    141      ref.null noexn
    142      ref.test (ref noexn)
    143    )
    144    (func (export "branchNull") (result i32)
    145      (block (result nullexnref)
    146        ref.null noexn
    147        br_on_cast 0 exnref nullexnref
    148        drop
    149        (return (i32.const 0))
    150      )
    151      drop
    152      (return (i32.const 1))
    153    )
    154    (func (export "branchNonNull") (result i32)
    155      (block (result (ref noexn))
    156        ref.null noexn
    157        br_on_cast 0 exnref (ref noexn)
    158        drop
    159        (return (i32.const 0))
    160      )
    161      drop
    162      (return (i32.const 1))
    163    )
    164    (func (export "branchFailNull") (result i32)
    165      (block (result exnref)
    166        ref.null noexn
    167        br_on_cast_fail 0 exnref (ref noexn)
    168        drop
    169        (return (i32.const 1))
    170      )
    171      drop
    172      (return (i32.const 0))
    173    )
    174    (func (export "branchFailNonNull") (result i32)
    175      (block (result (ref exn))
    176        ref.null noexn
    177        br_on_cast_fail 0 exnref (ref null noexn)
    178        drop
    179        (return (i32.const 1))
    180      )
    181      drop
    182      (return (i32.const 0))
    183    )
    184  )`).exports;
    185 
    186    // null exceptions can be casted to nullexnref
    187    refCastNull();
    188    assertEq(refTestNull(), 1);
    189    assertEq(branchNull(), 1);
    190    assertEq(branchFailNull(), 0);
    191 
    192    // null exceptions cannot be casted to (ref noexn)
    193    assertErrorMessage(() => refCastNonNull(), WebAssembly.RuntimeError, /bad cast/);
    194    assertEq(refTestNonNull(), 0);
    195    assertEq(branchNonNull(), 0);
    196    assertEq(branchFailNonNull(), 1);
    197 }