OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm --allow-natives-syntax --expose-gc |
| 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 9 |
| 10 (function SerializeAndDeserializeModule() { |
| 11 var builder = new WasmModuleBuilder(); |
| 12 builder.addMemory(1,1, true); |
| 13 var kSig_v_i = makeSig([kAstI32], []); |
| 14 var signature = builder.addType(kSig_v_i); |
| 15 builder.addImport("some_value", kSig_i); |
| 16 builder.addImport("writer", signature); |
| 17 |
| 18 builder.addFunction("main", kSig_i_i) |
| 19 .addBody([ |
| 20 kExprI32Const, 1, |
| 21 kExprGetLocal, 0, |
| 22 kExprI32LoadMem, 0, 0, |
| 23 kExprCallIndirect, kArity1, signature, |
| 24 kExprGetLocal,0, |
| 25 kExprI32LoadMem,0, 0, |
| 26 kExprCallImport, kArity0, 0, |
| 27 kExprI32Add |
| 28 ]).exportFunc(); |
| 29 |
| 30 // writer(mem[i]); |
| 31 // return mem[i] + some_value(); |
| 32 builder.addFunction("_wrap_writer", signature) |
| 33 .addBody([ |
| 34 kExprGetLocal, 0, |
| 35 kExprCallImport, kArity1, 1]); |
| 36 builder.appendToTable([0, 1]); |
| 37 |
| 38 |
| 39 var module = new WebAssembly.Module(builder.toBuffer()); |
| 40 var buff = %SerializeWasmModule(module); |
| 41 module = null; |
| 42 gc(); |
| 43 module = %DeserializeWasmModule(buff); |
| 44 |
| 45 var mem_1 = new ArrayBuffer(4); |
| 46 var view_1 = new Int32Array(mem_1); |
| 47 |
| 48 view_1[0] = 42; |
| 49 |
| 50 var outval_1; |
| 51 var i1 = new WebAssembly.Instance(module, {some_value: () => 1, |
| 52 writer: (x)=>outval_1 = x }, mem_1); |
| 53 |
| 54 assertEquals(43, i1.exports.main(0)); |
| 55 |
| 56 assertEquals(42, outval_1); |
| 57 })(); |
| 58 |
| 59 (function DeserializeInvalidObject() { |
| 60 var invalid_buffer = new ArrayBuffer(10); |
| 61 |
| 62 module = %DeserializeWasmModule(invalid_buffer); |
| 63 assertEquals(module, undefined); |
| 64 })(); |
| 65 |
| 66 (function RelationBetweenModuleAndClone() { |
| 67 let builder = new WasmModuleBuilder(); |
| 68 builder.addFunction("main", kSig_i) |
| 69 .addBody([kExprI8Const, 42]) |
| 70 .exportFunc(); |
| 71 |
| 72 var compiled_module = new WebAssembly.Module(builder.toBuffer()); |
| 73 var serialized = %SerializeWasmModule(compiled_module); |
| 74 var clone = %DeserializeWasmModule(serialized); |
| 75 |
| 76 assertNotNull(clone); |
| 77 assertFalse(clone == undefined); |
| 78 assertFalse(clone == compiled_module); |
| 79 assertEquals(clone.constructor, compiled_module.constructor); |
| 80 })() |
OLD | NEW |