exceptions-rethrow.js 3.77 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --expose-wasm --experimental-wasm-eh --allow-natives-syntax

load("test/mjsunit/wasm/wasm-module-builder.js");
8
load("test/mjsunit/wasm/exceptions-utils.js");
9

10
// Test that rethrow expressions work inside catch blocks.
11 12 13 14 15 16 17 18
(function TestRethrowInCatch() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let except = builder.addException(kSig_v_v);
  builder.addFunction("rethrow0", kSig_v_v)
      .addBody([
        kExprTry, kWasmStmt,
          kExprThrow, except,
19 20
        kExprCatch,
          kExprRethrow,
21 22 23
        kExprEnd,
  ]).exportFunc();
  builder.addFunction("rethrow1", kSig_i_i)
24
      .addLocals({except_count: 1})
25 26 27
      .addBody([
        kExprTry, kWasmI32,
          kExprThrow, except,
28
        kExprCatch,
29 30
          kExprLocalSet, 1,
          kExprLocalGet, 0,
31 32
          kExprI32Eqz,
          kExprIf, kWasmStmt,
33
            kExprLocalGet, 1,
34
            kExprRethrow,
35 36 37 38 39 40
          kExprEnd,
          kExprI32Const, 23,
        kExprEnd
  ]).exportFunc();
  let instance = builder.instantiate();

41 42
  assertWasmThrows(instance, except, [], () => instance.exports.rethrow0());
  assertWasmThrows(instance, except, [], () => instance.exports.rethrow1(0));
43 44 45
  assertEquals(23, instance.exports.rethrow1(1));
})();

46 47
// Test that rethrow expressions work properly even in the presence of multiple
// nested handlers being involved.
48 49 50 51 52 53
(function TestRethrowNested() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let except1 = builder.addException(kSig_v_v);
  let except2 = builder.addException(kSig_v_v);
  builder.addFunction("rethrow_nested", kSig_i_i)
54
      .addLocals({except_count: 2})
55 56 57
      .addBody([
        kExprTry, kWasmI32,
          kExprThrow, except2,
58
        kExprCatch,
59
          kExprLocalSet, 2,
60 61
          kExprTry, kWasmI32,
            kExprThrow, except1,
62
          kExprCatch,
63 64
            kExprLocalSet, 1,
            kExprLocalGet, 0,
65 66 67
            kExprI32Const, 0,
            kExprI32Eq,
            kExprIf, kWasmStmt,
68
              kExprLocalGet, 1,
69
              kExprRethrow,
70
            kExprEnd,
71
            kExprLocalGet, 0,
72 73 74
            kExprI32Const, 1,
            kExprI32Eq,
            kExprIf, kWasmStmt,
75
              kExprLocalGet, 2,
76
              kExprRethrow,
77 78 79 80 81 82 83
            kExprEnd,
            kExprI32Const, 23,
          kExprEnd,
        kExprEnd,
  ]).exportFunc();
  let instance = builder.instantiate();

84 85
  assertWasmThrows(instance, except1, [], () => instance.exports.rethrow_nested(0));
  assertWasmThrows(instance, except2, [], () => instance.exports.rethrow_nested(1));
86 87 88 89 90 91 92 93 94 95
  assertEquals(23, instance.exports.rethrow_nested(2));
})();

// Test that an exception being rethrow can be caught by another local catch
// block in the same function without ever unwinding the activation.
(function TestRethrowRecatch() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let except = builder.addException(kSig_v_v);
  builder.addFunction("rethrow_recatch", kSig_i_i)
96
      .addLocals({except_count: 1})
97 98 99
      .addBody([
        kExprTry, kWasmI32,
          kExprThrow, except,
100
        kExprCatch,
101
          kExprLocalSet, 1,
102
          kExprTry, kWasmI32,
103
            kExprLocalGet, 0,
104 105
            kExprI32Eqz,
            kExprIf, kWasmStmt,
106
              kExprLocalGet, 1,
107
              kExprRethrow,
108 109
            kExprEnd,
            kExprI32Const, 42,
110 111
          kExprCatch,
            kExprDrop,
112 113 114 115 116 117 118 119 120
            kExprI32Const, 23,
          kExprEnd,
        kExprEnd,
  ]).exportFunc();
  let instance = builder.instantiate();

  assertEquals(23, instance.exports.rethrow_recatch(0));
  assertEquals(42, instance.exports.rethrow_recatch(1));
})();