porffor 0.17.0-bf4206d7b → 0.17.0-c2dd8f88f
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 +6 -4
- package/compiler/2c.js +28 -11
- package/compiler/assemble.js +19 -4
- package/compiler/builtins/array.ts +79 -9
- package/compiler/builtins/date.ts +104 -106
- package/compiler/builtins/error.js +2 -5
- package/compiler/builtins/math.ts +6 -2
- package/compiler/builtins/set.ts +11 -14
- package/compiler/builtins/string_f64.ts +4 -6
- package/compiler/builtins/symbol.ts +2 -1
- package/compiler/builtins/typedarray.js +94 -0
- package/compiler/builtins/z_ecma262.ts +1 -0
- package/compiler/builtins.js +25 -0
- package/compiler/codegen.js +742 -157
- package/compiler/generated_builtins.js +2761 -668
- package/compiler/pgo.js +9 -1
- package/compiler/precompile.js +4 -2
- package/compiler/prototype.js +0 -69
- package/compiler/types.js +31 -5
- package/compiler/wasmSpec.js +2 -0
- package/compiler/wrap.js +69 -12
- package/package.json +1 -1
- package/rhemyn/README.md +7 -4
- package/rhemyn/compile.js +170 -76
- package/runner/debug.js +1 -1
- package/runner/index.js +5 -3
- package/runner/profile.js +1 -1
- package/runner/repl.js +16 -11
package/compiler/builtins/set.ts
CHANGED
@@ -8,6 +8,7 @@ memory.grow 0
|
|
8
8
|
i32.const 65536
|
9
9
|
i32.mul
|
10
10
|
i32.from_u
|
11
|
+
i32.const 0
|
11
12
|
return`;
|
12
13
|
};
|
13
14
|
|
@@ -163,22 +164,13 @@ export const __Set_prototype_clear = (_this: Set) => {
|
|
163
164
|
Porffor.wasm.i32.store(_this, 0, 0, 0);
|
164
165
|
};
|
165
166
|
|
166
|
-
export const Set = ()
|
167
|
-
throw new TypeError("Constructor Set requires 'new'");
|
168
|
-
};
|
167
|
+
export const Set = function (iterable: any): any {
|
168
|
+
if (!new.target) throw new TypeError("Constructor Set requires 'new'");
|
169
169
|
|
170
|
-
export const Set$constructor = (iterable: any): Set => {
|
171
170
|
const out: Set = __Porffor_allocate();
|
172
171
|
|
173
|
-
|
174
|
-
|
175
|
-
type == Porffor.TYPES.array,
|
176
|
-
type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
|
177
|
-
type == Porffor.TYPES.set
|
178
|
-
)) {
|
179
|
-
for (const x of iterable) {
|
180
|
-
__Set_prototype_add(out, x);
|
181
|
-
}
|
172
|
+
if (Porffor.rawType(iterable) != Porffor.TYPES.undefined) for (const x of iterable) {
|
173
|
+
__Set_prototype_add(out, x);
|
182
174
|
}
|
183
175
|
|
184
176
|
return out;
|
@@ -189,9 +181,14 @@ export const __Set_prototype_union = (_this: Set, other: any) => {
|
|
189
181
|
throw new TypeError("Set.prototype.union\'s \'other\' argument must be a Set");
|
190
182
|
}
|
191
183
|
|
192
|
-
const out: Set = Set
|
184
|
+
const out: Set = new Set(_this);
|
193
185
|
for (const x of other) {
|
194
186
|
out.add(x);
|
195
187
|
}
|
196
188
|
return out;
|
197
189
|
};
|
190
|
+
|
191
|
+
export const __Set_prototype_toString = (_this: Set) => {
|
192
|
+
const out: bytestring = '[object Set]';
|
193
|
+
return out;
|
194
|
+
};
|
@@ -1,10 +1,8 @@
|
|
1
|
-
|
2
|
-
export const String = (value: any): bytestring => {
|
3
|
-
if (Porffor.rawType(value) == Porffor.TYPES.symbol) return __Symbol_prototype_toString(value);
|
4
|
-
return __ecma262_ToString(value);
|
5
|
-
};
|
1
|
+
import type {} from './porffor.d.ts';
|
6
2
|
|
3
|
+
// todo: support non-bytestring properly
|
7
4
|
// todo: support constructor/string objects properly
|
8
|
-
export const String
|
5
|
+
export const String = function (value: any): bytestring {
|
6
|
+
if (!new.target && Porffor.rawType(value) == Porffor.TYPES.symbol) return __Symbol_prototype_toString(value);
|
9
7
|
return __ecma262_ToString(value);
|
10
8
|
};
|
@@ -17,7 +17,8 @@ export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
|
|
17
17
|
|
18
18
|
export const Symbol = (description: any): Symbol => {
|
19
19
|
// 1-based so always truthy as numeric value
|
20
|
-
|
20
|
+
const ptr: Symbol = __Porffor_symbol_descStore(true, description) + 1;
|
21
|
+
return ptr;
|
21
22
|
};
|
22
23
|
|
23
24
|
export const __Symbol_prototype_description$get = (_this: Symbol) => {
|
@@ -0,0 +1,94 @@
|
|
1
|
+
export default async () => {
|
2
|
+
let out = '';
|
3
|
+
|
4
|
+
const arrayCode = (await import('node:fs')).readFileSync(globalThis.precompileCompilerPath + '/builtins/array.ts', 'utf8');
|
5
|
+
const typedArrayFuncs = [...arrayCode.matchAll(/\/\/ @porf-typed-array[\s\S]+?^};$/gm)].map(x => x[0]);
|
6
|
+
|
7
|
+
const constr = name => out += `export const ${name} = function (arg: any): ${name} {
|
8
|
+
if (!new.target) throw new TypeError("Constructor ${name} requires 'new'");
|
9
|
+
|
10
|
+
const out: ${name} = Porffor.allocate();
|
11
|
+
let len: i32 = 0;
|
12
|
+
|
13
|
+
const type: i32 = Porffor.rawType(arg);
|
14
|
+
if (Porffor.fastOr(
|
15
|
+
type == Porffor.TYPES.array,
|
16
|
+
type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
|
17
|
+
type == Porffor.TYPES.set
|
18
|
+
)) {
|
19
|
+
let i: i32 = 0;
|
20
|
+
for (const x of arg) {
|
21
|
+
out[i++] = x;
|
22
|
+
}
|
23
|
+
len = i;
|
24
|
+
} else if (type == Porffor.TYPES.number) {
|
25
|
+
len = arg;
|
26
|
+
}
|
27
|
+
|
28
|
+
out.length = len;
|
29
|
+
return out;
|
30
|
+
};
|
31
|
+
|
32
|
+
export const __${name}_prototype_byteLength$get = (_this: ${name}) => {
|
33
|
+
return _this.length * ${name}.BYTES_PER_ELEMENT;
|
34
|
+
};
|
35
|
+
|
36
|
+
export const __${name}_prototype_at = (_this: ${name}, index: number) => {
|
37
|
+
const len: i32 = _this.length;
|
38
|
+
index |= 0;
|
39
|
+
if (index < 0) {
|
40
|
+
index = len + index;
|
41
|
+
if (index < 0) return undefined;
|
42
|
+
}
|
43
|
+
if (index >= len) return undefined;
|
44
|
+
|
45
|
+
return _this[index];
|
46
|
+
};
|
47
|
+
|
48
|
+
export const __${name}_prototype_slice = (_this: ${name}, start: number, end: number) => {
|
49
|
+
const len: i32 = _this.length;
|
50
|
+
if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
|
51
|
+
|
52
|
+
start |= 0;
|
53
|
+
end |= 0;
|
54
|
+
|
55
|
+
if (start < 0) {
|
56
|
+
start = len + start;
|
57
|
+
if (start < 0) start = 0;
|
58
|
+
}
|
59
|
+
if (start > len) start = len;
|
60
|
+
if (end < 0) {
|
61
|
+
end = len + end;
|
62
|
+
if (end < 0) end = 0;
|
63
|
+
}
|
64
|
+
if (end > len) end = len;
|
65
|
+
|
66
|
+
let out: ${name} = Porffor.allocate();
|
67
|
+
|
68
|
+
if (start > end) return out;
|
69
|
+
|
70
|
+
let i: i32 = start;
|
71
|
+
let j: i32 = 0;
|
72
|
+
while (i < end) {
|
73
|
+
out[j++] = _this[i++];
|
74
|
+
}
|
75
|
+
|
76
|
+
out.length = end - start;
|
77
|
+
return out;
|
78
|
+
};
|
79
|
+
|
80
|
+
${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}
|
81
|
+
`;
|
82
|
+
|
83
|
+
constr('Uint8Array');
|
84
|
+
constr('Int8Array');
|
85
|
+
constr('Uint8ClampedArray');
|
86
|
+
constr('Uint16Array');
|
87
|
+
constr('Int16Array');
|
88
|
+
constr('Uint32Array');
|
89
|
+
constr('Int32Array');
|
90
|
+
constr('Float32Array');
|
91
|
+
constr('Float64Array');
|
92
|
+
|
93
|
+
return out;
|
94
|
+
};
|
package/compiler/builtins.js
CHANGED
@@ -160,6 +160,16 @@ export const BuiltinVars = function() {
|
|
160
160
|
this.__performance_timeOrigin = [
|
161
161
|
[ Opcodes.call, importedFuncs.timeOrigin ]
|
162
162
|
];
|
163
|
+
|
164
|
+
this.__Uint8Array_BYTES_PER_ELEMENT = number(1);
|
165
|
+
this.__Int8Array_BYTES_PER_ELEMENT = number(1);
|
166
|
+
this.__Uint8ClampedArray_BYTES_PER_ELEMENT = number(1);
|
167
|
+
this.__Uint16Array_BYTES_PER_ELEMENT = number(2);
|
168
|
+
this.__Int16Array_BYTES_PER_ELEMENT = number(2);
|
169
|
+
this.__Uint32Array_BYTES_PER_ELEMENT = number(4);
|
170
|
+
this.__Int32Array_BYTES_PER_ELEMENT = number(4);
|
171
|
+
this.__Float32Array_BYTES_PER_ELEMENT = number(4);
|
172
|
+
this.__Float64Array_BYTES_PER_ELEMENT = number(8);
|
163
173
|
};
|
164
174
|
|
165
175
|
export const BuiltinFuncs = function() {
|
@@ -184,6 +194,21 @@ export const BuiltinFuncs = function() {
|
|
184
194
|
]
|
185
195
|
};
|
186
196
|
|
197
|
+
this['f64_**'] = this['i32_**'] = {
|
198
|
+
params: [ valtypeBinary, valtypeBinary ],
|
199
|
+
locals: [],
|
200
|
+
returns: [ valtypeBinary ],
|
201
|
+
returnType: TYPES.number,
|
202
|
+
wasm: (scope, { builtin }) => [
|
203
|
+
[ Opcodes.local_get, 0 ],
|
204
|
+
...number(TYPES.number, Valtype.i32),
|
205
|
+
[ Opcodes.local_get, 1 ],
|
206
|
+
...number(TYPES.number, Valtype.i32),
|
207
|
+
[ Opcodes.call, builtin('__Math_pow') ],
|
208
|
+
[ Opcodes.drop ],
|
209
|
+
]
|
210
|
+
};
|
211
|
+
|
187
212
|
// add bitwise ops by converting operands to i32 first
|
188
213
|
for (const [ char, op ] of [ ['&', Opcodes.i32_and], ['|', Opcodes.i32_or], ['^', Opcodes.i32_xor], ['<<', Opcodes.i32_shl], ['>>', Opcodes.i32_shr_s], ['>>>', Opcodes.i32_shr_u] ]) {
|
189
214
|
this[`f64_${char}`] = {
|