OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --expose-wasm | |
6 | |
7 load('test/mjsunit/wasm/wasm-constants.js'); | |
8 load('test/mjsunit/wasm/wasm-module-builder.js'); | |
9 | |
10 var debug = (arguments[0] == "debug"); | |
11 | |
12 (function BasicTest() { | |
13 var module = new WasmModuleBuilder(); | |
14 module.AddMemory(1, 2, false); | |
15 module.AddFunction("foo", [kAstI32]) | |
16 .AddBody([kExprI8Const, 11]) | |
17 .ExportAs("blarg"); | |
18 | |
19 var buffer = module.ToBuffer(debug); | |
20 var instance = _WASMEXP_.instantiateModule(buffer); | |
21 assertEquals(11, instance.exports.blarg()); | |
22 })(); | |
23 | |
24 (function ImportTest() { | |
25 var module = new WasmModuleBuilder(); | |
26 var index = module.AddImport("print", [kAstStmt, kAstI32]); | |
27 module.AddFunction("foo", [kAstStmt]) | |
28 .AddBody([kExprCallImport, index, kExprI8Const, 13]) | |
29 .ExportAs("main"); | |
30 | |
31 var buffer = module.ToBuffer(debug); | |
32 var instance = _WASMEXP_.instantiateModule(buffer, {print: print}); | |
33 print("should print 13! "); | |
34 instance.exports.main(); | |
35 })(); | |
36 | |
37 (function LocalsTest() { | |
38 var module = new WasmModuleBuilder(); | |
39 module.AddFunction(undefined, [kAstI32, kAstI32]) | |
40 .AddLocals({i32_count: 1}) | |
41 .AddBody([kExprSetLocal, 1, kExprGetLocal, 0]) | |
42 .ExportAs("main"); | |
43 | |
44 var buffer = module.ToBuffer(debug); | |
45 var instance = _WASMEXP_.instantiateModule(buffer); | |
46 assertEquals(19, instance.exports.main(19)); | |
47 assertEquals(27777, instance.exports.main(27777)); | |
48 })(); | |
49 | |
50 (function CallTest() { | |
51 var module = new WasmModuleBuilder(); | |
52 module.AddFunction("add", [kAstI32, kAstI32, kAstI32]) | |
53 .AddBody([kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1]) | |
54 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 :-)
| |
55 .AddBody([kExprCallFunction, 0, kExprGetLocal, 0, kExprGetLocal, 1]) | |
56 .ExportAs("main"); | |
57 | |
58 var instance = module.Instantiate(); | |
59 assertEquals(44, instance.exports.main(11, 33)); | |
60 assertEquals(7777, instance.exports.main(2222, 5555)); | |
61 })(); | |
62 | |
63 (function IndirectCallTest() { | |
64 var module = new WasmModuleBuilder(); | |
65 module.AddFunction("add", [kAstI32, kAstI32, kAstI32]).AddBody([ | |
66 kExprI32Add, kExprGetLocal, 0, kExprGetLocal, 1 | |
67 ]); | |
68 module.AddFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32]) | |
69 .AddBody([ | |
70 kExprCallIndirect, 0, kExprGetLocal, 0, kExprGetLocal, 1, kExprGetLocal, | |
71 2 | |
72 ]) | |
73 .ExportAs("main"); | |
74 module.AppendToFunctionTable([0]); | |
75 | |
76 var instance = module.Instantiate(); | |
77 assertEquals(44, instance.exports.main(0, 11, 33)); | |
78 assertEquals(7777, instance.exports.main(0, 2222, 5555)); | |
79 assertThrows(function() { instance.exports.main(1, 1, 1); }); | |
80 })(); | |
81 | |
82 (function DataSegmentTest() { | |
83 var module = new WasmModuleBuilder(); | |
84 module.AddMemory(1, 1, false); | |
85 module.AddFunction("load", [kAstI32, kAstI32]) | |
86 .AddBody([kExprI32LoadMem, 0, kExprGetLocal, 0]) | |
87 .ExportAs("load"); | |
88 module.AddDataSegment(0, [9, 9, 9, 9], true); | |
89 | |
90 var buffer = module.ToBuffer(debug); | |
91 var instance = _WASMEXP_.instantiateModule(buffer); | |
92 assertEquals(151587081, instance.exports.load(0)); | |
93 })(); | |
OLD | NEW |