blob: 11cc92a8ec37befc0200edc63e38215736a87273 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5// Flags: --expose-wasm
6
7load("test/mjsunit/wasm/wasm-constants.js");
Ben Murdochda12d292016-06-02 14:46:10 +01008load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009
Ben Murdochda12d292016-06-02 14:46:10 +010010function assertModule(module, memsize) {
11 // Check the module exists.
12 assertFalse(module === undefined);
13 assertFalse(module === null);
14 assertFalse(module === 0);
15 assertEquals("object", typeof module);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016
Ben Murdochda12d292016-06-02 14:46:10 +010017 // Check the memory is an ArrayBuffer.
18 var mem = module.exports.memory;
19 assertFalse(mem === undefined);
20 assertFalse(mem === null);
21 assertFalse(mem === 0);
22 assertEquals("object", typeof mem);
23 assertTrue(mem instanceof ArrayBuffer);
24 for (var i = 0; i < 4; i++) {
25 module.exports.memory = 0; // should be ignored
26 assertEquals(mem, module.exports.memory);
27 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028
Ben Murdochda12d292016-06-02 14:46:10 +010029 assertEquals(memsize, module.exports.memory.byteLength);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030}
31
Ben Murdochda12d292016-06-02 14:46:10 +010032function assertFunction(module, func) {
33 assertEquals("object", typeof module.exports);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034
Ben Murdochda12d292016-06-02 14:46:10 +010035 var exp = module.exports[func];
36 assertFalse(exp === undefined);
37 assertFalse(exp === null);
38 assertFalse(exp === 0);
39 assertEquals("function", typeof exp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040
Ben Murdochda12d292016-06-02 14:46:10 +010041 return exp;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042}
43
Ben Murdochda12d292016-06-02 14:46:10 +010044(function SubTest() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045
Ben Murdochda12d292016-06-02 14:46:10 +010046 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047
Ben Murdochda12d292016-06-02 14:46:10 +010048 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));
64})();
65
66
67(function NopTest() {
68
69 var builder = new WasmModuleBuilder();
70
71 var kPages = 2;
72 builder.addMemory(kPages, kPages, true);
73 builder.addFunction("nop", [kAstStmt])
74 .addBody([kExprNop])
75 .exportFunc();
76
77 var module = builder.instantiate();
78 assertModule(module, kPageSize * kPages);
79
80 var nop = assertFunction(module, "nop");
81 assertEquals(undefined, nop());
82})();
83
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084
85(function testLt() {
Ben Murdochda12d292016-06-02 14:46:10 +010086 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087
Ben Murdochda12d292016-06-02 14:46:10 +010088 var kPages = 3;
89 builder.addMemory(kPages, kPages, true);
90 builder.addFunction("flt", [kAstI32, kAstF64, kAstF64])
91 .addBody([
92 kExprF64Lt, // --
93 kExprGetLocal, 0, // --
94 kExprGetLocal, 1]) // --
95 .exportFunc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096
Ben Murdochda12d292016-06-02 14:46:10 +010097 var module = builder.instantiate();
98 assertModule(module, kPageSize * kPages);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099
Ben Murdochda12d292016-06-02 14:46:10 +0100100 var flt = assertFunction(module, "flt");
101 assertEquals(1, flt(-2, -1));
102 assertEquals(0, flt(7.3, 7.1));
103 assertEquals(1, flt(7.1, 7.3));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104})();