porffor 0.17.0-418ce1445 → 0.17.0-55678f69e
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/builtins/array.ts +22 -0
- package/compiler/builtins/base64.ts +4 -2
- package/compiler/builtins/math.ts +6 -2
- package/compiler/builtins/set.ts +7 -9
- package/compiler/builtins/typedarray.js +42 -0
- package/compiler/builtins.js +14 -0
- package/compiler/codegen.js +392 -38
- package/compiler/generated_builtins.js +313 -121
- package/compiler/types.js +31 -5
- package/compiler/wasmSpec.js +2 -0
- package/compiler/wrap.js +64 -9
- package/package.json +1 -1
- package/runner/index.js +4 -2
- package/runner/repl.js +11 -6
@@ -225,6 +225,16 @@ export const __Array_prototype_every = (_this: any[], callbackFn: any) => {
|
|
225
225
|
return true;
|
226
226
|
};
|
227
227
|
|
228
|
+
export const __Array_prototype_some = (_this: any[], callbackFn: any) => {
|
229
|
+
const len: i32 = _this.length;
|
230
|
+
let i: i32 = 0;
|
231
|
+
while (i < len) {
|
232
|
+
if (Boolean(callbackFn(_this[i], i++, _this))) return true;
|
233
|
+
}
|
234
|
+
|
235
|
+
return false;
|
236
|
+
};
|
237
|
+
|
228
238
|
export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialValue: any) => {
|
229
239
|
let acc: any = initialValue ?? _this[0];
|
230
240
|
|
@@ -237,6 +247,18 @@ export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialV
|
|
237
247
|
return acc;
|
238
248
|
};
|
239
249
|
|
250
|
+
export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, initialValue: any) => {
|
251
|
+
const len: i32 = _this.length;
|
252
|
+
let acc: any = initialValue ?? _this[len - 1];
|
253
|
+
|
254
|
+
let i: i32 = len;
|
255
|
+
while (i > 0) {
|
256
|
+
acc = callbackFn(acc, _this[--i], i, _this);
|
257
|
+
}
|
258
|
+
|
259
|
+
return acc;
|
260
|
+
};
|
261
|
+
|
240
262
|
export const __Array_prototype_toString = (_this: any[]) => {
|
241
263
|
// todo: this is bytestring only!
|
242
264
|
|
@@ -2,6 +2,8 @@
|
|
2
2
|
import type {} from './porffor.d.ts';
|
3
3
|
|
4
4
|
export const btoa = (input: bytestring): bytestring => {
|
5
|
+
// todo: throw on invalid chars
|
6
|
+
|
5
7
|
const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
6
8
|
const keyStrPtr: i32 = Porffor.wasm`local.get ${keyStr}`;
|
7
9
|
|
@@ -41,9 +43,9 @@ export const btoa = (input: bytestring): bytestring => {
|
|
41
43
|
return output;
|
42
44
|
};
|
43
45
|
|
44
|
-
// todo: impl atob by converting below to "porf ts"
|
45
46
|
export const atob = (input: bytestring): bytestring => {
|
46
|
-
// todo:
|
47
|
+
// todo: throw on non-base64 chars
|
48
|
+
|
47
49
|
const lut: bytestring = '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@>@@@?456789:;<=@@@@@@@\x00\x01\x02\x03\x04\x05\x06\x07\b\t\n\x0B\f\r\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19@@@@@@\x1A\x1B\x1C\x1D\x1E\x1F !"#$%&\'()*+,-./0123';
|
48
50
|
const lutPtr: i32 = Porffor.wasm`local.get ${lut}`;
|
49
51
|
|
@@ -16,7 +16,7 @@ export const __Math_exp = (x: number): number => {
|
|
16
16
|
return 1 / Math.exp(-x);
|
17
17
|
}
|
18
18
|
|
19
|
-
|
19
|
+
let k: number = Math.floor(x / Math.LN2);
|
20
20
|
const r: number = x - k * Math.LN2;
|
21
21
|
|
22
22
|
// Taylor series via Horner's method
|
@@ -30,7 +30,11 @@ export const __Math_exp = (x: number): number => {
|
|
30
30
|
i++;
|
31
31
|
}
|
32
32
|
|
33
|
-
|
33
|
+
while (k-- > 0) {
|
34
|
+
sum *= 2;
|
35
|
+
}
|
36
|
+
|
37
|
+
return sum;
|
34
38
|
};
|
35
39
|
|
36
40
|
export const __Math_log2 = (y: number): number => {
|
package/compiler/builtins/set.ts
CHANGED
@@ -170,15 +170,8 @@ export const Set = () => {
|
|
170
170
|
export const Set$constructor = (iterable: any): Set => {
|
171
171
|
const out: Set = __Porffor_allocate();
|
172
172
|
|
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
|
-
}
|
173
|
+
if (Porffor.rawType(iterable) != Porffor.TYPES.undefined) for (const x of iterable) {
|
174
|
+
__Set_prototype_add(out, x);
|
182
175
|
}
|
183
176
|
|
184
177
|
return out;
|
@@ -195,3 +188,8 @@ export const __Set_prototype_union = (_this: Set, other: any) => {
|
|
195
188
|
}
|
196
189
|
return out;
|
197
190
|
};
|
191
|
+
|
192
|
+
export const __Set_prototype_toString = (_this: Set) => {
|
193
|
+
const out: bytestring = '[object Set]';
|
194
|
+
return out;
|
195
|
+
};
|
@@ -0,0 +1,42 @@
|
|
1
|
+
export default () => {
|
2
|
+
let out = '';
|
3
|
+
|
4
|
+
const constr = name => out += `export const ${name} = () => {
|
5
|
+
throw new TypeError("Constructor ${name} requires 'new'");
|
6
|
+
};
|
7
|
+
|
8
|
+
export const ${name}$constructor = (arg: any): ${name} => {
|
9
|
+
const out: ${name} = Porffor.allocate();
|
10
|
+
let len: i32 = 0;
|
11
|
+
|
12
|
+
const type: i32 = Porffor.rawType(arg);
|
13
|
+
if (Porffor.fastOr(
|
14
|
+
type == Porffor.TYPES.array,
|
15
|
+
type == Porffor.TYPES.string, type == Porffor.TYPES.bytestring,
|
16
|
+
type == Porffor.TYPES.set
|
17
|
+
)) {
|
18
|
+
let i: i32 = 0;
|
19
|
+
for (const x of arg) {
|
20
|
+
out[i++] = x;
|
21
|
+
}
|
22
|
+
len = i;
|
23
|
+
} else if (type == Porffor.TYPES.number) {
|
24
|
+
len = arg;
|
25
|
+
}
|
26
|
+
|
27
|
+
out.length = len;
|
28
|
+
return out;
|
29
|
+
};`;
|
30
|
+
|
31
|
+
constr('Uint8Array');
|
32
|
+
constr('Int8Array');
|
33
|
+
constr('Uint8ClampedArray');
|
34
|
+
constr('Uint16Array');
|
35
|
+
constr('Int16Array');
|
36
|
+
constr('Uint32Array');
|
37
|
+
constr('Int32Array');
|
38
|
+
constr('Float32Array');
|
39
|
+
constr('Float64Array');
|
40
|
+
|
41
|
+
return out;
|
42
|
+
};
|
package/compiler/builtins.js
CHANGED
@@ -184,6 +184,20 @@ export const BuiltinFuncs = function() {
|
|
184
184
|
]
|
185
185
|
};
|
186
186
|
|
187
|
+
this['f64_**'] = this['i32_**'] = {
|
188
|
+
params: [ valtypeBinary, valtypeBinary ],
|
189
|
+
locals: [],
|
190
|
+
returns: [ valtypeBinary ],
|
191
|
+
returnType: TYPES.number,
|
192
|
+
wasm: (scope, { builtin }) => [
|
193
|
+
[ Opcodes.local_get, 0 ],
|
194
|
+
...number(TYPES.number, Valtype.i32),
|
195
|
+
[ Opcodes.local_get, 1 ],
|
196
|
+
...number(TYPES.number, Valtype.i32),
|
197
|
+
[ Opcodes.call, builtin('__Math_pow') ]
|
198
|
+
]
|
199
|
+
};
|
200
|
+
|
187
201
|
// add bitwise ops by converting operands to i32 first
|
188
202
|
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
203
|
this[`f64_${char}`] = {
|