porffor 0.56.5 → 0.56.7
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 +1 -1
- package/compiler/builtins/arraybuffer.ts +1 -1
- package/compiler/builtins/bigint.ts +1 -1
- package/compiler/builtins/typedarray.js +8 -16
- package/compiler/builtins.js +183 -0
- package/compiler/builtins_precompiled.js +769 -295
- package/compiler/codegen.js +92 -9
- package/compiler/encoding.js +1 -1
- package/compiler/index.js +2 -2
- package/compiler/types.js +1 -1
- package/compiler/wasmSpec.js +7 -1
- package/compiler/wrap.js +3 -1
- package/package.json +1 -1
- package/r.cjs +0 -92
- package/runner/index.js +1 -1
@@ -44,7 +44,7 @@ export const __Array_from = (arg: any, mapFn: any): any[] => {
|
|
44
44
|
Porffor.type(arg) == Porffor.TYPES.array,
|
45
45
|
Porffor.type(arg) == Porffor.TYPES.string, Porffor.type(arg) == Porffor.TYPES.bytestring,
|
46
46
|
Porffor.type(arg) == Porffor.TYPES.set,
|
47
|
-
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.
|
47
|
+
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.biguint64array)
|
48
48
|
)) {
|
49
49
|
let i: i32 = 0;
|
50
50
|
if (Porffor.type(mapFn) != Porffor.TYPES.undefined) {
|
@@ -3,7 +3,7 @@ import type {} from './porffor.d.ts';
|
|
3
3
|
export const __ArrayBuffer_isView = (value: any): boolean => {
|
4
4
|
return Porffor.fastOr(
|
5
5
|
Porffor.type(value) == Porffor.TYPES.dataview,
|
6
|
-
Porffor.fastAnd(Porffor.type(value) >= Porffor.TYPES.uint8array, Porffor.type(value) <= Porffor.TYPES.
|
6
|
+
Porffor.fastAnd(Porffor.type(value) >= Porffor.TYPES.uint8array, Porffor.type(value) <= Porffor.TYPES.biguint64array)
|
7
7
|
);
|
8
8
|
};
|
9
9
|
|
@@ -9,7 +9,9 @@ export default async () => {
|
|
9
9
|
// bufferPtr (i32) - buffer + byteOffset
|
10
10
|
// byteOffset (i32) - only used for getter
|
11
11
|
|
12
|
-
const
|
12
|
+
for (const x of [ 'Uint8', 'Int8', 'Uint8Clamped', 'Uint16', 'Int16', 'Uint32', 'Int32', 'Float32', 'Float64', 'BigInt64', 'BigUint64' ]) {
|
13
|
+
const name = x + 'Array';
|
14
|
+
out += `export const ${name} = function (arg: any, byteOffset: any, length: any): ${name} {
|
13
15
|
if (!new.target) throw new TypeError("Constructor ${name} requires 'new'");
|
14
16
|
|
15
17
|
const out: ${name} = Porffor.allocateBytes(12);
|
@@ -47,7 +49,7 @@ export default async () => {
|
|
47
49
|
Porffor.type(arg) == Porffor.TYPES.array,
|
48
50
|
Porffor.type(arg) == Porffor.TYPES.string, Porffor.type(arg) == Porffor.TYPES.bytestring,
|
49
51
|
Porffor.type(arg) == Porffor.TYPES.set,
|
50
|
-
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.
|
52
|
+
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.biguint64array)
|
51
53
|
)) {
|
52
54
|
let i: i32 = 0;
|
53
55
|
for (const x of arg) {
|
@@ -78,7 +80,7 @@ export const __${name}_from = (arg: any, mapFn: any): ${name} => {
|
|
78
80
|
Porffor.type(arg) == Porffor.TYPES.array,
|
79
81
|
Porffor.type(arg) == Porffor.TYPES.string, Porffor.type(arg) == Porffor.TYPES.bytestring,
|
80
82
|
Porffor.type(arg) == Porffor.TYPES.set,
|
81
|
-
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.
|
83
|
+
Porffor.fastAnd(Porffor.type(arg) >= Porffor.TYPES.uint8array, Porffor.type(arg) <= Porffor.TYPES.biguint64array)
|
82
84
|
)) {
|
83
85
|
let i: i32 = 0;
|
84
86
|
if (Porffor.type(mapFn) != Porffor.TYPES.undefined) {
|
@@ -166,7 +168,7 @@ export const __${name}_prototype_set = (_this: ${name}, array: any, offset: numb
|
|
166
168
|
Porffor.type(array) == Porffor.TYPES.array,
|
167
169
|
Porffor.type(array) == Porffor.TYPES.string, Porffor.type(array) == Porffor.TYPES.bytestring,
|
168
170
|
Porffor.type(array) == Porffor.TYPES.set,
|
169
|
-
Porffor.fastAnd(Porffor.type(array) >= Porffor.TYPES.uint8array, Porffor.type(array) <= Porffor.TYPES.
|
171
|
+
Porffor.fastAnd(Porffor.type(array) >= Porffor.TYPES.uint8array, Porffor.type(array) <= Porffor.TYPES.biguint64array)
|
170
172
|
)) {
|
171
173
|
let i: i32 = offset;
|
172
174
|
for (const x of array) {
|
@@ -202,18 +204,8 @@ export const __${name}_prototype_subarray = (_this: ${name}, start: number, end:
|
|
202
204
|
return out;
|
203
205
|
};
|
204
206
|
|
205
|
-
${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}
|
206
|
-
|
207
|
-
|
208
|
-
constr('Uint8Array');
|
209
|
-
constr('Int8Array');
|
210
|
-
constr('Uint8ClampedArray');
|
211
|
-
constr('Uint16Array');
|
212
|
-
constr('Int16Array');
|
213
|
-
constr('Uint32Array');
|
214
|
-
constr('Int32Array');
|
215
|
-
constr('Float32Array');
|
216
|
-
constr('Float64Array');
|
207
|
+
${typedArrayFuncs.reduce((acc, x) => acc + x.replace('// @porf-typed-array\n', '').replaceAll('Array', name).replaceAll('any[]', name) + '\n\n', '')}`;
|
208
|
+
};
|
217
209
|
|
218
210
|
return out;
|
219
211
|
};
|
package/compiler/builtins.js
CHANGED
@@ -92,6 +92,8 @@ export const BuiltinVars = function(ctx) {
|
|
92
92
|
this.__Int32Array_BYTES_PER_ELEMENT = [ number(4) ];
|
93
93
|
this.__Float32Array_BYTES_PER_ELEMENT = [ number(4) ];
|
94
94
|
this.__Float64Array_BYTES_PER_ELEMENT = [ number(8) ];
|
95
|
+
this.__BigInt64Array_BYTES_PER_ELEMENT = [ number(8) ];
|
96
|
+
this.__BigUint64Array_BYTES_PER_ELEMENT = [ number(8) ];
|
95
97
|
|
96
98
|
ObjectBuiltins.call(this, ctx, Prefs);
|
97
99
|
};
|
@@ -881,6 +883,187 @@ export const BuiltinFuncs = function() {
|
|
881
883
|
]
|
882
884
|
};
|
883
885
|
|
886
|
+
this.__Porffor_bigint_fromU64 = {
|
887
|
+
params: [ Valtype.i64 ],
|
888
|
+
locals: [ Valtype.i32, Valtype.i32, Valtype.i32 ],
|
889
|
+
localNames: [ 'x', 'hi', 'lo', 'ptr' ],
|
890
|
+
returns: [ Valtype.f64 ],
|
891
|
+
returnType: TYPES.bigint,
|
892
|
+
wasm: (scope, { builtin }) => [
|
893
|
+
// x is u64 so abs(x) = x
|
894
|
+
[ Opcodes.local_get, 0 ],
|
895
|
+
number(32, Valtype.i64),
|
896
|
+
[ Opcodes.i64_shr_u ],
|
897
|
+
[ Opcodes.i32_wrap_i64 ],
|
898
|
+
[ Opcodes.local_tee, 1 ],
|
899
|
+
|
900
|
+
[ Opcodes.local_get, 0 ],
|
901
|
+
[ Opcodes.i32_wrap_i64 ],
|
902
|
+
[ Opcodes.local_set, 2 ],
|
903
|
+
|
904
|
+
// if abs(x) < 0x8000000000000, return x as bigint
|
905
|
+
number(0x8000000000000 / (2 ** 32), Valtype.i32),
|
906
|
+
[ Opcodes.i32_lt_u ],
|
907
|
+
[ Opcodes.if, Blocktype.void ],
|
908
|
+
[ Opcodes.local_get, 0 ],
|
909
|
+
[ Opcodes.f64_convert_i64_u ],
|
910
|
+
[ Opcodes.return ],
|
911
|
+
[ Opcodes.end ],
|
912
|
+
|
913
|
+
number(16, Valtype.i32),
|
914
|
+
[ Opcodes.call, builtin('__Porffor_allocateBytes') ],
|
915
|
+
[ Opcodes.local_tee, 3 ],
|
916
|
+
|
917
|
+
// sign is already 0
|
918
|
+
// digit count = 2
|
919
|
+
number(2, Valtype.i32),
|
920
|
+
[ Opcodes.i32_store16, 0, 2 ],
|
921
|
+
|
922
|
+
// hi and lo as digits
|
923
|
+
[ Opcodes.local_get, 3 ],
|
924
|
+
[ Opcodes.local_get, 1 ],
|
925
|
+
[ Opcodes.i32_store, 0, 4 ],
|
926
|
+
|
927
|
+
[ Opcodes.local_get, 3 ],
|
928
|
+
[ Opcodes.local_get, 2 ],
|
929
|
+
[ Opcodes.i32_store, 0, 8 ],
|
930
|
+
|
931
|
+
[ Opcodes.local_get, 3 ],
|
932
|
+
Opcodes.i32_from_u,
|
933
|
+
number(0x8000000000000, Valtype.f64),
|
934
|
+
[ Opcodes.f64_add ]
|
935
|
+
]
|
936
|
+
};
|
937
|
+
|
938
|
+
this.__Porffor_bigint_fromS64 = {
|
939
|
+
params: [ Valtype.i64 ],
|
940
|
+
locals: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i64 ],
|
941
|
+
localNames: [ 'x', 'hi', 'lo', 'ptr', 'abs' ],
|
942
|
+
returns: [ Valtype.f64 ],
|
943
|
+
returnType: TYPES.bigint,
|
944
|
+
wasm: (scope, { builtin }) => [
|
945
|
+
// abs = abs(x) = ((x >> 63) ^ x) - (x >> 63)
|
946
|
+
[ Opcodes.local_get, 0 ],
|
947
|
+
[ Opcodes.i64_const, 63 ],
|
948
|
+
[ Opcodes.i64_shr_s ],
|
949
|
+
[ Opcodes.local_get, 0 ],
|
950
|
+
[ Opcodes.i64_xor ],
|
951
|
+
[ Opcodes.local_get, 0 ],
|
952
|
+
[ Opcodes.i64_const, 63 ],
|
953
|
+
[ Opcodes.i64_shr_s ],
|
954
|
+
[ Opcodes.i64_sub ],
|
955
|
+
[ Opcodes.local_tee, 4 ],
|
956
|
+
|
957
|
+
number(32, Valtype.i64),
|
958
|
+
[ Opcodes.i64_shr_u ],
|
959
|
+
[ Opcodes.i32_wrap_i64 ],
|
960
|
+
[ Opcodes.local_tee, 1 ],
|
961
|
+
|
962
|
+
[ Opcodes.local_get, 4 ],
|
963
|
+
[ Opcodes.i32_wrap_i64 ],
|
964
|
+
[ Opcodes.local_set, 2 ],
|
965
|
+
|
966
|
+
// if hi < (0x8000000000000 / 2**32), return (hi * 2**32) + lo as bigint
|
967
|
+
number(0x8000000000000 / (2 ** 32), Valtype.i32),
|
968
|
+
[ Opcodes.i32_lt_u ],
|
969
|
+
[ Opcodes.if, Blocktype.void ],
|
970
|
+
[ Opcodes.local_get, 0 ],
|
971
|
+
[ Opcodes.f64_convert_i64_s ],
|
972
|
+
[ Opcodes.return ],
|
973
|
+
[ Opcodes.end ],
|
974
|
+
|
975
|
+
number(16, Valtype.i32),
|
976
|
+
[ Opcodes.call, builtin('__Porffor_allocateBytes') ],
|
977
|
+
[ Opcodes.local_tee, 3 ],
|
978
|
+
|
979
|
+
// sign = x != abs
|
980
|
+
[ Opcodes.local_get, 0 ],
|
981
|
+
[ Opcodes.local_get, 4 ],
|
982
|
+
[ Opcodes.i64_ne ],
|
983
|
+
[ Opcodes.i32_store8, 0, 0 ],
|
984
|
+
|
985
|
+
// digit count = 2
|
986
|
+
[ Opcodes.local_get, 3 ],
|
987
|
+
number(2, Valtype.i32),
|
988
|
+
[ Opcodes.i32_store16, 0, 2 ],
|
989
|
+
|
990
|
+
// hi and lo as digits
|
991
|
+
[ Opcodes.local_get, 3 ],
|
992
|
+
[ Opcodes.local_get, 1 ],
|
993
|
+
[ Opcodes.i32_store, 0, 4 ],
|
994
|
+
|
995
|
+
[ Opcodes.local_get, 3 ],
|
996
|
+
[ Opcodes.local_get, 2 ],
|
997
|
+
[ Opcodes.i32_store, 0, 8 ],
|
998
|
+
|
999
|
+
[ Opcodes.local_get, 3 ],
|
1000
|
+
Opcodes.i32_from_u,
|
1001
|
+
number(0x8000000000000, Valtype.f64),
|
1002
|
+
[ Opcodes.f64_add ]
|
1003
|
+
]
|
1004
|
+
};
|
1005
|
+
|
1006
|
+
this.__Porffor_bigint_toI64 = {
|
1007
|
+
params: [ Valtype.f64 ],
|
1008
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
1009
|
+
localNames: [ 'x', 'ptr', 'digits' ],
|
1010
|
+
returns: [ Valtype.i64 ],
|
1011
|
+
returnType: TYPES.bigint,
|
1012
|
+
wasm: () => [
|
1013
|
+
// if abs(x) < 0x8000000000000, return x as u64
|
1014
|
+
[ Opcodes.local_get, 0 ],
|
1015
|
+
[ Opcodes.f64_abs ],
|
1016
|
+
number(0x8000000000000, Valtype.f64),
|
1017
|
+
[ Opcodes.f64_lt ],
|
1018
|
+
[ Opcodes.if, Blocktype.void ],
|
1019
|
+
[ Opcodes.local_get, 0 ],
|
1020
|
+
Opcodes.i64_trunc_sat_f64_s,
|
1021
|
+
[ Opcodes.return ],
|
1022
|
+
[ Opcodes.end ],
|
1023
|
+
|
1024
|
+
[ Opcodes.local_get, 0 ],
|
1025
|
+
number(0x8000000000000, Valtype.f64),
|
1026
|
+
[ Opcodes.f64_sub ],
|
1027
|
+
Opcodes.i32_to_u,
|
1028
|
+
[ Opcodes.local_tee, 1 ],
|
1029
|
+
|
1030
|
+
// if sign == 1, * -1, else * 1
|
1031
|
+
[ Opcodes.i32_load8_u, 0, 0 ],
|
1032
|
+
[ Opcodes.if, Valtype.i64 ],
|
1033
|
+
number(-1, Valtype.i64),
|
1034
|
+
[ Opcodes.else ],
|
1035
|
+
number(1, Valtype.i64),
|
1036
|
+
[ Opcodes.end ],
|
1037
|
+
|
1038
|
+
// move ptr to final 2 digits
|
1039
|
+
[ Opcodes.local_get, 1 ],
|
1040
|
+
[ Opcodes.i32_load16_u, 0, 2 ],
|
1041
|
+
[ Opcodes.local_tee, 2 ],
|
1042
|
+
[ Opcodes.i32_const, 2 ],
|
1043
|
+
[ Opcodes.i32_gt_u ],
|
1044
|
+
[ Opcodes.if, Blocktype.void ],
|
1045
|
+
[ Opcodes.local_get, 2 ],
|
1046
|
+
[ Opcodes.i32_const, 2 ],
|
1047
|
+
[ Opcodes.i32_sub ],
|
1048
|
+
[ Opcodes.i32_const, 4 ],
|
1049
|
+
[ Opcodes.i32_mul ],
|
1050
|
+
[ Opcodes.local_get, 1 ],
|
1051
|
+
[ Opcodes.i32_add ],
|
1052
|
+
[ Opcodes.local_set, 1 ],
|
1053
|
+
[ Opcodes.end ],
|
1054
|
+
|
1055
|
+
[ Opcodes.local_get, 1 ],
|
1056
|
+
[ Opcodes.i32_load, 0, 4 ],
|
1057
|
+
[ Opcodes.i64_extend_i32_u ],
|
1058
|
+
[ Opcodes.i64_const, 128, 128, 128, 128, 16 ], // todo: fix >2**32 for regular seb 128 encoding
|
1059
|
+
[ Opcodes.i64_mul ],
|
1060
|
+
[ Opcodes.local_get, 1 ],
|
1061
|
+
[ Opcodes.i32_load, 0, 8 ],
|
1062
|
+
[ Opcodes.i64_extend_i32_u ],
|
1063
|
+
[ Opcodes.i64_add ],
|
1064
|
+
[ Opcodes.i64_mul ] // * sign earlier
|
1065
|
+
]
|
1066
|
+
};
|
884
1067
|
|
885
1068
|
PrecompiledBuiltins.BuiltinFuncs.call(this);
|
886
1069
|
};
|