2021-07-29 16:24:27 +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.
|
|
|
|
|
2021-11-04 14:31:26 +00:00
|
|
|
// The test needs --no-liftoff because we can't serialize and deserialize
|
|
|
|
// Liftoff code.
|
2021-07-29 16:24:27 +00:00
|
|
|
// Flags: --allow-natives-syntax --wasm-lazy-compilation --expose-gc
|
2021-11-04 14:31:26 +00:00
|
|
|
// Flags: --no-liftoff
|
2021-07-29 16:24:27 +00:00
|
|
|
|
|
|
|
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
2021-11-04 14:31:26 +00:00
|
|
|
const num_functions = 3;
|
2021-07-29 16:24:27 +00:00
|
|
|
|
|
|
|
function create_builder() {
|
|
|
|
const builder = new WasmModuleBuilder();
|
2021-09-21 14:25:05 +00:00
|
|
|
builder.addImport("foo", "bar", kSig_i_v);
|
2021-07-29 16:24:27 +00:00
|
|
|
for (let i = 0; i < num_functions; ++i) {
|
|
|
|
builder.addFunction('f' + i, kSig_i_v)
|
|
|
|
.addBody(wasmI32Const(i))
|
|
|
|
.exportFunc();
|
|
|
|
}
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wire_bytes = create_builder().toBuffer();
|
|
|
|
|
|
|
|
function serializeModule() {
|
|
|
|
const module = new WebAssembly.Module(wire_bytes);
|
2021-11-04 14:31:26 +00:00
|
|
|
// Run one function so that serialization happens.
|
|
|
|
let instance = new WebAssembly.Instance(module, {foo: {bar: () => 1}});
|
|
|
|
instance.exports.f2();
|
2021-07-29 16:24:27 +00:00
|
|
|
const buff = %SerializeWasmModule(module);
|
|
|
|
return buff;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serialized_module = serializeModule();
|
|
|
|
// Do some GCs to make sure the first module got collected and removed from the
|
|
|
|
// module cache.
|
|
|
|
gc();
|
|
|
|
gc();
|
|
|
|
gc();
|
|
|
|
|
|
|
|
(function testSerializedModule() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const module = %DeserializeWasmModule(serialized_module, wire_bytes);
|
|
|
|
|
2021-09-21 14:25:05 +00:00
|
|
|
const instance = new WebAssembly.Instance(module, {foo: {bar: () => 1}});
|
2021-07-29 16:24:27 +00:00
|
|
|
assertEquals(0, instance.exports.f0());
|
|
|
|
assertEquals(1, instance.exports.f1());
|
|
|
|
})();
|