2019-03-19 09:28:36 +00:00
|
|
|
// 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");
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// First we just test that "exnref" global variables are allowed.
|
|
|
|
(function TestGlobalExnRefSupported() {
|
2019-03-19 09:28:36 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g = builder.addGlobal(kWasmExnRef);
|
|
|
|
builder.addFunction("push_and_drop_exnref", kSig_v_v)
|
2019-03-19 09:28:36 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetGlobal, g.index,
|
|
|
|
kExprDrop,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertDoesNotThrow(instance.exports.push_and_drop_exnref);
|
2019-03-19 09:28:36 +00:00
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test default value that global "exnref" variables are initialized with.
|
|
|
|
(function TestGlobalExnRefDefaultValue() {
|
2019-03-19 09:28:36 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g = builder.addGlobal(kWasmExnRef);
|
|
|
|
builder.addFunction('push_and_return_exnref', kSig_e_v)
|
2019-03-19 09:28:36 +00:00
|
|
|
.addBody([kExprGetGlobal, g.index])
|
|
|
|
.exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertEquals(null, instance.exports.push_and_return_exnref());
|
2019-03-19 09:28:36 +00:00
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test storing a caught exception into an exported mutable "exnref" global.
|
|
|
|
(function TestGlobalExnRefSetCaught() {
|
2019-03-19 12:57:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
2019-07-15 08:24:37 +00:00
|
|
|
let g = builder.addGlobal(kWasmExnRef, true).exportAs("exn");
|
|
|
|
builder.addFunction('catch_and_set_exnref', kSig_v_i)
|
2019-03-19 12:57:31 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmStmt,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2019-03-19 12:57:31 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprCatch,
|
|
|
|
kExprSetGlobal, g.index,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertDoesNotThrow(() => instance.exports.catch_and_set_exnref(23));
|
2019-03-19 12:57:31 +00:00
|
|
|
let exception = instance.exports.exn.value; // Exported mutable global.
|
|
|
|
assertInstanceof(exception, WebAssembly.RuntimeError);
|
|
|
|
assertEquals(except, %GetWasmExceptionId(exception, instance));
|
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test storing a parameter into an exported mutable "exnref" global.
|
|
|
|
(function TestGlobalExnRefSetParameter() {
|
2019-03-19 12:57:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g = builder.addGlobal(kWasmExnRef, true).exportAs("exn");
|
|
|
|
builder.addFunction('set_param_exnref', kSig_v_e)
|
2019-03-19 12:57:31 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmStmt,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2019-03-19 12:57:31 +00:00
|
|
|
kExprRethrow,
|
|
|
|
kExprCatch,
|
|
|
|
kExprSetGlobal, g.index,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let exception = "my fancy exception";
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertDoesNotThrow(() => instance.exports.set_param_exnref(exception));
|
2019-03-19 12:57:31 +00:00
|
|
|
assertEquals(exception, instance.exports.exn.value);
|
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test loading an imported "exnref" global and re-throwing the exception.
|
|
|
|
(function TestGlobalExnRefGetImportedAndRethrow() {
|
2019-03-19 12:57:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g_index = builder.addImportedGlobal("m", "exn", kWasmExnRef);
|
|
|
|
builder.addFunction('rethrow_exnref', kSig_v_v)
|
2019-03-19 12:57:31 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetGlobal, g_index,
|
|
|
|
kExprRethrow,
|
|
|
|
]).exportFunc();
|
|
|
|
let exception = "my fancy exception";
|
|
|
|
let instance = builder.instantiate({ "m": { "exn": exception }});
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.rethrow_exnref(), exception);
|
2019-03-19 12:57:31 +00:00
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test loading an exported mutable "exnref" being changed from the outside.
|
|
|
|
(function TestGlobalExnRefGetExportedMutableAndRethrow() {
|
2019-03-19 12:57:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g = builder.addGlobal(kWasmExnRef, true).exportAs("exn");
|
|
|
|
builder.addFunction('rethrow_exnref', kSig_v_v)
|
2019-03-19 12:57:31 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetGlobal, g.index,
|
|
|
|
kExprRethrow,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
let exception1 = instance.exports.exn.value = "my fancy exception";
|
2019-07-15 08:24:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.rethrow_exnref(), exception1);
|
2019-03-19 12:57:31 +00:00
|
|
|
let exception2 = instance.exports.exn.value = "an even fancier exception";
|
2019-07-15 08:24:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.rethrow_exnref(), exception2);
|
2019-03-19 12:57:31 +00:00
|
|
|
})();
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test loading an imported mutable "exnref" being changed from the outside.
|
2019-08-27 12:19:33 +00:00
|
|
|
(function TestGlobalExnRefGetImportedMutableAndRethrow() {
|
2019-03-19 12:57:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g_index = builder.addImportedGlobal("m", "exn", kWasmExnRef, true);
|
|
|
|
builder.addFunction('rethrow_exnref', kSig_v_v)
|
2019-03-19 12:57:31 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetGlobal, g_index,
|
|
|
|
kExprRethrow,
|
|
|
|
]).exportFunc();
|
|
|
|
let exception1 = "my fancy exception";
|
2019-07-15 08:24:37 +00:00
|
|
|
let desc = { value: 'exnref', mutable: true };
|
2019-03-19 12:57:31 +00:00
|
|
|
let mutable_global = new WebAssembly.Global(desc, exception1);
|
|
|
|
let instance = builder.instantiate({ "m": { "exn": mutable_global }});
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.rethrow_exnref(), exception1);
|
2019-03-19 12:57:31 +00:00
|
|
|
let exception2 = mutable_global.value = "an even fancier exception";
|
2019-07-15 08:24:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.rethrow_exnref(), exception2);
|
2019-08-27 12:19:33 +00:00
|
|
|
})();
|
2019-03-29 12:07:57 +00:00
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
// Test custom initialization index for a global "exnref" variable.
|
|
|
|
(function TestGlobalExnRefInitIndex() {
|
2019-03-29 12:07:57 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-15 08:24:37 +00:00
|
|
|
let g1_index = builder.addImportedGlobal("m", "exn", kWasmExnRef);
|
|
|
|
let g2 = builder.addGlobal(kWasmExnRef);
|
2019-03-29 12:07:57 +00:00
|
|
|
g2.init_index = g1_index; // Initialize {g2} to equal {g1}.
|
2019-07-15 08:24:37 +00:00
|
|
|
builder.addFunction('push_and_return_exnref', kSig_e_v)
|
2019-03-29 12:07:57 +00:00
|
|
|
.addBody([kExprGetGlobal, g2.index])
|
|
|
|
.exportFunc();
|
|
|
|
let exception = { x: "my fancy exception" };
|
|
|
|
let instance = builder.instantiate({ "m": { "exn": exception }});
|
|
|
|
|
2019-07-15 08:24:37 +00:00
|
|
|
assertSame(exception, instance.exports.push_and_return_exnref());
|
2019-03-29 12:07:57 +00:00
|
|
|
})();
|