-
-
Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathindex.js
355 lines (303 loc) · 12.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import fs from "fs";
import assert from "assert";
import { inspect } from "util";
import loader from "../index.js";
import { dirname } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
test("default.wasm");
test("legacy.wasm");
testInstantiate("default.wasm");
testInstantiate("legacy.wasm");
async function wrapToResponse(source) {
return new Response(await source, {
status: 200,
headers: new Headers([["Content-Type", "application/wasm"]])
});
}
function test(file) {
let buffer = fs.readFileSync(__dirname + "/build/" + file);
let result = loader.instantiateSync(buffer, {});
const exports = result.exports;
console.log(inspect(exports, true, 100, true));
// should export memory
assert(exports.memory instanceof WebAssembly.Memory);
// NOTE: Namespace exports have been removed in 0.20
// assert(typeof exports.memory.compare === "function");
// should be able to get an exported string
assert.strictEqual(exports.__getString(exports.COLOR), "red");
// should be able to allocate and work with a new small string
{
let str = "Hello world!𤭢";
let ref = exports.__newString(str);
assert.strictEqual(exports.__getString(ref), str);
assert.strictEqual(exports.strlen(ref), str.length);
}
// should be able to allocate and work with a new small ArrayBuffer
{
let input = new Uint8Array([1, 2, 3, 4]);
let bufPtr = exports.__newArrayBuffer(input.buffer);
let output = new Uint8Array(exports.__getArrayBuffer(bufPtr));
assert.deepStrictEqual(output, input);
}
// should be able to allocate and work with a new big string
{
let str = `
∀ ∁ ∂ ∃ ∄ ∅ ∆ ∇ ∈ ∉ ∊ ∋ ∌ ∍ ∎ ∏ ∐ ∑ − ∓ ∔ ∕ ∖ ∗ ∘ ∙ √ ∛
∜ ∝ ∞ ∟ ∠ ∡ ∢ ∣ ∤ ∥ ∦ ∧ ∨ ∩ ∪ ∫ ∬ ∭ ∮ ∯ ∰ ∱ ∲ ∳ ∴ ∵ ∶ ∷
∸ ∹ ∺ ∻ ∼ ∽ ∾ ∿ ≀ ≁ ≂ ≃ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≏ ≐ ≑ ≒ ≓
≔ ≕ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≠ ≡ ≢ ≣ ≤ ≥ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯
≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿
`;
let ref = exports.__newString(str);
assert.strictEqual(exports.__getString(ref), str);
assert.strictEqual(exports.strlen(ref), str.length);
}
// should be able to allocate and work with a string containing an isolated high surrogate
{
let str = "𝄞".substring(0, 1);
let ref = exports.__newString(str);
assert.strictEqual(exports.__getString(ref), str);
assert.strictEqual(exports.strlen(ref), str.length);
}
// should be able to allocate and work with a string containing an isolated low surrogate
{
let str = "𝄞".substring(1);
let ref = exports.__newString(str);
assert.strictEqual(exports.__getString(ref), str);
assert.strictEqual(exports.strlen(ref), str.length);
}
// should be able to allocate a typed array
{
let arr = [1, 2, 3, 4, 5, 0x80000000 | 0];
let ref = exports.__newArray(exports.INT32ARRAY_ID, arr);
assert(exports.__instanceof(ref, exports.INT32ARRAY_ID));
// should be able to get the values of an array
assert.deepStrictEqual(exports.__getArray(ref), arr);
// should be able to get a view on an array
assert.deepStrictEqual(exports.__getArrayView(ref), new Int32Array(arr));
// should be able to sum up its values
assert.strictEqual(exports.sum(ref), arr.reduce((a, b) => (a + b) | 0, 0) | 0);
}
// should be able to allocate a typed array
{
let arr = [1, 2, 3, 4, 5, 0x80000000 | 0];
let ref = exports.__newArray(exports.STATICARRAYI32_ID, arr);
assert(exports.__instanceof(ref, exports.STATICARRAYI32_ID));
// should be able to get the values of an array
assert.deepStrictEqual(exports.__getArray(ref), arr);
// should be able to get a view on an array
assert.deepStrictEqual(exports.__getArrayView(ref), new Int32Array(arr));
// should be able to sum up its values
assert.strictEqual(exports.sumStatic(ref), arr.reduce((a, b) => (a + b) | 0, 0) | 0);
}
/*
{
let arrU8Arr = new Uint8Array([0, 1, 2]);
let refU8Arr = module.__newUint8Array(arrU8Arr);
assert(module.__instanceof(refU8Arr, module.UINT8ARRAY_ID));
assert.deepStrictEqual(module.__getUint8Array(refU8Arr), arrU8Arr);
let arrU16Arr = new Uint16Array([0, 0x7FFF, 0xFFFF]);
let refU16Arr = module.__newUint16Array(arrU16Arr);
assert(module.__instanceof(refU16Arr, module.UINT16ARRAY_ID));
assert.deepStrictEqual(module.__getUint16Array(refU16Arr), arrU16Arr);
let arrI16Arr = new Int16Array([0, -1, -2]);
let refI16Arr = module.__newInt16Array(arrI16Arr);
assert(module.__instanceof(refI16Arr, module.INT16ARRAY_ID));
assert.deepStrictEqual(module.__getInt16Array(refI16Arr), arrI16Arr);
}
*/
// should be able to distinguish between signed and unsigned
{
let values = [0, 255, 127];
let arr = new Uint8Array(values);
let ref = exports.__newArray(exports.UINT8ARRAY_ID, arr);
assert(exports.__instanceof(ref, exports.UINT8ARRAY_ID));
assert.deepStrictEqual(exports.__getUint8Array(ref), arr);
assert.deepStrictEqual(exports.__getUint8ArrayView(ref), arr);
assert.deepStrictEqual(exports.__getArray(ref), values);
}
// should be able to distinguish between signed and unsigned for static array layout
{
let arr = [0, 255, 127];
let ref = exports.__newArray(exports.STATICARRAYU8_ID, arr);
assert(exports.__instanceof(ref, exports.STATICARRAYU8_ID));
assert.deepStrictEqual(exports.__getArray(ref), arr);
}
// should be able to distinguish between signed and unsigned
{
let values = [0, 0xFFFF, -0x00FF];
let arr = new Int16Array(values);
let ref = exports.__newArray(exports.INT16ARRAY_ID, arr);
assert(exports.__instanceof(ref, exports.INT16ARRAY_ID));
assert.deepStrictEqual(exports.__getInt16Array(ref), arr);
assert.deepStrictEqual(exports.__getInt16ArrayView(ref), arr);
assert.deepStrictEqual(exports.__getArray(ref), [0, -1, -255]);
}
// should be able to distinguish between signed and unsigned for static array layout
{
let arr = [0, 0xFFFF, -0x00FF];
let ref = exports.__newArray(exports.STATICARRAYI16_ID, arr);
assert(exports.__instanceof(ref, exports.STATICARRAYI16_ID));
assert.deepStrictEqual(exports.__getArray(ref), [0, -1, -255]);
}
// should be able to distinguish between signed and unsigned
{
let values = [1, -1 >>> 0, 0x80000000];
let arr = new Uint32Array(values);
let ref = exports.__newArray(exports.UINT32ARRAY_ID, arr);
assert(exports.__instanceof(ref, exports.UINT32ARRAY_ID));
assert.deepStrictEqual(exports.__getUint32Array(ref), arr);
assert.deepStrictEqual(exports.__getUint32ArrayView(ref), arr);
assert.deepStrictEqual(exports.__getArray(ref), values);
}
// should be able to distinguish between signed and unsigned with static array layout
{
let arr = [1, -1 >>> 0, 0x80000000];
let ref = exports.__newArray(exports.STATICARRAYU32_ID, arr);
assert(exports.__instanceof(ref, exports.STATICARRAYU32_ID));
assert.deepStrictEqual(exports.__getArray(ref), arr);
}
// should be able to distinguish between integer and float
{
let values = [0.0, 1.5, 2.5];
let arr = new Float32Array(values);
let ref = exports.__newArray(exports.FLOAT32ARRAY_ID, arr);
assert(exports.__instanceof(ref, exports.FLOAT32ARRAY_ID));
assert.deepStrictEqual(exports.__getFloat32Array(ref), arr);
assert.deepStrictEqual(exports.__getFloat32ArrayView(ref), arr);
assert.deepStrictEqual(exports.__getArray(ref), values);
}
// should be able to distinguish between integer and float static arrays
{
let arr = [0.0, 1.5, 2.5];
let ref = exports.__newArray(exports.STATICARRAYF32_ID, arr);
assert(exports.__instanceof(ref, exports.STATICARRAYF32_ID));
assert.deepStrictEqual(exports.__getArray(ref), arr);
}
// should be able to create empty arrays
{
let ref = exports.__newArray(exports.ARRAYI32_ID);
assert(exports.__instanceof(ref, exports.ARRAYI32_ID));
assert.deepStrictEqual(exports.__getArray(ref), []);
}
// should be able to create arrays with capacity
{
let ref = exports.__newArray(exports.ARRAYI32_ID, 32);
assert(exports.__instanceof(ref, exports.ARRAYI32_ID));
assert.strictEqual(exports.__getArray(ref).length, 32);
}
// should be able to work with normal arrays
{
let arr = [1, 2, 3, 4, 5];
let ref = exports.__newArray(exports.ARRAYI32_ID, arr);
assert(exports.__instanceof(ref, exports.ARRAYI32_ID));
exports.changeLength(ref, 3);
assert.deepStrictEqual(exports.__getArray(ref), [1, 2, 3]);
}
// should be able to pin references externally
{
let ptr = exports.__newString("test");
exports.__pin(ptr);
try { exports.__pin(ptr); assert(false); } catch (e) { /* nop */ }
exports.__unpin(ptr);
try { exports.__unpin(ptr); assert(false); } catch (e) { /* nop */ }
}
// should be able to correctly call a function with variable arguments
assert.strictEqual(exports.varadd(), 3);
assert.strictEqual(exports.varadd(2, 3), 5);
assert.strictEqual(exports.varadd(2), 4);
// TBD: table is no more exported by default to allow more optimizations
// should be able to get a function from the table and just call it with variable arguments
// let fn = module.getFunction(module.varadd_ref);
// assert.strictEqual(fn(), 3);
// assert.strictEqual(fn(2, 3), 5);
// assert.strictEqual(fn(2), 4);
// should be able to create a new function and call it from WASM
// ref = module.newFunction(module.varadd);
// assert.strictEqual(module.calladd(ref, 2, 3), 5);
// NOTE: Class exports have been removed in 0.20
// should be able to use a class
// let car = new exports.Car(5);
// assert.strictEqual(car.numDoors, 5);
// assert.strictEqual(car.isDoorsOpen, 0);
// car.openDoors();
// assert.strictEqual(car.isDoorsOpen, 1);
// car.closeDoors();
// assert.strictEqual(car.isDoorsOpen, 0);
// assert(typeof +car === "number"); // uses Car.prototype.valueOf to obtain `thisPtr`
// should be able to return a function
{
const addFunc = exports.__getFunction(exports.getVaraddFunc());
assert(typeof addFunc === "function");
assert.strictEqual(addFunc(1, 2), 3);
const invalidFunc = exports.__getFunction(0);
assert(invalidFunc == null);
}
// should be able to use trace
exports.dotrace(42);
// should be able to mutate an array in place using getArrayView
{
let ptr = exports.__newArray(exports.FLOAT32ARRAY_ID, [1, 2, 3]);
let view = exports.__getArrayView(ptr);
assert.deepStrictEqual(view, new Float32Array([1, 2, 3]));
exports.modifyFloat32Array(ptr, 0, 4);
assert.deepStrictEqual(view, new Float32Array([4, 2, 3]));
}
// should be able to mutate an array in place using getFloat32Array
{
let ptr = exports.newFloat32Array(3);
let view = exports.__getFloat32ArrayView(ptr);
let arr = exports.__getFloat32Array(ptr);
assert.deepStrictEqual(view, new Float32Array([0, 0, 0]));
assert.deepStrictEqual(arr, new Float32Array([0, 0, 0]));
exports.modifyFloat32Array(ptr, 0, 3);
exports.modifyFloat32Array(ptr, 1, 2);
exports.modifyFloat32Array(ptr, 2, 1);
assert.deepStrictEqual(view, new Float32Array([3, 2, 1]));
assert.deepStrictEqual(arr, new Float32Array([0, 0, 0]));
}
}
function testInstantiate(file) {
// should be able to instantiate from a buffer
(async () => {
const { exports, instance, module } = await loader.instantiate(
fs.readFileSync(__dirname + "/build/" + file),
{}
);
assert(exports.memory);
assert(instance && instance instanceof WebAssembly.Instance);
assert(module && module instanceof WebAssembly.Module);
})();
// should be able to instantiate from a wasm module
(async () => {
const { exports, instance, module } = await loader.instantiate(
new WebAssembly.Module(fs.readFileSync(__dirname + "/build/" + file)),
{}
);
assert(exports.memory);
assert(instance && instance instanceof WebAssembly.Instance);
assert(module && module instanceof WebAssembly.Module);
})();
// should be able to instantiate from a promise yielding a buffer
(async () => {
const { exports, instance, module } = await loader.instantiate(
fs.promises.readFile(__dirname + "/build/" + file),
{}
);
assert(exports.memory);
assert(instance && instance instanceof WebAssembly.Instance);
assert(module && module instanceof WebAssembly.Module);
})();
// should be able to mimic instantiateStreaming under node (for now)
(async () => {
const { exports, instance, module } = await loader.instantiateStreaming(
wrapToResponse(fs.promises.readFile(__dirname + "/build/" + file)),
{}
);
assert(exports.memory);
assert(instance && instance instanceof WebAssembly.Instance);
assert(module && module instanceof WebAssembly.Module);
})();
}