serialize-lazy-module.js 2.24 KB
Newer Older
1 2 3 4
// Copyright 2018 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: --wasm-lazy-compilation --allow-natives-syntax --expose-gc
8
// Flags: --no-liftoff
9

10
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

(function SerializeUncompiledModule() {
  print(arguments.callee.name);
  const [wire_bytes, i1, buff] = (function GenerateInstance() {
    const builder = new WasmModuleBuilder();

    // Add 20 functions.
    for (let i = 0; i < 20; ++i) {
      builder.addFunction('f' + i, kSig_i_i)
          .addBody([kExprI32Const, i])
          .exportFunc();
    }

    const wire_bytes = builder.toBuffer();
    const module = new WebAssembly.Module(wire_bytes);
26 27 28
    // Run one function so that serialization happens.
    let instance = new WebAssembly.Instance(module);
    instance.exports.f3();
29
    const buff = %SerializeWasmModule(module);
30
    return [wire_bytes, instance, buff];
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  })();

  gc();
  const module = %DeserializeWasmModule(buff, wire_bytes);

  const i2 = new WebAssembly.Instance(module);

  assertEquals(13, i2.exports.f13());
  assertEquals(11, i1.exports.f11());
})();

(function SerializePartlyCompiledModule() {
  print(arguments.callee.name);
  const [wire_bytes, i1, buff] = (function GenerateInstance() {
    const builder = new WasmModuleBuilder();

    // Add 20 functions.
    for (let i = 0; i < 20; ++i) {
      builder.addFunction('f' + i, kSig_i_i)
          .addBody([kExprI32Const, i])
          .exportFunc();
    }

    const wire_bytes = builder.toBuffer();
    const module = new WebAssembly.Module(wire_bytes);
    const i1 = new WebAssembly.Instance(module);
57 58 59
    // Run one function so that serialization happens.
    i1.exports.f3();
    const buff = %SerializeWasmModule(module);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    assertEquals(2, i1.exports.f2());
    assertEquals(11, i1.exports.f11());

    return [wire_bytes, i1, buff];
  })();

  gc();
  const module = %DeserializeWasmModule(buff, wire_bytes);

  const i2 = new WebAssembly.Instance(module);

  assertEquals(13, i2.exports.f13());
  assertEquals(11, i1.exports.f11());
  assertEquals(9, i1.exports.f9());
})();