summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/exnref/throw-ref.js
blob: bed8c4eca7a931e58de7dbffb859f5bb6da50fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
wasmFailValidateText(`(module
  (func
    throw_ref
  )
)`, /popping value from empty stack/);

wasmValidateText(`(module
  (func (param exnref)
    local.get 0
    throw_ref
  )
)`);

// Can rethrow a value
{
  let {test} = wasmEvalText(`(module
    (tag $a)
    (func (export "test")
      (block (result exnref)
        try_table (catch_all_ref 0)
          throw $a
        end
        unreachable
      )
      throw_ref
    )
  )`).exports;

  assertErrorMessage(test, WebAssembly.Exception, /.*/);
}

// Rethrowing a value inside a try works
{
  let {test} = wasmEvalText(`(module
    (tag $E)
    (func (export "test") (param $shouldRethrow i32) (result i32)
      (local $e exnref)
      (block $catch (result exnref)
        (try_table (catch_ref $E $catch) (throw $E))
        unreachable
      )
      (local.set $e)
      (block $catch (result exnref)
        (try_table (result i32) (catch_ref $E $catch)
          (if (i32.eqz (local.get $shouldRethrow))
            (then (throw_ref (local.get $e)))
          )
          (i32.const 2)
        )
        (return)
      )
      (drop) (i32.const 1)
    )
  )`).exports;
  assertEq(test(0), 1);
  assertEq(test(1), 2);
}

// Traps on null
{
  let {test} = wasmEvalText(`(module
    (tag $a)
    (func (export "test")
      ref.null exn
      throw_ref
    )
  )`).exports;

  assertErrorMessage(test, WebAssembly.RuntimeError, /null/);
}