| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
| 6 | 6 |
| 7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
| 8 | 9 |
| 9 function instantiate(sig, body) { | 10 function instantiate(sig, body) { |
| 10 var module = new Array(); | 11 var builder = new WasmModuleBuilder(); |
| 11 module = module.concat([ | |
| 12 // -- header | |
| 13 kWasmH0, kWasmH1, kWasmH2, kWasmH3, | |
| 14 kWasmV0, kWasmV1, kWasmV2, kWasmV3 | |
| 15 ]); | |
| 16 module = module.concat([ | |
| 17 // -- signatures | |
| 18 kDeclSignatures, 1, | |
| 19 ]); | |
| 20 module = module.concat(sig); | |
| 21 module = module.concat([ | |
| 22 // -- functions | |
| 23 kDeclFunctions, 1, | |
| 24 0, // decl flags | |
| 25 0, 0, // signature | |
| 26 body.length, 0, // body size | |
| 27 ]); | |
| 28 module = module.concat(body); | |
| 29 module = module.concat([ | |
| 30 // -- declare start function | |
| 31 kDeclStartFunction, | |
| 32 0 | |
| 33 ]); | |
| 34 | 12 |
| 35 var data = bytes.apply(this, module); | 13 var func = builder.addFunction("", sig) |
| 36 print(module); | 14 .addBody(body); |
| 37 print(data instanceof ArrayBuffer); | 15 |
| 38 print(data.byteLength); | 16 builder.addStart(func.index); |
| 39 return _WASMEXP_.instantiateModule(data); | 17 |
| 18 return builder.instantiate(); |
| 40 } | 19 } |
| 41 | 20 |
| 42 function assertFails(sig, body) { | 21 function assertFails(sig, body) { |
| 43 try { | 22 try { |
| 44 var module = instantiate(sig, body); | 23 var module = instantiate(sig, body); |
| 45 print("expected failure, but passes"); | 24 print("expected failure, but passes"); |
| 46 assertFalse(true); | 25 assertFalse(true); |
| 47 } catch (expected) { | 26 } catch (expected) { |
| 48 print("ok: " + expected); | 27 print("ok: " + expected); |
| 49 } | 28 } |
| 50 } | 29 } |
| 51 | 30 |
| 52 function assertVerifies(sig, body) { | 31 function assertVerifies(sig, body) { |
| 53 var module = instantiate(sig, body); | 32 var module = instantiate(sig, body); |
| 54 assertFalse(module === undefined); | 33 assertFalse(module === undefined); |
| 55 assertFalse(module === null); | 34 assertFalse(module === null); |
| 56 assertFalse(module === 0); | 35 assertFalse(module === 0); |
| 57 assertEquals("object", typeof module); | 36 assertEquals("object", typeof module); |
| 58 return module; | 37 return module; |
| 59 } | 38 } |
| 60 | 39 |
| 61 assertVerifies([0, kAstStmt], [kExprNop]); | 40 assertVerifies([kAstStmt], [kExprNop]); |
| 62 assertVerifies([0, kAstI32], [kExprI8Const, 0]); | 41 assertVerifies([kAstI32], [kExprI8Const, 0]); |
| 63 | 42 |
| 64 // Arguments aren't allow to start functions. | 43 // Arguments aren't allow to start functions. |
| 65 assertFails([1, kAstI32, kAstI32], [kExprGetLocal, 0]); | 44 assertFails([kAstI32, kAstI32], [kExprGetLocal, 0]); |
| 66 assertFails([2, kAstI32, kAstI32, kAstF32], [kExprGetLocal, 0]); | 45 assertFails([kAstI32, kAstI32, kAstF32], [kExprGetLocal, 0]); |
| 67 assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]); | 46 assertFails([kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]); |
| 68 | 47 |
| 69 (function testInvalidIndex() { | 48 (function testInvalidIndex() { |
| 70 var kBodySize = 1; | 49 print("testInvalidIndex"); |
| 71 var data = bytesWithHeader( | 50 var builder = new WasmModuleBuilder(); |
| 72 // -- signatures | |
| 73 kDeclSignatures, 1, | |
| 74 0, kAstStmt, | |
| 75 // -- functions | |
| 76 kDeclFunctions, 1, | |
| 77 0, // decl flags | |
| 78 0, 0, // signature | |
| 79 kBodySize, 0, // body size | |
| 80 kExprNop, // body | |
| 81 // -- declare start function | |
| 82 kDeclStartFunction, | |
| 83 1 | |
| 84 ); | |
| 85 | 51 |
| 86 assertThrows(function() { _WASMEXP_.instantiateModule(data); }); | 52 var func = builder.addFunction("", [kAstStmt]) |
| 53 .addBody([kExprNop]); |
| 54 |
| 55 builder.addStart(func.index + 1); |
| 56 |
| 57 assertThrows(builder.instantiate); |
| 87 })(); | 58 })(); |
| 88 | 59 |
| 89 | 60 |
| 90 (function testTwoStartFuncs() { | 61 (function testTwoStartFuncs() { |
| 91 var kBodySize = 1; | 62 print("testTwoStartFuncs"); |
| 92 var data = bytesWithHeader( | 63 var builder = new WasmModuleBuilder(); |
| 93 // -- signatures | |
| 94 kDeclSignatures, 1, | |
| 95 0, kAstStmt, | |
| 96 // -- functions | |
| 97 kDeclFunctions, 1, | |
| 98 0, // decl flags | |
| 99 0, 0, // signature | |
| 100 kBodySize, 0, // body size | |
| 101 kExprNop, // body | |
| 102 // -- declare start function | |
| 103 kDeclStartFunction, | |
| 104 0, | |
| 105 // -- declare start function | |
| 106 kDeclStartFunction, | |
| 107 0 | |
| 108 ); | |
| 109 | 64 |
| 110 assertThrows(function() { _WASMEXP_.instantiateModule(data); }); | 65 var func = builder.addFunction("", [kAstStmt]) |
| 66 .addBody([kExprNop]); |
| 67 |
| 68 builder.addExplicitSection([kDeclStartFunction, 0]); |
| 69 builder.addExplicitSection([kDeclStartFunction, 0]); |
| 70 |
| 71 assertThrows(builder.instantiate); |
| 111 })(); | 72 })(); |
| 112 | 73 |
| 113 | 74 |
| 114 (function testRun() { | 75 (function testRun() { |
| 115 var kBodySize = 6; | 76 print("testRun"); |
| 77 var builder = new WasmModuleBuilder(); |
| 116 | 78 |
| 117 var data = bytesWithHeader( | 79 builder.addMemory(12, 12, true); |
| 118 kDeclMemory, | |
| 119 12, 12, 1, // memory | |
| 120 // -- signatures | |
| 121 kDeclSignatures, 1, | |
| 122 0, kAstStmt, | |
| 123 // -- start function | |
| 124 kDeclFunctions, 1, | |
| 125 0, // decl flags | |
| 126 0, 0, // signature | |
| 127 kBodySize, 0, // code size | |
| 128 // -- start body | |
| 129 kExprI32StoreMem, 0, kExprI8Const, 0, kExprI8Const, 77, | |
| 130 // -- declare start function | |
| 131 kDeclStartFunction, | |
| 132 0 | |
| 133 ); | |
| 134 | 80 |
| 135 var module = _WASMEXP_.instantiateModule(data); | 81 var func = builder.addFunction("", [kAstStmt]) |
| 82 .addBody([kExprI32StoreMem, 0, kExprI8Const, 0, kExprI8Const, 77]); |
| 83 |
| 84 builder.addStart(func.index); |
| 85 |
| 86 var module = builder.instantiate(); |
| 136 var memory = module.memory; | 87 var memory = module.memory; |
| 137 var view = new Int8Array(memory); | 88 var view = new Int8Array(memory); |
| 138 assertEquals(77, view[0]); | 89 assertEquals(77, view[0]); |
| 139 })(); | 90 })(); |
| 140 | 91 |
| 141 (function testStartFFI() { | 92 (function testStartFFI() { |
| 142 var kBodySize = 2; | 93 print("testStartFFI"); |
| 143 var kNameOffset = kHeaderSize + 4 + 9 + 7 + 3; | |
| 144 | |
| 145 var data = bytesWithHeader( | |
| 146 // -- signatures | |
| 147 kDeclSignatures, 1, | |
| 148 0, kAstStmt, | |
| 149 // -- imported function | |
| 150 kDeclFunctions, 2, | |
| 151 kDeclFunctionImport | kDeclFunctionName, // decl flags | |
| 152 0, 0, // signature | |
| 153 kNameOffset, 0, 0, 0, | |
| 154 // -- start function | |
| 155 0, // decl flags | |
| 156 0, 0, // signature | |
| 157 kBodySize, 0, // code size | |
| 158 // -- start body | |
| 159 kExprCallFunction, 0, | |
| 160 // -- declare start function | |
| 161 kDeclStartFunction, | |
| 162 1, | |
| 163 kDeclEnd, | |
| 164 'f', 'o', 'o', 0 | |
| 165 ); | |
| 166 | |
| 167 var ranned = false; | 94 var ranned = false; |
| 168 var ffi = new Object(); | 95 var ffi = { foo : function() { |
| 169 ffi.foo = function() { | |
| 170 print("we ranned at stert!"); | 96 print("we ranned at stert!"); |
| 171 ranned = true; | 97 ranned = true; |
| 172 } | 98 }}; |
| 173 var module = _WASMEXP_.instantiateModule(data, ffi); | 99 |
| 174 var memory = module.memory; | 100 var builder = new WasmModuleBuilder(); |
| 175 var view = new Int8Array(memory); | 101 var sig_index = builder.addSignature([kAstStmt]); |
| 102 |
| 103 builder.addImport("foo", sig_index); |
| 104 var func = builder.addFunction("", sig_index) |
| 105 .addBody([kExprCallImport, 0]); |
| 106 |
| 107 builder.addStart(func.index); |
| 108 |
| 109 var module = builder.instantiate(ffi); |
| 176 assertTrue(ranned); | 110 assertTrue(ranned); |
| 177 })(); | 111 })(); |
| OLD | NEW |