test-serialization-with-lazy-compilation.js 1.5 KB
Newer Older
1 2 3 4
// 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.

5 6
// The test needs --no-liftoff because we can't serialize and deserialize
// Liftoff code.
7
// Flags: --allow-natives-syntax --wasm-lazy-compilation --expose-gc
8
// Flags: --no-liftoff
9 10 11

d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');

12
const num_functions = 3;
13 14 15

function create_builder() {
  const builder = new WasmModuleBuilder();
16
  builder.addImport("foo", "bar", kSig_i_v);
17 18 19 20 21 22 23 24 25 26 27 28
  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);
29 30 31
  // Run one function so that serialization happens.
  let instance = new WebAssembly.Instance(module, {foo: {bar: () => 1}});
  instance.exports.f2();
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  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);

47
  const instance = new WebAssembly.Instance(module, {foo: {bar: () => 1}});
48 49 50
  assertEquals(0, instance.exports.f0());
  assertEquals(1, instance.exports.f1());
})();