@wasmgroundup/emit 0.2.5 → 0.2.6

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
@@ -5,6 +5,7 @@ declare enum valtype {
5
5
  f32 = 125,
6
6
  f64 = 124
7
7
  }
8
+ declare function blocktype(t?: valtype): number;
8
9
  declare function vec<T extends BytecodeFragment>(elements: T): BytecodeFragment;
9
10
  declare function section(id: number, contents: BytecodeFragment): (number | BytecodeFragment)[];
10
11
  declare function functype(paramTypes: valtype[], resultTypes: valtype[]): BytecodeFragment;
@@ -25,6 +26,8 @@ declare const exportdesc: {
25
26
  declare function module(sections: any): BytecodeFragment;
26
27
  declare const instr: {
27
28
  nop: number;
29
+ if: number;
30
+ else: number;
28
31
  end: number;
29
32
  call: number;
30
33
  local: {
@@ -79,4 +82,4 @@ declare const mut: {
79
82
  declare function globaltype(t: any, m: any): any[];
80
83
  declare function global(gt: any, e: any): any[];
81
84
  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, };
85
+ export { blocktype, 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
@@ -33,6 +33,10 @@ var valtype;
33
33
  valtype[valtype["f32"] = 125] = "f32";
34
34
  valtype[valtype["f64"] = 124] = "f64";
35
35
  })(valtype || (valtype = {}));
36
+ // t: valtype
37
+ function blocktype(t) {
38
+ return t ?? 0x40;
39
+ }
36
40
  function vec(elements) {
37
41
  return [u32(elements.length), ...elements];
38
42
  }
@@ -87,6 +91,8 @@ var numtype;
87
91
  })(numtype || (numtype = {}));
88
92
  const instr = {
89
93
  nop: 0x01,
94
+ if: 0x04,
95
+ else: 0x05,
90
96
  end: 0x0b,
91
97
  call: 0x10,
92
98
  local: {
@@ -201,4 +207,4 @@ function global(gt, e) {
201
207
  function globalsec(globs) {
202
208
  return section(SECTION_ID_GLOBAL, vec(globs));
203
209
  }
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, };
210
+ export { blocktype, 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
@@ -2,6 +2,7 @@ import { expect, test } from "bun:test";
2
2
 
3
3
  import {
4
4
  BytecodeFragment,
5
+ blocktype,
5
6
  code,
6
7
  codesec,
7
8
  export_,
@@ -175,3 +176,34 @@ test("globals", async () => {
175
176
  exports.incVar();
176
177
  expect(exports.getVar()).toBe(1);
177
178
  });
179
+
180
+ test("if", async () => {
181
+ const makeModule = () => {
182
+ const mod = module([
183
+ typesec([functype([valtype.i32], [valtype.f64])]),
184
+ funcsec([typeidx(0)]),
185
+ exportsec([export_("maybePi", exportdesc.func(0))]),
186
+ codesec([
187
+ code(
188
+ func(
189
+ [],
190
+ // prettier-ignore
191
+ [
192
+ instr.local.get, 0,
193
+ instr.if, blocktype(valtype.f64),
194
+ instr.f64.const, f64(PI),
195
+ instr.else,
196
+ instr.f64.const, f64(0),
197
+ instr.end,
198
+ instr.end,
199
+ ],
200
+ ),
201
+ ),
202
+ ]),
203
+ ]);
204
+ return fragmentToUInt8Array(mod);
205
+ };
206
+ const { exports } = (await WebAssembly.instantiate(makeModule())).instance;
207
+ expect(exports.maybePi(1)).toBe(PI);
208
+ expect(exports.maybePi(0)).toBe(0);
209
+ });
package/index.ts CHANGED
@@ -41,6 +41,11 @@ enum valtype {
41
41
  f64 = 0x7c,
42
42
  }
43
43
 
44
+ // t: valtype
45
+ function blocktype(t?: valtype): number {
46
+ return t ?? 0x40;
47
+ }
48
+
44
49
  function vec<T extends BytecodeFragment>(elements: T): BytecodeFragment {
45
50
  return [u32(elements.length), ...elements];
46
51
  }
@@ -116,6 +121,8 @@ enum numtype {
116
121
 
117
122
  const instr = {
118
123
  nop: 0x01,
124
+ if: 0x04,
125
+ else: 0x05,
119
126
  end: 0x0b,
120
127
  call: 0x10,
121
128
 
@@ -254,6 +261,7 @@ function globalsec(globs) {
254
261
  }
255
262
 
256
263
  export {
264
+ blocktype,
257
265
  BytecodeFragment,
258
266
  code,
259
267
  codesec,
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.5",
4
+ "version": "0.2.6",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "repository": {