@wasmgroundup/emit 0.2.4 → 0.2.5

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
@@ -20,6 +20,7 @@ declare function exportsec(exports: BytecodeFragment): BytecodeFragment;
20
20
  declare const funcidx: typeof u32;
21
21
  declare const exportdesc: {
22
22
  func(idx: number): BytecodeFragment;
23
+ global(idx: number): BytecodeFragment;
23
24
  };
24
25
  declare function module(sections: any): BytecodeFragment;
25
26
  declare const instr: {
@@ -31,6 +32,10 @@ declare const instr: {
31
32
  set: number;
32
33
  tee: number;
33
34
  };
35
+ global: {
36
+ get: number;
37
+ set: number;
38
+ };
34
39
  i32: {
35
40
  const: number;
36
41
  add: number;
@@ -67,4 +72,11 @@ declare function importsec(ims: BytecodeFragment): BytecodeFragment;
67
72
  declare const importdesc: {
68
73
  func(x: number): BytecodeFragment;
69
74
  };
70
- export { BytecodeFragment, code, codesec, export_, exportdesc, exportsec, f64, func, funcidx, funcsec, functype, i32, import_, importdesc, importsec, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
75
+ declare const mut: {
76
+ const: number;
77
+ var: number;
78
+ };
79
+ declare function globaltype(t: any, m: any): any[];
80
+ declare function global(gt: any, e: any): any[];
81
+ declare function globalsec(globs: any): (number | BytecodeFragment)[];
82
+ export { BytecodeFragment, code, codesec, export_, exportdesc, exportsec, f64, func, funcidx, funcsec, functype, global, globalsec, globaltype, i32, import_, importdesc, importsec, instr, locals, module, mut, name, section, typeidx, typesec, valtype, vec, };
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const SECTION_ID_TYPE = 1;
2
2
  const SECTION_ID_IMPORT = 2;
3
3
  const SECTION_ID_FUNCTION = 3;
4
+ const SECTION_ID_GLOBAL = 6;
4
5
  const SECTION_ID_EXPORT = 7;
5
6
  const SECTION_ID_CODE = 10;
6
7
  const TYPE_FUNCTION = 0x60;
@@ -73,6 +74,9 @@ const exportdesc = {
73
74
  func(idx) {
74
75
  return [0x00, funcidx(idx)];
75
76
  },
77
+ global(idx) {
78
+ return [0x03, globalidx(idx)];
79
+ },
76
80
  };
77
81
  function module(sections) {
78
82
  return [magic(), version(), sections];
@@ -90,6 +94,10 @@ const instr = {
90
94
  set: 0x21,
91
95
  tee: 0x22,
92
96
  },
97
+ global: {
98
+ get: 0x23,
99
+ set: 0x24,
100
+ },
93
101
  i32: {
94
102
  const: 0x41,
95
103
  add: 0x6a,
@@ -176,4 +184,21 @@ const importdesc = {
176
184
  return [0x00, funcidx(x)];
177
185
  },
178
186
  };
179
- export { code, codesec, export_, exportdesc, exportsec, f64, func, funcidx, funcsec, functype, i32, import_, importdesc, importsec, instr, locals, module, name, section, typeidx, typesec, valtype, vec, };
187
+ const globalidx = u32;
188
+ const mut = {
189
+ const: 0x00,
190
+ var: 0x01,
191
+ };
192
+ // t:valtype m:mut
193
+ function globaltype(t, m) {
194
+ return [t, m];
195
+ }
196
+ // gt:globaltype e:expr
197
+ function global(gt, e) {
198
+ return [gt, e];
199
+ }
200
+ // glob*:vec(global)
201
+ function globalsec(globs) {
202
+ return section(SECTION_ID_GLOBAL, vec(globs));
203
+ }
204
+ export { code, codesec, export_, exportdesc, exportsec, f64, func, funcidx, funcsec, functype, global, globalsec, globaltype, i32, import_, importdesc, importsec, instr, locals, module, mut, name, section, typeidx, typesec, valtype, vec, };
package/index.test.ts CHANGED
@@ -12,16 +12,27 @@ import {
12
12
  funcidx,
13
13
  funcsec,
14
14
  functype,
15
+ global,
16
+ globalsec,
17
+ globaltype,
18
+ i32,
15
19
  import_,
16
20
  importdesc,
17
21
  importsec,
18
22
  instr,
19
23
  module,
24
+ mut,
20
25
  typeidx,
21
26
  typesec,
22
27
  valtype,
23
28
  } from "./index";
24
29
 
30
+ const PI = 3.141592653589793115997963468544185161590576171875;
31
+
32
+ function fragmentToUInt8Array(frag: BytecodeFragment): Uint8Array {
33
+ return Uint8Array.from((frag as any).flat(Infinity));
34
+ }
35
+
25
36
  test("simple modules", async () => {
26
37
  const makeModule = (paramTypes, resultTypes, body) => {
27
38
  const mod = module([
@@ -30,7 +41,7 @@ test("simple modules", async () => {
30
41
  exportsec([export_("main", exportdesc.func(0))]),
31
42
  codesec([code(func([], body))]),
32
43
  ]);
33
- return Uint8Array.from(mod.flat(Infinity));
44
+ return fragmentToUInt8Array(mod);
34
45
  };
35
46
  const runMain = async (bytes, args) => {
36
47
  const { instance } = await WebAssembly.instantiate(bytes);
@@ -74,7 +85,7 @@ test("imports", async () => {
74
85
  code(func([], [instr.local.get, 0, instr.call, funcidx(0), instr.end])),
75
86
  ]),
76
87
  ]);
77
- return Uint8Array.from(mod.flat(Infinity));
88
+ return fragmentToUInt8Array(mod);
78
89
  };
79
90
 
80
91
  const { instance } = await WebAssembly.instantiate(makeModule(), {
@@ -90,23 +101,77 @@ test("imports", async () => {
90
101
  });
91
102
 
92
103
  test("f64", async () => {
93
- const PI = 3.141592653589793115997963468544185161590576171875;
94
104
  const makeModule = () => {
95
105
  const mod = module([
96
106
  typesec([functype([], [valtype.f64])]),
97
- funcsec([typeidx(0), typeidx(0)]),
107
+ funcsec([typeidx(0), typeidx(0), typeidx(0)]),
98
108
  exportsec([
99
109
  export_("pi", exportdesc.func(0)),
100
110
  export_("nan", exportdesc.func(1)),
111
+ export_("one", exportdesc.func(2)),
101
112
  ]),
102
113
  codesec([
103
114
  code(func([], [instr.f64.const, f64(PI), instr.end])),
104
115
  code(func([], [instr.f64.const, f64(Number.NaN), instr.end])),
116
+ code(func([], [instr.f64.const, f64(1), instr.end])),
105
117
  ]),
106
118
  ]);
107
- return Uint8Array.from(mod.flat(Infinity));
119
+ return fragmentToUInt8Array(mod);
108
120
  };
109
121
  const { instance } = await WebAssembly.instantiate(makeModule());
110
122
  expect(instance.exports.pi()).toBe(PI);
111
123
  expect(instance.exports.nan()).toBe(Number.NaN);
124
+ expect(instance.exports.one()).toBe(1);
125
+ });
126
+
127
+ test("globals", async () => {
128
+ const makeModule = () => {
129
+ const mod = module([
130
+ typesec([
131
+ functype([], [valtype.f64]), // pi
132
+ functype([], [valtype.i32]), // getVar
133
+ functype([], []), // incVar
134
+ ]),
135
+ funcsec([typeidx(0), typeidx(1), typeidx(2)]),
136
+ globalsec([
137
+ global(globaltype(valtype.f64, mut.const), [
138
+ instr.f64.const,
139
+ f64(PI),
140
+ instr.end,
141
+ ]),
142
+ global(globaltype(valtype.i32, mut.var), [
143
+ instr.i32.const,
144
+ i32(0),
145
+ instr.end,
146
+ ]),
147
+ ]),
148
+ exportsec([
149
+ export_("pi", exportdesc.func(0)),
150
+ export_("getVar", exportdesc.func(1)),
151
+ export_("incVar", exportdesc.func(2)),
152
+ ]),
153
+ codesec([
154
+ code(func([], [instr.global.get, 0, instr.end])), // pi
155
+ code(func([], [instr.global.get, 1, instr.end])), // getVar
156
+ code(
157
+ // prettier-ignore
158
+ func([], [
159
+ instr.global.get, 1,
160
+ instr.i32.const, 1,
161
+ instr.i32.add,
162
+ instr.global.set, 1,
163
+ instr.end,
164
+ ],
165
+ ),
166
+ ), // incVar
167
+ ]),
168
+ ]);
169
+ return fragmentToUInt8Array(mod);
170
+ };
171
+
172
+ const { exports } = (await WebAssembly.instantiate(makeModule())).instance;
173
+ expect(exports.pi()).toBe(PI);
174
+ expect(exports.getVar()).toBe(0);
175
+ exports.incVar();
176
+ expect(exports.getVar()).toBe(1);
112
177
  });
package/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  const SECTION_ID_TYPE = 1;
2
2
  const SECTION_ID_IMPORT = 2;
3
3
  const SECTION_ID_FUNCTION = 3;
4
+ const SECTION_ID_GLOBAL = 6;
4
5
  const SECTION_ID_EXPORT = 7;
5
6
  const SECTION_ID_CODE = 10;
6
7
 
@@ -100,6 +101,9 @@ const exportdesc = {
100
101
  func(idx: number): BytecodeFragment {
101
102
  return [0x00, funcidx(idx)];
102
103
  },
104
+ global(idx: number): BytecodeFragment {
105
+ return [0x03, globalidx(idx)];
106
+ },
103
107
  };
104
108
 
105
109
  function module(sections): BytecodeFragment {
@@ -120,6 +124,11 @@ const instr = {
120
124
  set: 0x21,
121
125
  tee: 0x22,
122
126
  },
127
+ global: {
128
+ get: 0x23,
129
+ set: 0x24,
130
+ },
131
+
123
132
  i32: {
124
133
  const: 0x41,
125
134
  add: 0x6a,
@@ -222,6 +231,28 @@ const importdesc = {
222
231
  },
223
232
  };
224
233
 
234
+ const globalidx = u32;
235
+
236
+ const mut = {
237
+ const: 0x00,
238
+ var: 0x01,
239
+ };
240
+
241
+ // t:valtype m:mut
242
+ function globaltype(t, m) {
243
+ return [t, m];
244
+ }
245
+
246
+ // gt:globaltype e:expr
247
+ function global(gt, e) {
248
+ return [gt, e];
249
+ }
250
+
251
+ // glob*:vec(global)
252
+ function globalsec(globs) {
253
+ return section(SECTION_ID_GLOBAL, vec(globs));
254
+ }
255
+
225
256
  export {
226
257
  BytecodeFragment,
227
258
  code,
@@ -234,6 +265,9 @@ export {
234
265
  funcidx,
235
266
  funcsec,
236
267
  functype,
268
+ global,
269
+ globalsec,
270
+ globaltype,
237
271
  i32,
238
272
  import_,
239
273
  importdesc,
@@ -241,6 +275,7 @@ export {
241
275
  instr,
242
276
  locals,
243
277
  module,
278
+ mut,
244
279
  name,
245
280
  section,
246
281
  typeidx,
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.4",
4
+ "version": "0.2.5",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {