Chromium Code Reviews| Index: test/mjsunit/wasm/test-wasm-module-builder.js |
| diff --git a/test/mjsunit/wasm/test-wasm-module-builder.js b/test/mjsunit/wasm/test-wasm-module-builder.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1bab7293fb9419abe387a3bfbd004094def75c82 |
| --- /dev/null |
| +++ b/test/mjsunit/wasm/test-wasm-module-builder.js |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2016 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Flags: --expose-wasm |
| + |
| +load('test/mjsunit/wasm/wasm-constants.js'); |
| +load('test/mjsunit/wasm/wasm-module-builder.js'); |
| + |
| +var debug = (arguments[0] == "debug"); |
| + |
| +(function BasicTest() { |
| + var module = new WasmModuleBuilder(); |
| + module.AddMemory(1, 2, false); |
| + module.AddFunction("foo", [kAstI32]) |
| + .AddBody([kExprI8Const, 11]) |
| + .ExportAs("blarg"); |
| + |
| + var buffer = module.ToBuffer(debug); |
| + var instance = _WASMEXP_.instantiateModule(buffer); |
| + assertEquals(11, instance.exports.blarg()); |
| +})(); |
| + |
| +(function ImportTest() { |
| + var module = new WasmModuleBuilder(); |
| + var index = module.AddImport("print", [kAstStmt, kAstI32]); |
| + module.AddFunction("foo", [kAstStmt]) |
| + .AddBody([kExprCallImport, index, kExprI8Const, 13]) |
| + .ExportAs("main"); |
| + |
| + var buffer = module.ToBuffer(debug); |
| + var instance = _WASMEXP_.instantiateModule(buffer, {print: print}); |
| + print("should print 13! "); |
| + instance.exports.main(); |
| +})(); |
| + |
| +(function LocalsTest() { |
| + var module = new WasmModuleBuilder(); |
| + module.AddFunction(undefined, [kAstI32, kAstI32]) |
| + .AddLocals({i32_count: 1}) |
| + .AddBody([kExprSetLocal, 1, kExprGetLocal, 0]) |
| + .ExportAs("main"); |
| + |
| + var buffer = module.ToBuffer(debug); |
| + var instance = _WASMEXP_.instantiateModule(buffer); |
| + assertEquals(19, instance.exports.main(19)); |
| + assertEquals(27777, instance.exports.main(27777)); |
| +})(); |
| + |
| +(function CallTest() { |
| + var module = new WasmModuleBuilder(); |
| + module.AddFunction("add", [kAstI32, kAstI32, kAstI32]) |
| + .AddBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]) |
| + module.AddFunction("main", [kAstI32, kAstI32, kAstI32]) |
|
binji
2016/03/04 05:13:26
weird indentation here. missing semicolon on the p
titzer
2016/03/05 01:26:22
Good catch. Semicolon insertion to the rescue :-)
|
| + .AddBody([kExprCallFunction, 0, kExprGetLocal, 0, kExprGetLocal, 1]) |
| + .ExportAs("main"); |
| + |
| + var instance = module.Instantiate(); |
| + assertEquals(44, instance.exports.main(11, 33)); |
| + assertEquals(7777, instance.exports.main(2222, 5555)); |
| +})(); |
| + |
| +(function IndirectCallTest() { |
| + var module = new WasmModuleBuilder(); |
| + module.AddFunction("add", [kAstI32, kAstI32, kAstI32]).AddBody([ |
| + kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1 |
| + ]); |
| + module.AddFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) |
| + .AddBody([ |
| + kExprCallIndirect, 0, kExprGetLocal, 0, kExprGetLocal, 1, kExprGetLocal, |
| + 2 |
| + ]) |
| + .ExportAs("main"); |
| + module.AppendToFunctionTable([0]); |
| + |
| + var instance = module.Instantiate(); |
| + assertEquals(44, instance.exports.main(0, 11, 33)); |
| + assertEquals(7777, instance.exports.main(0, 2222, 5555)); |
| + assertThrows(function() { instance.exports.main(1, 1, 1); }); |
| +})(); |
| + |
| +(function DataSegmentTest() { |
| + var module = new WasmModuleBuilder(); |
| + module.AddMemory(1, 1, false); |
| + module.AddFunction("load", [kAstI32, kAstI32]) |
| + .AddBody([kExprI32LoadMem, 0, kExprGetLocal, 0]) |
| + .ExportAs("load"); |
| + module.AddDataSegment(0, [9, 9, 9, 9], true); |
| + |
| + var buffer = module.ToBuffer(debug); |
| + var instance = _WASMEXP_.instantiateModule(buffer); |
| + assertEquals(151587081, instance.exports.load(0)); |
| +})(); |