@wasmgroundup/emit 0.2.3 → 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;
@@ -60,10 +65,18 @@ declare const instr: {
60
65
  };
61
66
  declare function u32(v: number | bigint): number[];
62
67
  declare function i32(v: number | bigint): number[];
68
+ declare function f64(v: number): number[];
63
69
  declare function locals(n: number, type: valtype): BytecodeFragment;
64
70
  declare function import_(mod: string, nm: string, d: BytecodeFragment): BytecodeFragment;
65
71
  declare function importsec(ims: BytecodeFragment): BytecodeFragment;
66
72
  declare const importdesc: {
67
73
  func(x: number): BytecodeFragment;
68
74
  };
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, };
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,
@@ -136,7 +144,6 @@ function u32(v) {
136
144
  }
137
145
  return r;
138
146
  }
139
- ///! START i32-v1 #priv #api #dedent
140
147
  function i32(v) {
141
148
  let val = BigInt(v);
142
149
  const r = [];
@@ -155,6 +162,11 @@ function i32(v) {
155
162
  }
156
163
  return r;
157
164
  }
165
+ function f64(v) {
166
+ var buf = new ArrayBuffer(8);
167
+ new Float64Array(buf)[0] = v;
168
+ return Array.from(new Uint8Array(buf));
169
+ }
158
170
  function locals(n, type) {
159
171
  return [u32(n), type];
160
172
  }
@@ -172,4 +184,21 @@ const importdesc = {
172
184
  return [0x00, funcidx(x)];
173
185
  },
174
186
  };
175
- export { code, codesec, export_, exportdesc, exportsec, 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
@@ -7,20 +7,32 @@ import {
7
7
  export_,
8
8
  exportdesc,
9
9
  exportsec,
10
+ f64,
10
11
  func,
11
12
  funcidx,
12
13
  funcsec,
13
14
  functype,
15
+ global,
16
+ globalsec,
17
+ globaltype,
18
+ i32,
14
19
  import_,
15
20
  importdesc,
16
21
  importsec,
17
22
  instr,
18
23
  module,
24
+ mut,
19
25
  typeidx,
20
26
  typesec,
21
27
  valtype,
22
28
  } from "./index";
23
29
 
30
+ const PI = 3.141592653589793115997963468544185161590576171875;
31
+
32
+ function fragmentToUInt8Array(frag: BytecodeFragment): Uint8Array {
33
+ return Uint8Array.from((frag as any).flat(Infinity));
34
+ }
35
+
24
36
  test("simple modules", async () => {
25
37
  const makeModule = (paramTypes, resultTypes, body) => {
26
38
  const mod = module([
@@ -29,7 +41,7 @@ test("simple modules", async () => {
29
41
  exportsec([export_("main", exportdesc.func(0))]),
30
42
  codesec([code(func([], body))]),
31
43
  ]);
32
- return Uint8Array.from(mod.flat(Infinity));
44
+ return fragmentToUInt8Array(mod);
33
45
  };
34
46
  const runMain = async (bytes, args) => {
35
47
  const { instance } = await WebAssembly.instantiate(bytes);
@@ -73,7 +85,7 @@ test("imports", async () => {
73
85
  code(func([], [instr.local.get, 0, instr.call, funcidx(0), instr.end])),
74
86
  ]),
75
87
  ]);
76
- return Uint8Array.from(mod.flat(Infinity));
88
+ return fragmentToUInt8Array(mod);
77
89
  };
78
90
 
79
91
  const { instance } = await WebAssembly.instantiate(makeModule(), {
@@ -87,3 +99,79 @@ test("imports", async () => {
87
99
  expect(instance.exports.main(1)).toBe(2);
88
100
  expect(instance.exports.main(2)).toBe(3);
89
101
  });
102
+
103
+ test("f64", async () => {
104
+ const makeModule = () => {
105
+ const mod = module([
106
+ typesec([functype([], [valtype.f64])]),
107
+ funcsec([typeidx(0), typeidx(0), typeidx(0)]),
108
+ exportsec([
109
+ export_("pi", exportdesc.func(0)),
110
+ export_("nan", exportdesc.func(1)),
111
+ export_("one", exportdesc.func(2)),
112
+ ]),
113
+ codesec([
114
+ code(func([], [instr.f64.const, f64(PI), instr.end])),
115
+ code(func([], [instr.f64.const, f64(Number.NaN), instr.end])),
116
+ code(func([], [instr.f64.const, f64(1), instr.end])),
117
+ ]),
118
+ ]);
119
+ return fragmentToUInt8Array(mod);
120
+ };
121
+ const { instance } = await WebAssembly.instantiate(makeModule());
122
+ expect(instance.exports.pi()).toBe(PI);
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);
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,
@@ -169,7 +178,6 @@ function u32(v: number | bigint): number[] {
169
178
  return r;
170
179
  }
171
180
 
172
- ///! START i32-v1 #priv #api #dedent
173
181
  function i32(v: number | bigint): number[] {
174
182
  let val = BigInt(v);
175
183
  const r = [];
@@ -192,6 +200,12 @@ function i32(v: number | bigint): number[] {
192
200
  return r;
193
201
  }
194
202
 
203
+ function f64(v: number): number[] {
204
+ var buf = new ArrayBuffer(8);
205
+ new Float64Array(buf)[0] = v;
206
+ return Array.from(new Uint8Array(buf));
207
+ }
208
+
195
209
  function locals(n: number, type: valtype): BytecodeFragment {
196
210
  return [u32(n), type];
197
211
  }
@@ -217,6 +231,28 @@ const importdesc = {
217
231
  },
218
232
  };
219
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
+
220
256
  export {
221
257
  BytecodeFragment,
222
258
  code,
@@ -224,10 +260,14 @@ export {
224
260
  export_,
225
261
  exportdesc,
226
262
  exportsec,
263
+ f64,
227
264
  func,
228
265
  funcidx,
229
266
  funcsec,
230
267
  functype,
268
+ global,
269
+ globalsec,
270
+ globaltype,
231
271
  i32,
232
272
  import_,
233
273
  importdesc,
@@ -235,6 +275,7 @@ export {
235
275
  instr,
236
276
  locals,
237
277
  module,
278
+ mut,
238
279
  name,
239
280
  section,
240
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.3",
4
+ "version": "0.2.5",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {