OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 var module = (function () { | 10 var module = (function () { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 return builder.instantiate(); | 120 return builder.instantiate(); |
121 })(); | 121 })(); |
122 | 122 |
123 assertEquals(33, module.exports.main(1, 0)); | 123 assertEquals(33, module.exports.main(1, 0)); |
124 assertEquals(66, module.exports.main(2, 0)); | 124 assertEquals(66, module.exports.main(2, 0)); |
125 assertEquals(34, module.exports.main(1, 1)); | 125 assertEquals(34, module.exports.main(1, 1)); |
126 assertEquals(35, module.exports.main(2, 1)); | 126 assertEquals(35, module.exports.main(2, 1)); |
127 assertEquals(32, module.exports.main(1, 2)); | 127 assertEquals(32, module.exports.main(1, 2)); |
128 assertEquals(31, module.exports.main(2, 2)); | 128 assertEquals(31, module.exports.main(2, 2)); |
129 assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)"); | 129 assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)"); |
| 130 |
| 131 (function ConstBaseTest() { |
| 132 print("ConstBaseTest..."); |
| 133 function instanceWithTable(base, length) { |
| 134 var builder = new WasmModuleBuilder(); |
| 135 |
| 136 var mul = builder.addFunction("mul", kSig_i_ii) |
| 137 .addBody([ |
| 138 kExprGetLocal, 0, // -- |
| 139 kExprGetLocal, 1, // -- |
| 140 kExprI32Mul // -- |
| 141 ]); |
| 142 var add = builder.addFunction("add", kSig_i_ii) |
| 143 .addBody([ |
| 144 kExprGetLocal, 0, // -- |
| 145 kExprGetLocal, 1, // -- |
| 146 kExprI32Add // -- |
| 147 ]); |
| 148 var sub = builder.addFunction("sub", kSig_i_ii) |
| 149 .addBody([ |
| 150 kExprGetLocal, 0, // -- |
| 151 kExprGetLocal, 1, // -- |
| 152 kExprI32Sub // -- |
| 153 ]); |
| 154 builder.addFunction("main", kSig_i_ii) |
| 155 .addBody([ |
| 156 kExprI32Const, 33, // -- |
| 157 kExprGetLocal, 0, // -- |
| 158 kExprGetLocal, 1, // -- |
| 159 kExprCallIndirect, 0]) // -- |
| 160 .exportAs("main"); |
| 161 |
| 162 builder.setFunctionTableLength(length); |
| 163 builder.addFunctionTableInit(base, false, [add.index, sub.index, mul.index])
; |
| 164 |
| 165 return builder.instantiate(); |
| 166 } |
| 167 |
| 168 for (var i = 0; i < 5; i++) { |
| 169 print(" base = " + i); |
| 170 var module = instanceWithTable(i, 10); |
| 171 main = module.exports.main; |
| 172 for (var j = 0; j < i; j++) { |
| 173 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
| 174 } |
| 175 assertEquals(34, main(1, i + 0)); |
| 176 assertEquals(35, main(2, i + 0)); |
| 177 assertEquals(32, main(1, i + 1)); |
| 178 assertEquals(31, main(2, i + 1)); |
| 179 assertEquals(33, main(1, i + 2)); |
| 180 assertEquals(66, main(2, i + 2)); |
| 181 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
| 182 } |
| 183 })(); |
| 184 |
| 185 (function GlobalBaseTest() { |
| 186 print("GlobalBaseTest..."); |
| 187 |
| 188 var builder = new WasmModuleBuilder(); |
| 189 |
| 190 var mul = builder.addFunction("mul", kSig_i_ii) |
| 191 .addBody([ |
| 192 kExprGetLocal, 0, // -- |
| 193 kExprGetLocal, 1, // -- |
| 194 kExprI32Mul // -- |
| 195 ]); |
| 196 var add = builder.addFunction("add", kSig_i_ii) |
| 197 .addBody([ |
| 198 kExprGetLocal, 0, // -- |
| 199 kExprGetLocal, 1, // -- |
| 200 kExprI32Add // -- |
| 201 ]); |
| 202 var sub = builder.addFunction("sub", kSig_i_ii) |
| 203 .addBody([ |
| 204 kExprGetLocal, 0, // -- |
| 205 kExprGetLocal, 1, // -- |
| 206 kExprI32Sub // -- |
| 207 ]); |
| 208 builder.addFunction("main", kSig_i_ii) |
| 209 .addBody([ |
| 210 kExprI32Const, 33, // -- |
| 211 kExprGetLocal, 0, // -- |
| 212 kExprGetLocal, 1, // -- |
| 213 kExprCallIndirect, 0]) // -- |
| 214 .exportAs("main"); |
| 215 |
| 216 builder.setFunctionTableLength(10); |
| 217 var g = builder.addImportedGlobal("base", undefined, kAstI32); |
| 218 builder.addFunctionTableInit(g, true, [mul.index, add.index, sub.index]); |
| 219 |
| 220 var module = new WebAssembly.Module(builder.toBuffer()); |
| 221 |
| 222 for (var i = 0; i < 5; i++) { |
| 223 print(" base = " + i); |
| 224 var instance = new WebAssembly.Instance(module, {base: i}); |
| 225 main = instance.exports.main; |
| 226 for (var j = 0; j < i; j++) { |
| 227 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
| 228 } |
| 229 assertEquals(33, main(1, i + 0)); |
| 230 assertEquals(66, main(2, i + 0)); |
| 231 assertEquals(34, main(1, i + 1)); |
| 232 assertEquals(35, main(2, i + 1)); |
| 233 assertEquals(32, main(1, i + 2)); |
| 234 assertEquals(31, main(2, i + 2)); |
| 235 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
| 236 } |
| 237 })(); |
OLD | NEW |