Index: test/mjsunit/wasm/start-function.js |
diff --git a/test/mjsunit/wasm/start-function.js b/test/mjsunit/wasm/start-function.js |
index 409857de345f7a307097b76f1417b7d6e5621dcf..c5077bcf93f92969e026bae7ad19dd3fac48a883 100644 |
--- a/test/mjsunit/wasm/start-function.js |
+++ b/test/mjsunit/wasm/start-function.js |
@@ -5,38 +5,17 @@ |
// Flags: --expose-wasm |
load("test/mjsunit/wasm/wasm-constants.js"); |
+load("test/mjsunit/wasm/wasm-module-builder.js"); |
function instantiate(sig, body) { |
- var module = new Array(); |
- module = module.concat([ |
- // -- header |
- kWasmH0, kWasmH1, kWasmH2, kWasmH3, |
- kWasmV0, kWasmV1, kWasmV2, kWasmV3 |
- ]); |
- module = module.concat([ |
- // -- signatures |
- kDeclSignatures, 1, |
- ]); |
- module = module.concat(sig); |
- module = module.concat([ |
- // -- functions |
- kDeclFunctions, 1, |
- 0, // decl flags |
- 0, 0, // signature |
- body.length, 0, // body size |
- ]); |
- module = module.concat(body); |
- module = module.concat([ |
- // -- declare start function |
- kDeclStartFunction, |
- 0 |
- ]); |
- |
- var data = bytes.apply(this, module); |
- print(module); |
- print(data instanceof ArrayBuffer); |
- print(data.byteLength); |
- return _WASMEXP_.instantiateModule(data); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ var func = builder.addFunction("", sig) |
+ .addBody(body); |
+ |
+ builder.addStart(func.index); |
+ |
+ return builder.instantiate(); |
} |
function assertFails(sig, body) { |
@@ -58,120 +37,75 @@ function assertVerifies(sig, body) { |
return module; |
} |
-assertVerifies([0, kAstStmt], [kExprNop]); |
-assertVerifies([0, kAstI32], [kExprI8Const, 0]); |
+assertVerifies([kAstStmt], [kExprNop]); |
+assertVerifies([kAstI32], [kExprI8Const, 0]); |
// Arguments aren't allow to start functions. |
-assertFails([1, kAstI32, kAstI32], [kExprGetLocal, 0]); |
-assertFails([2, kAstI32, kAstI32, kAstF32], [kExprGetLocal, 0]); |
-assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]); |
+assertFails([kAstI32, kAstI32], [kExprGetLocal, 0]); |
+assertFails([kAstI32, kAstI32, kAstF32], [kExprGetLocal, 0]); |
+assertFails([kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]); |
(function testInvalidIndex() { |
- var kBodySize = 1; |
- var data = bytesWithHeader( |
- // -- signatures |
- kDeclSignatures, 1, |
- 0, kAstStmt, |
- // -- functions |
- kDeclFunctions, 1, |
- 0, // decl flags |
- 0, 0, // signature |
- kBodySize, 0, // body size |
- kExprNop, // body |
- // -- declare start function |
- kDeclStartFunction, |
- 1 |
- ); |
- |
- assertThrows(function() { _WASMEXP_.instantiateModule(data); }); |
+ print("testInvalidIndex"); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ var func = builder.addFunction("", [kAstStmt]) |
+ .addBody([kExprNop]); |
+ |
+ builder.addStart(func.index + 1); |
+ |
+ assertThrows(builder.instantiate); |
})(); |
(function testTwoStartFuncs() { |
- var kBodySize = 1; |
- var data = bytesWithHeader( |
- // -- signatures |
- kDeclSignatures, 1, |
- 0, kAstStmt, |
- // -- functions |
- kDeclFunctions, 1, |
- 0, // decl flags |
- 0, 0, // signature |
- kBodySize, 0, // body size |
- kExprNop, // body |
- // -- declare start function |
- kDeclStartFunction, |
- 0, |
- // -- declare start function |
- kDeclStartFunction, |
- 0 |
- ); |
- |
- assertThrows(function() { _WASMEXP_.instantiateModule(data); }); |
+ print("testTwoStartFuncs"); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ var func = builder.addFunction("", [kAstStmt]) |
+ .addBody([kExprNop]); |
+ |
+ builder.addExplicitSection([kDeclStartFunction, 0]); |
+ builder.addExplicitSection([kDeclStartFunction, 0]); |
+ |
+ assertThrows(builder.instantiate); |
})(); |
(function testRun() { |
- var kBodySize = 6; |
- |
- var data = bytesWithHeader( |
- kDeclMemory, |
- 12, 12, 1, // memory |
- // -- signatures |
- kDeclSignatures, 1, |
- 0, kAstStmt, |
- // -- start function |
- kDeclFunctions, 1, |
- 0, // decl flags |
- 0, 0, // signature |
- kBodySize, 0, // code size |
- // -- start body |
- kExprI32StoreMem, 0, kExprI8Const, 0, kExprI8Const, 77, |
- // -- declare start function |
- kDeclStartFunction, |
- 0 |
- ); |
- |
- var module = _WASMEXP_.instantiateModule(data); |
+ print("testRun"); |
+ var builder = new WasmModuleBuilder(); |
+ |
+ builder.addMemory(12, 12, true); |
+ |
+ var func = builder.addFunction("", [kAstStmt]) |
+ .addBody([kExprI32StoreMem, 0, kExprI8Const, 0, kExprI8Const, 77]); |
+ |
+ builder.addStart(func.index); |
+ |
+ var module = builder.instantiate(); |
var memory = module.memory; |
var view = new Int8Array(memory); |
assertEquals(77, view[0]); |
})(); |
(function testStartFFI() { |
- var kBodySize = 2; |
- var kNameOffset = kHeaderSize + 4 + 9 + 7 + 3; |
- |
- var data = bytesWithHeader( |
- // -- signatures |
- kDeclSignatures, 1, |
- 0, kAstStmt, |
- // -- imported function |
- kDeclFunctions, 2, |
- kDeclFunctionImport | kDeclFunctionName, // decl flags |
- 0, 0, // signature |
- kNameOffset, 0, 0, 0, |
- // -- start function |
- 0, // decl flags |
- 0, 0, // signature |
- kBodySize, 0, // code size |
- // -- start body |
- kExprCallFunction, 0, |
- // -- declare start function |
- kDeclStartFunction, |
- 1, |
- kDeclEnd, |
- 'f', 'o', 'o', 0 |
- ); |
- |
+ print("testStartFFI"); |
var ranned = false; |
- var ffi = new Object(); |
- ffi.foo = function() { |
+ var ffi = { foo : function() { |
print("we ranned at stert!"); |
ranned = true; |
- } |
- var module = _WASMEXP_.instantiateModule(data, ffi); |
- var memory = module.memory; |
- var view = new Int8Array(memory); |
+ }}; |
+ |
+ var builder = new WasmModuleBuilder(); |
+ var sig_index = builder.addSignature([kAstStmt]); |
+ |
+ builder.addImport("foo", sig_index); |
+ var func = builder.addFunction("", sig_index) |
+ .addBody([kExprCallImport, 0]); |
+ |
+ builder.addStart(func.index); |
+ |
+ var module = builder.instantiate(ffi); |
assertTrue(ranned); |
})(); |