1 import * as assert from '../assert.js';
2 import Builder from '../Builder.js';
4 const assertOpThrows = (opFn, message) => {
5 let f = (new Builder()).Type().End().Code().Function();
6 assert.throws(opFn, Error, message, f);
9 (function EmptyModule() {
10 const b = new Builder();
11 const j = JSON.parse(b.json());
12 assert.isNotUndef(j.preamble);
13 assert.isNotUndef(j.preamble["magic number"]);
14 assert.isNotUndef(j.preamble.version);
15 assert.isNotUndef(j.section);
16 assert.eq(j.section.length, 0);
19 (function CustomMagicNumber() {
20 const b = (new Builder()).setPreamble({ "magic number": 1337 });
21 const j = JSON.parse(b.json());
22 assert.eq(j.preamble["magic number"], 1337);
23 assert.isNotUndef(j.preamble.version);
26 (function CustomVersion() {
27 const b = (new Builder()).setPreamble({ "version": 1337 });
28 const j = JSON.parse(b.json());
29 assert.eq(j.preamble["version"], 1337);
30 assert.isNotUndef(j.preamble.version);
33 (function CustomSection() {
34 const b = new Builder();
35 b.Unknown("custom section")
39 const j = JSON.parse(b.json());
40 assert.eq(j.section.length, 1);
41 assert.eq(j.section[0].name, "custom section");
42 assert.eq(j.section[0].data.length, 3);
43 assert.eq(j.section[0].data[0], 0x00);
44 assert.eq(j.section[0].data[1], 0x42);
45 assert.eq(j.section[0].data[2], 0xFF);
48 (function CustomSectionAllBytes() {
49 const b = new Builder();
50 let u = b.Unknown("custom section");
51 for (let i = 0; i !== 0xFF + 1; ++i)
53 const j = JSON.parse(b.json());
54 assert.eq(j.section[0].data.length, 256);
55 for (let i = 0; i !== 0xFF + 1; ++i)
56 assert.eq(j.section[0].data[i], i);
59 (function CustomSectionInvalidByte() {
60 const u = (new Builder()).Unknown("custom section");
61 assert.throws(() => u.Byte(0xFF + 1), Error, `Not the same: "0" and "256": Unknown section expected byte, got: "256"`);
64 (function TwoCustomSections() {
65 const b = new Builder();
66 b.Unknown("custom section")
73 const j = JSON.parse(b.json());
74 assert.eq(j.section.length, 2);
75 assert.eq(j.section[0].name, "custom section");
76 assert.eq(j.section[0].data.length, 3);
77 assert.eq(j.section[1].name, "☃");
78 assert.eq(j.section[1].data.length, 1);
79 assert.eq(j.section[1].data[0], 0x11);
82 (function SectionsWithSameCustomName() {
83 const b = (new Builder()).Unknown("foo").End();
84 assert.throws(() => b.Unknown("foo"), Error, `Expected falsy: Cannot have two sections with the same name "foo" and ID 0`);
87 (function EmptyTypeSection() {
88 const b = (new Builder()).Type().End();
89 const j = JSON.parse(b.json());
90 assert.eq(j.section.length, 1);
91 assert.eq(j.section[0].name, "Type");
92 assert.eq(j.section[0].data.length, 0);
95 (function TwoTypeSections() {
96 const b = (new Builder()).Type().End();
97 assert.throws(() => b.Type(), Error, `Expected falsy: Cannot have two sections with the same name "Type" and ID 1`);
100 (function SimpleTypeSection() {
101 const b = (new Builder()).Type()
108 .Func(["i32", "i64", "f32", "f64"])
110 const j = JSON.parse(b.json());
111 assert.eq(j.section[0].data.length, 7);
112 assert.eq(j.section[0].data[0].params, []);
113 assert.eq(j.section[0].data[0].ret, "void");
114 assert.eq(j.section[0].data[1].params, []);
115 assert.eq(j.section[0].data[1].ret, "void");
116 assert.eq(j.section[0].data[2].params, []);
117 assert.eq(j.section[0].data[2].ret, "i32");
118 assert.eq(j.section[0].data[3].params, []);
119 assert.eq(j.section[0].data[3].ret, "i64");
120 assert.eq(j.section[0].data[4].params, []);
121 assert.eq(j.section[0].data[4].ret, "f32");
122 assert.eq(j.section[0].data[5].params, []);
123 assert.eq(j.section[0].data[5].ret, "f64");
124 assert.eq(j.section[0].data[6].params, ["i32", "i64", "f32", "f64"]);
125 assert.eq(j.section[0].data[6].ret, "void");
128 (function EmptyImportSection() {
129 const b = (new Builder()).Import().End();
130 const j = JSON.parse(b.json());
131 assert.eq(j.section.length, 1);
132 assert.eq(j.section[0].name, "Import");
133 assert.eq(j.section[0].data.length, 0);
136 (function ImportBeforeTypeSections() {
137 const b = (new Builder()).Import().End();
138 assert.throws(() => b.Type(), Error, `Expected: "2" > "1": Bad section ordering: "Import" cannot precede "Type"`);
141 (function ImportFunctionWithoutTypeSection() {
142 const i = (new Builder()).Import();
143 assert.throws(() => i.Function("foo", "bar", 0), Error, `Shouldn't be undefined: Can not use type 0 if a type section is not present`);
146 (function ImportFunctionWithInvalidType() {
147 const i = (new Builder()).Type().End().Import();
148 assert.throws(() => i.Function("foo", "bar", 0), Error, `Shouldn't be undefined: Type 0 does not exist in type section`);
151 (function ImportFunction() {
152 const b = (new Builder())
153 .Type().Func([]).End()
155 .Function("foo", "bar", 0)
157 const j = JSON.parse(b.json());
158 assert.eq(j.section[1].data.length, 1);
159 assert.eq(j.section[1].data[0].module, "foo");
160 assert.eq(j.section[1].data[0].field, "bar");
161 assert.eq(j.section[1].data[0].type, 0);
162 assert.eq(j.section[1].data[0].kind, "Function");
165 (function ImportFunctionsWithExistingTypes() {
166 const b = (new Builder())
170 .Func(["i64", "i32"])
171 .Func(["i64", "i64"])
174 .Function("foo", "bar", { params: [] })
175 .Function("foo", "baz", { params: [], ret: "i32" })
176 .Function("foo", "boo", { params: ["i64", "i64"] })
178 const j = JSON.parse(b.json());
179 assert.eq(j.section[0].data.length, 4);
180 assert.eq(j.section[1].data.length, 3);
181 assert.eq(j.section[1].data[0].type, 0);
182 assert.eq(j.section[1].data[1].type, 1);
183 assert.eq(j.section[1].data[2].type, 3);
186 (function ImportFunctionWithNewType() {
187 const b = (new Builder())
190 .Function("foo", "bar", { params: [] })
191 .Function("foo", "baz", { params: [], ret: "i32" })
192 .Function("foo", "boo", { params: ["i64", "i64"] })
194 const j = JSON.parse(b.json());
195 assert.eq(j.section[0].data.length, 3);
196 assert.eq(j.section[0].data[0].ret, "void");
197 assert.eq(j.section[0].data[0].params, []);
198 assert.eq(j.section[0].data[1].ret, "i32");
199 assert.eq(j.section[0].data[1].params, []);
200 assert.eq(j.section[0].data[2].ret, "void");
201 assert.eq(j.section[0].data[2].params, ["i64", "i64"]);
204 (function EmptyExportSection() {
205 const b = (new Builder()).Export().End();
206 const j = JSON.parse(b.json());
207 assert.eq(j.section.length, 1);
208 assert.eq(j.section[0].name, "Export");
209 assert.eq(j.section[0].data.length, 0);
212 (function ExportFunctionWithoutTypeSection() {
213 const e = (new Builder()).Export();
214 assert.throws(() => e.Function("foo", 0, 0), Error, `Shouldn't be undefined: Can not use type 0 if a type section is not present`);
217 (function ExportFunctionWithInvalidType() {
218 const e = (new Builder()).Type().End().Export();
219 assert.throws(() => e.Function("foo", 0, 0), Error, `Shouldn't be undefined: Type 0 does not exist in type section`);
222 (function ExportAnImport() {
223 const b = (new Builder())
225 .Import().Function("foo", "bar", { params: [] }).End()
226 .Export().Function("ExportAnImport", { module: "foo", field: "bar" }).End();
227 const j = JSON.parse(b.json());
228 assert.eq(j.section[2].name, "Export");
229 assert.eq(j.section[2].data.length, 1);
230 assert.eq(j.section[2].data[0].field, "ExportAnImport");
231 assert.eq(j.section[2].data[0].type, 0);
232 assert.eq(j.section[2].data[0].index, 0);
233 assert.eq(j.section[2].data[0].kind, "Function");
236 (function ExportMismatchedImport() {
237 const e = (new Builder())
239 .Import().Function("foo", "bar", { params: [] }).End()
241 assert.throws(() => e.Function("foo", 0, { params: ["i32"] }), Error, `Not the same: "1" and "0": Re-exporting import "bar" as "foo" has mismatching type`);
244 (function EmptyCodeSection() {
245 const b = new Builder();
247 const j = JSON.parse(b.json());
248 assert.eq(j.section.length, 1);
249 assert.eq(j.section[0].name, "Code");
250 assert.eq(j.section[0].data.length, 0);
253 (function CodeSectionWithEmptyFunction() {
254 const b = new Builder();
258 const j = JSON.parse(b.json());
259 assert.eq(j.section.length, 2);
260 assert.eq(j.section[0].name, "Type");
261 assert.eq(j.section[0].data.length, 1);
262 assert.eq(j.section[0].data[0].params, []);
263 assert.eq(j.section[0].data[0].ret, "void");
264 assert.eq(j.section[1].name, "Code");
265 assert.eq(j.section[1].data.length, 1);
266 assert.eq(j.section[1].data[0].name, undefined);
267 assert.eq(j.section[1].data[0].type, 0);
268 assert.eq(j.section[1].data[0].parameterCount, 0);
269 assert.eq(j.section[1].data[0].locals.length, 0);
270 assert.eq(j.section[1].data[0].code.length, 0);
273 (function CodeSectionWithEmptyFunctionWithParameters() {
274 const b = new Builder();
277 .Function({ params: ["i32", "i64", "f32", "f64"] });
278 const j = JSON.parse(b.json());
279 assert.eq(j.section.length, 2);
280 assert.eq(j.section[0].data.length, 1);
281 assert.eq(j.section[0].data[0].params, ["i32", "i64", "f32", "f64"]);
282 assert.eq(j.section[0].data[0].ret, "void");
283 assert.eq(j.section[1].data.length, 1);
284 assert.eq(j.section[1].data[0].type, 0);
285 assert.eq(j.section[1].data[0].parameterCount, 4);
286 assert.eq(j.section[1].data[0].locals[0], "i32");
287 assert.eq(j.section[1].data[0].locals[1], "i64");
288 assert.eq(j.section[1].data[0].locals[2], "f32");
289 assert.eq(j.section[1].data[0].locals[3], "f64");
290 assert.eq(j.section[1].data[0].code.length, 0);
293 (function InvalidFunctionParameters() {
294 for (let invalid of ["", "void", "bool", "any", "struct", 0, 3.14, undefined, [], {}]) {
295 const c = (new Builder()).Code();
296 assert.throws(() => c.Function({ params: [invalid] }), Error, `Expected truthy: Type parameter ${invalid} needs a valid value type`);
300 (function SimpleFunction() {
301 const b = new Builder();
308 const j = JSON.parse(b.json());
309 assert.eq(j.section[1].data.length, 1);
310 assert.eq(j.section[1].data[0].locals.length, 0);
311 assert.eq(j.section[1].data[0].code.length, 3);
312 assert.eq(j.section[1].data[0].code[0].name, "nop");
313 assert.eq(j.section[1].data[0].code[1].name, "nop");
314 assert.eq(j.section[1].data[0].code[2].name, "end");
317 (function TwoSimpleFunctions() {
318 const b = new Builder();
328 const j = JSON.parse(b.json());
329 assert.eq(j.section[1].data.length, 2);
330 assert.eq(j.section[1].data[0].locals.length, 0);
331 assert.eq(j.section[1].data[0].code.length, 3);
332 assert.eq(j.section[1].data[0].code[0].name, "nop");
333 assert.eq(j.section[1].data[0].code[1].name, "nop");
334 assert.eq(j.section[1].data[0].code[2].name, "end");
335 assert.eq(j.section[1].data[1].locals.length, 0);
336 assert.eq(j.section[1].data[1].code.length, 2);
337 assert.eq(j.section[1].data[1].code[0].name, "return");
338 assert.eq(j.section[1].data[1].code[1].name, "end");
341 (function NamedFunctions() {
342 const b = new Builder().Type().End().Code()
343 .Function("hello").End()
344 .Function("world", { params: ["i32"] }).End()
346 const j = JSON.parse(b.json());
347 assert.eq(j.section[1].data[0].name, "hello");
348 assert.eq(j.section[1].data[0].parameterCount, 0);
349 assert.eq(j.section[1].data[1].name, "world");
350 assert.eq(j.section[1].data[1].parameterCount, 1);
353 (function ExportSimpleFunctions() {
354 const b = (new Builder())
357 .Function("foo", 0, { params: [] })
359 .Function("betterNameForBar", "bar")
362 .Function({ params: [] }).Nop().End()
363 .Function("bar", { params: [] }).Nop().End()
365 const j = JSON.parse(b.json());
366 assert.eq(j.section[0].data.length, 1);
367 assert.eq(j.section[0].data[0].ret, "void");
368 assert.eq(j.section[0].data[0].params, []);
369 assert.eq(j.section[1].data.length, 3);
370 assert.eq(j.section[1].data[0].field, "foo");
371 assert.eq(j.section[1].data[0].type, 0);
372 assert.eq(j.section[1].data[0].index, 0);
373 assert.eq(j.section[1].data[0].kind, "Function");
374 assert.eq(j.section[1].data[1].field, "bar");
375 assert.eq(j.section[1].data[1].type, 0);
376 assert.eq(j.section[1].data[1].index, 1);
377 assert.eq(j.section[1].data[1].kind, "Function");
378 assert.eq(j.section[1].data[2].field, "betterNameForBar");
379 assert.eq(j.section[1].data[2].type, 0);
380 assert.eq(j.section[1].data[2].index, 1);
381 assert.eq(j.section[1].data[2].kind, "Function");
384 (function ExportUndefinedFunction() {
385 const c = (new Builder()).Type().End().Export().Function("foo").End().Code();
386 assert.throws(() => c.End(), Error, `Should be number, got undefined: Export section contains undefined function "foo"`);
389 (function TwoBuildersAtTheSameTime() {
390 const b = [new Builder(), new Builder()];
391 const f = b.map(builder => builder.Type().End().Code().Function());
393 f[1].Return().End().End();
394 f[0].Nop().End().End();
395 const j = b.map(builder => JSON.parse(builder.json()));
396 assert.eq(j[0].section[1].data[0].code.length, 3);
397 assert.eq(j[0].section[1].data[0].code[0].name, "nop");
398 assert.eq(j[0].section[1].data[0].code[1].name, "nop");
399 assert.eq(j[0].section[1].data[0].code[2].name, "end");
400 assert.eq(j[1].section[1].data[0].code.length, 2);
401 assert.eq(j[1].section[1].data[0].code[0].name, "return");
402 assert.eq(j[1].section[1].data[0].code[1].name, "end");
405 (function CheckedOpcodeArgumentsTooMany() {
406 assertOpThrows(f => f.Nop("uh-oh!"), `Not the same: "1" and "0": "nop" expects exactly 0 immediates, got 1`);
409 (function UncheckedOpcodeArgumentsTooMany() {
410 (new Builder()).setChecked(false).Type().End().Code().Function().Nop("This is fine.", "I'm OK with the events that are unfolding currently.");
413 (function CheckedOpcodeArgumentsNotEnough() {
414 assertOpThrows(f => f.I32Const(), `Not the same: "0" and "1": "i32.const" expects exactly 1 immediate, got 0`);
417 (function UncheckedOpcodeArgumentsNotEnough() {
418 (new Builder()).setChecked(false).Type().End().Code().Function().I32Const();
421 (function CallNoArguments() {
422 const b = (new Builder()).Type().End().Code().Function().Call(0).End().End();
423 const j = JSON.parse(b.json());
424 assert.eq(j.section[1].data[0].code.length, 2);
425 assert.eq(j.section[1].data[0].code[0].name, "call");
426 assert.eq(j.section[1].data[0].code[0].arguments.length, 0);
427 assert.eq(j.section[1].data[0].code[0].immediates.length, 1);
428 assert.eq(j.section[1].data[0].code[0].immediates[0], 0);
429 assert.eq(j.section[1].data[0].code[1].name, "end");
432 (function CallInvalid() {
433 for (let c of [-1, 0x100000000, "0", {}, Infinity, -Infinity, NaN, -NaN, null])
434 assertOpThrows(f => f.Call(c), `Expected truthy: Invalid value on call: got "${c}", expected i32`);
437 (function I32ConstValid() {
438 for (let c of [0, 1, 2, 42, 1337, 0xFF, 0xFFFF, 0x7FFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF]) {
439 const b = (new Builder()).Type().End().Code().Function().I32Const(c).Return().End().End();
440 const j = JSON.parse(b.json());
441 assert.eq(j.section[1].data[0].code[0].name, "i32.const");
442 assert.eq(j.section[1].data[0].code[0].arguments.length, 0);
443 assert.eq(j.section[1].data[0].code[0].immediates.length, 1);
444 assert.eq(j.section[1].data[0].code[0].immediates[0], c);
448 (function I32ConstInvalid() {
449 for (let c of [-1, 0x100000000, 0.1, -0.1, "0", {}, Infinity, null])
450 assertOpThrows(f => f.I32Const(c), `Expected truthy: Invalid value on i32.const: got "${c}", expected i32`);
453 // FIXME: test i64. https://bugs.webkit.org/show_bug.cgi?id=163420
455 (function F32ConstValid() {
456 for (let c of [0, -0., 0.2, Math.PI, 0x100000000]) {
457 const b = (new Builder()).Type().End().Code().Function().F32Const(c).Return().End().End();
458 const j = JSON.parse(b.json());
459 assert.eq(j.section[1].data[0].code[0].name, "f32.const");
460 assert.eq(j.section[1].data[0].code[0].arguments.length, 0);
461 assert.eq(j.section[1].data[0].code[0].immediates.length, 1);
462 assert.eq(j.section[1].data[0].code[0].immediates[0], c);
466 (function F32ConstInvalid() {
467 for (let c of ["0", {}, Infinity, -Infinity, NaN, -NaN, null])
468 assertOpThrows(f => f.F32Const(c), `Expected truthy: Invalid value on f32.const: got "${c}", expected f32`);
471 (function F64ConstValid() {
472 for (let c of [0, -0., 0.2, Math.PI, 0x100000000]) {
473 const b = (new Builder()).Type().End().Code().Function().F64Const(c).Return().End().End();
474 const j = JSON.parse(b.json());
475 assert.eq(j.section[1].data[0].code[0].name, "f64.const");
476 assert.eq(j.section[1].data[0].code[0].arguments.length, 0);
477 assert.eq(j.section[1].data[0].code[0].immediates.length, 1);
478 assert.eq(j.section[1].data[0].code[0].immediates[0], c);
482 (function F64ConstInvalid() {
483 for (let c of ["0", {}, Infinity, -Infinity, NaN, -NaN, null])
484 assertOpThrows(f => f.F64Const(c), `Expected truthy: Invalid value on f64.const: got "${c}", expected f64`);
487 (function CallOneFromStack() {
488 const b = (new Builder()).Type().End().Code()
489 .Function({ params: ["i32"] })
494 const j = JSON.parse(b.json());
495 assert.eq(j.section[1].data[0].code.length, 3);
496 assert.eq(j.section[1].data[0].code[0].name, "i32.const");
497 assert.eq(j.section[1].data[0].code[0].immediates[0], 42);
498 assert.eq(j.section[1].data[0].code[1].name, "call");
499 // FIXME: assert.eq(j.section[1].data[0].code[1].arguments.length, 1); https://bugs.webkit.org/show_bug.cgi?id=163267
500 assert.eq(j.section[1].data[0].code[1].immediates.length, 1);
501 assert.eq(j.section[1].data[0].code[1].immediates[0], 0);
502 assert.eq(j.section[1].data[0].code[2].name, "end");
505 // FIXME https://bugs.webkit.org/show_bug.cgi?id=163267 all of these:
506 // test too many pops.
507 // test not enough pops (function has non-empty stack at the end).
508 // test mismatched pop types.
509 // test mismatched function signature (calling with wrong arg types).
510 // test invalid function index.
511 // test function names (both setting and calling them).
513 (function CallManyFromStack() {
514 const b = (new Builder()).Type().End().Code()
515 .Function({ params: ["i32", "i32", "i32", "i32"] })
516 .I32Const(42).I32Const(1337).I32Const(0xBEEF).I32Const(0xFFFF)
520 const j = JSON.parse(b.json());
521 assert.eq(j.section[1].data[0].code.length, 6);
522 assert.eq(j.section[1].data[0].code[4].name, "call");
523 // FIXME: assert.eq(j.section[1].data[0].code[4].arguments.length, 4); https://bugs.webkit.org/show_bug.cgi?id=163267
524 assert.eq(j.section[1].data[0].code[4].immediates.length, 1);
525 assert.eq(j.section[1].data[0].code[4].immediates[0], 0);
528 (function OpcodeAdd() {
529 const b = (new Builder()).Type().End().Code()
531 .I32Const(42).I32Const(1337)
536 const j = JSON.parse(b.json());
537 assert.eq(j.section[1].data[0].code.length, 5);
538 assert.eq(j.section[1].data[0].code[2].name, "i32.add");
539 // FIXME: assert.eq(j.section[1].data[0].code[2].arguments.length, 2); https://bugs.webkit.org/show_bug.cgi?id=163267
540 assert.eq(j.section[1].data[0].code[3].name, "return");
541 // FIXME check return. https://bugs.webkit.org/show_bug.cgi?id=163267
544 (function OpcodeUnreachable() {
545 const b = (new Builder()).Type().End().Code().Function().Unreachable().End().End();
546 const j = JSON.parse(b.json());
547 assert.eq(j.section[1].data[0].code.length, 2);
548 assert.eq(j.section[1].data[0].code[0].name, "unreachable");
551 (function OpcodeUnreachableCombinations() {
552 (new Builder()).Type().End().Code().Function().Nop().Unreachable().End().End().json();
553 (new Builder()).Type().End().Code().Function().Unreachable().Nop().End().End().json();
554 (new Builder()).Type().End().Code().Function().Return().Unreachable().End().End().json();
555 (new Builder()).Type().End().Code().Function().Unreachable().Return().End().End().json();
556 (new Builder()).Type().End().Code().Function().Call(0).Unreachable().End().End().json();
557 (new Builder()).Type().End().Code().Function().Unreachable().Call(0).End().End().json();
560 (function OpcodeSelect() {
561 const b = (new Builder()).Type().End().Code().Function()
562 .I32Const(1).I32Const(2).I32Const(0)
567 const j = JSON.parse(b.json());
568 assert.eq(j.section[1].data[0].code.length, 6);
569 assert.eq(j.section[1].data[0].code[3].name, "select");
572 (function MemoryImport() {
573 const builder = (new Builder())
576 .Memory("__module__", "__field__", {initial: 30, maximum: 31})
580 const json = JSON.parse(builder.json());
581 assert.eq(json.section.length, 3);
582 assert.eq(json.section[1].name, "Import");
583 assert.eq(json.section[1].data.length, 1);
584 assert.eq(json.section[1].data[0].module, "__module__");
585 assert.eq(json.section[1].data[0].field, "__field__");
586 assert.eq(json.section[1].data[0].kind, "Memory");
587 assert.eq(json.section[1].data[0].memoryDescription.initial, 30);
588 assert.eq(json.section[1].data[0].memoryDescription.maximum, 31);
591 // FIXME test type mismatch with select. https://bugs.webkit.org/show_bug.cgi?id=163267