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
3// found in the LICENSE file.
4
5// Flags: --expose-wasm
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9var module = (function () {
10  var kBodySize = 5;
11  var kNameOffset = 21 + kBodySize + 1;
12
13  return _WASMEXP_.instantiateModule(bytes(
14    // -- memory
15    kDeclMemory,
16    12, 12, 1,
17    // -- signatures
18    kDeclSignatures, 1,
19    2, kAstI32, kAstI32, kAstI32, // int, int -> int
20    // -- functions
21    kDeclFunctions, 1,
22    kDeclFunctionName | kDeclFunctionExport,
23    0, 0,
24    kNameOffset, 0, 0, 0,         // name offset
25    kBodySize, 0,
26    // -- body
27    kExprI32Sub,                  // --
28    kExprGetLocal, 0,             // --
29    kExprGetLocal, 1,             // --
30    kDeclEnd,
31    's', 'u', 'b', 0              // name
32  ));
33})();
34
35// Check the module exists.
36assertFalse(module === undefined);
37assertFalse(module === null);
38assertFalse(module === 0);
39assertEquals("object", typeof module);
40
41// Check the memory is an ArrayBuffer.
42var mem = module.memory;
43assertFalse(mem === undefined);
44assertFalse(mem === null);
45assertFalse(mem === 0);
46assertEquals("object", typeof mem);
47assertTrue(mem instanceof ArrayBuffer);
48for (var i = 0; i < 4; i++) {
49  module.memory = 0;  // should be ignored
50  assertEquals(mem, module.memory);
51}
52
53assertEquals(4096, module.memory.byteLength);
54
55// Check the properties of the sub function.
56assertEquals("function", typeof module.sub);
57
58assertEquals(-55, module.sub(33, 88));
59assertEquals(-55555, module.sub(33333, 88888));
60assertEquals(-5555555, module.sub(3333333, 8888888));
61
62
63var module = (function() {
64  var kBodySize = 1;
65  var kNameOffset2 = 19 + kBodySize + 1;
66
67  return _WASMEXP_.instantiateModule(bytes(
68    // -- memory
69    kDeclMemory,
70    12, 12, 1,
71    // -- signatures
72    kDeclSignatures, 1,
73    0, kAstStmt,                // signature: void -> void
74    // -- functions
75    kDeclFunctions, 1,
76    kDeclFunctionName | kDeclFunctionExport,
77    0, 0,                       // signature index
78    kNameOffset2, 0, 0, 0,      // name offset
79    kBodySize, 0,
80    kExprNop,                   // body
81    kDeclEnd,
82    'n', 'o', 'p', 0            // name
83  ));
84})();
85
86// Check the module exists.
87assertFalse(module === undefined);
88assertFalse(module === null);
89assertFalse(module === 0);
90assertEquals("object", typeof module);
91
92// Check the memory is an ArrayBuffer.
93var mem = module.memory;
94assertFalse(mem === undefined);
95assertFalse(mem === null);
96assertFalse(mem === 0);
97assertEquals("object", typeof mem);
98assertTrue(mem instanceof ArrayBuffer);
99for (var i = 0; i < 4; i++) {
100  module.memory = 0;  // should be ignored
101  assertEquals(mem, module.memory);
102}
103
104assertEquals(4096, module.memory.byteLength);
105
106// Check the properties of the sub function.
107assertFalse(module.nop === undefined);
108assertFalse(module.nop === null);
109assertFalse(module.nop === 0);
110assertEquals("function", typeof module.nop);
111
112assertEquals(undefined, module.nop());
113
114(function testLt() {
115  var kBodySize = 5;
116  var kNameOffset = 21 + kBodySize + 1;
117
118  var data = bytes(
119    // -- memory
120    kDeclMemory,
121    12, 12, 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})();
146