@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/index.ts
DELETED
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
const SECTION_ID_TYPE = 1;
|
|
2
|
-
const SECTION_ID_IMPORT = 2;
|
|
3
|
-
const SECTION_ID_FUNCTION = 3;
|
|
4
|
-
const SECTION_ID_TABLE = 4;
|
|
5
|
-
const SECTION_ID_MEMORY = 5;
|
|
6
|
-
const SECTION_ID_GLOBAL = 6;
|
|
7
|
-
const SECTION_ID_EXPORT = 7;
|
|
8
|
-
const SECTION_ID_START = 8;
|
|
9
|
-
const SECTION_ID_ELEMENT = 9;
|
|
10
|
-
const SECTION_ID_CODE = 10;
|
|
11
|
-
const SECTION_ID_DATA = 11;
|
|
12
|
-
|
|
13
|
-
const TYPE_FUNCTION = 0x60;
|
|
14
|
-
|
|
15
|
-
type BytecodeFragment = (number | BytecodeFragment)[];
|
|
16
|
-
|
|
17
|
-
function stringToBytes(s: string): number[] {
|
|
18
|
-
const bytes = new TextEncoder().encode(s);
|
|
19
|
-
return Array.from(bytes);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function int32ToBytes(v: number): number[] {
|
|
23
|
-
// prettier-ignore
|
|
24
|
-
return [
|
|
25
|
-
v & 0xff,
|
|
26
|
-
(v >> 8) & 0xff,
|
|
27
|
-
(v >> 16) & 0xff,
|
|
28
|
-
(v >> 24) & 0xff,
|
|
29
|
-
];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function magic(): number[] {
|
|
33
|
-
// [0x00, 0x61, 0x73, 0x6d]
|
|
34
|
-
return stringToBytes("\0asm");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function version(): number[] {
|
|
38
|
-
// [0x01, 0x00, 0x00, 0x00]
|
|
39
|
-
return int32ToBytes(1);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
enum valtype {
|
|
43
|
-
i32 = 0x7f,
|
|
44
|
-
i64 = 0x7e,
|
|
45
|
-
f32 = 0x7d,
|
|
46
|
-
f64 = 0x7c,
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// t: valtype
|
|
50
|
-
function blocktype(t?: valtype): number {
|
|
51
|
-
return t ?? 0x40;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function vec<T extends BytecodeFragment>(elements: T): BytecodeFragment {
|
|
55
|
-
return [u32(elements.length), ...elements];
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function section(id: number, contents: BytecodeFragment) {
|
|
59
|
-
const sizeInBytes = (contents as any[]).flat(Infinity).length;
|
|
60
|
-
return [id, u32(sizeInBytes), contents];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function functype(
|
|
64
|
-
paramTypes: valtype[],
|
|
65
|
-
resultTypes: valtype[],
|
|
66
|
-
): BytecodeFragment {
|
|
67
|
-
return [TYPE_FUNCTION, vec(paramTypes), vec(resultTypes)];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function typesec(functypes: BytecodeFragment): BytecodeFragment {
|
|
71
|
-
return section(SECTION_ID_TYPE, vec(functypes));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const typeidx = u32;
|
|
75
|
-
|
|
76
|
-
function funcsec(typeidxs: BytecodeFragment): BytecodeFragment {
|
|
77
|
-
return section(SECTION_ID_FUNCTION, vec(typeidxs));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function code(func: BytecodeFragment): BytecodeFragment {
|
|
81
|
-
const sizeInBytes = (func as any[]).flat(Infinity).length;
|
|
82
|
-
return [u32(sizeInBytes), func];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function func(
|
|
86
|
-
locals: BytecodeFragment,
|
|
87
|
-
body: BytecodeFragment,
|
|
88
|
-
): BytecodeFragment {
|
|
89
|
-
return [vec(locals), body];
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function codesec(codes: BytecodeFragment): BytecodeFragment {
|
|
93
|
-
return section(SECTION_ID_CODE, vec(codes));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function name(s: string): BytecodeFragment {
|
|
97
|
-
return vec(stringToBytes(s));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function export_(nm: string, exportdesc: BytecodeFragment): BytecodeFragment {
|
|
101
|
-
return [name(nm), exportdesc];
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function exportsec(exports: BytecodeFragment): BytecodeFragment {
|
|
105
|
-
return section(SECTION_ID_EXPORT, vec(exports));
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const funcidx = u32;
|
|
109
|
-
|
|
110
|
-
const exportdesc = {
|
|
111
|
-
func(idx: number): BytecodeFragment {
|
|
112
|
-
return [0x00, funcidx(idx)];
|
|
113
|
-
},
|
|
114
|
-
table(idx: number): BytecodeFragment {
|
|
115
|
-
return [0x01, tableidx(idx)];
|
|
116
|
-
},
|
|
117
|
-
mem(idx: number): BytecodeFragment {
|
|
118
|
-
return [0x02, memidx(idx)];
|
|
119
|
-
},
|
|
120
|
-
global(idx: number): BytecodeFragment {
|
|
121
|
-
return [0x03, globalidx(idx)];
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
function module(sections): BytecodeFragment {
|
|
126
|
-
return [magic(), version(), sections];
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const instr = {
|
|
130
|
-
unreachable: 0x00,
|
|
131
|
-
nop: 0x01,
|
|
132
|
-
block: 0x02,
|
|
133
|
-
loop: 0x03,
|
|
134
|
-
if: 0x04,
|
|
135
|
-
else: 0x05,
|
|
136
|
-
end: 0x0b,
|
|
137
|
-
br: 0x0c,
|
|
138
|
-
br_if: 0x0d,
|
|
139
|
-
br_table: 0x0e,
|
|
140
|
-
return: 0x0f,
|
|
141
|
-
call: 0x10,
|
|
142
|
-
call_indirect: 0x11,
|
|
143
|
-
drop: 0x1a,
|
|
144
|
-
select: 0x1b,
|
|
145
|
-
|
|
146
|
-
local: {
|
|
147
|
-
get: 0x20,
|
|
148
|
-
set: 0x21,
|
|
149
|
-
tee: 0x22,
|
|
150
|
-
},
|
|
151
|
-
global: {
|
|
152
|
-
get: 0x23,
|
|
153
|
-
set: 0x24,
|
|
154
|
-
tee: 0x25,
|
|
155
|
-
},
|
|
156
|
-
memory: {
|
|
157
|
-
size: 0x3f,
|
|
158
|
-
grow: 0x40,
|
|
159
|
-
},
|
|
160
|
-
|
|
161
|
-
i32: {
|
|
162
|
-
load: 0x28,
|
|
163
|
-
const: 0x41,
|
|
164
|
-
eqz: 0x45,
|
|
165
|
-
eq: 0x46,
|
|
166
|
-
ne: 0x47,
|
|
167
|
-
lt_s: 0x48,
|
|
168
|
-
lt_u: 0x49,
|
|
169
|
-
gt_s: 0x4a,
|
|
170
|
-
gt_u: 0x4b,
|
|
171
|
-
le_s: 0x4c,
|
|
172
|
-
le_u: 0x4d,
|
|
173
|
-
ge_s: 0x4e,
|
|
174
|
-
ge_u: 0x4f,
|
|
175
|
-
add: 0x6a,
|
|
176
|
-
sub: 0x6b,
|
|
177
|
-
mul: 0x6c,
|
|
178
|
-
},
|
|
179
|
-
i64: {
|
|
180
|
-
store32: 0x3e,
|
|
181
|
-
const: 0x42,
|
|
182
|
-
ne: 0x52,
|
|
183
|
-
add: 0x7c,
|
|
184
|
-
sub: 0x7d,
|
|
185
|
-
mul: 0x7e,
|
|
186
|
-
},
|
|
187
|
-
f32: {
|
|
188
|
-
const: 0x43,
|
|
189
|
-
ne: 0x5c,
|
|
190
|
-
add: 0x92,
|
|
191
|
-
sub: 0x93,
|
|
192
|
-
mul: 0x94,
|
|
193
|
-
div: 0x95,
|
|
194
|
-
},
|
|
195
|
-
f64: {
|
|
196
|
-
load: 0x2b,
|
|
197
|
-
store: 0x39,
|
|
198
|
-
const: 0x44,
|
|
199
|
-
ne: 0x62,
|
|
200
|
-
add: 0xa0,
|
|
201
|
-
sub: 0xa1,
|
|
202
|
-
mul: 0xa2,
|
|
203
|
-
div: 0xa3,
|
|
204
|
-
reinterpret_i64: 0xbf,
|
|
205
|
-
},
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
const SEVEN_BIT_MASK_BIG_INT = 0b01111111n;
|
|
209
|
-
const CONTINUATION_BIT = 0b10000000;
|
|
210
|
-
|
|
211
|
-
function u32(v: number | bigint): number[] {
|
|
212
|
-
let val = BigInt(v);
|
|
213
|
-
let more = true;
|
|
214
|
-
const r = [];
|
|
215
|
-
|
|
216
|
-
while (more) {
|
|
217
|
-
const b = Number(val & SEVEN_BIT_MASK_BIG_INT);
|
|
218
|
-
val = val >> 7n;
|
|
219
|
-
more = val !== 0n;
|
|
220
|
-
if (more) {
|
|
221
|
-
r.push(b | CONTINUATION_BIT);
|
|
222
|
-
} else {
|
|
223
|
-
r.push(b);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
return r;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function i32(v: number | bigint): number[] {
|
|
230
|
-
let val = BigInt(v);
|
|
231
|
-
const r = [];
|
|
232
|
-
|
|
233
|
-
let more = true;
|
|
234
|
-
while (more) {
|
|
235
|
-
const b = Number(val & 0b01111111n);
|
|
236
|
-
const signBitSet = !!(b & 0x40);
|
|
237
|
-
|
|
238
|
-
val = val >> 7n;
|
|
239
|
-
|
|
240
|
-
if ((val === 0n && !signBitSet) || (val === -1n && signBitSet)) {
|
|
241
|
-
more = false;
|
|
242
|
-
r.push(b);
|
|
243
|
-
} else {
|
|
244
|
-
r.push(b | CONTINUATION_BIT);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return r;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function f64(v: number): number[] {
|
|
252
|
-
var buf = new ArrayBuffer(8);
|
|
253
|
-
new Float64Array(buf)[0] = v;
|
|
254
|
-
return Array.from(new Uint8Array(buf));
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function locals(n: number, type: valtype): BytecodeFragment {
|
|
258
|
-
return [u32(n), type];
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// mod:name nm:name d:importdesc
|
|
262
|
-
function import_(
|
|
263
|
-
mod: string,
|
|
264
|
-
nm: string,
|
|
265
|
-
d: BytecodeFragment,
|
|
266
|
-
): BytecodeFragment {
|
|
267
|
-
return [name(mod), name(nm), d];
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// im*:vec(import)
|
|
271
|
-
function importsec(ims: BytecodeFragment): BytecodeFragment {
|
|
272
|
-
return section(SECTION_ID_IMPORT, vec(ims));
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const importdesc = {
|
|
276
|
-
// x:typeidx
|
|
277
|
-
func(x: BytecodeFragment): BytecodeFragment {
|
|
278
|
-
return [0x00, x];
|
|
279
|
-
},
|
|
280
|
-
// tt:tabletype
|
|
281
|
-
table(tt: BytecodeFragment): BytecodeFragment {
|
|
282
|
-
return [0x01, tt];
|
|
283
|
-
},
|
|
284
|
-
// mt:memtype
|
|
285
|
-
mem(mt: BytecodeFragment): BytecodeFragment {
|
|
286
|
-
return [0x02, mt];
|
|
287
|
-
},
|
|
288
|
-
// gt:globaltype
|
|
289
|
-
global(gt: BytecodeFragment): BytecodeFragment {
|
|
290
|
-
return [0x03, gt];
|
|
291
|
-
},
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
const memidx = u32;
|
|
295
|
-
|
|
296
|
-
function memsec(mems: BytecodeFragment[]) {
|
|
297
|
-
return section(SECTION_ID_MEMORY, vec(mems));
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// lim:limits
|
|
301
|
-
function memtype(limits: BytecodeFragment) {
|
|
302
|
-
return limits;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const limits = {
|
|
306
|
-
min(n: number) {
|
|
307
|
-
return [0x00, u32(n)];
|
|
308
|
-
},
|
|
309
|
-
minmax(n: number, m: number) {
|
|
310
|
-
return [0x01, u32(n), u32(m)];
|
|
311
|
-
},
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
const globalidx = u32;
|
|
315
|
-
|
|
316
|
-
const mut = {
|
|
317
|
-
const: 0x00,
|
|
318
|
-
var: 0x01,
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
// t:valtype m:mut
|
|
322
|
-
function globaltype(t, m) {
|
|
323
|
-
return [t, m];
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
// gt:globaltype e:expr
|
|
327
|
-
function global(gt, e) {
|
|
328
|
-
return [gt, e];
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// glob*:vec(global)
|
|
332
|
-
function globalsec(globs: BytecodeFragment[]) {
|
|
333
|
-
return section(SECTION_ID_GLOBAL, vec(globs));
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
function tabletype(elemtype: number, limits: BytecodeFragment) {
|
|
337
|
-
return [elemtype, limits];
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
function table(tabletype: BytecodeFragment) {
|
|
341
|
-
return tabletype;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function tablesec(tables: BytecodeFragment[]) {
|
|
345
|
-
return section(SECTION_ID_TABLE, vec(tables));
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
const elemtype = { funcref: 0x70 };
|
|
349
|
-
|
|
350
|
-
const tableidx = u32;
|
|
351
|
-
|
|
352
|
-
const start = funcidx;
|
|
353
|
-
|
|
354
|
-
// st:start
|
|
355
|
-
function startsec(st: BytecodeFragment) {
|
|
356
|
-
return section(SECTION_ID_START, st);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// x:memidx e:expr b∗:vec(byte)
|
|
360
|
-
function data(
|
|
361
|
-
x: BytecodeFragment,
|
|
362
|
-
e: BytecodeFragment,
|
|
363
|
-
bs: BytecodeFragment[],
|
|
364
|
-
) {
|
|
365
|
-
return [x, e, vec(bs)];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function datasec(segs: BytecodeFragment[]) {
|
|
369
|
-
return section(SECTION_ID_DATA, vec(segs));
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// x:tableidx e:expr y∗:vec(funcidx)
|
|
373
|
-
function elem(
|
|
374
|
-
x: BytecodeFragment,
|
|
375
|
-
e: BytecodeFragment,
|
|
376
|
-
ys: BytecodeFragment[],
|
|
377
|
-
) {
|
|
378
|
-
return [x, e, vec(ys)];
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
function elemsec(segs: BytecodeFragment[]) {
|
|
382
|
-
return section(SECTION_ID_ELEMENT, vec(segs));
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
function memarg(align: number, offset: number) {
|
|
386
|
-
return [u32(align), u32(offset)];
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
export {
|
|
390
|
-
blocktype,
|
|
391
|
-
BytecodeFragment,
|
|
392
|
-
code,
|
|
393
|
-
codesec,
|
|
394
|
-
data,
|
|
395
|
-
datasec,
|
|
396
|
-
elem,
|
|
397
|
-
elemsec,
|
|
398
|
-
elemtype,
|
|
399
|
-
export_,
|
|
400
|
-
exportdesc,
|
|
401
|
-
exportsec,
|
|
402
|
-
f64,
|
|
403
|
-
func,
|
|
404
|
-
funcidx,
|
|
405
|
-
funcsec,
|
|
406
|
-
functype,
|
|
407
|
-
global,
|
|
408
|
-
globalsec,
|
|
409
|
-
globaltype,
|
|
410
|
-
i32,
|
|
411
|
-
import_,
|
|
412
|
-
importdesc,
|
|
413
|
-
importsec,
|
|
414
|
-
instr,
|
|
415
|
-
limits,
|
|
416
|
-
locals,
|
|
417
|
-
memarg,
|
|
418
|
-
memsec,
|
|
419
|
-
memtype,
|
|
420
|
-
module,
|
|
421
|
-
mut,
|
|
422
|
-
name,
|
|
423
|
-
section,
|
|
424
|
-
start,
|
|
425
|
-
startsec,
|
|
426
|
-
table,
|
|
427
|
-
tableidx,
|
|
428
|
-
tablesec,
|
|
429
|
-
tabletype,
|
|
430
|
-
typeidx,
|
|
431
|
-
typesec,
|
|
432
|
-
u32,
|
|
433
|
-
valtype,
|
|
434
|
-
vec,
|
|
435
|
-
};
|
package/tsconfig.json
DELETED
|
Binary file
|