OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
| 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
8 | 9 |
9 var module = (function () { | 10 function assertModule(module, memsize) { |
10 var kBodySize = 5; | 11 // Check the module exists. |
11 var kNameOffset = kHeaderSize + 21 + kBodySize + 1; | 12 assertFalse(module === undefined); |
| 13 assertFalse(module === null); |
| 14 assertFalse(module === 0); |
| 15 assertEquals("object", typeof module); |
12 | 16 |
13 return _WASMEXP_.instantiateModule(bytesWithHeader( | 17 // Check the memory is an ArrayBuffer. |
14 // -- memory | 18 var mem = module.memory; |
15 kDeclMemory, | 19 assertFalse(mem === undefined); |
16 1, 1, 1, | 20 assertFalse(mem === null); |
17 // -- signatures | 21 assertFalse(mem === 0); |
18 kDeclSignatures, 1, | 22 assertEquals("object", typeof mem); |
19 2, kAstI32, kAstI32, kAstI32, // int, int -> int | 23 assertTrue(mem instanceof ArrayBuffer); |
20 // -- functions | 24 for (var i = 0; i < 4; i++) { |
21 kDeclFunctions, 1, | 25 module.memory = 0; // should be ignored |
22 kDeclFunctionName | kDeclFunctionExport, | 26 assertEquals(mem, module.memory); |
23 0, 0, | 27 } |
24 kNameOffset, 0, 0, 0, // name offset | 28 |
25 kBodySize, 0, | 29 assertEquals(memsize, module.memory.byteLength); |
26 // -- body | 30 } |
27 kExprI32Sub, // -- | 31 |
28 kExprGetLocal, 0, // -- | 32 function assertFunction(module, func) { |
29 kExprGetLocal, 1, // -- | 33 assertEquals("object", typeof module.exports); |
30 kDeclEnd, | 34 |
31 's', 'u', 'b', 0 // name | 35 var exp = module.exports[func]; |
32 )); | 36 assertFalse(exp === undefined); |
| 37 assertFalse(exp === null); |
| 38 assertFalse(exp === 0); |
| 39 assertEquals("function", typeof exp); |
| 40 |
| 41 return exp; |
| 42 } |
| 43 |
| 44 (function SubTest() { |
| 45 |
| 46 var builder = new WasmModuleBuilder(); |
| 47 |
| 48 builder.addMemory(1, 1, true); |
| 49 builder.addFunction("sub", [kAstI32, kAstI32, kAstI32]) |
| 50 .addBody([ |
| 51 kExprI32Sub, // -- |
| 52 kExprGetLocal, 0, // -- |
| 53 kExprGetLocal, 1]) // -- |
| 54 .exportFunc() |
| 55 |
| 56 var module = builder.instantiate(); |
| 57 assertModule(module, kPageSize); |
| 58 |
| 59 // Check the properties of the sub function. |
| 60 var sub = assertFunction(module, "sub"); |
| 61 assertEquals(-55, sub(33, 88)); |
| 62 assertEquals(-55555, sub(33333, 88888)); |
| 63 assertEquals(-5555555, sub(3333333, 8888888)); |
33 })(); | 64 })(); |
34 | 65 |
35 // Check the module exists. | |
36 assertFalse(module === undefined); | |
37 assertFalse(module === null); | |
38 assertFalse(module === 0); | |
39 assertEquals("object", typeof module); | |
40 | 66 |
41 // Check the memory is an ArrayBuffer. | 67 (function NopTest() { |
42 var mem = module.memory; | |
43 assertFalse(mem === undefined); | |
44 assertFalse(mem === null); | |
45 assertFalse(mem === 0); | |
46 assertEquals("object", typeof mem); | |
47 assertTrue(mem instanceof ArrayBuffer); | |
48 for (var i = 0; i < 4; i++) { | |
49 module.memory = 0; // should be ignored | |
50 assertEquals(mem, module.memory); | |
51 } | |
52 | 68 |
53 assertEquals(65536, module.memory.byteLength); | 69 var builder = new WasmModuleBuilder(); |
54 | 70 |
55 // Check the properties of the sub function. | 71 var kPages = 2; |
56 assertEquals("function", typeof module.sub); | 72 builder.addMemory(kPages, kPages, true); |
| 73 builder.addFunction("nop", [kAstStmt]) |
| 74 .addBody([kExprNop]) |
| 75 .exportFunc(); |
57 | 76 |
58 assertEquals(-55, module.sub(33, 88)); | 77 var module = builder.instantiate(); |
59 assertEquals(-55555, module.sub(33333, 88888)); | 78 assertModule(module, kPageSize * kPages); |
60 assertEquals(-5555555, module.sub(3333333, 8888888)); | 79 |
| 80 var nop = assertFunction(module, "nop"); |
| 81 assertEquals(undefined, nop()); |
| 82 })(); |
61 | 83 |
62 | 84 |
63 var module = (function() { | 85 (function testLt() { |
64 var kBodySize = 1; | 86 var builder = new WasmModuleBuilder(); |
65 var kNameOffset2 = kHeaderSize + 19 + kBodySize + 1; | |
66 | 87 |
67 return _WASMEXP_.instantiateModule(bytesWithHeader( | 88 var kPages = 3; |
68 // -- memory | 89 builder.addMemory(kPages, kPages, true); |
69 kDeclMemory, | 90 builder.addFunction("flt", [kAstI32, kAstF64, kAstF64]) |
70 1, 1, 1, | 91 .addBody([ |
71 // -- signatures | 92 kExprF64Lt, // -- |
72 kDeclSignatures, 1, | 93 kExprGetLocal, 0, // -- |
73 0, kAstStmt, // signature: void -> void | 94 kExprGetLocal, 1]) // -- |
74 // -- functions | 95 .exportFunc(); |
75 kDeclFunctions, 1, | 96 |
76 kDeclFunctionName | kDeclFunctionExport, | 97 var module = builder.instantiate(); |
77 0, 0, // signature index | 98 assertModule(module, kPageSize * kPages); |
78 kNameOffset2, 0, 0, 0, // name offset | 99 |
79 kBodySize, 0, | 100 var flt = assertFunction(module, "flt"); |
80 kExprNop, // body | 101 assertEquals(1, flt(-2, -1)); |
81 kDeclEnd, | 102 assertEquals(0, flt(7.3, 7.1)); |
82 'n', 'o', 'p', 0 // name | 103 assertEquals(1, flt(7.1, 7.3)); |
83 )); | |
84 })(); | 104 })(); |
85 | |
86 // Check the module exists. | |
87 assertFalse(module === undefined); | |
88 assertFalse(module === null); | |
89 assertFalse(module === 0); | |
90 assertEquals("object", typeof module); | |
91 | |
92 // Check the memory is an ArrayBuffer. | |
93 var mem = module.memory; | |
94 assertFalse(mem === undefined); | |
95 assertFalse(mem === null); | |
96 assertFalse(mem === 0); | |
97 assertEquals("object", typeof mem); | |
98 assertTrue(mem instanceof ArrayBuffer); | |
99 for (var i = 0; i < 4; i++) { | |
100 module.memory = 0; // should be ignored | |
101 assertEquals(mem, module.memory); | |
102 } | |
103 | |
104 assertEquals(65536, module.memory.byteLength); | |
105 | |
106 // Check the properties of the sub function. | |
107 assertFalse(module.nop === undefined); | |
108 assertFalse(module.nop === null); | |
109 assertFalse(module.nop === 0); | |
110 assertEquals("function", typeof module.nop); | |
111 | |
112 assertEquals(undefined, module.nop()); | |
113 | |
114 (function testLt() { | |
115 var kBodySize = 5; | |
116 var kNameOffset = kHeaderSize + 21 + kBodySize + 1; | |
117 | |
118 var data = bytesWithHeader( | |
119 // -- memory | |
120 kDeclMemory, | |
121 1, 1, 1, | |
122 // -- signatures | |
123 kDeclSignatures, 1, | |
124 2, kAstI32, kAstF64, kAstF64, // (f64,f64)->int | |
125 // -- functions | |
126 kDeclFunctions, 1, | |
127 kDeclFunctionName | kDeclFunctionExport, | |
128 0, 0, // signature index | |
129 kNameOffset, 0, 0, 0, // name offset | |
130 kBodySize, 0, | |
131 // -- body | |
132 kExprF64Lt, // -- | |
133 kExprGetLocal, 0, // -- | |
134 kExprGetLocal, 1, // -- | |
135 kDeclEnd, | |
136 'f', 'l', 't', 0 // name | |
137 ); | |
138 | |
139 var module = _WASMEXP_.instantiateModule(data); | |
140 | |
141 assertEquals("function", typeof module.flt); | |
142 assertEquals(1, module.flt(-2, -1)); | |
143 assertEquals(0, module.flt(7.3, 7.1)); | |
144 assertEquals(1, module.flt(7.1, 7.3)); | |
145 })(); | |
OLD | NEW |