exceptions-global.js 6.07 KB
Newer Older
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
// Copyright 2019 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

// Note that this test does not pass --experimental-wasm-anyref on purpose so
// that we make sure the two flags can be controlled separately/independently.

load("test/mjsunit/wasm/wasm-module-builder.js");

// First we just test that "except_ref" global variables are allowed.
(function TestGlobalExceptRefSupported() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g = builder.addGlobal(kWasmExceptRef);
  builder.addFunction("push_and_drop_except_ref", kSig_v_v)
      .addBody([
        kExprGetGlobal, g.index,
        kExprDrop,
      ]).exportFunc();
  let instance = builder.instantiate();

  assertDoesNotThrow(instance.exports.push_and_drop_except_ref);
})();

// Test default value that global "except_ref" variables are initialized with.
(function TestGlobalExceptRefDefaultValue() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g = builder.addGlobal(kWasmExceptRef);
  builder.addFunction('push_and_return_except_ref', kSig_e_v)
      .addBody([kExprGetGlobal, g.index])
      .exportFunc();
  let instance = builder.instantiate();

  assertEquals(null, instance.exports.push_and_return_except_ref());
})();

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
// Test storing a caught exception into an exported mutable "except_ref" global.
(function TestGlobalExceptRefSetCaught() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let except = builder.addException(kSig_v_i);
  let g = builder.addGlobal(kWasmExceptRef, true).exportAs("exn");
  builder.addFunction('catch_and_set_except_ref', kSig_v_i)
      .addBody([
        kExprTry, kWasmStmt,
          kExprGetLocal, 0,
          kExprThrow, except,
        kExprCatch,
          kExprSetGlobal, g.index,
        kExprEnd,
      ]).exportFunc();
  let instance = builder.instantiate();

  assertDoesNotThrow(() => instance.exports.catch_and_set_except_ref(23));
  let exception = instance.exports.exn.value;  // Exported mutable global.
  assertInstanceof(exception, WebAssembly.RuntimeError);
  assertEquals(except, %GetWasmExceptionId(exception, instance));
})();

// Test storing a parameter into an exported mutable "except_ref" global.
(function TestGlobalExceptRefSetParameter() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g = builder.addGlobal(kWasmExceptRef, true).exportAs("exn");
  builder.addFunction('set_param_except_ref', kSig_v_e)
      .addBody([
        kExprTry, kWasmStmt,
          kExprGetLocal, 0,
          kExprRethrow,
        kExprCatch,
          kExprSetGlobal, g.index,
        kExprEnd,
      ]).exportFunc();
  let exception = "my fancy exception";
  let instance = builder.instantiate();

  assertDoesNotThrow(() => instance.exports.set_param_except_ref(exception));
  assertEquals(exception, instance.exports.exn.value);
})();

// Test loading an imported "except_ref" global and re-throwing the exception.
(function TestGlobalExceptRefGetImportedAndRethrow() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g_index = builder.addImportedGlobal("m", "exn", kWasmExceptRef);
  builder.addFunction('rethrow_except_ref', kSig_v_v)
      .addBody([
        kExprGetGlobal, g_index,
        kExprRethrow,
      ]).exportFunc();
  let exception = "my fancy exception";
  let instance = builder.instantiate({ "m": { "exn": exception }});

  assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception);
})();

// Test loading an exported mutable "except_ref" being changed from the outside.
(function TestGlobalExceptRefGetExportedMutableAndRethrow() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g = builder.addGlobal(kWasmExceptRef, true).exportAs("exn");
  builder.addFunction('rethrow_except_ref', kSig_v_v)
      .addBody([
        kExprGetGlobal, g.index,
        kExprRethrow,
      ]).exportFunc();
  let instance = builder.instantiate();

  let exception1 = instance.exports.exn.value = "my fancy exception";
  assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception1);
  let exception2 = instance.exports.exn.value = "an even fancier exception";
  assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception2);
})();

// TODO(mstarzinger): Add the following test once proposal makes it clear how
// far interaction with the mutable globals proposal is intended to go.
// Test loading an imported mutable "except_ref" being changed from the outside.
/*(function TestGlobalExceptRefGetImportedMutableAndRethrow() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g_index = builder.addImportedGlobal("m", "exn", kWasmExceptRef, true);
  builder.addFunction('rethrow_except_ref', kSig_v_v)
      .addBody([
        kExprGetGlobal, g_index,
        kExprRethrow,
      ]).exportFunc();
  let exception1 = "my fancy exception";
  let desc = { value: 'except_ref', mutable: true };
  let mutable_global = new WebAssembly.Global(desc, exception1);
  let instance = builder.instantiate({ "m": { "exn": mutable_global }});

  assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception1);
  let exception2 = mutable_global.value = "an even fancier exception";
  assertThrowsEquals(() => instance.exports.rethrow_except_ref(), exception2);
})();*/
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

// Test custom initialization index for a global "except_ref" variable.
(function TestGlobalExceptRefInitIndex() {
  print(arguments.callee.name);
  let builder = new WasmModuleBuilder();
  let g1_index = builder.addImportedGlobal("m", "exn", kWasmExceptRef);
  let g2 = builder.addGlobal(kWasmExceptRef);
  g2.init_index = g1_index;  // Initialize {g2} to equal {g1}.
  builder.addFunction('push_and_return_except_ref', kSig_e_v)
      .addBody([kExprGetGlobal, g2.index])
      .exportFunc();
  let exception = { x: "my fancy exception" };
  let instance = builder.instantiate({ "m": { "exn": exception }});

  assertSame(exception, instance.exports.push_and_return_except_ref());
})();