Commit c38cd169 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Use the JavaScript WasmModuleBuilder utility in JS tests.

R=ahaas@chromium.org,binji@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1770913002

Cr-Commit-Position: refs/heads/master@{#34559}
parent 065ae3dd
......@@ -5,141 +5,100 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function assertModule(module, memsize) {
// Check the module exists.
assertFalse(module === undefined);
assertFalse(module === null);
assertFalse(module === 0);
assertEquals("object", typeof module);
// Check the memory is an ArrayBuffer.
var mem = module.memory;
assertFalse(mem === undefined);
assertFalse(mem === null);
assertFalse(mem === 0);
assertEquals("object", typeof mem);
assertTrue(mem instanceof ArrayBuffer);
for (var i = 0; i < 4; i++) {
module.memory = 0; // should be ignored
assertEquals(mem, module.memory);
}
assertEquals(memsize, module.memory.byteLength);
}
function assertFunction(module, func) {
assertEquals("object", typeof module.exports);
var exp = module.exports[func];
assertFalse(exp === undefined);
assertFalse(exp === null);
assertFalse(exp === 0);
assertEquals("function", typeof exp);
var module = (function () {
var kBodySize = 5;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory
kDeclMemory,
1, 1, 1,
// -- signatures
kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // int, int -> int
// -- functions
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0,
// -- body
return exp;
}
(function SubTest() {
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true);
builder.addFunction("sub", [kAstI32, kAstI32, kAstI32])
.addBody([
kExprI32Sub, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kDeclEnd,
's', 'u', 'b', 0 // name
));
})();
kExprGetLocal, 1]) // --
.exportFunc()
// Check the module exists.
assertFalse(module === undefined);
assertFalse(module === null);
assertFalse(module === 0);
assertEquals("object", typeof module);
// Check the memory is an ArrayBuffer.
var mem = module.memory;
assertFalse(mem === undefined);
assertFalse(mem === null);
assertFalse(mem === 0);
assertEquals("object", typeof mem);
assertTrue(mem instanceof ArrayBuffer);
for (var i = 0; i < 4; i++) {
module.memory = 0; // should be ignored
assertEquals(mem, module.memory);
}
var module = builder.instantiate();
assertModule(module, kPageSize);
assertEquals(65536, module.memory.byteLength);
// Check the properties of the sub function.
assertEquals("function", typeof module.sub);
assertEquals(-55, module.sub(33, 88));
assertEquals(-55555, module.sub(33333, 88888));
assertEquals(-5555555, module.sub(3333333, 8888888));
var module = (function() {
var kBodySize = 1;
var kNameOffset2 = kHeaderSize + 19 + kBodySize + 1;
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- memory
kDeclMemory,
1, 1, 1,
// -- signatures
kDeclSignatures, 1,
0, kAstStmt, // signature: void -> void
// -- functions
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature index
kNameOffset2, 0, 0, 0, // name offset
kBodySize, 0,
kExprNop, // body
kDeclEnd,
'n', 'o', 'p', 0 // name
));
// Check the properties of the sub function.
var sub = assertFunction(module, "sub");
assertEquals(-55, sub(33, 88));
assertEquals(-55555, sub(33333, 88888));
assertEquals(-5555555, sub(3333333, 8888888));
})();
// Check the module exists.
assertFalse(module === undefined);
assertFalse(module === null);
assertFalse(module === 0);
assertEquals("object", typeof module);
// Check the memory is an ArrayBuffer.
var mem = module.memory;
assertFalse(mem === undefined);
assertFalse(mem === null);
assertFalse(mem === 0);
assertEquals("object", typeof mem);
assertTrue(mem instanceof ArrayBuffer);
for (var i = 0; i < 4; i++) {
module.memory = 0; // should be ignored
assertEquals(mem, module.memory);
}
assertEquals(65536, module.memory.byteLength);
(function NopTest() {
// Check the properties of the sub function.
assertFalse(module.nop === undefined);
assertFalse(module.nop === null);
assertFalse(module.nop === 0);
assertEquals("function", typeof module.nop);
var builder = new WasmModuleBuilder();
var kPages = 2;
builder.addMemory(kPages, kPages, true);
builder.addFunction("nop", [kAstStmt])
.addBody([kExprNop])
.exportFunc();
var module = builder.instantiate();
assertModule(module, kPageSize * kPages);
var nop = assertFunction(module, "nop");
assertEquals(undefined, nop());
})();
assertEquals(undefined, module.nop());
(function testLt() {
var kBodySize = 5;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
var data = bytesWithHeader(
// -- memory
kDeclMemory,
1, 1, 1,
// -- signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int
// -- functions
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature index
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0,
// -- body
var builder = new WasmModuleBuilder();
var kPages = 3;
builder.addMemory(kPages, kPages, true);
builder.addFunction("flt", [kAstI32, kAstF64, kAstF64])
.addBody([
kExprF64Lt, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kDeclEnd,
'f', 'l', 't', 0 // name
);
kExprGetLocal, 1]) // --
.exportFunc();
var module = _WASMEXP_.instantiateModule(data);
var module = builder.instantiate();
assertModule(module, kPageSize * kPages);
assertEquals("function", typeof module.flt);
assertEquals(1, module.flt(-2, -1));
assertEquals(0, module.flt(7.3, 7.1));
assertEquals(1, module.flt(7.1, 7.3));
var flt = assertFunction(module, "flt");
assertEquals(1, flt(-2, -1));
assertEquals(0, flt(7.3, 7.1));
assertEquals(1, flt(7.1, 7.3));
})();
......@@ -5,6 +5,7 @@
// Flags: --expose-wasm --expose-gc --allow-natives-syntax
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function assertTraps(code, msg) {
var threwException = true;
......@@ -29,38 +30,18 @@ function assertTraps(code, msg) {
}
function makeDivRem(opcode) {
var kBodySize = 5;
var kNameMainOffset = kHeaderSize + 6 + 11 + kBodySize + 1;
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // (int,int) -> int
// -- main function
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameMainOffset, 0, 0, 0, // name offset
kBodySize, 0,
// main body
opcode, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
// names
kDeclEnd,
'm', 'a', 'i', 'n', 0 // --
);
var module = _WASMEXP_.instantiateModule(data);
assertEquals("function", typeof module.main);
return module.main;
function makeBinop(opcode) {
var builder = new WasmModuleBuilder();
builder.addFunction("main", [kAstI32, kAstI32, kAstI32])
.addBody([opcode, kExprGetLocal, 0, kExprGetLocal, 1])
.exportFunc();
return builder.instantiate().exports.main;
}
var divs = makeDivRem(kExprI32DivS);
var divu = makeDivRem(kExprI32DivU);
var divs = makeBinop(kExprI32DivS);
var divu = makeBinop(kExprI32DivU);
assertEquals( 33, divs( 333, 10));
assertEquals(-33, divs(-336, 10));
......@@ -78,8 +59,8 @@ assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
assertEquals(0, divu(0x80000000, -1));
var rems = makeDivRem(kExprI32RemS);
var remu = makeDivRem(kExprI32RemU);
var rems = makeBinop(kExprI32RemS);
var remu = makeBinop(kExprI32RemU);
assertEquals( 3, rems( 333, 10));
assertEquals(-6, rems(-336, 10));
......
......@@ -5,38 +5,20 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
(function testExportedMain() {
var kBodySize = 3;
var kReturnValue = 99;
var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 8 + 1;
var kReturnValue = 88;
var builder = new WasmModuleBuilder();
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
0, kAstI32, // void -> i32
// -- main function
kDeclFunctions,
1,
0, // decl flags
0, 0, // signature index
kBodySize, 0,
// main body
builder.addFunction("main", [kAstI32])
.addBody([
kExprReturn,
kExprI8Const,
kReturnValue,
// exports
kDeclExportTable,
1,
0, 0, // func index index
kNameMainOffset, 0, 0, 0, // function name offset
// names
kDeclEnd,
'm', 'a', 'i', 'n', 0 // --
);
kReturnValue])
.exportFunc();
var ffi = new Object();
var module = _WASMEXP_.instantiateModule(data, ffi);
var module = builder.instantiate();
assertEquals("object", typeof module.exports);
assertEquals("function", typeof module.exports.main);
......@@ -45,45 +27,24 @@ load("test/mjsunit/wasm/wasm-constants.js");
})();
(function testExportedTwice() {
var kBodySize = 3;
var kReturnValue = 99;
var kNameMainOffset = kHeaderSize + 4 + 7 + kBodySize + 14 + 1;
var kNameFooOffset = kNameMainOffset + 5;
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1,
0, kAstI32, // void -> i32
// -- main function
kDeclFunctions,
1,
0, // decl flags
0, 0, // signature index
kBodySize, 0,
// main body
var builder = new WasmModuleBuilder();
builder.addFunction("main", [kAstI32])
.addBody([
kExprReturn,
kExprI8Const,
kReturnValue,
// exports
kDeclExportTable,
2,
0, 0, // func index index
kNameMainOffset, 0, 0, 0, // function name offset
0, 0, // func index index
kNameFooOffset, 0, 0, 0, // function name offset
// names
kDeclEnd,
'b', 'l', 'a', 'h', 0, // --
'f', 'o', 'o', 0 // --
);
kReturnValue])
.exportAs("blah")
.exportAs("foo");
var ffi = new Object();
var module = _WASMEXP_.instantiateModule(data, ffi);
var module = builder.instantiate();
assertEquals("object", typeof module.exports);
assertEquals("function", typeof module.exports.blah);
assertEquals("function", typeof module.exports.foo);
assertEquals(kReturnValue, module.exports.blah());
assertEquals(kReturnValue, module.exports.foo());
assertEquals(kReturnValue, module.exports.blah());
})();
......@@ -5,40 +5,21 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function testCallFFI(ffi) {
var kBodySize = 6;
var kNameAddOffset = kHeaderSize + 28 + kBodySize + 1;
var kNameMainOffset = kNameAddOffset + 4;
var builder = new WasmModuleBuilder();
var data = bytesWithHeader(
kDeclMemory,
1, 1, 1, // memory
// -- signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int
// -- foreign function
kDeclFunctions, 2,
kDeclFunctionName | kDeclFunctionImport,
0, 0, // signature index
kNameAddOffset, 0, 0, 0, // name offset
// -- main function
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature index
kNameMainOffset, 0, 0, 0, // name offset
kBodySize, 0,
// main body
kExprCallFunction, 0, // --
var sig_index = [kAstI32, kAstF64, kAstF64];
builder.addImport("fun", sig_index);
builder.addFunction("main", sig_index)
.addBody([
kExprCallImport, 0, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
// names
kDeclEnd,
'f', 'u', 'n', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
kExprGetLocal, 1]) // --
.exportFunc();
print("instantiate FFI");
var module = _WASMEXP_.instantiateModule(data, ffi);
var module = builder.instantiate(ffi);
}
// everything is good.
......
......@@ -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 builder = new WasmModuleBuilder();
var module = _WASMEXP_.instantiateModule(data, ffi);
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();
assertEquals("function", typeof module.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);
}
}
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
builder.addImport("func", [kAstStmt, type, type]);
builder.addFunction("main", [kAstI32, type, type])
.addBody([
kExprBlock, 2, // --
kExprCallFunction, 0, // --
kExprCallImport, 0, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprI8Const, 99, // --
// names
kDeclEnd,
'f', 'u', 'n', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
kExprI8Const, 99]) // --
.exportFunc()
var module = _WASMEXP_.instantiateModule(data, ffi);
assertEquals("function", typeof module.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;
......@@ -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
var builder = new WasmModuleBuilder();
builder.addImport("print", [kAstStmt, kAstI32]);
builder.addImport("print", [kAstStmt, kAstF64]);
builder.addFunction("main", [kAstStmt, kAstF64])
.addBody([
kExprBlock, 2, // --
kExprCallFunction, 0, // --
kExprCallImport, 0, // --
kExprI8Const, 97, // --
kExprCallFunction, 1, // --
kExprGetLocal, 0, // --
// names
kDeclEnd,
'p', 'r', 'i', 'n', 't', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
kExprCallImport, 1, // --
kExprGetLocal, 0]) // --
.exportFunc()
var module = _WASMEXP_.instantiateModule(data, ffi);
assertEquals("function", typeof module.main);
for (var i = -9; i < 900; i += 6.125) {
module.main(i);
}
var main = builder.instantiate({print: print}).exports.main;
for (var i = -9; i < 900; i += 6.125) main(i);
}
testCallPrint();
......
......@@ -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
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, // --
// 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);
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
var builder = new WasmModuleBuilder();
builder.addImport("func", [kAstStmt, type, type]);
builder.addFunction("main", [kAstI32, type, type])
.addBody([
kExprBlock, 2, // --
kExprCallFunction, 0, // --
kExprCallImport, 0, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
kExprI8Const, 99, // --
// names
kDeclEnd,
'f', 'u', 'n', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
kExprI8Const, 99])
.exportFunc("main");
var module = _WASMEXP_.instantiateModule(data, ffi);
assertEquals("function", typeof module.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
var builder = new WasmModuleBuilder();
builder.addImport("print", [kAstStmt, kAstI32]);
builder.addImport("print", [kAstStmt, kAstF64]);
builder.addFunction("main", [kAstStmt, kAstF64])
.addBody([
kExprBlock, 2, // --
kExprCallFunction, 0, // --
kExprCallImport, 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);
kExprCallImport, 1, // --
kExprGetLocal, 0]) // --
.exportFunc();
assertEquals("function", typeof module.main);
var main = builder.instantiate({print: print}).exports.main;
for (var i = -9; i < 900; i += 6.125) {
module.main(i);
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 builder = new WasmModuleBuilder();
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
builder.addImport("foo", [kAstI32]);
builder.addImport("bar", [kAstI32]);
builder.addFunction("main", [kAstI32])
.addBody([
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());
kExprCallImport, 1]) // --
.exportFunc();
var main = builder.instantiate({foo: foo, bar: bar}).exports.main;
assertEquals(expected, main());
}
testCallImport2(function() { return 33; }, function () { return 44; }, 77);
......@@ -5,69 +5,42 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
var module = (function () {
var kFuncWithBody = 9;
var kFuncImported = 7;
var kBodySize1 = 5;
var kBodySize2 = 8;
var kFuncTableSize = 8;
var kSubOffset = kHeaderSize + 13 + kFuncWithBody + kBodySize1 + kFuncImported + kFuncWithBody + kBodySize2 + kFuncTableSize + 1;
var kAddOffset = kSubOffset + 4;
var kMainOffset = kAddOffset + 4;
var ffi = new Object();
ffi.add = (function(a, b) { return a + b | 0; });
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- signatures
kDeclSignatures, 2,
2, kAstI32, kAstI32, kAstI32, // int, int -> int
3, kAstI32, kAstI32, kAstI32, kAstI32, // int, int, int -> int
// -- function #0 (sub)
kDeclFunctions, 3,
kDeclFunctionName,
0, 0, // signature offset
kSubOffset, 0, 0, 0, // name offset
kBodySize1, 0, // body size
kExprI32Sub, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
// -- function #1 (add)
kDeclFunctionName | kDeclFunctionImport,
0, 0, // signature offset
kAddOffset, 0, 0, 0, // name offset
// -- function #2 (main)
kDeclFunctionName | kDeclFunctionExport,
1, 0, // signature offset
kMainOffset, 0, 0, 0, // name offset
kBodySize2, 0, // body size
kExprCallIndirect, 0,
var builder = new WasmModuleBuilder();
var sig_index = builder.addSignature([kAstI32, kAstI32, kAstI32]);
builder.addImport("add", sig_index);
builder.addFunction("add", sig_index)
.addBody([
kExprCallImport, 0, kExprGetLocal, 0, kExprGetLocal, 1
]);
builder.addFunction("sub", sig_index)
.addBody([
kExprI32Sub, kExprGetLocal, 0, kExprGetLocal, 1
]);
builder.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32])
.addBody([
kExprCallIndirect, sig_index,
kExprGetLocal, 0,
kExprGetLocal, 1,
kExprGetLocal, 2,
// -- function table
kDeclFunctionTable,
3,
0, 0,
1, 0,
2, 0,
kDeclEnd,
's', 'u', 'b', 0, // name
'a', 'd', 'd', 0, // name
'm', 'a', 'i', 'n', 0 // name
), ffi);
kExprGetLocal, 2])
.exportFunc()
builder.appendToFunctionTable([0, 1, 2]);
return builder.instantiate({add: function(a, b) { return a + b | 0; }});
})();
// Check the module exists.
assertFalse(module === undefined);
assertFalse(module === null);
assertFalse(module === 0);
assertEquals("object", typeof module);
assertEquals("function", typeof module.main);
assertEquals("object", typeof module.exports);
assertEquals("function", typeof module.exports.main);
assertEquals(5, module.main(0, 12, 7));
assertEquals(19, module.main(1, 12, 7));
assertEquals(5, module.exports.main(1, 12, 7));
assertEquals(19, module.exports.main(0, 12, 7));
assertTraps(kTrapFuncSigMismatch, "module.main(2, 12, 33)");
assertTraps(kTrapFuncInvalid, "module.main(3, 12, 33)");
assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)");
......@@ -5,33 +5,20 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
var kReturnValue = 117;
var kBodySize = 2;
var kNameOffset = kHeaderSize + 19 + kBodySize + 1;
var data = bytesWithHeader(
// -- memory
kDeclMemory,
1, 1, 1,
// -- signatures
kDeclSignatures, 1,
0, kAstI32, // signature: void -> int
// -- main function
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature index
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0, // body size
// -- body
kExprI8Const, // --
kReturnValue, // --
kDeclEnd,
'm', 'a', 'i', 'n', 0 // name
);
var module = _WASMEXP_.instantiateModule(data);
var module = (function Build() {
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true);
builder.addFunction("main", [kAstI32])
.addBody([kExprI8Const, kReturnValue])
.exportFunc();
return builder.instantiate();
})();
// Check the module exists.
assertFalse(module === undefined);
......@@ -54,9 +41,10 @@ for (var i = 0; i < 4; i++) {
assertEquals(65536, module.memory.byteLength);
// Check the properties of the main function.
assertFalse(module.main === undefined);
assertFalse(module.main === null);
assertFalse(module.main === 0);
assertEquals("function", typeof module.main);
var main = module.exports.main;
assertFalse(main === undefined);
assertFalse(main === null);
assertFalse(main === 0);
assertEquals("function", typeof main);
assertEquals(kReturnValue, module.main());
assertEquals(kReturnValue, main());
......@@ -5,27 +5,16 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
var kReturnValue = 97;
(function BasicTest() {
var kReturnValue = 107;
var builder = new WasmModuleBuilder();
var kBodySize = 2;
var kNameOffset = kHeaderSize + 15 + kBodySize + 1;
builder.addFunction("main", [kAstI32])
.addBody([kExprI8Const, kReturnValue])
.exportFunc();
var data = bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstI32, // signature: void -> int
// -- main function
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature index
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0, // body size
// -- body
kExprI8Const, // --
kReturnValue, // --
kDeclEnd,
'm', 'a', 'i', 'n', 0 // name
);
assertEquals(kReturnValue, _WASMEXP_.instantiateModule(data).main());
var main = builder.instantiate().exports.main;
assertEquals(kReturnValue, main());
})();
......@@ -5,30 +5,16 @@
// Flags: --expose-wasm --expose-gc --stress-compaction
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
var kMemSize = 65536;
function genModule(memory) {
var kBodySize = 27;
var kNameMainOffset = kHeaderSize + 28 + kBodySize + 1;
var data = bytesWithHeader(
kDeclMemory,
1, 1, 1, // memory
// -- signatures
kDeclSignatures, 1,
1, kAstI32, kAstI32, // int->int
// -- main function
kDeclFunctions, 1,
kDeclFunctionLocals | kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameMainOffset, 0, 0, 0, // name offset
1, 0, // local int32 count
0, 0, // local int64 count
0, 0, // local float32 count
0, 0, // local float64 count
kBodySize, 0, // code size
// main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true);
builder.addFunction("main", [kAstI32, kAstI32])
.addBody([
kExprBlock,2,
kExprLoop,1,
kExprIf,
......@@ -39,18 +25,16 @@ function genModule(memory) {
kExprBr,2, kExprI8Const, 255,
kExprSetLocal,0,
kExprI32Sub,kExprGetLocal,0,kExprI8Const,4,
kExprI8Const,0,
// names
kDeclEnd,
'm', 'a', 'i', 'n', 0 // --
);
kExprI8Const,0])
.exportFunc();
return _WASMEXP_.instantiateModule(data, null, memory);
return builder.instantiate(null, memory);
}
function testPokeMemory() {
var module = genModule(null);
var buffer = module.memory;
var main = module.exports.main;
assertEquals(kMemSize, buffer.byteLength);
var array = new Int8Array(buffer);
......@@ -61,21 +45,21 @@ function testPokeMemory() {
}
for (var i = 0; i < 10; i++) {
assertEquals(0, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize - 4));
array[kMemSize/2 + i] = 1;
assertEquals(0, module.main(kMemSize/2 - 4));
assertEquals(-1, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize/2 - 4));
assertEquals(-1, main(kMemSize - 4));
array[kMemSize/2 + i] = 0;
assertEquals(0, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize - 4));
}
}
testPokeMemory();
function testSurvivalAcrossGc() {
var checker = genModule(null).main;
var checker = genModule(null).exports.main;
for (var i = 0; i < 5; i++) {
print("gc run ", i);
assertEquals(0, checker(kMemSize - 4));
......@@ -92,6 +76,7 @@ testSurvivalAcrossGc();
function testPokeOuterMemory() {
var buffer = new ArrayBuffer(kMemSize);
var module = genModule(buffer);
var main = module.exports.main;
assertEquals(kMemSize, buffer.byteLength);
var array = new Int8Array(buffer);
......@@ -102,14 +87,14 @@ function testPokeOuterMemory() {
}
for (var i = 0; i < 10; i++) {
assertEquals(0, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize - 4));
array[kMemSize/2 + i] = 1;
assertEquals(0, module.main(kMemSize/2 - 4));
assertEquals(-1, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize/2 - 4));
assertEquals(-1, main(kMemSize - 4));
array[kMemSize/2 + i] = 0;
assertEquals(0, module.main(kMemSize - 4));
assertEquals(0, main(kMemSize - 4));
}
}
......@@ -117,7 +102,7 @@ testPokeOuterMemory();
function testOuterMemorySurvivalAcrossGc() {
var buffer = new ArrayBuffer(kMemSize);
var checker = genModule(buffer).main;
var checker = genModule(buffer).exports.main;
for (var i = 0; i < 5; i++) {
print("gc run ", i);
assertEquals(0, checker(kMemSize - 4));
......@@ -132,39 +117,21 @@ testOuterMemorySurvivalAcrossGc();
function testOOBThrows() {
var kBodySize = 8;
var kNameMainOffset = kHeaderSize + 29 + kBodySize + 1;
var data = bytesWithHeader(
kDeclMemory,
1, 1, 1, // memory = 64KB
// -- signatures
kDeclSignatures, 1,
2, kAstI32, kAstI32, kAstI32, // int->int
// -- main function
kDeclFunctions, 1,
kDeclFunctionLocals | kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameMainOffset, 0, 0, 0, // name offset
1, 0, // local int32 count
0, 0, // local int64 count
0, 0, // local float32 count
0, 0, // local float64 count
kBodySize, 0, // code size
// geti: return mem[a] = mem[b]
kExprI32StoreMem, 0, kExprGetLocal, 0, kExprI32LoadMem, 0, kExprGetLocal, 1,
// names
kDeclEnd,
'g','e','t','i', 0 // --
);
var memory = null;
var module = _WASMEXP_.instantiateModule(data, null, memory);
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true);
builder.addFunction("geti", [kAstI32, kAstI32, kAstI32])
.addBody([
kExprI32StoreMem, 0, kExprGetLocal, 0, kExprI32LoadMem, 0, kExprGetLocal, 1
])
.exportFunc();
var module = builder.instantiate();
var offset;
function read() { return module.geti(0, offset); }
function write() { return module.geti(offset, 0); }
function read() { return module.exports.geti(0, offset); }
function write() { return module.exports.geti(offset, 0); }
for (offset = 0; offset < 65533; offset++) {
assertEquals(0, read());
......
......@@ -5,54 +5,39 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function runSelect2(module, which, a, b) {
assertEquals(which == 0 ? a : b, module.select(a, b));
function runSelect2(select, which, a, b) {
assertEquals(which == 0 ? a : b, select(a, b));
}
function testSelect2(type) {
var kBodySize = 2;
var kNameOffset = kHeaderSize + 21 + kBodySize + 1;
for (var which = 0; which < 2; which++) {
print("type = " + type + ", which = " + which);
var data = bytesWithHeader(
// -- memory
kDeclMemory,
1, 1, 1, // memory
// -- signatures
kDeclSignatures, 1,
2, type, type, type, // signature: (t,t)->t
// -- select
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0, // body size
kExprGetLocal, which, // --
kDeclEnd,
's','e','l','e','c','t',0 // name
);
var module = _WASMEXP_.instantiateModule(data);
assertEquals("function", typeof module.select);
runSelect2(module, which, 99, 97);
runSelect2(module, which, -99, -97);
var builder = new WasmModuleBuilder();
builder.addFunction("select", [type, type, type])
.addBody([kExprGetLocal, which])
.exportFunc()
var select = builder.instantiate().exports.select;
runSelect2(select, which, 99, 97);
runSelect2(select, which, -99, -97);
if (type != kAstF32) {
runSelect2(module, which, 0x80000000 | 0, 0x7fffffff | 0);
runSelect2(module, which, 0x80000001 | 0, 0x7ffffffe | 0);
runSelect2(module, which, 0xffffffff | 0, 0xfffffffe | 0);
runSelect2(module, which, -2147483647, 2147483646);
runSelect2(module, which, -2147483646, 2147483645);
runSelect2(module, which, -2147483648, 2147483647);
runSelect2(select, which, 0x80000000 | 0, 0x7fffffff | 0);
runSelect2(select, which, 0x80000001 | 0, 0x7ffffffe | 0);
runSelect2(select, which, 0xffffffff | 0, 0xfffffffe | 0);
runSelect2(select, which, -2147483647, 2147483646);
runSelect2(select, which, -2147483646, 2147483645);
runSelect2(select, which, -2147483648, 2147483647);
}
if (type != kAstI32 && type != kAstI64) {
runSelect2(module, which, -1.25, 5.25);
runSelect2(module, which, Infinity, -Infinity);
runSelect2(select, which, -1.25, 5.25);
runSelect2(select, which, Infinity, -Infinity);
}
}
}
......@@ -63,20 +48,20 @@ testSelect2(kAstF32);
testSelect2(kAstF64);
function runSelect10(module, which, a, b) {
function runSelect10(select, which, a, b) {
var x = -1;
var result = [
module.select(a, b, x, x, x, x, x, x, x, x),
module.select(x, a, b, x, x, x, x, x, x, x),
module.select(x, x, a, b, x, x, x, x, x, x),
module.select(x, x, x, a, b, x, x, x, x, x),
module.select(x, x, x, x, a, b, x, x, x, x),
module.select(x, x, x, x, x, a, b, x, x, x),
module.select(x, x, x, x, x, x, a, b, x, x),
module.select(x, x, x, x, x, x, x, a, b, x),
module.select(x, x, x, x, x, x, x, x, a, b),
module.select(x, x, x, x, x, x, x, x, x, a)
select(a, b, x, x, x, x, x, x, x, x),
select(x, a, b, x, x, x, x, x, x, x),
select(x, x, a, b, x, x, x, x, x, x),
select(x, x, x, a, b, x, x, x, x, x),
select(x, x, x, x, a, b, x, x, x, x),
select(x, x, x, x, x, a, b, x, x, x),
select(x, x, x, x, x, x, a, b, x, x),
select(x, x, x, x, x, x, x, a, b, x),
select(x, x, x, x, x, x, x, x, a, b),
select(x, x, x, x, x, x, x, x, x, a)
];
for (var i = 0; i < 10; i++) {
......@@ -86,49 +71,36 @@ function runSelect10(module, which, a, b) {
}
}
function testSelect10(type) {
function testSelect10(t) {
var kBodySize = 2;
var kNameOffset = kHeaderSize + 29 + kBodySize + 1;
for (var which = 0; which < 10; which++) {
print("type = " + type + ", which = " + which);
var t = type;
var data = bytesWithHeader(
kDeclMemory,
1, 1, 1, // memory
// signatures
kDeclSignatures, 1,
10, t,t,t,t,t,t,t,t,t,t,t, // (tx10)->t
// main function
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameOffset, 0, 0, 0, // name offset
kBodySize, 0, // body size
kExprGetLocal, which, // --
kDeclEnd,
's','e','l','e','c','t',0 // name
);
var module = _WASMEXP_.instantiateModule(data);
assertEquals("function", typeof module.select);
runSelect10(module, which, 99, 97);
runSelect10(module, which, -99, -97);
if (type != kAstF32) {
runSelect10(module, which, 0x80000000 | 0, 0x7fffffff | 0);
runSelect10(module, which, 0x80000001 | 0, 0x7ffffffe | 0);
runSelect10(module, which, 0xffffffff | 0, 0xfffffffe | 0);
runSelect10(module, which, -2147483647, 2147483646);
runSelect10(module, which, -2147483646, 2147483645);
runSelect10(module, which, -2147483648, 2147483647);
print("type = " + t + ", which = " + which);
var builder = new WasmModuleBuilder();
builder.addFunction("select", [t,t,t,t,t,t,t,t,t,t,t])
.addBody([kExprGetLocal, which])
.exportFunc();
var select = builder.instantiate().exports.select;
assertEquals("function", typeof select);
runSelect10(select, which, 99, 97);
runSelect10(select, which, -99, -97);
if (t != kAstF32) {
runSelect10(select, which, 0x80000000 | 0, 0x7fffffff | 0);
runSelect10(select, which, 0x80000001 | 0, 0x7ffffffe | 0);
runSelect10(select, which, 0xffffffff | 0, 0xfffffffe | 0);
runSelect10(select, which, -2147483647, 2147483646);
runSelect10(select, which, -2147483646, 2147483645);
runSelect10(select, which, -2147483648, 2147483647);
}
if (type != kAstI32 && type != kAstI64) {
runSelect10(module, which, -1.25, 5.25);
runSelect10(module, which, Infinity, -Infinity);
if (t != kAstI32 && t != kAstI64) {
runSelect10(select, which, -1.25, 5.25);
runSelect10(select, which, Infinity, -Infinity);
}
}
}
......
......@@ -5,44 +5,14 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function testStack(func, check) {
var kBodySize = 2;
var kNameFunOffset = kHeaderSize + 22 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytesWithHeader(
// signatures
kDeclSignatures, 1, // --
0, kAstStmt, // () -> void
// -- 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, // --
// names
kDeclEnd, // --
'f', 'u', 'n', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
var module = _WASMEXP_.instantiateModule(data, ffi);
assertEquals("function", typeof module.main);
module.main();
check();
}
var expected = "Error\n" +
// The line numbers below will change as this test gains / loses lines..
" at STACK (stack.js:24:11)\n" + // --
" at <WASM> (<anonymous>)\n" + // TODO(jfb): wasm stack here.
" at testStack (stack.js:38:18)\n" + // --
" at stack.js:40:3"; // --
// The stack trace contains file path, only keep "stack.js".
function stripPath(s) {
......@@ -55,16 +25,16 @@ function STACK() {
stack = e.stack;
}
function check_STACK() {
assertEquals(expected, stripPath(stack));
}
(function testStack() {
var builder = new WasmModuleBuilder();
var expected = "Error\n" +
// The line numbers below will change as this test gains / loses lines..
" at STACK (stack.js:54:11)\n" + // --
" at <WASM> (<anonymous>)\n" + // --
" at testStack (stack.js:43:10)\n" +
// TODO(jfb) Add WebAssembly stack here.
" at stack.js:70:1";
builder.addImport("func", [kAstStmt]);
builder.addFunction(undefined, [kAstStmt])
.addBody([kExprCallImport, 0])
.exportAs("main");
testStack(STACK, check_STACK);
var module = builder.instantiate({func: STACK});
module.exports.main();
assertEquals(expected, stripPath(stack));
})();
......@@ -5,44 +5,21 @@
// Flags: --expose-wasm --expose-gc --allow-natives-syntax
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function makeFFI(func) {
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);
var builder = new WasmModuleBuilder();
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]) // --
.exportFunc()
return module.main;
return builder.instantiate({func: func}).exports.main;
}
......
......@@ -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);
})();
......@@ -5,42 +5,20 @@
// Flags: --expose-wasm
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
var module = (function () {
var kFuncWithBody = 9;
var kFuncImported = 7;
var kBodySize1 = 1;
var kMainOffset = kHeaderSize + 6 + kFuncWithBody + kBodySize1 + 1;
var main = (function () {
var builder = new WasmModuleBuilder();
builder.addFunction("main", [kAstStmt])
.addBody([kExprUnreachable])
.exportAs("main");
var ffi = new Object();
ffi.add = (function(a, b) { return a + b | 0; });
return _WASMEXP_.instantiateModule(bytesWithHeader(
// -- signatures
kDeclSignatures, 1,
0, kAstStmt, // void -> void
// -- function #0 (unreachable)
kDeclFunctions, 1,
kDeclFunctionName | kDeclFunctionExport,
0, 0, // signature offset
kMainOffset, 0, 0, 0, // name offset
kBodySize1, 0, // body size
kExprUnreachable,
kDeclEnd,
'm', 'a', 'i', 'n', 0 // name
), ffi);
return builder.instantiate().exports.main;
})();
// Check the module exists.
assertFalse(module === undefined);
assertFalse(module === null);
assertFalse(module === 0);
assertEquals("object", typeof module);
assertEquals("function", typeof module.main);
var exception = "";
try {
assertEquals(0, module.main());
assertEquals(0, main());
} catch(e) {
print("correctly caught: " + e);
exception = e;
......
......@@ -27,6 +27,7 @@ var kWasmV2 = 0;
var kWasmV3 = 0;
var kHeaderSize = 8;
var kPageSize = 65536;
function bytesWithHeader() {
var buffer = new ArrayBuffer(kHeaderSize + arguments.length);
......
......@@ -23,6 +23,11 @@ WasmFunctionBuilder.prototype.exportAs = function(name) {
return this;
}
WasmFunctionBuilder.prototype.exportFunc = function() {
this.exports.push(this.name);
return this;
}
WasmFunctionBuilder.prototype.addBody = function(body) {
this.body = body;
return this;
......@@ -40,14 +45,24 @@ function WasmModuleBuilder() {
this.exports = [];
this.function_table = [];
this.data_segments = [];
this.explicit = [];
return this;
}
WasmModuleBuilder.prototype.addStart = function(start_index) {
this.start_index = start_index;
}
WasmModuleBuilder.prototype.addMemory = function(min, max, exp) {
this.memory = {min: min, max: max, exp: exp};
return this;
}
WasmModuleBuilder.prototype.addExplicitSection = function(bytes) {
this.explicit.push(bytes);
return this;
}
// Add a signature; format is [rettype, param0, param1, ...]
WasmModuleBuilder.prototype.addSignature = function(sig) {
// TODO: canonicalize signatures?
......@@ -199,6 +214,13 @@ WasmModuleBuilder.prototype.toArray = function(debug) {
}
}
// Add start function section.
if (this.start_index != undefined) {
if (debug) print("emitting start function @ " + bytes.length);
emit_u8(bytes, kDeclStartFunction);
emit_varint(bytes, this.start_index);
}
if (this.function_table.length > 0) {
if (debug) print("emitting function table @ " + bytes.length);
emit_u8(bytes, kDeclFunctionTable);
......@@ -232,6 +254,14 @@ WasmModuleBuilder.prototype.toArray = function(debug) {
}
}
// Emit any explicitly added sections
for (exp of this.explicit) {
if (debug) print("emitting explicit @ " + bytes.length);
for (var i = 0; i < exp.length; i++) {
emit_u8(bytes, exp[i]);
}
}
// End the module.
if (debug) print("emitting end @ " + bytes.length);
emit_u8(bytes, kDeclEnd);
......@@ -305,7 +335,11 @@ WasmModuleBuilder.prototype.toBuffer = function(debug) {
return buffer;
}
WasmModuleBuilder.prototype.instantiate = function(ffi) {
WasmModuleBuilder.prototype.instantiate = function(ffi, memory) {
var buffer = this.toBuffer();
if (memory != undefined) {
return _WASMEXP_.instantiateModule(buffer, ffi, memory);
} else {
return _WASMEXP_.instantiateModule(buffer, ffi);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment