Index: test/mjsunit/wasm/import-table.js |
diff --git a/test/mjsunit/wasm/import-table.js b/test/mjsunit/wasm/import-table.js |
index 53860bf20644db9d670f2478842971b1281f5c59..75799016519ae9099cb938f64432565eb8000109 100644 |
--- a/test/mjsunit/wasm/import-table.js |
+++ b/test/mjsunit/wasm/import-table.js |
@@ -5,49 +5,25 @@ |
// Flags: --expose-wasm |
load("test/mjsunit/wasm/wasm-constants.js"); |
+load("test/mjsunit/wasm/wasm-module-builder.js"); |
function testCallImport(func, check) { |
- var kBodySize = 6; |
- var kNameFunOffset = kHeaderSize + 29 + kBodySize + 1; |
- var kNameMainOffset = kNameFunOffset + 4; |
+ var builder = new WasmModuleBuilder(); |
- var ffi = new Object(); |
- ffi.fun = func; |
- |
- var data = bytesWithHeader( |
- // signatures |
- kDeclSignatures, 1, |
- 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int |
- // -- main function |
- kDeclFunctions, |
- 1, |
- kDeclFunctionName | kDeclFunctionExport, |
- 0, 0, |
- kNameMainOffset, 0, 0, 0, // name offset |
- kBodySize, 0, |
- // main body |
- kExprCallImport, 0, // -- |
- kExprGetLocal, 0, // -- |
- kExprGetLocal, 1, // -- |
- // imports |
- kDeclImportTable, |
- 1, |
- 0, 0, // sig index |
- 0, 0, 0, 0, // module name offset |
- kNameFunOffset, 0, 0, 0, // function name offset |
- // names |
- kDeclEnd, |
- 'f', 'u', 'n', 0, // -- |
- 'm', 'a', 'i', 'n', 0 // -- |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- |
- assertEquals("function", typeof module.main); |
+ 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]) // -- |
+ .exportAs("main"); |
+ |
+ 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); |
} |
} |
@@ -192,10 +168,6 @@ testCallImport(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; |
@@ -204,7 +176,7 @@ function testCallBinopVoid(type, func, check) { |
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; |
@@ -212,42 +184,25 @@ function testCallBinopVoid(type, func, check) { |
args_b = arguments[1]; |
} |
- 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); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ builder.addImport("func", [kAstStmt, type, type]); |
+ builder.addFunction("main", [kAstI32, type, type]) |
+ .addBody([ |
+ kExprBlock, 2, // -- |
+ kExprCallImport, 0, // -- |
+ kExprGetLocal, 0, // -- |
+ kExprGetLocal, 1, // -- |
+ kExprI8Const, 99]) |
+ .exportFunc("main"); |
+ |
+ 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; |
@@ -285,50 +240,22 @@ 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 += 16.125) { |
+ main(i); |
} |
} |
@@ -337,51 +264,19 @@ testCallPrint(); |
function testCallImport2(foo, bar, expected) { |
- var kBodySize = 5; |
- var kNameFooOffset = kHeaderSize + 37 + kBodySize + 1; |
- var kNameBarOffset = kNameFooOffset + 4; |
- var kNameMainOffset = kNameBarOffset + 4; |
- |
- var ffi = new Object(); |
- ffi.foo = foo; |
- ffi.bar = bar; |
- |
- var data = bytesWithHeader( |
- // signatures |
- kDeclSignatures, 1, |
- 0, kAstI32, // void -> i32 |
- // -- main function |
- kDeclFunctions, |
- 1, |
- kDeclFunctionName | kDeclFunctionExport, |
- 0, 0, |
- kNameMainOffset, 0, 0, 0, // name offset |
- kBodySize, 0, |
- // main body |
- kExprI32Add, // -- |
- kExprCallImport, 0, // -- |
- kExprCallImport, 1, // -- |
- // imports |
- kDeclImportTable, |
- 2, |
- 0, 0, // sig index |
- 0, 0, 0, 0, // module name offset |
- kNameFooOffset, 0, 0, 0, // function name offset |
- 0, 0, // sig index |
- 0, 0, 0, 0, // module name offset |
- kNameBarOffset, 0, 0, 0, // function name offset |
- // names |
- kDeclEnd, |
- 'f', 'o', 'o', 0, // -- |
- 'b', 'a', 'r', 0, // -- |
- 'm', 'a', 'i', 'n', 0 // -- |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- |
- assertEquals("function", typeof module.main); |
- |
- assertEquals(expected, module.main()); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ builder.addImport("foo", [kAstI32]); |
+ builder.addImport("bar", [kAstI32]); |
+ builder.addFunction("main", [kAstI32]) |
+ .addBody([ |
+ kExprI32Add, // -- |
+ kExprCallImport, 0, // -- |
+ kExprCallImport, 1]) // -- |
+ .exportFunc(); |
+ |
+ var main = builder.instantiate({foo: foo, bar: bar}).exports.main; |
+ assertEquals(expected, main()); |
} |
testCallImport2(function() { return 33; }, function () { return 44; }, 77); |