@wasmgroundup/emit 0.2.2 → 0.2.4
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 +8 -1
- package/dist/index.js +23 -3
- package/index.test.ts +63 -10
- package/index.ts +34 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ declare function module(sections: any): BytecodeFragment;
|
|
|
25
25
|
declare const instr: {
|
|
26
26
|
nop: number;
|
|
27
27
|
end: number;
|
|
28
|
+
call: number;
|
|
28
29
|
local: {
|
|
29
30
|
get: number;
|
|
30
31
|
set: number;
|
|
@@ -59,5 +60,11 @@ declare const instr: {
|
|
|
59
60
|
};
|
|
60
61
|
declare function u32(v: number | bigint): number[];
|
|
61
62
|
declare function i32(v: number | bigint): number[];
|
|
63
|
+
declare function f64(v: number): number[];
|
|
62
64
|
declare function locals(n: number, type: valtype): BytecodeFragment;
|
|
63
|
-
|
|
65
|
+
declare function import_(mod: string, nm: string, d: BytecodeFragment): BytecodeFragment;
|
|
66
|
+
declare function importsec(ims: BytecodeFragment): BytecodeFragment;
|
|
67
|
+
declare const importdesc: {
|
|
68
|
+
func(x: number): BytecodeFragment;
|
|
69
|
+
};
|
|
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, };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const SECTION_ID_TYPE = 1;
|
|
2
|
+
const SECTION_ID_IMPORT = 2;
|
|
2
3
|
const SECTION_ID_FUNCTION = 3;
|
|
3
4
|
const SECTION_ID_EXPORT = 7;
|
|
4
5
|
const SECTION_ID_CODE = 10;
|
|
@@ -83,6 +84,7 @@ var numtype;
|
|
|
83
84
|
const instr = {
|
|
84
85
|
nop: 0x01,
|
|
85
86
|
end: 0x0b,
|
|
87
|
+
call: 0x10,
|
|
86
88
|
local: {
|
|
87
89
|
get: 0x20,
|
|
88
90
|
set: 0x21,
|
|
@@ -112,7 +114,7 @@ const instr = {
|
|
|
112
114
|
add: 0xa0,
|
|
113
115
|
sub: 0xa1,
|
|
114
116
|
mul: 0xa2,
|
|
115
|
-
div: 0xa3
|
|
117
|
+
div: 0xa3,
|
|
116
118
|
},
|
|
117
119
|
};
|
|
118
120
|
const SEVEN_BIT_MASK_BIG_INT = 127n;
|
|
@@ -134,7 +136,6 @@ function u32(v) {
|
|
|
134
136
|
}
|
|
135
137
|
return r;
|
|
136
138
|
}
|
|
137
|
-
///! START i32-v1 #priv #api #dedent
|
|
138
139
|
function i32(v) {
|
|
139
140
|
let val = BigInt(v);
|
|
140
141
|
const r = [];
|
|
@@ -153,7 +154,26 @@ function i32(v) {
|
|
|
153
154
|
}
|
|
154
155
|
return r;
|
|
155
156
|
}
|
|
157
|
+
function f64(v) {
|
|
158
|
+
var buf = new ArrayBuffer(8);
|
|
159
|
+
new Float64Array(buf)[0] = v;
|
|
160
|
+
return Array.from(new Uint8Array(buf));
|
|
161
|
+
}
|
|
156
162
|
function locals(n, type) {
|
|
157
163
|
return [u32(n), type];
|
|
158
164
|
}
|
|
159
|
-
|
|
165
|
+
// mod:name nm:name d:importdesc
|
|
166
|
+
function import_(mod, nm, d) {
|
|
167
|
+
return [name(mod), name(nm), d];
|
|
168
|
+
}
|
|
169
|
+
// im*:vec(import)
|
|
170
|
+
function importsec(ims) {
|
|
171
|
+
return section(SECTION_ID_IMPORT, vec(ims));
|
|
172
|
+
}
|
|
173
|
+
const importdesc = {
|
|
174
|
+
// x:typeidx
|
|
175
|
+
func(x) {
|
|
176
|
+
return [0x00, funcidx(x)];
|
|
177
|
+
},
|
|
178
|
+
};
|
|
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, };
|
package/index.test.ts
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { expect, test } from "bun:test";
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
valtype,
|
|
8
|
-
funcsec,
|
|
9
|
-
typeidx,
|
|
10
|
-
exportsec,
|
|
4
|
+
BytecodeFragment,
|
|
5
|
+
code,
|
|
6
|
+
codesec,
|
|
11
7
|
export_,
|
|
12
8
|
exportdesc,
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
exportsec,
|
|
10
|
+
f64,
|
|
15
11
|
func,
|
|
16
|
-
|
|
12
|
+
funcidx,
|
|
13
|
+
funcsec,
|
|
14
|
+
functype,
|
|
15
|
+
import_,
|
|
16
|
+
importdesc,
|
|
17
|
+
importsec,
|
|
17
18
|
instr,
|
|
19
|
+
module,
|
|
20
|
+
typeidx,
|
|
21
|
+
typesec,
|
|
22
|
+
valtype,
|
|
18
23
|
} from "./index";
|
|
19
24
|
|
|
20
25
|
test("simple modules", async () => {
|
|
@@ -57,3 +62,51 @@ test("simple modules", async () => {
|
|
|
57
62
|
),
|
|
58
63
|
).toBe(99);
|
|
59
64
|
});
|
|
65
|
+
|
|
66
|
+
test("imports", async () => {
|
|
67
|
+
const makeModule = () => {
|
|
68
|
+
const mod = module([
|
|
69
|
+
typesec([functype([valtype.i32], [valtype.i32])]),
|
|
70
|
+
importsec([import_("builtins", "addOne", importdesc.func(0))]),
|
|
71
|
+
funcsec([typeidx(0)]),
|
|
72
|
+
exportsec([export_("main", exportdesc.func(1))]),
|
|
73
|
+
codesec([
|
|
74
|
+
code(func([], [instr.local.get, 0, instr.call, funcidx(0), instr.end])),
|
|
75
|
+
]),
|
|
76
|
+
]);
|
|
77
|
+
return Uint8Array.from(mod.flat(Infinity));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const { instance } = await WebAssembly.instantiate(makeModule(), {
|
|
81
|
+
builtins: {
|
|
82
|
+
addOne(x) {
|
|
83
|
+
return x + 1;
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(instance.exports.main(1)).toBe(2);
|
|
89
|
+
expect(instance.exports.main(2)).toBe(3);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("f64", async () => {
|
|
93
|
+
const PI = 3.141592653589793115997963468544185161590576171875;
|
|
94
|
+
const makeModule = () => {
|
|
95
|
+
const mod = module([
|
|
96
|
+
typesec([functype([], [valtype.f64])]),
|
|
97
|
+
funcsec([typeidx(0), typeidx(0)]),
|
|
98
|
+
exportsec([
|
|
99
|
+
export_("pi", exportdesc.func(0)),
|
|
100
|
+
export_("nan", exportdesc.func(1)),
|
|
101
|
+
]),
|
|
102
|
+
codesec([
|
|
103
|
+
code(func([], [instr.f64.const, f64(PI), instr.end])),
|
|
104
|
+
code(func([], [instr.f64.const, f64(Number.NaN), instr.end])),
|
|
105
|
+
]),
|
|
106
|
+
]);
|
|
107
|
+
return Uint8Array.from(mod.flat(Infinity));
|
|
108
|
+
};
|
|
109
|
+
const { instance } = await WebAssembly.instantiate(makeModule());
|
|
110
|
+
expect(instance.exports.pi()).toBe(PI);
|
|
111
|
+
expect(instance.exports.nan()).toBe(Number.NaN);
|
|
112
|
+
});
|
package/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const SECTION_ID_TYPE = 1;
|
|
2
|
+
const SECTION_ID_IMPORT = 2;
|
|
2
3
|
const SECTION_ID_FUNCTION = 3;
|
|
3
4
|
const SECTION_ID_EXPORT = 7;
|
|
4
5
|
const SECTION_ID_CODE = 10;
|
|
@@ -112,6 +113,7 @@ enum numtype {
|
|
|
112
113
|
const instr = {
|
|
113
114
|
nop: 0x01,
|
|
114
115
|
end: 0x0b,
|
|
116
|
+
call: 0x10,
|
|
115
117
|
|
|
116
118
|
local: {
|
|
117
119
|
get: 0x20,
|
|
@@ -142,7 +144,7 @@ const instr = {
|
|
|
142
144
|
add: 0xa0,
|
|
143
145
|
sub: 0xa1,
|
|
144
146
|
mul: 0xa2,
|
|
145
|
-
div: 0xa3
|
|
147
|
+
div: 0xa3,
|
|
146
148
|
},
|
|
147
149
|
};
|
|
148
150
|
|
|
@@ -167,7 +169,6 @@ function u32(v: number | bigint): number[] {
|
|
|
167
169
|
return r;
|
|
168
170
|
}
|
|
169
171
|
|
|
170
|
-
///! START i32-v1 #priv #api #dedent
|
|
171
172
|
function i32(v: number | bigint): number[] {
|
|
172
173
|
let val = BigInt(v);
|
|
173
174
|
const r = [];
|
|
@@ -190,10 +191,37 @@ function i32(v: number | bigint): number[] {
|
|
|
190
191
|
return r;
|
|
191
192
|
}
|
|
192
193
|
|
|
194
|
+
function f64(v: number): number[] {
|
|
195
|
+
var buf = new ArrayBuffer(8);
|
|
196
|
+
new Float64Array(buf)[0] = v;
|
|
197
|
+
return Array.from(new Uint8Array(buf));
|
|
198
|
+
}
|
|
199
|
+
|
|
193
200
|
function locals(n: number, type: valtype): BytecodeFragment {
|
|
194
201
|
return [u32(n), type];
|
|
195
202
|
}
|
|
196
203
|
|
|
204
|
+
// mod:name nm:name d:importdesc
|
|
205
|
+
function import_(
|
|
206
|
+
mod: string,
|
|
207
|
+
nm: string,
|
|
208
|
+
d: BytecodeFragment,
|
|
209
|
+
): BytecodeFragment {
|
|
210
|
+
return [name(mod), name(nm), d];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// im*:vec(import)
|
|
214
|
+
function importsec(ims: BytecodeFragment): BytecodeFragment {
|
|
215
|
+
return section(SECTION_ID_IMPORT, vec(ims));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const importdesc = {
|
|
219
|
+
// x:typeidx
|
|
220
|
+
func(x: number): BytecodeFragment {
|
|
221
|
+
return [0x00, funcidx(x)];
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
|
|
197
225
|
export {
|
|
198
226
|
BytecodeFragment,
|
|
199
227
|
code,
|
|
@@ -201,11 +229,15 @@ export {
|
|
|
201
229
|
export_,
|
|
202
230
|
exportdesc,
|
|
203
231
|
exportsec,
|
|
232
|
+
f64,
|
|
204
233
|
func,
|
|
205
234
|
funcidx,
|
|
206
235
|
funcsec,
|
|
207
236
|
functype,
|
|
208
237
|
i32,
|
|
238
|
+
import_,
|
|
239
|
+
importdesc,
|
|
240
|
+
importsec,
|
|
209
241
|
instr,
|
|
210
242
|
locals,
|
|
211
243
|
module,
|