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 testCallFFI(func, check) { | 10 function testCallFFI(func, check) { |
10 var kBodySize = 6; | 11 var builder = new WasmModuleBuilder(); |
11 var kNameFunOffset = kHeaderSize + 24 + 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 .exportFunc(); |
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 // -- foreign function | |
22 kDeclFunctions, 2, | |
23 kDeclFunctionName | kDeclFunctionImport, | |
24 0, 0, | |
25 kNameFunOffset, 0, 0, 0, // name offset | |
26 // -- main function | |
27 kDeclFunctionName | kDeclFunctionExport, | |
28 0, 0, | |
29 kNameMainOffset, 0, 0, 0, // name offset | |
30 kBodySize, 0, | |
31 // main body | |
32 kExprCallFunction, 0, // -- | |
33 kExprGetLocal, 0, // -- | |
34 kExprGetLocal, 1, // -- | |
35 // names | |
36 kDeclEnd, | |
37 'f', 'u', 'n', 0, // -- | |
38 'm', 'a', 'i', 'n', 0 // -- | |
39 ); | |
40 | |
41 var module = _WASMEXP_.instantiateModule(data, ffi); | |
42 | |
43 assertEquals("function", typeof module.main); | |
44 | 23 |
45 for (var i = 0; i < 100000; i += 10003) { | 24 for (var i = 0; i < 100000; i += 10003) { |
46 var a = 22.5 + i, b = 10.5 + i; | 25 var a = 22.5 + i, b = 10.5 + i; |
47 var r = module.main(a, b); | 26 var r = main(a, b); |
48 check(r, a, b); | 27 check(r, a, b); |
49 } | 28 } |
50 } | 29 } |
51 | 30 |
52 var global = (function() { return this; })(); | 31 var global = (function() { return this; })(); |
53 var params = [-99, -99, -99, -99]; | 32 var params = [-99, -99, -99, -99, -99]; |
54 var was_called = false; | 33 var was_called = false; |
55 var length = -1; | 34 var length = -1; |
56 | 35 |
57 function FOREIGN_SUB(a, b) { | 36 function FOREIGN_SUB(a, b) { |
58 print("FOREIGN_SUB(" + a + ", " + b + ")"); | 37 print("FOREIGN_SUB(" + a + ", " + b + ")"); |
59 was_called = true; | 38 was_called = true; |
60 params[0] = this; | 39 params[0] = this; |
61 params[1] = a; | 40 params[1] = a; |
62 params[2] = b; | 41 params[2] = b; |
63 return (a - b) | 0; | 42 return (a - b) | 0; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 testCallFFI(returnValue(null), checkReturn(0)); | 161 testCallFFI(returnValue(null), checkReturn(0)); |
183 testCallFFI(returnValue("0"), checkReturn(0)); | 162 testCallFFI(returnValue("0"), checkReturn(0)); |
184 testCallFFI(returnValue("-77"), checkReturn(-77)); | 163 testCallFFI(returnValue("-77"), checkReturn(-77)); |
185 | 164 |
186 var objWithValueOf = {valueOf: function() { return 198; }} | 165 var objWithValueOf = {valueOf: function() { return 198; }} |
187 | 166 |
188 testCallFFI(returnValue(objWithValueOf), checkReturn(198)); | 167 testCallFFI(returnValue(objWithValueOf), checkReturn(198)); |
189 | 168 |
190 | 169 |
191 function testCallBinopVoid(type, func, check) { | 170 function testCallBinopVoid(type, func, check) { |
192 var kBodySize = 10; | |
193 var kNameFunOffset = kHeaderSize + 28 + kBodySize + 1; | |
194 var kNameMainOffset = kNameFunOffset + 4; | |
195 | |
196 var ffi = new Object(); | |
197 | |
198 var passed_length = -1; | 171 var passed_length = -1; |
199 var passed_a = -1; | 172 var passed_a = -1; |
200 var passed_b = -1; | 173 var passed_b = -1; |
201 var args_a = -1; | 174 var args_a = -1; |
202 var args_b = -1; | 175 var args_b = -1; |
203 | 176 |
204 ffi.fun = function(a, b) { | 177 ffi = {func: function(a, b) { |
205 passed_length = arguments.length; | 178 passed_length = arguments.length; |
206 passed_a = a; | 179 passed_a = a; |
207 passed_b = b; | 180 passed_b = b; |
208 args_a = arguments[0]; | 181 args_a = arguments[0]; |
209 args_b = arguments[1]; | 182 args_b = arguments[1]; |
210 } | 183 }}; |
211 | 184 |
212 var data = bytesWithHeader( | 185 var builder = new WasmModuleBuilder(); |
213 // -- signatures | |
214 kDeclSignatures, 2, | |
215 2, kAstStmt, type, type, // (type,type)->void | |
216 2, kAstI32, type, type, // (type,type)->int | |
217 // -- foreign function | |
218 kDeclFunctions, 2, | |
219 kDeclFunctionName | kDeclFunctionImport, | |
220 0, 0, // signature index | |
221 kNameFunOffset, 0, 0, 0, // name offset | |
222 // -- main function | |
223 kDeclFunctionName | kDeclFunctionExport, | |
224 1, 0, // signature index | |
225 kNameMainOffset, 0, 0, 0, // name offset | |
226 kBodySize, 0, // body size | |
227 // main body | |
228 kExprBlock, 2, // -- | |
229 kExprCallFunction, 0, // -- | |
230 kExprGetLocal, 0, // -- | |
231 kExprGetLocal, 1, // -- | |
232 kExprI8Const, 99, // -- | |
233 // names | |
234 kDeclEnd, | |
235 'f', 'u', 'n', 0, // -- | |
236 'm', 'a', 'i', 'n', 0 // -- | |
237 ); | |
238 | 186 |
239 var module = _WASMEXP_.instantiateModule(data, ffi); | 187 builder.addImport("func", [kAstStmt, type, type]); |
| 188 builder.addFunction("main", [kAstI32, type, type]) |
| 189 .addBody([ |
| 190 kExprBlock, 2, // -- |
| 191 kExprCallImport, 0, // -- |
| 192 kExprGetLocal, 0, // -- |
| 193 kExprGetLocal, 1, // -- |
| 194 kExprI8Const, 99]) // -- |
| 195 .exportFunc() |
240 | 196 |
241 assertEquals("function", typeof module.main); | 197 var main = builder.instantiate(ffi).exports.main; |
242 | 198 |
243 print("testCallBinopVoid", type); | 199 print("testCallBinopVoid", type); |
244 | 200 |
245 for (var i = 0; i < 100000; i += 10003.1) { | 201 for (var i = 0; i < 100000; i += 10003.1) { |
246 var a = 22.5 + i, b = 10.5 + i; | 202 var a = 22.5 + i, b = 10.5 + i; |
247 var r = module.main(a, b); | 203 var r = main(a, b); |
248 assertEquals(99, r); | 204 assertEquals(99, r); |
249 assertEquals(2, passed_length); | 205 assertEquals(2, passed_length); |
250 var expected_a, expected_b; | 206 var expected_a, expected_b; |
251 switch (type) { | 207 switch (type) { |
252 case kAstI32: { | 208 case kAstI32: { |
253 expected_a = a | 0; | 209 expected_a = a | 0; |
254 expected_b = b | 0; | 210 expected_b = b | 0; |
255 break; | 211 break; |
256 } | 212 } |
257 case kAstF32: { | 213 case kAstF32: { |
(...skipping 17 matching lines...) Expand all Loading... |
275 | 231 |
276 | 232 |
277 testCallBinopVoid(kAstI32); | 233 testCallBinopVoid(kAstI32); |
278 // TODO testCallBinopVoid(kAstI64); | 234 // TODO testCallBinopVoid(kAstI64); |
279 testCallBinopVoid(kAstF32); | 235 testCallBinopVoid(kAstF32); |
280 testCallBinopVoid(kAstF64); | 236 testCallBinopVoid(kAstF64); |
281 | 237 |
282 | 238 |
283 | 239 |
284 function testCallPrint() { | 240 function testCallPrint() { |
285 var kBodySize = 10; | 241 var builder = new WasmModuleBuilder(); |
286 var kNamePrintOffset = kHeaderSize + 10 + 7 + 7 + 9 + kBodySize + 1; | |
287 var kNameMainOffset = kNamePrintOffset + 6; | |
288 | 242 |
289 var ffi = new Object(); | 243 builder.addImport("print", [kAstStmt, kAstI32]); |
290 ffi.print = print; | 244 builder.addImport("print", [kAstStmt, kAstF64]); |
| 245 builder.addFunction("main", [kAstStmt, kAstF64]) |
| 246 .addBody([ |
| 247 kExprBlock, 2, // -- |
| 248 kExprCallImport, 0, // -- |
| 249 kExprI8Const, 97, // -- |
| 250 kExprCallImport, 1, // -- |
| 251 kExprGetLocal, 0]) // -- |
| 252 .exportFunc() |
291 | 253 |
292 var data = bytesWithHeader( | 254 var main = builder.instantiate({print: print}).exports.main; |
293 // -- signatures | 255 for (var i = -9; i < 900; i += 6.125) main(i); |
294 kDeclSignatures, 2, | |
295 1, kAstStmt, kAstI32, // i32->void | |
296 1, kAstStmt, kAstF64, // f64->int | |
297 kDeclFunctions, 3, | |
298 // -- import print i32 | |
299 kDeclFunctionName | kDeclFunctionImport, | |
300 0, 0, // signature index | |
301 kNamePrintOffset, 0, 0, 0, // name offset | |
302 // -- import print f64 | |
303 kDeclFunctionName | kDeclFunctionImport, | |
304 1, 0, // signature index | |
305 kNamePrintOffset, 0, 0, 0, // name offset | |
306 // -- decl main | |
307 kDeclFunctionName | kDeclFunctionExport, | |
308 1, 0, // signature index | |
309 kNameMainOffset, 0, 0, 0, // name offset | |
310 kBodySize, 0, // body size | |
311 // main body | |
312 kExprBlock, 2, // -- | |
313 kExprCallFunction, 0, // -- | |
314 kExprI8Const, 97, // -- | |
315 kExprCallFunction, 1, // -- | |
316 kExprGetLocal, 0, // -- | |
317 // names | |
318 kDeclEnd, | |
319 'p', 'r', 'i', 'n', 't', 0, // -- | |
320 'm', 'a', 'i', 'n', 0 // -- | |
321 ); | |
322 | |
323 var module = _WASMEXP_.instantiateModule(data, ffi); | |
324 | |
325 assertEquals("function", typeof module.main); | |
326 | |
327 for (var i = -9; i < 900; i += 6.125) { | |
328 module.main(i); | |
329 } | |
330 } | 256 } |
331 | 257 |
332 testCallPrint(); | 258 testCallPrint(); |
333 testCallPrint(); | 259 testCallPrint(); |
OLD | NEW |