@wasmgroundup/emit 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +21 -0
  2. package/index.js +210 -8
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1 +1,22 @@
1
1
  # emit
2
+
3
+ _(From the book [WebAssembly from the Ground Up](https://wasmgroundup.com) — learn Wasm by building a simple compiler in JavaScript.)_
4
+
5
+ A JavaScript library to emit WebAssembly 1.0 binary modules.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm i @wasmgroundup/emit
11
+ ```
12
+
13
+ ## Features
14
+
15
+ - Simple API following the [spec](https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/)'s naming conventions
16
+ - Full support for WebAssembly 1.0 specification
17
+ - Generate binary WASM modules directly from JavaScript
18
+ - Zero dependencies
19
+
20
+ ## License
21
+
22
+ MIT (see LICENSE file)
package/index.js CHANGED
@@ -14,12 +14,9 @@ export function version() {
14
14
  return [0x01, 0x00, 0x00, 0x00];
15
15
  }
16
16
 
17
- export const SEVEN_BIT_MASK_BIG_INT = 0b01111111n;
18
17
  export const CONTINUATION_BIT = 0b10000000;
19
-
20
- export function u32(v) {
21
- assert(v >= 0, `Value is negative: ${v}`);
22
-
18
+ export const SEVEN_BIT_MASK_BIG_INT = 0b01111111n;
19
+ export function leb128(v) {
23
20
  let val = BigInt(v);
24
21
  let more = true;
25
22
  const r = [];
@@ -38,13 +35,23 @@ export function u32(v) {
38
35
  return r;
39
36
  }
40
37
 
41
- export function i32(v) {
38
+ export const MIN_U32 = 0;
39
+ export const MAX_U32 = 2 ** 32 - 1;
40
+ export function u32(v) {
41
+ if (v < MIN_U32 || v > MAX_U32) {
42
+ throw Error(`Value out of range for u32: ${v}`);
43
+ }
44
+
45
+ return leb128(v);
46
+ }
47
+
48
+ export function sleb128(v) {
42
49
  let val = BigInt(v);
50
+ let more = true;
43
51
  const r = [];
44
52
 
45
- let more = true;
46
53
  while (more) {
47
- const b = Number(val & 0b01111111n);
54
+ const b = Number(val & SEVEN_BIT_MASK_BIG_INT);
48
55
  const signBitSet = !!(b & 0x40);
49
56
 
50
57
  val = val >> 7n;
@@ -60,6 +67,21 @@ export function i32(v) {
60
67
  return r;
61
68
  }
62
69
 
70
+ export const MIN_I32 = -(2 ** 32 / 2);
71
+ export const MAX_I32 = 2 ** 32 / 2 - 1;
72
+ export const I32_NEG_OFFSET = 2 ** 32;
73
+ export function i32(v) {
74
+ if (v < MIN_I32 || v > MAX_U32) {
75
+ throw Error(`Value out of range for i32: ${v}`);
76
+ }
77
+
78
+ if (v > MAX_I32) {
79
+ return sleb128(v - I32_NEG_OFFSET);
80
+ }
81
+
82
+ return sleb128(v);
83
+ }
84
+
63
85
  export function section(id, contents) {
64
86
  const sizeInBytes = contents.flat(Infinity).length;
65
87
  return [id, u32(sizeInBytes), contents];
@@ -391,3 +413,183 @@ export function assert(cond, msg) {
391
413
  throw new Error(msg);
392
414
  }
393
415
  }
416
+
417
+ instr.local ??= {};
418
+ instr.global ??= {};
419
+ instr.memory ??= {};
420
+ instr.i32 ??= {};
421
+ instr.i64 ??= {};
422
+ instr.f32 ??= {};
423
+ instr.f64 ??= {};
424
+ instr.unreachable = 0x0;
425
+ instr.nop = 0x1;
426
+ instr.block = 0x2;
427
+ instr.loop = 0x3;
428
+ instr.if = 0x4;
429
+ instr.else = 0x5;
430
+ instr.end = 0xb;
431
+ instr.br = 0xc;
432
+ instr.br_if = 0xd;
433
+ instr.br_table = 0xe;
434
+ instr.return = 0xf;
435
+ instr.call = 0x10;
436
+ instr.call_indirect = 0x11;
437
+ instr.drop = 0x1a;
438
+ instr.select = 0x1b;
439
+ instr.local.get = 0x20;
440
+ instr.local.set = 0x21;
441
+ instr.local.tee = 0x22;
442
+ instr.global.get = 0x23;
443
+ instr.global.set = 0x24;
444
+ instr.i32.load = 0x28;
445
+ instr.i64.load = 0x29;
446
+ instr.f32.load = 0x2a;
447
+ instr.f64.load = 0x2b;
448
+ instr.i32.load8_s = 0x2c;
449
+ instr.i32.load8_u = 0x2d;
450
+ instr.i32.load16_s = 0x2e;
451
+ instr.i32.load16_u = 0x2f;
452
+ instr.i64.load8_s = 0x30;
453
+ instr.i64.load8_u = 0x31;
454
+ instr.i64.load16_s = 0x32;
455
+ instr.i64.load16_u = 0x33;
456
+ instr.i64.load32_s = 0x34;
457
+ instr.i64.load32_u = 0x35;
458
+ instr.i32.store = 0x36;
459
+ instr.i64.store = 0x37;
460
+ instr.f32.store = 0x38;
461
+ instr.f64.store = 0x39;
462
+ instr.i32.store8 = 0x3a;
463
+ instr.i32.store16 = 0x3b;
464
+ instr.i64.store8 = 0x3c;
465
+ instr.i64.store16 = 0x3d;
466
+ instr.i64.store32 = 0x3e;
467
+ instr.memory.size = 0x3f;
468
+ instr.memory.grow = 0x40;
469
+ instr.i32.const = 0x41;
470
+ instr.i64.const = 0x42;
471
+ instr.f32.const = 0x43;
472
+ instr.f64.const = 0x44;
473
+ instr.i32.eqz = 0x45;
474
+ instr.i32.eq = 0x46;
475
+ instr.i32.ne = 0x47;
476
+ instr.i32.lt_s = 0x48;
477
+ instr.i32.lt_u = 0x49;
478
+ instr.i32.gt_s = 0x4a;
479
+ instr.i32.gt_u = 0x4b;
480
+ instr.i32.le_s = 0x4c;
481
+ instr.i32.le_u = 0x4d;
482
+ instr.i32.ge_s = 0x4e;
483
+ instr.i32.ge_u = 0x4f;
484
+ instr.i64.eqz = 0x50;
485
+ instr.i64.eq = 0x51;
486
+ instr.i64.ne = 0x52;
487
+ instr.i64.lt_s = 0x53;
488
+ instr.i64.lt_u = 0x54;
489
+ instr.i64.gt_s = 0x55;
490
+ instr.i64.gt_u = 0x56;
491
+ instr.i64.le_s = 0x57;
492
+ instr.i64.le_u = 0x58;
493
+ instr.i64.ge_s = 0x59;
494
+ instr.i64.ge_u = 0x5a;
495
+ instr.f32.eq = 0x5b;
496
+ instr.f32.ne = 0x5c;
497
+ instr.f32.lt = 0x5d;
498
+ instr.f32.gt = 0x5e;
499
+ instr.f32.le = 0x5f;
500
+ instr.f32.ge = 0x60;
501
+ instr.f64.eq = 0x61;
502
+ instr.f64.ne = 0x62;
503
+ instr.f64.lt = 0x63;
504
+ instr.f64.gt = 0x64;
505
+ instr.f64.le = 0x65;
506
+ instr.f64.ge = 0x66;
507
+ instr.i32.clz = 0x67;
508
+ instr.i32.ctz = 0x68;
509
+ instr.i32.popcnt = 0x69;
510
+ instr.i32.add = 0x6a;
511
+ instr.i32.sub = 0x6b;
512
+ instr.i32.mul = 0x6c;
513
+ instr.i32.div_s = 0x6d;
514
+ instr.i32.div_u = 0x6e;
515
+ instr.i32.rem_s = 0x6f;
516
+ instr.i32.rem_u = 0x70;
517
+ instr.i32.and = 0x71;
518
+ instr.i32.or = 0x72;
519
+ instr.i32.xor = 0x73;
520
+ instr.i32.shl = 0x74;
521
+ instr.i32.shr_s = 0x75;
522
+ instr.i32.shr_u = 0x76;
523
+ instr.i32.rotl = 0x77;
524
+ instr.i32.rotr = 0x78;
525
+ instr.i64.clz = 0x79;
526
+ instr.i64.ctz = 0x7a;
527
+ instr.i64.popcnt = 0x7b;
528
+ instr.i64.add = 0x7c;
529
+ instr.i64.sub = 0x7d;
530
+ instr.i64.mul = 0x7e;
531
+ instr.i64.div_s = 0x7f;
532
+ instr.i64.div_u = 0x80;
533
+ instr.i64.rem_s = 0x81;
534
+ instr.i64.rem_u = 0x82;
535
+ instr.i64.and = 0x83;
536
+ instr.i64.or = 0x84;
537
+ instr.i64.xor = 0x85;
538
+ instr.i64.shl = 0x86;
539
+ instr.i64.shr_s = 0x87;
540
+ instr.i64.shr_u = 0x88;
541
+ instr.i64.rotl = 0x89;
542
+ instr.i64.rotr = 0x8a;
543
+ instr.f32.abs = 0x8b;
544
+ instr.f32.neg = 0x8c;
545
+ instr.f32.ceil = 0x8d;
546
+ instr.f32.floor = 0x8e;
547
+ instr.f32.trunc = 0x8f;
548
+ instr.f32.nearest = 0x90;
549
+ instr.f32.sqrt = 0x91;
550
+ instr.f32.add = 0x92;
551
+ instr.f32.sub = 0x93;
552
+ instr.f32.mul = 0x94;
553
+ instr.f32.div = 0x95;
554
+ instr.f32.min = 0x96;
555
+ instr.f32.max = 0x97;
556
+ instr.f32.copysign = 0x98;
557
+ instr.f64.abs = 0x99;
558
+ instr.f64.neg = 0x9a;
559
+ instr.f64.ceil = 0x9b;
560
+ instr.f64.floor = 0x9c;
561
+ instr.f64.trunc = 0x9d;
562
+ instr.f64.nearest = 0x9e;
563
+ instr.f64.sqrt = 0x9f;
564
+ instr.f64.add = 0xa0;
565
+ instr.f64.sub = 0xa1;
566
+ instr.f64.mul = 0xa2;
567
+ instr.f64.div = 0xa3;
568
+ instr.f64.min = 0xa4;
569
+ instr.f64.max = 0xa5;
570
+ instr.f64.copysign = 0xa6;
571
+ instr.i32.wrap_i64 = 0xa7;
572
+ instr.i32.trunc_f32_s = 0xa8;
573
+ instr.i32.trunc_f32_u = 0xa9;
574
+ instr.i32.trunc_f64_s = 0xaa;
575
+ instr.i32.trunc_f64_u = 0xab;
576
+ instr.i64.extend_i32_s = 0xac;
577
+ instr.i64.extend_i32_u = 0xad;
578
+ instr.i64.trunc_f32_s = 0xae;
579
+ instr.i64.trunc_f32_u = 0xaf;
580
+ instr.i64.trunc_f64_s = 0xb0;
581
+ instr.i64.trunc_f64_u = 0xb1;
582
+ instr.f32.convert_i32_s = 0xb2;
583
+ instr.f32.convert_i32_u = 0xb3;
584
+ instr.f32.convert_i64_s = 0xb4;
585
+ instr.f32.convert_i64_u = 0xb5;
586
+ instr.f32.demote_f64 = 0xb6;
587
+ instr.f64.convert_i32_s = 0xb7;
588
+ instr.f64.convert_i32_u = 0xb8;
589
+ instr.f64.convert_i64_s = 0xb9;
590
+ instr.f64.convert_i64_u = 0xba;
591
+ instr.f64.promote_f32 = 0xbb;
592
+ instr.i32.reinterpret_f32 = 0xbc;
593
+ instr.i64.reinterpret_f64 = 0xbd;
594
+ instr.f32.reinterpret_i32 = 0xbe;
595
+ instr.f64.reinterpret_i64 = 0xbf;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wasmgroundup/emit",
3
3
  "description": "A library for creating binary-encoded WebAssembly modules",
4
- "version": "0.3.2",
4
+ "version": "0.4.0",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "repository": {