2021-10-26 10:27:07 +00:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2022-01-05 16:22:05 +00:00
|
|
|
// Flags: --allow-natives-syntax --experimental-wasm-stack-switching
|
|
|
|
// Flags: --experimental-wasm-type-reflection --expose-gc
|
2021-10-26 10:27:07 +00:00
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
2021-11-05 14:02:33 +00:00
|
|
|
(function TestSuspender() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let suspender = new WebAssembly.Suspender();
|
|
|
|
assertTrue(suspender.toString() == "[object WebAssembly.Suspender]");
|
|
|
|
assertThrows(() => WebAssembly.Suspender(), TypeError,
|
|
|
|
/WebAssembly.Suspender must be invoked with 'new'/);
|
|
|
|
})();
|
|
|
|
|
2021-10-26 10:27:07 +00:00
|
|
|
(function TestStackSwitchNoSuspend() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addGlobal(kWasmI32, true).exportAs('g');
|
|
|
|
builder.addFunction("test", kSig_v_v)
|
|
|
|
.addBody([kExprI32Const, 42, kExprGlobalSet, 0]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
2021-11-05 16:52:18 +00:00
|
|
|
let suspender = new WebAssembly.Suspender();
|
|
|
|
let wrapper = suspender.returnPromiseOnSuspend(instance.exports.test);
|
2021-10-26 10:27:07 +00:00
|
|
|
wrapper();
|
|
|
|
assertEquals(42, instance.exports.g.value);
|
|
|
|
})();
|
2021-12-08 10:50:38 +00:00
|
|
|
|
2022-01-05 16:22:05 +00:00
|
|
|
(function TestStackSwitchSuspend() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addGlobal(kWasmI32, true).exportAs('g');
|
|
|
|
import_index = builder.addImport('m', 'import', kSig_i_v);
|
|
|
|
builder.addFunction("test", kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
kExprCallFunction, import_index, // suspend
|
|
|
|
kExprGlobalSet, 0 // resume
|
|
|
|
]).exportFunc();
|
|
|
|
let suspender = new WebAssembly.Suspender();
|
|
|
|
function js_import() {
|
|
|
|
// TODO(thibaudm): Return the value as a promise.
|
|
|
|
return 42;
|
|
|
|
};
|
|
|
|
let wasm_js_import = new WebAssembly.Function({parameters: [], results: ['i32']}, js_import);
|
|
|
|
let suspending_wasm_js_import = suspender.suspendOnReturnedPromise(wasm_js_import);
|
|
|
|
let instance = builder.instantiate({m: {import: suspending_wasm_js_import}});
|
|
|
|
let wrapped_export = suspender.returnPromiseOnSuspend(instance.exports.test);
|
|
|
|
let combined_promise = wrapped_export();
|
|
|
|
// TODO(thibaudm): Once we generate the actual wrapper, we expect 0 here
|
|
|
|
// The global will only be set after the promise resolves.
|
|
|
|
assertEquals(42, instance.exports.g.value);
|
|
|
|
})();
|
|
|
|
|
2021-12-08 10:50:38 +00:00
|
|
|
(function TestStackSwitchGC() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let gc_index = builder.addImport('m', 'gc', kSig_v_v);
|
|
|
|
builder.addFunction("test", kSig_v_v)
|
|
|
|
.addBody([kExprCallFunction, gc_index]).exportFunc();
|
|
|
|
let instance = builder.instantiate({'m': {'gc': gc}});
|
|
|
|
let suspender = new WebAssembly.Suspender();
|
|
|
|
let wrapper = suspender.returnPromiseOnSuspend(instance.exports.test);
|
|
|
|
wrapper();
|
|
|
|
})();
|