@wasmgroundup/emit 0.2.1 → 0.2.3

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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type BytecodeFragment = (number | BytecodeFragment)[];
1
+ type BytecodeFragment = (number | BytecodeFragment)[];
2
2
  declare enum valtype {
3
3
  i32 = 127,
4
4
  i64 = 126,
@@ -25,6 +25,7 @@ declare function module(sections: any): BytecodeFragment;
25
25
  declare const instr: {
26
26
  nop: number;
27
27
  end: number;
28
+ call: number;
28
29
  local: {
29
30
  get: number;
30
31
  set: number;
@@ -47,15 +48,22 @@ declare const instr: {
47
48
  add: number;
48
49
  sub: number;
49
50
  mul: number;
51
+ div: number;
50
52
  };
51
53
  f64: {
52
54
  const: number;
53
55
  add: number;
54
56
  sub: number;
55
57
  mul: number;
58
+ div: number;
56
59
  };
57
60
  };
58
61
  declare function u32(v: number | bigint): number[];
59
62
  declare function i32(v: number | bigint): number[];
60
63
  declare function locals(n: number, type: valtype): BytecodeFragment;
61
- export { code, codesec, export_, exportdesc, exportsec, func, funcidx, funcsec, functype, i32, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
64
+ declare function import_(mod: string, nm: string, d: BytecodeFragment): BytecodeFragment;
65
+ declare function importsec(ims: BytecodeFragment): BytecodeFragment;
66
+ declare const importdesc: {
67
+ func(x: number): BytecodeFragment;
68
+ };
69
+ export { BytecodeFragment, code, codesec, export_, exportdesc, exportsec, func, funcidx, funcsec, functype, i32, import_, importdesc, importsec, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const SECTION_ID_TYPE = 1;
2
+ const SECTION_ID_IMPORT = 2;
2
3
  const SECTION_ID_FUNCTION = 3;
3
4
  const SECTION_ID_EXPORT = 7;
4
5
  const SECTION_ID_CODE = 10;
@@ -83,6 +84,7 @@ var numtype;
83
84
  const instr = {
84
85
  nop: 0x01,
85
86
  end: 0x0b,
87
+ call: 0x10,
86
88
  local: {
87
89
  get: 0x20,
88
90
  set: 0x21,
@@ -105,12 +107,14 @@ const instr = {
105
107
  add: 0x92,
106
108
  sub: 0x93,
107
109
  mul: 0x94,
110
+ div: 0x95,
108
111
  },
109
112
  f64: {
110
113
  const: 0x44,
111
114
  add: 0xa0,
112
115
  sub: 0xa1,
113
116
  mul: 0xa2,
117
+ div: 0xa3,
114
118
  },
115
119
  };
116
120
  const SEVEN_BIT_MASK_BIG_INT = 127n;
@@ -154,4 +158,18 @@ function i32(v) {
154
158
  function locals(n, type) {
155
159
  return [u32(n), type];
156
160
  }
157
- export { code, codesec, export_, exportdesc, exportsec, func, funcidx, funcsec, functype, i32, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
161
+ // mod:name nm:name d:importdesc
162
+ function import_(mod, nm, d) {
163
+ return [name(mod), name(nm), d];
164
+ }
165
+ // im*:vec(import)
166
+ function importsec(ims) {
167
+ return section(SECTION_ID_IMPORT, vec(ims));
168
+ }
169
+ const importdesc = {
170
+ // x:typeidx
171
+ func(x) {
172
+ return [0x00, funcidx(x)];
173
+ },
174
+ };
175
+ export { code, codesec, export_, exportdesc, exportsec, func, funcidx, funcsec, functype, i32, import_, importdesc, importsec, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
package/index.test.ts CHANGED
@@ -1,20 +1,24 @@
1
1
  import { expect, test } from "bun:test";
2
2
 
3
3
  import {
4
- module,
5
- typesec,
6
- functype,
7
- valtype,
8
- funcsec,
9
- typeidx,
10
- exportsec,
4
+ BytecodeFragment,
5
+ code,
6
+ codesec,
11
7
  export_,
12
8
  exportdesc,
13
- codesec,
14
- code,
9
+ exportsec,
15
10
  func,
16
- locals,
11
+ funcidx,
12
+ funcsec,
13
+ functype,
14
+ import_,
15
+ importdesc,
16
+ importsec,
17
17
  instr,
18
+ module,
19
+ typeidx,
20
+ typesec,
21
+ valtype,
18
22
  } from "./index";
19
23
 
20
24
  test("simple modules", async () => {
@@ -57,3 +61,29 @@ test("simple modules", async () => {
57
61
  ),
58
62
  ).toBe(99);
59
63
  });
64
+
65
+ test("imports", async () => {
66
+ const makeModule = () => {
67
+ const mod = module([
68
+ typesec([functype([valtype.i32], [valtype.i32])]),
69
+ importsec([import_("builtins", "addOne", importdesc.func(0))]),
70
+ funcsec([typeidx(0)]),
71
+ exportsec([export_("main", exportdesc.func(1))]),
72
+ codesec([
73
+ code(func([], [instr.local.get, 0, instr.call, funcidx(0), instr.end])),
74
+ ]),
75
+ ]);
76
+ return Uint8Array.from(mod.flat(Infinity));
77
+ };
78
+
79
+ const { instance } = await WebAssembly.instantiate(makeModule(), {
80
+ builtins: {
81
+ addOne(x) {
82
+ return x + 1;
83
+ },
84
+ },
85
+ });
86
+
87
+ expect(instance.exports.main(1)).toBe(2);
88
+ expect(instance.exports.main(2)).toBe(3);
89
+ });
package/index.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  const SECTION_ID_TYPE = 1;
2
+ const SECTION_ID_IMPORT = 2;
2
3
  const SECTION_ID_FUNCTION = 3;
3
4
  const SECTION_ID_EXPORT = 7;
4
5
  const SECTION_ID_CODE = 10;
5
6
 
6
7
  const TYPE_FUNCTION = 0x60;
7
8
 
8
- export type BytecodeFragment = (number | BytecodeFragment)[];
9
+ type BytecodeFragment = (number | BytecodeFragment)[];
9
10
 
10
11
  function stringToBytes(s: string): number[] {
11
12
  const bytes = new TextEncoder().encode(s);
@@ -112,6 +113,7 @@ enum numtype {
112
113
  const instr = {
113
114
  nop: 0x01,
114
115
  end: 0x0b,
116
+ call: 0x10,
115
117
 
116
118
  local: {
117
119
  get: 0x20,
@@ -135,12 +137,14 @@ const instr = {
135
137
  add: 0x92,
136
138
  sub: 0x93,
137
139
  mul: 0x94,
140
+ div: 0x95,
138
141
  },
139
142
  f64: {
140
143
  const: 0x44,
141
144
  add: 0xa0,
142
145
  sub: 0xa1,
143
146
  mul: 0xa2,
147
+ div: 0xa3,
144
148
  },
145
149
  };
146
150
 
@@ -192,7 +196,29 @@ function locals(n: number, type: valtype): BytecodeFragment {
192
196
  return [u32(n), type];
193
197
  }
194
198
 
199
+ // mod:name nm:name d:importdesc
200
+ function import_(
201
+ mod: string,
202
+ nm: string,
203
+ d: BytecodeFragment,
204
+ ): BytecodeFragment {
205
+ return [name(mod), name(nm), d];
206
+ }
207
+
208
+ // im*:vec(import)
209
+ function importsec(ims: BytecodeFragment): BytecodeFragment {
210
+ return section(SECTION_ID_IMPORT, vec(ims));
211
+ }
212
+
213
+ const importdesc = {
214
+ // x:typeidx
215
+ func(x: number): BytecodeFragment {
216
+ return [0x00, funcidx(x)];
217
+ },
218
+ };
219
+
195
220
  export {
221
+ BytecodeFragment,
196
222
  code,
197
223
  codesec,
198
224
  export_,
@@ -203,6 +229,9 @@ export {
203
229
  funcsec,
204
230
  functype,
205
231
  i32,
232
+ import_,
233
+ importdesc,
234
+ importsec,
206
235
  instr,
207
236
  locals,
208
237
  module,
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.2.1",
4
+ "version": "0.2.3",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {