porffor 0.14.0-4e46400a9 → 0.14.0-5951093a3
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/compiler/assemble.js +12 -1
- package/compiler/builtins/array.ts +6 -4
- package/compiler/builtins/boolean.ts +1 -1
- package/compiler/builtins/set.ts +5 -6
- package/compiler/builtins/symbol.ts +61 -0
- package/compiler/builtins.js +13 -6
- package/compiler/codegen.js +316 -136
- package/compiler/decompile.js +1 -1
- package/compiler/generated_builtins.js +76 -31
- package/compiler/index.js +3 -8
- package/compiler/precompile.js +1 -1
- package/compiler/prefs.js +6 -2
- package/compiler/prototype.js +180 -157
- package/compiler/wrap.js +82 -40
- package/package.json +1 -1
- package/runner/repl.js +18 -2
package/compiler/assemble.js
CHANGED
@@ -65,6 +65,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
|
|
65
65
|
importFuncs = [...imports.values()];
|
66
66
|
|
67
67
|
// fix call indexes for non-imports
|
68
|
+
// also fix call_indirect types
|
68
69
|
const delta = importedFuncs.length - importFuncs.length;
|
69
70
|
for (const f of funcs) {
|
70
71
|
f.originalIndex = f.index;
|
@@ -74,6 +75,16 @@ export default (funcs, globals, tags, pages, data, flags) => {
|
|
74
75
|
if ((inst[0] === Opcodes.call || inst[0] === Opcodes.return_call) && inst[1] >= importedFuncs.length) {
|
75
76
|
inst[1] -= delta;
|
76
77
|
}
|
78
|
+
|
79
|
+
if (inst[0] === Opcodes.call_indirect) {
|
80
|
+
const params = [];
|
81
|
+
for (let i = 0; i < inst[1]; i++) {
|
82
|
+
params.push(valtypeBinary, Valtype.i32);
|
83
|
+
}
|
84
|
+
|
85
|
+
const returns = [ valtypeBinary, Valtype.i32 ];
|
86
|
+
inst[1] = getType(params, returns);
|
87
|
+
}
|
77
88
|
}
|
78
89
|
}
|
79
90
|
}
|
@@ -101,7 +112,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
|
|
101
112
|
encodeVector([ [
|
102
113
|
0x00,
|
103
114
|
Opcodes.i32_const, 0, Opcodes.end,
|
104
|
-
encodeVector(funcs.map(x => x.index))
|
115
|
+
...encodeVector(funcs.map(x => x.index))
|
105
116
|
] ])
|
106
117
|
);
|
107
118
|
|
@@ -27,14 +27,16 @@ export const __Array_prototype_slice = (_this: any[], start: number, end: number
|
|
27
27
|
let outPtr: i32 = Porffor.wasm`local.get ${out}`;
|
28
28
|
let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
|
29
29
|
|
30
|
-
const thisPtrEnd: i32 = thisPtr + end *
|
30
|
+
const thisPtrEnd: i32 = thisPtr + end * 9;
|
31
31
|
|
32
|
-
thisPtr += start *
|
32
|
+
thisPtr += start * 9;
|
33
33
|
|
34
34
|
while (thisPtr < thisPtrEnd) {
|
35
35
|
Porffor.wasm.f64.store(outPtr, Porffor.wasm.f64.load(thisPtr, 0, 4), 0, 4);
|
36
|
-
thisPtr
|
37
|
-
|
36
|
+
Porffor.wasm.i32.store8(outPtr + 8, Porffor.wasm.i32.load8_u(thisPtr + 8, 0, 4), 0, 4);
|
37
|
+
|
38
|
+
thisPtr += 9;
|
39
|
+
outPtr += 9;
|
38
40
|
}
|
39
41
|
|
40
42
|
out.length = end - start;
|
package/compiler/builtins/set.ts
CHANGED
@@ -56,14 +56,13 @@ i32.store8 0 12`;
|
|
56
56
|
};
|
57
57
|
|
58
58
|
|
59
|
-
|
60
|
-
export const __Set_prototype_size = (_this: Set) => {
|
59
|
+
export const __Set_prototype_size$get = (_this: Set) => {
|
61
60
|
return Porffor.wasm.i32.load(_this, 0, 0);
|
62
61
|
};
|
63
62
|
|
64
63
|
export const __Set_prototype_values = (_this: Set) => {
|
65
64
|
// todo: this should return an iterator not array
|
66
|
-
const size: number =
|
65
|
+
const size: number = Porffor.wasm.i32.load(_this, 0, 0);
|
67
66
|
|
68
67
|
const out: any[] = __Porffor_allocate();
|
69
68
|
for (let i: number = 0; i < size; i++) {
|
@@ -79,7 +78,7 @@ export const __Set_prototype_keys = (_this: Set) => {
|
|
79
78
|
};
|
80
79
|
|
81
80
|
export const __Set_prototype_has = (_this: Set, value: any) => {
|
82
|
-
const size: number =
|
81
|
+
const size: number = Porffor.wasm.i32.load(_this, 0, 0);
|
83
82
|
|
84
83
|
for (let i: number = 0; i < size; i++) {
|
85
84
|
if (__Porffor_set_read(_this, i) === value) return true;
|
@@ -89,7 +88,7 @@ export const __Set_prototype_has = (_this: Set, value: any) => {
|
|
89
88
|
};
|
90
89
|
|
91
90
|
export const __Set_prototype_add = (_this: Set, value: any) => {
|
92
|
-
const size: number =
|
91
|
+
const size: number = Porffor.wasm.i32.load(_this, 0, 0);
|
93
92
|
|
94
93
|
// check if already in set
|
95
94
|
for (let i: number = 0; i < size; i++) {
|
@@ -107,7 +106,7 @@ export const __Set_prototype_add = (_this: Set, value: any) => {
|
|
107
106
|
};
|
108
107
|
|
109
108
|
export const __Set_prototype_delete = (_this: Set, value: any) => {
|
110
|
-
const size: number =
|
109
|
+
const size: number = Porffor.wasm.i32.load(_this, 0, 0);
|
111
110
|
|
112
111
|
// check if already in set
|
113
112
|
for (let i: number = 0; i < size; i++) {
|
@@ -0,0 +1,61 @@
|
|
1
|
+
export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
|
2
|
+
const ptr: bytestring = '';
|
3
|
+
|
4
|
+
if (op) { // write
|
5
|
+
const size: number = Porffor.wasm.i32.load(ptr, 0, 0);
|
6
|
+
Porffor.wasm.i32.store(ptr, size + 1, 0, 0)
|
7
|
+
|
8
|
+
// reuse set internals to store description
|
9
|
+
__Porffor_set_write(ptr, size, value);
|
10
|
+
return size;
|
11
|
+
} else { // read
|
12
|
+
return __Porffor_set_read(ptr, value);
|
13
|
+
}
|
14
|
+
};
|
15
|
+
|
16
|
+
export const Symbol = (description: any): Symbol => {
|
17
|
+
// 1-based so always truthy as numeric value
|
18
|
+
return __Porffor_symbol_descStore(true, description) + 1;
|
19
|
+
};
|
20
|
+
|
21
|
+
// todo: this should be a getter somehow not a method
|
22
|
+
export const __Symbol_prototype_description$get = (_this: Symbol) => {
|
23
|
+
const description: bytestring = __Porffor_symbol_descStore(false,
|
24
|
+
Porffor.wasm`local.get ${_this}` - 1);
|
25
|
+
return description;
|
26
|
+
};
|
27
|
+
|
28
|
+
export const __Symbol_prototype_toString = (_this: Symbol) => {
|
29
|
+
let out: bytestring = '';
|
30
|
+
|
31
|
+
// Symbol(
|
32
|
+
Porffor.wasm.i32.store8(out, 83, 0, 4);
|
33
|
+
Porffor.wasm.i32.store8(out, 121, 0, 5);
|
34
|
+
Porffor.wasm.i32.store8(out, 109, 0, 6);
|
35
|
+
Porffor.wasm.i32.store8(out, 98, 0, 7);
|
36
|
+
Porffor.wasm.i32.store8(out, 111, 0, 8);
|
37
|
+
Porffor.wasm.i32.store8(out, 108, 0, 9);
|
38
|
+
Porffor.wasm.i32.store8(out, 40, 0, 10);
|
39
|
+
|
40
|
+
const description: bytestring = __Porffor_symbol_descStore(false,
|
41
|
+
Porffor.wasm`local.get ${_this}` - 1);
|
42
|
+
|
43
|
+
const descLen: i32 = description.length;
|
44
|
+
let outPtr: i32 = Porffor.wasm`local.get ${out}` + 7;
|
45
|
+
let descPtr: i32 = Porffor.wasm`local.get ${description}`;
|
46
|
+
const descPtrEnd: i32 = descPtr + descLen;
|
47
|
+
while (descPtr < descPtrEnd) {
|
48
|
+
Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(descPtr++, 0, 4), 0, 4);
|
49
|
+
}
|
50
|
+
|
51
|
+
// )
|
52
|
+
Porffor.wasm.i32.store8(Porffor.wasm`local.get ${out}` + descLen, 41, 0, 11);
|
53
|
+
|
54
|
+
out.length = 8 + descLen;
|
55
|
+
|
56
|
+
return out;
|
57
|
+
};
|
58
|
+
|
59
|
+
export const __Symbol_prototype_valueOf = (_this: Symbol) => {
|
60
|
+
return _this;
|
61
|
+
};
|
package/compiler/builtins.js
CHANGED
@@ -198,7 +198,8 @@ export const BuiltinFuncs = function() {
|
|
198
198
|
returns: [ valtypeBinary ],
|
199
199
|
wasm: [
|
200
200
|
[ Opcodes.local_get, 0 ]
|
201
|
-
]
|
201
|
+
],
|
202
|
+
constr: true
|
202
203
|
};
|
203
204
|
|
204
205
|
// just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
|
@@ -210,7 +211,8 @@ export const BuiltinFuncs = function() {
|
|
210
211
|
wasm: [
|
211
212
|
// [ Opcodes.local_get, 0 ]
|
212
213
|
...number(1)
|
213
|
-
]
|
214
|
+
],
|
215
|
+
constr: true
|
214
216
|
};
|
215
217
|
|
216
218
|
|
@@ -219,6 +221,7 @@ export const BuiltinFuncs = function() {
|
|
219
221
|
typedParams: true,
|
220
222
|
locals: [ Valtype.i32, Valtype.i32 ],
|
221
223
|
returns: [],
|
224
|
+
callsSelf: true,
|
222
225
|
wasm: (scope, { typeSwitch }) => [
|
223
226
|
...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
|
224
227
|
[TYPES.number]: [
|
@@ -315,7 +318,7 @@ export const BuiltinFuncs = function() {
|
|
315
318
|
|
316
319
|
// make end pointer
|
317
320
|
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
318
|
-
...number(ValtypeSize[valtype], Valtype.i32),
|
321
|
+
...number(ValtypeSize[valtype] + 1, Valtype.i32),
|
319
322
|
[ Opcodes.i32_mul ],
|
320
323
|
|
321
324
|
[ Opcodes.local_get, 2 ],
|
@@ -326,12 +329,16 @@ export const BuiltinFuncs = function() {
|
|
326
329
|
|
327
330
|
// print current char
|
328
331
|
[ Opcodes.local_get, 2 ],
|
329
|
-
[ Opcodes.load,
|
330
|
-
|
332
|
+
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
333
|
+
|
334
|
+
[ Opcodes.local_get, 2 ],
|
335
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
336
|
+
|
337
|
+
[ Opcodes.call, -1 ],
|
331
338
|
|
332
339
|
// increment pointer by sizeof valtype
|
333
340
|
[ Opcodes.local_get, 2 ],
|
334
|
-
...number(ValtypeSize[valtype], Valtype.i32),
|
341
|
+
...number(ValtypeSize[valtype] + 1, Valtype.i32),
|
335
342
|
[ Opcodes.i32_add ],
|
336
343
|
[ Opcodes.local_tee, 2 ],
|
337
344
|
|