2016-08-09 15:19:25 +00:00
|
|
|
// Copyright 2015 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 --allow-natives-syntax --expose-gc
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
|
|
|
(function SerializeAndDeserializeModule() {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(1,1, true);
|
|
|
|
var kSig_v_i = makeSig([kAstI32], []);
|
|
|
|
var signature = builder.addType(kSig_v_i);
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addImport("", "some_value", kSig_i_v);
|
|
|
|
builder.addImport("", "writer", signature);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
|
|
|
builder.addFunction("main", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0,
|
|
|
|
kExprI32LoadMem, 0, 0,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprI32Const, 1,
|
2016-10-26 16:56:05 +00:00
|
|
|
kExprCallIndirect, signature, kTableZero,
|
2016-08-09 15:19:25 +00:00
|
|
|
kExprGetLocal,0,
|
|
|
|
kExprI32LoadMem,0, 0,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprCallFunction, 0,
|
2016-08-09 15:19:25 +00:00
|
|
|
kExprI32Add
|
|
|
|
]).exportFunc();
|
|
|
|
|
|
|
|
// writer(mem[i]);
|
|
|
|
// return mem[i] + some_value();
|
|
|
|
builder.addFunction("_wrap_writer", signature)
|
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0,
|
2016-09-27 20:46:10 +00:00
|
|
|
kExprCallFunction, 1]);
|
|
|
|
builder.appendToTable([2, 3]);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
2016-10-22 15:14:40 +00:00
|
|
|
var wire_bytes = builder.toBuffer();
|
|
|
|
var module = new WebAssembly.Module(wire_bytes);
|
2016-08-09 15:19:25 +00:00
|
|
|
var buff = %SerializeWasmModule(module);
|
|
|
|
module = null;
|
|
|
|
gc();
|
2016-10-22 15:14:40 +00:00
|
|
|
module = %DeserializeWasmModule(buff, wire_bytes);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
2016-10-28 18:13:01 +00:00
|
|
|
var mem_1 = new WebAssembly.Memory({initial: 1});
|
|
|
|
var view_1 = new Int32Array(mem_1.buffer);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
|
|
|
view_1[0] = 42;
|
|
|
|
|
|
|
|
var outval_1;
|
2016-12-20 15:32:56 +00:00
|
|
|
var i1 = new WebAssembly.Instance(module, {"": {some_value: () => 1,
|
|
|
|
writer: (x)=>outval_1 = x }}, mem_1);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
|
|
|
assertEquals(43, i1.exports.main(0));
|
|
|
|
|
|
|
|
assertEquals(42, outval_1);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function DeserializeInvalidObject() {
|
|
|
|
var invalid_buffer = new ArrayBuffer(10);
|
|
|
|
|
2016-10-22 15:14:40 +00:00
|
|
|
module = %DeserializeWasmModule(invalid_buffer, invalid_buffer);
|
2016-08-09 15:19:25 +00:00
|
|
|
assertEquals(module, undefined);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function RelationBetweenModuleAndClone() {
|
|
|
|
let builder = new WasmModuleBuilder();
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", kSig_i_v)
|
2016-08-09 15:19:25 +00:00
|
|
|
.addBody([kExprI8Const, 42])
|
|
|
|
.exportFunc();
|
|
|
|
|
2016-10-22 15:14:40 +00:00
|
|
|
var wire_bytes = builder.toBuffer();
|
|
|
|
var compiled_module = new WebAssembly.Module(wire_bytes);
|
2016-08-09 15:19:25 +00:00
|
|
|
var serialized = %SerializeWasmModule(compiled_module);
|
2016-10-22 15:14:40 +00:00
|
|
|
var clone = %DeserializeWasmModule(serialized, wire_bytes);
|
2016-08-09 15:19:25 +00:00
|
|
|
|
|
|
|
assertNotNull(clone);
|
|
|
|
assertFalse(clone == undefined);
|
|
|
|
assertFalse(clone == compiled_module);
|
|
|
|
assertEquals(clone.constructor, compiled_module.constructor);
|
2016-09-16 05:19:30 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function SerializeAfterInstantiation() {
|
|
|
|
let builder = new WasmModuleBuilder();
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", kSig_i_v)
|
2016-09-16 05:19:30 +00:00
|
|
|
.addBody([kExprI8Const, 42])
|
|
|
|
.exportFunc();
|
|
|
|
|
2016-10-22 15:14:40 +00:00
|
|
|
var wire_bytes = builder.toBuffer()
|
|
|
|
var compiled_module = new WebAssembly.Module(wire_bytes);
|
2016-09-16 05:19:30 +00:00
|
|
|
var instance1 = new WebAssembly.Instance(compiled_module);
|
|
|
|
var instance2 = new WebAssembly.Instance(compiled_module);
|
|
|
|
var serialized = %SerializeWasmModule(compiled_module);
|
2016-10-22 15:14:40 +00:00
|
|
|
var clone = %DeserializeWasmModule(serialized, wire_bytes);
|
2016-09-16 05:19:30 +00:00
|
|
|
|
|
|
|
assertNotNull(clone);
|
|
|
|
assertFalse(clone == undefined);
|
|
|
|
assertFalse(clone == compiled_module);
|
|
|
|
assertEquals(clone.constructor, compiled_module.constructor);
|
|
|
|
var instance3 = new WebAssembly.Instance(clone);
|
|
|
|
assertFalse(instance3 == undefined);
|
|
|
|
})();
|