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 function testCallImport(func, check) { | 10 function testCallImport(func, check) { |
11 var builder = new WasmModuleBuilder(); | 11 var builder = new WasmModuleBuilder(); |
12 | 12 |
13 var sig_index = builder.addSignature([kAstI32, kAstF64, kAstF64]); | 13 var sig_index = builder.addSignature([kAstI32, kAstF64, kAstF64]); |
14 builder.addImport("func", sig_index); | 14 builder.addImport("func", sig_index); |
15 builder.addFunction("main", sig_index) | 15 builder.addFunction("main", sig_index) |
16 .addBody([ | 16 .addBody([ |
17 kExprCallImport, 0, // -- | |
18 kExprGetLocal, 0, // -- | 17 kExprGetLocal, 0, // -- |
19 kExprGetLocal, 1]) // -- | 18 kExprGetLocal, 1, // -- |
| 19 kExprCallImport, 2, 0]) // -- |
20 .exportAs("main"); | 20 .exportAs("main"); |
21 | 21 |
22 var main = builder.instantiate({func: func}).exports.main; | 22 var main = builder.instantiate({func: func}).exports.main; |
23 | 23 |
24 for (var i = 0; i < 100000; i += 10003) { | 24 for (var i = 0; i < 100000; i += 10003) { |
25 var a = 22.5 + i, b = 10.5 + i; | 25 var a = 22.5 + i, b = 10.5 + i; |
26 var r = main(a, b); | 26 var r = main(a, b); |
27 check(r, a, b); | 27 check(r, a, b); |
28 } | 28 } |
29 } | 29 } |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 passed_b = b; | 182 passed_b = b; |
183 args_a = arguments[0]; | 183 args_a = arguments[0]; |
184 args_b = arguments[1]; | 184 args_b = arguments[1]; |
185 } | 185 } |
186 | 186 |
187 var builder = new WasmModuleBuilder(); | 187 var builder = new WasmModuleBuilder(); |
188 | 188 |
189 builder.addImport("func", [kAstStmt, type, type]); | 189 builder.addImport("func", [kAstStmt, type, type]); |
190 builder.addFunction("main", [kAstI32, type, type]) | 190 builder.addFunction("main", [kAstI32, type, type]) |
191 .addBody([ | 191 .addBody([ |
192 kExprBlock, 2, // -- | |
193 kExprCallImport, 0, // -- | |
194 kExprGetLocal, 0, // -- | 192 kExprGetLocal, 0, // -- |
195 kExprGetLocal, 1, // -- | 193 kExprGetLocal, 1, // -- |
196 kExprI8Const, 99]) | 194 kExprCallImport, 2, 0, // -- |
| 195 kExprI8Const, 99, // -- |
| 196 ]) |
197 .exportFunc("main"); | 197 .exportFunc("main"); |
198 | 198 |
199 var main = builder.instantiate(ffi).exports.main; | 199 var main = builder.instantiate(ffi).exports.main; |
200 | 200 |
201 print("testCallBinopVoid", type); | 201 print("testCallBinopVoid", type); |
202 | 202 |
203 for (var i = 0; i < 100000; i += 10003.1) { | 203 for (var i = 0; i < 100000; i += 10003.1) { |
204 var a = 22.5 + i, b = 10.5 + i; | 204 var a = 22.5 + i, b = 10.5 + i; |
205 var r = main(a, b); | 205 var r = main(a, b); |
206 assertEquals(99, r); | 206 assertEquals(99, r); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 testCallBinopVoid(kAstF64); | 238 testCallBinopVoid(kAstF64); |
239 | 239 |
240 | 240 |
241 | 241 |
242 function testCallPrint() { | 242 function testCallPrint() { |
243 var builder = new WasmModuleBuilder(); | 243 var builder = new WasmModuleBuilder(); |
244 builder.addImport("print", [kAstStmt, kAstI32]); | 244 builder.addImport("print", [kAstStmt, kAstI32]); |
245 builder.addImport("print", [kAstStmt, kAstF64]); | 245 builder.addImport("print", [kAstStmt, kAstF64]); |
246 builder.addFunction("main", [kAstStmt, kAstF64]) | 246 builder.addFunction("main", [kAstStmt, kAstF64]) |
247 .addBody([ | 247 .addBody([ |
248 kExprBlock, 2, // -- | 248 kExprI8Const, 97, // -- |
249 kExprCallImport, 0, // -- | 249 kExprCallImport, kArity1, 0, // -- |
250 kExprI8Const, 97, // -- | 250 kExprGetLocal, 0, // -- |
251 kExprCallImport, 1, // -- | 251 kExprCallImport, kArity1, 1 // -- |
252 kExprGetLocal, 0]) // -- | 252 ]) |
253 .exportFunc(); | 253 .exportFunc(); |
254 | 254 |
255 var main = builder.instantiate({print: print}).exports.main; | 255 var main = builder.instantiate({print: print}).exports.main; |
256 | 256 |
257 for (var i = -9; i < 900; i += 16.125) { | 257 for (var i = -9; i < 900; i += 16.125) { |
258 main(i); | 258 main(i); |
259 } | 259 } |
260 } | 260 } |
261 | 261 |
262 testCallPrint(); | 262 testCallPrint(); |
263 testCallPrint(); | 263 testCallPrint(); |
264 | 264 |
265 | 265 |
266 function testCallImport2(foo, bar, expected) { | 266 function testCallImport2(foo, bar, expected) { |
267 var builder = new WasmModuleBuilder(); | 267 var builder = new WasmModuleBuilder(); |
268 | 268 |
269 builder.addImport("foo", [kAstI32]); | 269 builder.addImport("foo", [kAstI32]); |
270 builder.addImport("bar", [kAstI32]); | 270 builder.addImport("bar", [kAstI32]); |
271 builder.addFunction("main", [kAstI32]) | 271 builder.addFunction("main", [kAstI32]) |
272 .addBody([ | 272 .addBody([ |
| 273 kExprCallImport, kArity0, 0, // -- |
| 274 kExprCallImport, kArity0, 1, // -- |
273 kExprI32Add, // -- | 275 kExprI32Add, // -- |
274 kExprCallImport, 0, // -- | 276 ]) // -- |
275 kExprCallImport, 1]) // -- | |
276 .exportFunc(); | 277 .exportFunc(); |
277 | 278 |
278 var main = builder.instantiate({foo: foo, bar: bar}).exports.main; | 279 var main = builder.instantiate({foo: foo, bar: bar}).exports.main; |
279 assertEquals(expected, main()); | 280 assertEquals(expected, main()); |
280 } | 281 } |
281 | 282 |
282 testCallImport2(function() { return 33; }, function () { return 44; }, 77); | 283 testCallImport2(function() { return 33; }, function () { return 44; }, 77); |
OLD | NEW |