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 | 9 |
9 function testCallImport(func, check) { | 10 function testCallImport(func, check) { |
10 var kBodySize = 6; | 11 var builder = new WasmModuleBuilder(); |
11 var kNameFunOffset = kHeaderSize + 29 + kBodySize + 1; | |
12 var kNameMainOffset = kNameFunOffset + 4; | |
13 | 12 |
14 var ffi = new Object(); | 13 var sig_index = builder.addSignature([kAstI32, kAstF64, kAstF64]); |
15 ffi.fun = func; | 14 builder.addImport("func", sig_index); |
| 15 builder.addFunction("main", sig_index) |
| 16 .addBody([ |
| 17 kExprCallImport, 0, // -- |
| 18 kExprGetLocal, 0, // -- |
| 19 kExprGetLocal, 1]) // -- |
| 20 .exportAs("main"); |
16 | 21 |
17 var data = bytesWithHeader( | 22 var main = builder.instantiate({func: func}).exports.main; |
18 // signatures | |
19 kDeclSignatures, 1, | |
20 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int | |
21 // -- main function | |
22 kDeclFunctions, | |
23 1, | |
24 kDeclFunctionName | kDeclFunctionExport, | |
25 0, 0, | |
26 kNameMainOffset, 0, 0, 0, // name offset | |
27 kBodySize, 0, | |
28 // main body | |
29 kExprCallImport, 0, // -- | |
30 kExprGetLocal, 0, // -- | |
31 kExprGetLocal, 1, // -- | |
32 // imports | |
33 kDeclImportTable, | |
34 1, | |
35 0, 0, // sig index | |
36 0, 0, 0, 0, // module name offset | |
37 kNameFunOffset, 0, 0, 0, // function name offset | |
38 // names | |
39 kDeclEnd, | |
40 'f', 'u', 'n', 0, // -- | |
41 'm', 'a', 'i', 'n', 0 // -- | |
42 ); | |
43 | |
44 var module = _WASMEXP_.instantiateModule(data, ffi); | |
45 | |
46 assertEquals("function", typeof module.main); | |
47 | 23 |
48 for (var i = 0; i < 100000; i += 10003) { | 24 for (var i = 0; i < 100000; i += 10003) { |
49 var a = 22.5 + i, b = 10.5 + i; | 25 var a = 22.5 + i, b = 10.5 + i; |
50 var r = module.main(a, b); | 26 var r = main(a, b); |
51 check(r, a, b); | 27 check(r, a, b); |
52 } | 28 } |
53 } | 29 } |
54 | 30 |
55 var global = (function() { return this; })(); | 31 var global = (function() { return this; })(); |
56 var params = [-99, -99, -99, -99]; | 32 var params = [-99, -99, -99, -99]; |
57 var was_called = false; | 33 var was_called = false; |
58 var length = -1; | 34 var length = -1; |
59 | 35 |
60 function FOREIGN_SUB(a, b) { | 36 function FOREIGN_SUB(a, b) { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 testCallImport(returnValue(null), checkReturn(0)); | 161 testCallImport(returnValue(null), checkReturn(0)); |
186 testCallImport(returnValue("0"), checkReturn(0)); | 162 testCallImport(returnValue("0"), checkReturn(0)); |
187 testCallImport(returnValue("-77"), checkReturn(-77)); | 163 testCallImport(returnValue("-77"), checkReturn(-77)); |
188 | 164 |
189 var objWithValueOf = {valueOf: function() { return 198; }} | 165 var objWithValueOf = {valueOf: function() { return 198; }} |
190 | 166 |
191 testCallImport(returnValue(objWithValueOf), checkReturn(198)); | 167 testCallImport(returnValue(objWithValueOf), checkReturn(198)); |
192 | 168 |
193 | 169 |
194 function testCallBinopVoid(type, func, check) { | 170 function testCallBinopVoid(type, func, check) { |
195 var kBodySize = 10; | |
196 var kNameFunOffset = kHeaderSize + 28 + kBodySize + 1; | |
197 var kNameMainOffset = kNameFunOffset + 4; | |
198 | |
199 var ffi = new Object(); | 171 var ffi = new Object(); |
200 | 172 |
201 var passed_length = -1; | 173 var passed_length = -1; |
202 var passed_a = -1; | 174 var passed_a = -1; |
203 var passed_b = -1; | 175 var passed_b = -1; |
204 var args_a = -1; | 176 var args_a = -1; |
205 var args_b = -1; | 177 var args_b = -1; |
206 | 178 |
207 ffi.fun = function(a, b) { | 179 ffi.func = function(a, b) { |
208 passed_length = arguments.length; | 180 passed_length = arguments.length; |
209 passed_a = a; | 181 passed_a = a; |
210 passed_b = b; | 182 passed_b = b; |
211 args_a = arguments[0]; | 183 args_a = arguments[0]; |
212 args_b = arguments[1]; | 184 args_b = arguments[1]; |
213 } | 185 } |
214 | 186 |
215 var data = bytesWithHeader( | 187 var builder = new WasmModuleBuilder(); |
216 // -- signatures | |
217 kDeclSignatures, 2, | |
218 2, kAstStmt, type, type, // (type,type)->void | |
219 2, kAstI32, type, type, // (type,type)->int | |
220 // -- foreign function | |
221 kDeclFunctions, 2, | |
222 kDeclFunctionName | kDeclFunctionImport, | |
223 0, 0, // signature index | |
224 kNameFunOffset, 0, 0, 0, // name offset | |
225 // -- main function | |
226 kDeclFunctionName | kDeclFunctionExport, | |
227 1, 0, // signature index | |
228 kNameMainOffset, 0, 0, 0, // name offset | |
229 kBodySize, 0, // body size | |
230 // main body | |
231 kExprBlock, 2, // -- | |
232 kExprCallFunction, 0, // -- | |
233 kExprGetLocal, 0, // -- | |
234 kExprGetLocal, 1, // -- | |
235 kExprI8Const, 99, // -- | |
236 // names | |
237 kDeclEnd, | |
238 'f', 'u', 'n', 0, // -- | |
239 'm', 'a', 'i', 'n', 0 // -- | |
240 ); | |
241 | 188 |
242 var module = _WASMEXP_.instantiateModule(data, ffi); | 189 builder.addImport("func", [kAstStmt, type, type]); |
| 190 builder.addFunction("main", [kAstI32, type, type]) |
| 191 .addBody([ |
| 192 kExprBlock, 2, // -- |
| 193 kExprCallImport, 0, // -- |
| 194 kExprGetLocal, 0, // -- |
| 195 kExprGetLocal, 1, // -- |
| 196 kExprI8Const, 99]) |
| 197 .exportFunc("main"); |
243 | 198 |
244 assertEquals("function", typeof module.main); | 199 var main = builder.instantiate(ffi).exports.main; |
245 | 200 |
246 print("testCallBinopVoid", type); | 201 print("testCallBinopVoid", type); |
247 | 202 |
248 for (var i = 0; i < 100000; i += 10003.1) { | 203 for (var i = 0; i < 100000; i += 10003.1) { |
249 var a = 22.5 + i, b = 10.5 + i; | 204 var a = 22.5 + i, b = 10.5 + i; |
250 var r = module.main(a, b); | 205 var r = main(a, b); |
251 assertEquals(99, r); | 206 assertEquals(99, r); |
252 assertEquals(2, passed_length); | 207 assertEquals(2, passed_length); |
253 var expected_a, expected_b; | 208 var expected_a, expected_b; |
254 switch (type) { | 209 switch (type) { |
255 case kAstI32: { | 210 case kAstI32: { |
256 expected_a = a | 0; | 211 expected_a = a | 0; |
257 expected_b = b | 0; | 212 expected_b = b | 0; |
258 break; | 213 break; |
259 } | 214 } |
260 case kAstF32: { | 215 case kAstF32: { |
(...skipping 17 matching lines...) Expand all Loading... |
278 | 233 |
279 | 234 |
280 testCallBinopVoid(kAstI32); | 235 testCallBinopVoid(kAstI32); |
281 // TODO testCallBinopVoid(kAstI64); | 236 // TODO testCallBinopVoid(kAstI64); |
282 testCallBinopVoid(kAstF32); | 237 testCallBinopVoid(kAstF32); |
283 testCallBinopVoid(kAstF64); | 238 testCallBinopVoid(kAstF64); |
284 | 239 |
285 | 240 |
286 | 241 |
287 function testCallPrint() { | 242 function testCallPrint() { |
288 var kBodySize = 10; | 243 var builder = new WasmModuleBuilder(); |
289 var kNamePrintOffset = kHeaderSize + 10 + 7 + 7 + 9 + kBodySize + 1; | 244 builder.addImport("print", [kAstStmt, kAstI32]); |
290 var kNameMainOffset = kNamePrintOffset + 6; | 245 builder.addImport("print", [kAstStmt, kAstF64]); |
| 246 builder.addFunction("main", [kAstStmt, kAstF64]) |
| 247 .addBody([ |
| 248 kExprBlock, 2, // -- |
| 249 kExprCallImport, 0, // -- |
| 250 kExprI8Const, 97, // -- |
| 251 kExprCallImport, 1, // -- |
| 252 kExprGetLocal, 0]) // -- |
| 253 .exportFunc(); |
291 | 254 |
292 var ffi = new Object(); | 255 var main = builder.instantiate({print: print}).exports.main; |
293 ffi.print = print; | |
294 | 256 |
295 var data = bytesWithHeader( | 257 for (var i = -9; i < 900; i += 16.125) { |
296 // -- signatures | 258 main(i); |
297 kDeclSignatures, 2, | |
298 1, kAstStmt, kAstI32, // i32->void | |
299 1, kAstStmt, kAstF64, // f64->int | |
300 kDeclFunctions, 3, | |
301 // -- import print i32 | |
302 kDeclFunctionName | kDeclFunctionImport, | |
303 0, 0, // signature index | |
304 kNamePrintOffset, 0, 0, 0, // name offset | |
305 // -- import print f64 | |
306 kDeclFunctionName | kDeclFunctionImport, | |
307 1, 0, // signature index | |
308 kNamePrintOffset, 0, 0, 0, // name offset | |
309 // -- decl main | |
310 kDeclFunctionName | kDeclFunctionExport, | |
311 1, 0, // signature index | |
312 kNameMainOffset, 0, 0, 0, // name offset | |
313 kBodySize, 0, // body size | |
314 // main body | |
315 kExprBlock, 2, // -- | |
316 kExprCallFunction, 0, // -- | |
317 kExprI8Const, 97, // -- | |
318 kExprCallFunction, 1, // -- | |
319 kExprGetLocal, 0, // -- | |
320 // names | |
321 kDeclEnd, | |
322 'p', 'r', 'i', 'n', 't', 0, // -- | |
323 'm', 'a', 'i', 'n', 0 // -- | |
324 ); | |
325 | |
326 var module = _WASMEXP_.instantiateModule(data, ffi); | |
327 | |
328 assertEquals("function", typeof module.main); | |
329 | |
330 for (var i = -9; i < 900; i += 6.125) { | |
331 module.main(i); | |
332 } | 259 } |
333 } | 260 } |
334 | 261 |
335 testCallPrint(); | 262 testCallPrint(); |
336 testCallPrint(); | 263 testCallPrint(); |
337 | 264 |
338 | 265 |
339 function testCallImport2(foo, bar, expected) { | 266 function testCallImport2(foo, bar, expected) { |
340 var kBodySize = 5; | 267 var builder = new WasmModuleBuilder(); |
341 var kNameFooOffset = kHeaderSize + 37 + kBodySize + 1; | |
342 var kNameBarOffset = kNameFooOffset + 4; | |
343 var kNameMainOffset = kNameBarOffset + 4; | |
344 | 268 |
345 var ffi = new Object(); | 269 builder.addImport("foo", [kAstI32]); |
346 ffi.foo = foo; | 270 builder.addImport("bar", [kAstI32]); |
347 ffi.bar = bar; | 271 builder.addFunction("main", [kAstI32]) |
| 272 .addBody([ |
| 273 kExprI32Add, // -- |
| 274 kExprCallImport, 0, // -- |
| 275 kExprCallImport, 1]) // -- |
| 276 .exportFunc(); |
348 | 277 |
349 var data = bytesWithHeader( | 278 var main = builder.instantiate({foo: foo, bar: bar}).exports.main; |
350 // signatures | 279 assertEquals(expected, main()); |
351 kDeclSignatures, 1, | |
352 0, kAstI32, // void -> i32 | |
353 // -- main function | |
354 kDeclFunctions, | |
355 1, | |
356 kDeclFunctionName | kDeclFunctionExport, | |
357 0, 0, | |
358 kNameMainOffset, 0, 0, 0, // name offset | |
359 kBodySize, 0, | |
360 // main body | |
361 kExprI32Add, // -- | |
362 kExprCallImport, 0, // -- | |
363 kExprCallImport, 1, // -- | |
364 // imports | |
365 kDeclImportTable, | |
366 2, | |
367 0, 0, // sig index | |
368 0, 0, 0, 0, // module name offset | |
369 kNameFooOffset, 0, 0, 0, // function name offset | |
370 0, 0, // sig index | |
371 0, 0, 0, 0, // module name offset | |
372 kNameBarOffset, 0, 0, 0, // function name offset | |
373 // names | |
374 kDeclEnd, | |
375 'f', 'o', 'o', 0, // -- | |
376 'b', 'a', 'r', 0, // -- | |
377 'm', 'a', 'i', 'n', 0 // -- | |
378 ); | |
379 | |
380 var module = _WASMEXP_.instantiateModule(data, ffi); | |
381 | |
382 assertEquals("function", typeof module.main); | |
383 | |
384 assertEquals(expected, module.main()); | |
385 } | 280 } |
386 | 281 |
387 testCallImport2(function() { return 33; }, function () { return 44; }, 77); | 282 testCallImport2(function() { return 33; }, function () { return 44; }, 77); |
OLD | NEW |