@wasmgroundup/emit 0.2.2 → 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
@@ -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;
@@ -60,4 +61,9 @@ declare const instr: {
60
61
  declare function u32(v: number | bigint): number[];
61
62
  declare function i32(v: number | bigint): number[];
62
63
  declare function locals(n: number, type: valtype): BytecodeFragment;
63
- export { BytecodeFragment, 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,
@@ -112,7 +114,7 @@ const instr = {
112
114
  add: 0xa0,
113
115
  sub: 0xa1,
114
116
  mul: 0xa2,
115
- div: 0xa3
117
+ div: 0xa3,
116
118
  },
117
119
  };
118
120
  const SEVEN_BIT_MASK_BIG_INT = 127n;
@@ -156,4 +158,18 @@ function i32(v) {
156
158
  function locals(n, type) {
157
159
  return [u32(n), type];
158
160
  }
159
- 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,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;
@@ -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,
@@ -142,7 +144,7 @@ const instr = {
142
144
  add: 0xa0,
143
145
  sub: 0xa1,
144
146
  mul: 0xa2,
145
- div: 0xa3
147
+ div: 0xa3,
146
148
  },
147
149
  };
148
150
 
@@ -194,6 +196,27 @@ function locals(n: number, type: valtype): BytecodeFragment {
194
196
  return [u32(n), type];
195
197
  }
196
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
+
197
220
  export {
198
221
  BytecodeFragment,
199
222
  code,
@@ -206,6 +229,9 @@ export {
206
229
  funcsec,
207
230
  functype,
208
231
  i32,
232
+ import_,
233
+ importdesc,
234
+ importsec,
209
235
  instr,
210
236
  locals,
211
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.2",
4
+ "version": "0.2.3",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {