@wasmgroundup/emit 0.2.18 → 0.4.0
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/README.md +21 -0
- package/index.js +595 -0
- package/index.test.ts +12 -4
- package/package.json +3 -15
- package/dist/index.d.ts +0 -144
- package/dist/index.js +0 -312
- package/index.ts +0 -435
- package/tsconfig.json +0 -8
- package/wasmgroundup-emit-0.1.0.tgz +0 -0
package/README.md
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
1
|
# emit
|
|
2
|
+
|
|
3
|
+
_(From the book [WebAssembly from the Ground Up](https://wasmgroundup.com) — learn Wasm by building a simple compiler in JavaScript.)_
|
|
4
|
+
|
|
5
|
+
A JavaScript library to emit WebAssembly 1.0 binary modules.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm i @wasmgroundup/emit
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Simple API following the [spec](https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/)'s naming conventions
|
|
16
|
+
- Full support for WebAssembly 1.0 specification
|
|
17
|
+
- Generate binary WASM modules directly from JavaScript
|
|
18
|
+
- Zero dependencies
|
|
19
|
+
|
|
20
|
+
## License
|
|
21
|
+
|
|
22
|
+
MIT (see LICENSE file)
|
package/index.js
ADDED
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
// WebAssembly 1.0 Module Builder
|
|
2
|
+
// https://wasmgroundup.com/
|
|
3
|
+
export function stringToBytes(s) {
|
|
4
|
+
const bytes = new TextEncoder().encode(s);
|
|
5
|
+
return Array.from(bytes);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function magic() {
|
|
9
|
+
// [0x00, 0x61, 0x73, 0x6d]
|
|
10
|
+
return stringToBytes('\0asm');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function version() {
|
|
14
|
+
return [0x01, 0x00, 0x00, 0x00];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const CONTINUATION_BIT = 0b10000000;
|
|
18
|
+
export const SEVEN_BIT_MASK_BIG_INT = 0b01111111n;
|
|
19
|
+
export function leb128(v) {
|
|
20
|
+
let val = BigInt(v);
|
|
21
|
+
let more = true;
|
|
22
|
+
const r = [];
|
|
23
|
+
|
|
24
|
+
while (more) {
|
|
25
|
+
const b = Number(val & SEVEN_BIT_MASK_BIG_INT);
|
|
26
|
+
val = val >> 7n;
|
|
27
|
+
more = val !== 0n;
|
|
28
|
+
if (more) {
|
|
29
|
+
r.push(b | CONTINUATION_BIT);
|
|
30
|
+
} else {
|
|
31
|
+
r.push(b);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return r;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const MIN_U32 = 0;
|
|
39
|
+
export const MAX_U32 = 2 ** 32 - 1;
|
|
40
|
+
export function u32(v) {
|
|
41
|
+
if (v < MIN_U32 || v > MAX_U32) {
|
|
42
|
+
throw Error(`Value out of range for u32: ${v}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return leb128(v);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function sleb128(v) {
|
|
49
|
+
let val = BigInt(v);
|
|
50
|
+
let more = true;
|
|
51
|
+
const r = [];
|
|
52
|
+
|
|
53
|
+
while (more) {
|
|
54
|
+
const b = Number(val & SEVEN_BIT_MASK_BIG_INT);
|
|
55
|
+
const signBitSet = !!(b & 0x40);
|
|
56
|
+
|
|
57
|
+
val = val >> 7n;
|
|
58
|
+
|
|
59
|
+
if ((val === 0n && !signBitSet) || (val === -1n && signBitSet)) {
|
|
60
|
+
more = false;
|
|
61
|
+
r.push(b);
|
|
62
|
+
} else {
|
|
63
|
+
r.push(b | CONTINUATION_BIT);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return r;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const MIN_I32 = -(2 ** 32 / 2);
|
|
71
|
+
export const MAX_I32 = 2 ** 32 / 2 - 1;
|
|
72
|
+
export const I32_NEG_OFFSET = 2 ** 32;
|
|
73
|
+
export function i32(v) {
|
|
74
|
+
if (v < MIN_I32 || v > MAX_U32) {
|
|
75
|
+
throw Error(`Value out of range for i32: ${v}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (v > MAX_I32) {
|
|
79
|
+
return sleb128(v - I32_NEG_OFFSET);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return sleb128(v);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function section(id, contents) {
|
|
86
|
+
const sizeInBytes = contents.flat(Infinity).length;
|
|
87
|
+
return [id, u32(sizeInBytes), contents];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function vec(elements) {
|
|
91
|
+
return [u32(elements.length), elements];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const SECTION_ID_TYPE = 1;
|
|
95
|
+
|
|
96
|
+
export function functype(paramTypes, resultTypes) {
|
|
97
|
+
return [0x60, vec(paramTypes), vec(resultTypes)];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function typesec(functypes) {
|
|
101
|
+
return section(SECTION_ID_TYPE, vec(functypes));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const SECTION_ID_FUNCTION = 3;
|
|
105
|
+
|
|
106
|
+
export const typeidx = (x) => u32(x);
|
|
107
|
+
|
|
108
|
+
export function funcsec(typeidxs) {
|
|
109
|
+
return section(SECTION_ID_FUNCTION, vec(typeidxs));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const SECTION_ID_CODE = 10;
|
|
113
|
+
|
|
114
|
+
export function code(func) {
|
|
115
|
+
const sizeInBytes = func.flat(Infinity).length;
|
|
116
|
+
return [u32(sizeInBytes), func];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function func(locals, body) {
|
|
120
|
+
return [vec(locals), body];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function codesec(codes) {
|
|
124
|
+
return section(SECTION_ID_CODE, vec(codes));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export const instr = {
|
|
128
|
+
end: 0x0b,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const SECTION_ID_EXPORT = 7;
|
|
132
|
+
|
|
133
|
+
export function name(s) {
|
|
134
|
+
return vec(stringToBytes(s));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function export_(nm, exportdesc) {
|
|
138
|
+
return [name(nm), exportdesc];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function exportsec(exports) {
|
|
142
|
+
return section(SECTION_ID_EXPORT, vec(exports));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export const funcidx = (x) => u32(x);
|
|
146
|
+
|
|
147
|
+
export const exportdesc = {
|
|
148
|
+
func(idx) {
|
|
149
|
+
return [0x00, funcidx(idx)];
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export function module(sections) {
|
|
154
|
+
return [magic(), version(), sections];
|
|
155
|
+
}
|
|
156
|
+
export const valtype = {
|
|
157
|
+
i32: 0x7f,
|
|
158
|
+
i64: 0x7e,
|
|
159
|
+
f32: 0x7d,
|
|
160
|
+
f64: 0x7c,
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
instr.i32 = {const: 0x41};
|
|
164
|
+
instr.i64 = {const: 0x42};
|
|
165
|
+
instr.f32 = {const: 0x43};
|
|
166
|
+
instr.f64 = {const: 0x44};
|
|
167
|
+
instr.i32.add = 0x6a;
|
|
168
|
+
instr.i32.sub = 0x6b;
|
|
169
|
+
instr.i32.mul = 0x6c;
|
|
170
|
+
instr.i32.div_s = 0x6d;
|
|
171
|
+
instr.local = {};
|
|
172
|
+
instr.local.get = 0x20;
|
|
173
|
+
instr.local.set = 0x21;
|
|
174
|
+
instr.local.tee = 0x22;
|
|
175
|
+
|
|
176
|
+
export function locals(n, type) {
|
|
177
|
+
return [u32(n), type];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const localidx = (x) => u32(x);
|
|
181
|
+
|
|
182
|
+
instr.drop = 0x1a;
|
|
183
|
+
instr.call = 0x10;
|
|
184
|
+
instr.if = 0x04;
|
|
185
|
+
instr.else = 0x05;
|
|
186
|
+
|
|
187
|
+
export const blocktype = {empty: 0x40, ...valtype};
|
|
188
|
+
|
|
189
|
+
instr.i32.eq = 0x46; // a == b
|
|
190
|
+
instr.i32.ne = 0x47; // a != b
|
|
191
|
+
instr.i32.lt_s = 0x48; // a < b (signed)
|
|
192
|
+
instr.i32.lt_u = 0x49; // a < b (unsigned)
|
|
193
|
+
instr.i32.gt_s = 0x4a; // a > b (signed)
|
|
194
|
+
instr.i32.gt_u = 0x4b; // a > b (unsigned)
|
|
195
|
+
instr.i32.le_s = 0x4c; // a <= b (signed)
|
|
196
|
+
instr.i32.le_u = 0x4d; // a <= b (unsigned)
|
|
197
|
+
instr.i32.ge_s = 0x4e; // a >= b (signed)
|
|
198
|
+
instr.i32.ge_u = 0x4f; // a >= b (unsigned)
|
|
199
|
+
|
|
200
|
+
instr.i32.eqz = 0x45; // a == 0
|
|
201
|
+
|
|
202
|
+
instr.i32.and = 0x71;
|
|
203
|
+
instr.i32.or = 0x72;
|
|
204
|
+
|
|
205
|
+
export const labelidx = u32;
|
|
206
|
+
|
|
207
|
+
instr.block = 0x02;
|
|
208
|
+
instr.loop = 0x03;
|
|
209
|
+
instr.br = 0x0c;
|
|
210
|
+
instr.br_if = 0x0d;
|
|
211
|
+
export const SECTION_ID_IMPORT = 2;
|
|
212
|
+
|
|
213
|
+
// mod:name nm:name d:importdesc
|
|
214
|
+
export function import_(mod, nm, d) {
|
|
215
|
+
return [name(mod), name(nm), d];
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// im*:vec(import)
|
|
219
|
+
export function importsec(ims) {
|
|
220
|
+
return section(SECTION_ID_IMPORT, vec(ims));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export const importdesc = {
|
|
224
|
+
// x:typeidx
|
|
225
|
+
func(x) {
|
|
226
|
+
return [0x00, typeidx(x)];
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
export const SECTION_ID_MEMORY = 5;
|
|
230
|
+
|
|
231
|
+
export function memsec(mems) {
|
|
232
|
+
return section(SECTION_ID_MEMORY, vec(mems));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function mem(memtype) {
|
|
236
|
+
return memtype;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export function memtype(limits) {
|
|
240
|
+
return limits;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export const limits = {
|
|
244
|
+
// n:u32
|
|
245
|
+
min(n) {
|
|
246
|
+
return [0x00, u32(n)];
|
|
247
|
+
},
|
|
248
|
+
// n:u32, m:u32
|
|
249
|
+
minmax(n, m) {
|
|
250
|
+
return [0x01, u32(n), u32(m)];
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
export const memidx = u32;
|
|
255
|
+
|
|
256
|
+
exportdesc.mem = (idx) => [0x02, memidx(idx)];
|
|
257
|
+
|
|
258
|
+
instr.memory = {
|
|
259
|
+
size: 0x3f, // [] -> [i32]
|
|
260
|
+
grow: 0x40, // [i32] -> [i32]
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
instr.i32.load = 0x28; // [i32] -> [i32]
|
|
264
|
+
instr.i32.store = 0x36; // [i32, i32] -> []
|
|
265
|
+
|
|
266
|
+
// align:u32, offset:u32
|
|
267
|
+
export function memarg(align, offset) {
|
|
268
|
+
return [u32(align), u32(offset)];
|
|
269
|
+
}
|
|
270
|
+
export function int32ToBytes(v) {
|
|
271
|
+
return [v & 0xff, (v >> 8) & 0xff, (v >> 16) & 0xff, (v >> 24) & 0xff];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
instr.unreachable = 0x00;
|
|
275
|
+
|
|
276
|
+
export const SECTION_ID_DATA = 11;
|
|
277
|
+
|
|
278
|
+
// x:memidx e:expr bs:vec(byte)
|
|
279
|
+
export function data(x, e, bs) {
|
|
280
|
+
return [x, e, vec(bs)];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export function datasec(segs) {
|
|
284
|
+
return section(SECTION_ID_DATA, vec(segs));
|
|
285
|
+
}
|
|
286
|
+
export const SECTION_ID_CUSTOM = 0;
|
|
287
|
+
|
|
288
|
+
export function custom(name, bytes) {
|
|
289
|
+
return [name, bytes];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function customsec(custom) {
|
|
293
|
+
return section(SECTION_ID_CUSTOM, custom);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function namesec(namedata) {
|
|
297
|
+
return customsec(custom(name('name'), namedata));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// n:name
|
|
301
|
+
export function namedata(modulenamesubsec, funcnamesubsec, localnamesubsec) {
|
|
302
|
+
return [modulenamesubsec, funcnamesubsec, localnamesubsec];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export const CUSTOM_NAME_SUB_SEC_MODULE = 0;
|
|
306
|
+
export function modulenamesubsec(n) {
|
|
307
|
+
return namesubsection(CUSTOM_NAME_SUB_SEC_MODULE, name(n));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
export const CUSTOM_NAME_SUB_SEC_FUNC = 1;
|
|
311
|
+
export function funcnamesubsec(namemap) {
|
|
312
|
+
return namesubsection(CUSTOM_NAME_SUB_SEC_FUNC, namemap);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// N:byte
|
|
316
|
+
export function namesubsection(N, B) {
|
|
317
|
+
const flatB = B.flat(Infinity);
|
|
318
|
+
const size = u32(flatB.length);
|
|
319
|
+
return [N, size, flatB];
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function namemap(nameassocs) {
|
|
323
|
+
return vec(nameassocs);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function nameassoc(idx, n) {
|
|
327
|
+
return [idx, name(n)];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export const CUSTOM_NAME_SUB_SEC_LOCAL = 2;
|
|
331
|
+
export function localnamesubsec(indirectnamemap) {
|
|
332
|
+
return namesubsection(CUSTOM_NAME_SUB_SEC_LOCAL, indirectnamemap);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export function indirectnamemap(indirectnameassocs) {
|
|
336
|
+
return vec(indirectnameassocs);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function indirectnameassoc(idx, namemap) {
|
|
340
|
+
return [idx, namemap];
|
|
341
|
+
}
|
|
342
|
+
export const SECTION_ID_START = 8;
|
|
343
|
+
|
|
344
|
+
export const start = (x) => funcidx(x);
|
|
345
|
+
|
|
346
|
+
// st:start
|
|
347
|
+
export function startsec(st) {
|
|
348
|
+
return section(SECTION_ID_START, st);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
instr.global = {};
|
|
352
|
+
instr.global.get = 0x23;
|
|
353
|
+
instr.global.set = 0x24;
|
|
354
|
+
|
|
355
|
+
export const globalidx = (x) => u32(x);
|
|
356
|
+
|
|
357
|
+
export const SECTION_ID_GLOBAL = 6;
|
|
358
|
+
|
|
359
|
+
export const mut = {
|
|
360
|
+
const: 0x00,
|
|
361
|
+
var: 0x01,
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
// t:valtype m:mut
|
|
365
|
+
export function globaltype(t, m) {
|
|
366
|
+
return [t, m];
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// gt:globaltype e:expr
|
|
370
|
+
export function global(gt, e) {
|
|
371
|
+
return [gt, e];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// glob*:vec(global)
|
|
375
|
+
export function globalsec(globs) {
|
|
376
|
+
return section(SECTION_ID_GLOBAL, vec(globs));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export const SECTION_ID_TABLE = 4;
|
|
380
|
+
|
|
381
|
+
export const elemtype = {funcref: 0x70};
|
|
382
|
+
|
|
383
|
+
// et:elemtype lim:limits
|
|
384
|
+
export function tabletype(et, lim) {
|
|
385
|
+
return [et, lim];
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
// tt:tabletype
|
|
389
|
+
export function table(tt) {
|
|
390
|
+
return tt;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function tablesec(tables) {
|
|
394
|
+
return section(SECTION_ID_TABLE, vec(tables));
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export const tableidx = (x) => u32(x);
|
|
398
|
+
|
|
399
|
+
instr.call_indirect = 0x11; // [i32] -> []
|
|
400
|
+
|
|
401
|
+
export const SECTION_ID_ELEMENT = 9;
|
|
402
|
+
|
|
403
|
+
// x:tableidx e:expr y∗:vec(funcidx)
|
|
404
|
+
export function elem(x, e, ys) {
|
|
405
|
+
return [x, e, vec(ys)];
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export function elemsec(segs) {
|
|
409
|
+
return section(SECTION_ID_ELEMENT, vec(segs));
|
|
410
|
+
}
|
|
411
|
+
export function assert(cond, msg) {
|
|
412
|
+
if (!cond) {
|
|
413
|
+
throw new Error(msg);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
instr.local ??= {};
|
|
418
|
+
instr.global ??= {};
|
|
419
|
+
instr.memory ??= {};
|
|
420
|
+
instr.i32 ??= {};
|
|
421
|
+
instr.i64 ??= {};
|
|
422
|
+
instr.f32 ??= {};
|
|
423
|
+
instr.f64 ??= {};
|
|
424
|
+
instr.unreachable = 0x0;
|
|
425
|
+
instr.nop = 0x1;
|
|
426
|
+
instr.block = 0x2;
|
|
427
|
+
instr.loop = 0x3;
|
|
428
|
+
instr.if = 0x4;
|
|
429
|
+
instr.else = 0x5;
|
|
430
|
+
instr.end = 0xb;
|
|
431
|
+
instr.br = 0xc;
|
|
432
|
+
instr.br_if = 0xd;
|
|
433
|
+
instr.br_table = 0xe;
|
|
434
|
+
instr.return = 0xf;
|
|
435
|
+
instr.call = 0x10;
|
|
436
|
+
instr.call_indirect = 0x11;
|
|
437
|
+
instr.drop = 0x1a;
|
|
438
|
+
instr.select = 0x1b;
|
|
439
|
+
instr.local.get = 0x20;
|
|
440
|
+
instr.local.set = 0x21;
|
|
441
|
+
instr.local.tee = 0x22;
|
|
442
|
+
instr.global.get = 0x23;
|
|
443
|
+
instr.global.set = 0x24;
|
|
444
|
+
instr.i32.load = 0x28;
|
|
445
|
+
instr.i64.load = 0x29;
|
|
446
|
+
instr.f32.load = 0x2a;
|
|
447
|
+
instr.f64.load = 0x2b;
|
|
448
|
+
instr.i32.load8_s = 0x2c;
|
|
449
|
+
instr.i32.load8_u = 0x2d;
|
|
450
|
+
instr.i32.load16_s = 0x2e;
|
|
451
|
+
instr.i32.load16_u = 0x2f;
|
|
452
|
+
instr.i64.load8_s = 0x30;
|
|
453
|
+
instr.i64.load8_u = 0x31;
|
|
454
|
+
instr.i64.load16_s = 0x32;
|
|
455
|
+
instr.i64.load16_u = 0x33;
|
|
456
|
+
instr.i64.load32_s = 0x34;
|
|
457
|
+
instr.i64.load32_u = 0x35;
|
|
458
|
+
instr.i32.store = 0x36;
|
|
459
|
+
instr.i64.store = 0x37;
|
|
460
|
+
instr.f32.store = 0x38;
|
|
461
|
+
instr.f64.store = 0x39;
|
|
462
|
+
instr.i32.store8 = 0x3a;
|
|
463
|
+
instr.i32.store16 = 0x3b;
|
|
464
|
+
instr.i64.store8 = 0x3c;
|
|
465
|
+
instr.i64.store16 = 0x3d;
|
|
466
|
+
instr.i64.store32 = 0x3e;
|
|
467
|
+
instr.memory.size = 0x3f;
|
|
468
|
+
instr.memory.grow = 0x40;
|
|
469
|
+
instr.i32.const = 0x41;
|
|
470
|
+
instr.i64.const = 0x42;
|
|
471
|
+
instr.f32.const = 0x43;
|
|
472
|
+
instr.f64.const = 0x44;
|
|
473
|
+
instr.i32.eqz = 0x45;
|
|
474
|
+
instr.i32.eq = 0x46;
|
|
475
|
+
instr.i32.ne = 0x47;
|
|
476
|
+
instr.i32.lt_s = 0x48;
|
|
477
|
+
instr.i32.lt_u = 0x49;
|
|
478
|
+
instr.i32.gt_s = 0x4a;
|
|
479
|
+
instr.i32.gt_u = 0x4b;
|
|
480
|
+
instr.i32.le_s = 0x4c;
|
|
481
|
+
instr.i32.le_u = 0x4d;
|
|
482
|
+
instr.i32.ge_s = 0x4e;
|
|
483
|
+
instr.i32.ge_u = 0x4f;
|
|
484
|
+
instr.i64.eqz = 0x50;
|
|
485
|
+
instr.i64.eq = 0x51;
|
|
486
|
+
instr.i64.ne = 0x52;
|
|
487
|
+
instr.i64.lt_s = 0x53;
|
|
488
|
+
instr.i64.lt_u = 0x54;
|
|
489
|
+
instr.i64.gt_s = 0x55;
|
|
490
|
+
instr.i64.gt_u = 0x56;
|
|
491
|
+
instr.i64.le_s = 0x57;
|
|
492
|
+
instr.i64.le_u = 0x58;
|
|
493
|
+
instr.i64.ge_s = 0x59;
|
|
494
|
+
instr.i64.ge_u = 0x5a;
|
|
495
|
+
instr.f32.eq = 0x5b;
|
|
496
|
+
instr.f32.ne = 0x5c;
|
|
497
|
+
instr.f32.lt = 0x5d;
|
|
498
|
+
instr.f32.gt = 0x5e;
|
|
499
|
+
instr.f32.le = 0x5f;
|
|
500
|
+
instr.f32.ge = 0x60;
|
|
501
|
+
instr.f64.eq = 0x61;
|
|
502
|
+
instr.f64.ne = 0x62;
|
|
503
|
+
instr.f64.lt = 0x63;
|
|
504
|
+
instr.f64.gt = 0x64;
|
|
505
|
+
instr.f64.le = 0x65;
|
|
506
|
+
instr.f64.ge = 0x66;
|
|
507
|
+
instr.i32.clz = 0x67;
|
|
508
|
+
instr.i32.ctz = 0x68;
|
|
509
|
+
instr.i32.popcnt = 0x69;
|
|
510
|
+
instr.i32.add = 0x6a;
|
|
511
|
+
instr.i32.sub = 0x6b;
|
|
512
|
+
instr.i32.mul = 0x6c;
|
|
513
|
+
instr.i32.div_s = 0x6d;
|
|
514
|
+
instr.i32.div_u = 0x6e;
|
|
515
|
+
instr.i32.rem_s = 0x6f;
|
|
516
|
+
instr.i32.rem_u = 0x70;
|
|
517
|
+
instr.i32.and = 0x71;
|
|
518
|
+
instr.i32.or = 0x72;
|
|
519
|
+
instr.i32.xor = 0x73;
|
|
520
|
+
instr.i32.shl = 0x74;
|
|
521
|
+
instr.i32.shr_s = 0x75;
|
|
522
|
+
instr.i32.shr_u = 0x76;
|
|
523
|
+
instr.i32.rotl = 0x77;
|
|
524
|
+
instr.i32.rotr = 0x78;
|
|
525
|
+
instr.i64.clz = 0x79;
|
|
526
|
+
instr.i64.ctz = 0x7a;
|
|
527
|
+
instr.i64.popcnt = 0x7b;
|
|
528
|
+
instr.i64.add = 0x7c;
|
|
529
|
+
instr.i64.sub = 0x7d;
|
|
530
|
+
instr.i64.mul = 0x7e;
|
|
531
|
+
instr.i64.div_s = 0x7f;
|
|
532
|
+
instr.i64.div_u = 0x80;
|
|
533
|
+
instr.i64.rem_s = 0x81;
|
|
534
|
+
instr.i64.rem_u = 0x82;
|
|
535
|
+
instr.i64.and = 0x83;
|
|
536
|
+
instr.i64.or = 0x84;
|
|
537
|
+
instr.i64.xor = 0x85;
|
|
538
|
+
instr.i64.shl = 0x86;
|
|
539
|
+
instr.i64.shr_s = 0x87;
|
|
540
|
+
instr.i64.shr_u = 0x88;
|
|
541
|
+
instr.i64.rotl = 0x89;
|
|
542
|
+
instr.i64.rotr = 0x8a;
|
|
543
|
+
instr.f32.abs = 0x8b;
|
|
544
|
+
instr.f32.neg = 0x8c;
|
|
545
|
+
instr.f32.ceil = 0x8d;
|
|
546
|
+
instr.f32.floor = 0x8e;
|
|
547
|
+
instr.f32.trunc = 0x8f;
|
|
548
|
+
instr.f32.nearest = 0x90;
|
|
549
|
+
instr.f32.sqrt = 0x91;
|
|
550
|
+
instr.f32.add = 0x92;
|
|
551
|
+
instr.f32.sub = 0x93;
|
|
552
|
+
instr.f32.mul = 0x94;
|
|
553
|
+
instr.f32.div = 0x95;
|
|
554
|
+
instr.f32.min = 0x96;
|
|
555
|
+
instr.f32.max = 0x97;
|
|
556
|
+
instr.f32.copysign = 0x98;
|
|
557
|
+
instr.f64.abs = 0x99;
|
|
558
|
+
instr.f64.neg = 0x9a;
|
|
559
|
+
instr.f64.ceil = 0x9b;
|
|
560
|
+
instr.f64.floor = 0x9c;
|
|
561
|
+
instr.f64.trunc = 0x9d;
|
|
562
|
+
instr.f64.nearest = 0x9e;
|
|
563
|
+
instr.f64.sqrt = 0x9f;
|
|
564
|
+
instr.f64.add = 0xa0;
|
|
565
|
+
instr.f64.sub = 0xa1;
|
|
566
|
+
instr.f64.mul = 0xa2;
|
|
567
|
+
instr.f64.div = 0xa3;
|
|
568
|
+
instr.f64.min = 0xa4;
|
|
569
|
+
instr.f64.max = 0xa5;
|
|
570
|
+
instr.f64.copysign = 0xa6;
|
|
571
|
+
instr.i32.wrap_i64 = 0xa7;
|
|
572
|
+
instr.i32.trunc_f32_s = 0xa8;
|
|
573
|
+
instr.i32.trunc_f32_u = 0xa9;
|
|
574
|
+
instr.i32.trunc_f64_s = 0xaa;
|
|
575
|
+
instr.i32.trunc_f64_u = 0xab;
|
|
576
|
+
instr.i64.extend_i32_s = 0xac;
|
|
577
|
+
instr.i64.extend_i32_u = 0xad;
|
|
578
|
+
instr.i64.trunc_f32_s = 0xae;
|
|
579
|
+
instr.i64.trunc_f32_u = 0xaf;
|
|
580
|
+
instr.i64.trunc_f64_s = 0xb0;
|
|
581
|
+
instr.i64.trunc_f64_u = 0xb1;
|
|
582
|
+
instr.f32.convert_i32_s = 0xb2;
|
|
583
|
+
instr.f32.convert_i32_u = 0xb3;
|
|
584
|
+
instr.f32.convert_i64_s = 0xb4;
|
|
585
|
+
instr.f32.convert_i64_u = 0xb5;
|
|
586
|
+
instr.f32.demote_f64 = 0xb6;
|
|
587
|
+
instr.f64.convert_i32_s = 0xb7;
|
|
588
|
+
instr.f64.convert_i32_u = 0xb8;
|
|
589
|
+
instr.f64.convert_i64_s = 0xb9;
|
|
590
|
+
instr.f64.convert_i64_u = 0xba;
|
|
591
|
+
instr.f64.promote_f32 = 0xbb;
|
|
592
|
+
instr.i32.reinterpret_f32 = 0xbc;
|
|
593
|
+
instr.i64.reinterpret_f64 = 0xbd;
|
|
594
|
+
instr.f32.reinterpret_i32 = 0xbe;
|
|
595
|
+
instr.f64.reinterpret_i64 = 0xbf;
|
package/index.test.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { expect, test } from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
BytecodeFragment,
|
|
5
4
|
blocktype,
|
|
6
5
|
code,
|
|
7
6
|
codesec,
|
|
8
7
|
export_,
|
|
9
8
|
exportdesc,
|
|
10
9
|
exportsec,
|
|
11
|
-
f64,
|
|
12
10
|
func,
|
|
13
11
|
funcidx,
|
|
14
12
|
funcsec,
|
|
@@ -27,11 +25,21 @@ import {
|
|
|
27
25
|
typesec,
|
|
28
26
|
u32,
|
|
29
27
|
valtype,
|
|
30
|
-
} from "./index";
|
|
28
|
+
} from "./index.js";
|
|
29
|
+
|
|
30
|
+
function blocktype(t?: valtype): number {
|
|
31
|
+
return t ?? 0x40;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function f64(v: number): number[] {
|
|
35
|
+
var buf = new ArrayBuffer(8);
|
|
36
|
+
new Float64Array(buf)[0] = v;
|
|
37
|
+
return Array.from(new Uint8Array(buf));
|
|
38
|
+
}
|
|
31
39
|
|
|
32
40
|
const PI = 3.141592653589793115997963468544185161590576171875;
|
|
33
41
|
|
|
34
|
-
function fragmentToUInt8Array(frag
|
|
42
|
+
function fragmentToUInt8Array(frag): Uint8Array {
|
|
35
43
|
return Uint8Array.from((frag as any).flat(Infinity));
|
|
36
44
|
}
|
|
37
45
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wasmgroundup/emit",
|
|
3
3
|
"description": "A library for creating binary-encoded WebAssembly modules",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"main": "
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
6
|
+
"main": "index.js",
|
|
8
7
|
"repository": {
|
|
9
8
|
"type": "git",
|
|
10
9
|
"url": "git+https://github.com/wasmgroundup/emit.git"
|
|
@@ -18,16 +17,5 @@
|
|
|
18
17
|
"bugs": {
|
|
19
18
|
"url": "https://github.com/wasmgroundup/emit/issues"
|
|
20
19
|
},
|
|
21
|
-
"homepage": "https://github.com/wasmgroundup/emit#readme"
|
|
22
|
-
"scripts": {
|
|
23
|
-
"check": "tsc --noEmit",
|
|
24
|
-
"format": "prettier --write .",
|
|
25
|
-
"prepublishOnly": "tsc"
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"prettier": "^3.0.3"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"typescript": "^5.2.2"
|
|
32
|
-
}
|
|
20
|
+
"homepage": "https://github.com/wasmgroundup/emit#readme"
|
|
33
21
|
}
|