Index: test/mjsunit/wasm/ffi.js |
diff --git a/test/mjsunit/wasm/ffi.js b/test/mjsunit/wasm/ffi.js |
index 1cc23210e824dc18c3133f7e8f36746e2169568f..61fcf02b3ca832bfddd8fd1e70bdde8f3384b84d 100644 |
--- a/test/mjsunit/wasm/ffi.js |
+++ b/test/mjsunit/wasm/ffi.js |
@@ -5,52 +5,31 @@ |
// Flags: --expose-wasm |
load("test/mjsunit/wasm/wasm-constants.js"); |
+load("test/mjsunit/wasm/wasm-module-builder.js"); |
function testCallFFI(func, check) { |
- var kBodySize = 6; |
- var kNameFunOffset = kHeaderSize + 24 + kBodySize + 1; |
- var kNameMainOffset = kNameFunOffset + 4; |
- |
- var ffi = new Object(); |
- ffi.fun = func; |
- |
- var data = bytesWithHeader( |
- // signatures |
- kDeclSignatures, 1, |
- 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int |
- // -- foreign function |
- kDeclFunctions, 2, |
- kDeclFunctionName | kDeclFunctionImport, |
- 0, 0, |
- kNameFunOffset, 0, 0, 0, // name offset |
- // -- main function |
- kDeclFunctionName | kDeclFunctionExport, |
- 0, 0, |
- kNameMainOffset, 0, 0, 0, // name offset |
- kBodySize, 0, |
- // main body |
- kExprCallFunction, 0, // -- |
- kExprGetLocal, 0, // -- |
- kExprGetLocal, 1, // -- |
- // names |
- kDeclEnd, |
- 'f', 'u', 'n', 0, // -- |
- 'm', 'a', 'i', 'n', 0 // -- |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- |
- assertEquals("function", typeof module.main); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ var sig_index = builder.addSignature([kAstI32, kAstF64, kAstF64]); |
+ builder.addImport("func", sig_index); |
+ builder.addFunction("main", sig_index) |
+ .addBody([ |
+ kExprCallImport, 0, // -- |
+ kExprGetLocal, 0, // -- |
+ kExprGetLocal, 1]) // -- |
+ .exportFunc(); |
+ |
+ var main = builder.instantiate({func: func}).exports.main; |
for (var i = 0; i < 100000; i += 10003) { |
var a = 22.5 + i, b = 10.5 + i; |
- var r = module.main(a, b); |
+ var r = main(a, b); |
check(r, a, b); |
} |
} |
var global = (function() { return this; })(); |
-var params = [-99, -99, -99, -99]; |
+var params = [-99, -99, -99, -99, -99]; |
var was_called = false; |
var length = -1; |
@@ -189,62 +168,39 @@ testCallFFI(returnValue(objWithValueOf), checkReturn(198)); |
function testCallBinopVoid(type, func, check) { |
- var kBodySize = 10; |
- var kNameFunOffset = kHeaderSize + 28 + kBodySize + 1; |
- var kNameMainOffset = kNameFunOffset + 4; |
- |
- var ffi = new Object(); |
- |
var passed_length = -1; |
var passed_a = -1; |
var passed_b = -1; |
var args_a = -1; |
var args_b = -1; |
- ffi.fun = function(a, b) { |
+ ffi = {func: function(a, b) { |
passed_length = arguments.length; |
passed_a = a; |
passed_b = b; |
args_a = arguments[0]; |
args_b = arguments[1]; |
- } |
+ }}; |
+ |
+ var builder = new WasmModuleBuilder(); |
- var data = bytesWithHeader( |
- // -- signatures |
- kDeclSignatures, 2, |
- 2, kAstStmt, type, type, // (type,type)->void |
- 2, kAstI32, type, type, // (type,type)->int |
- // -- foreign function |
- kDeclFunctions, 2, |
- kDeclFunctionName | kDeclFunctionImport, |
- 0, 0, // signature index |
- kNameFunOffset, 0, 0, 0, // name offset |
- // -- main function |
- kDeclFunctionName | kDeclFunctionExport, |
- 1, 0, // signature index |
- kNameMainOffset, 0, 0, 0, // name offset |
- kBodySize, 0, // body size |
- // main body |
- kExprBlock, 2, // -- |
- kExprCallFunction, 0, // -- |
- kExprGetLocal, 0, // -- |
- kExprGetLocal, 1, // -- |
- kExprI8Const, 99, // -- |
- // names |
- kDeclEnd, |
- 'f', 'u', 'n', 0, // -- |
- 'm', 'a', 'i', 'n', 0 // -- |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- |
- assertEquals("function", typeof module.main); |
+ builder.addImport("func", [kAstStmt, type, type]); |
+ builder.addFunction("main", [kAstI32, type, type]) |
+ .addBody([ |
+ kExprBlock, 2, // -- |
+ kExprCallImport, 0, // -- |
+ kExprGetLocal, 0, // -- |
+ kExprGetLocal, 1, // -- |
+ kExprI8Const, 99]) // -- |
+ .exportFunc() |
+ |
+ var main = builder.instantiate(ffi).exports.main; |
print("testCallBinopVoid", type); |
for (var i = 0; i < 100000; i += 10003.1) { |
var a = 22.5 + i, b = 10.5 + i; |
- var r = module.main(a, b); |
+ var r = main(a, b); |
assertEquals(99, r); |
assertEquals(2, passed_length); |
var expected_a, expected_b; |
@@ -282,51 +238,21 @@ testCallBinopVoid(kAstF64); |
function testCallPrint() { |
- var kBodySize = 10; |
- var kNamePrintOffset = kHeaderSize + 10 + 7 + 7 + 9 + kBodySize + 1; |
- var kNameMainOffset = kNamePrintOffset + 6; |
- |
- var ffi = new Object(); |
- ffi.print = print; |
- |
- var data = bytesWithHeader( |
- // -- signatures |
- kDeclSignatures, 2, |
- 1, kAstStmt, kAstI32, // i32->void |
- 1, kAstStmt, kAstF64, // f64->int |
- kDeclFunctions, 3, |
- // -- import print i32 |
- kDeclFunctionName | kDeclFunctionImport, |
- 0, 0, // signature index |
- kNamePrintOffset, 0, 0, 0, // name offset |
- // -- import print f64 |
- kDeclFunctionName | kDeclFunctionImport, |
- 1, 0, // signature index |
- kNamePrintOffset, 0, 0, 0, // name offset |
- // -- decl main |
- kDeclFunctionName | kDeclFunctionExport, |
- 1, 0, // signature index |
- kNameMainOffset, 0, 0, 0, // name offset |
- kBodySize, 0, // body size |
- // main body |
- kExprBlock, 2, // -- |
- kExprCallFunction, 0, // -- |
- kExprI8Const, 97, // -- |
- kExprCallFunction, 1, // -- |
- kExprGetLocal, 0, // -- |
- // names |
- kDeclEnd, |
- 'p', 'r', 'i', 'n', 't', 0, // -- |
- 'm', 'a', 'i', 'n', 0 // -- |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- |
- assertEquals("function", typeof module.main); |
- |
- for (var i = -9; i < 900; i += 6.125) { |
- module.main(i); |
- } |
+ var builder = new WasmModuleBuilder(); |
+ |
+ builder.addImport("print", [kAstStmt, kAstI32]); |
+ builder.addImport("print", [kAstStmt, kAstF64]); |
+ builder.addFunction("main", [kAstStmt, kAstF64]) |
+ .addBody([ |
+ kExprBlock, 2, // -- |
+ kExprCallImport, 0, // -- |
+ kExprI8Const, 97, // -- |
+ kExprCallImport, 1, // -- |
+ kExprGetLocal, 0]) // -- |
+ .exportFunc() |
+ |
+ var main = builder.instantiate({print: print}).exports.main; |
+ for (var i = -9; i < 900; i += 6.125) main(i); |
} |
testCallPrint(); |