porffor 0.2.0-fbab1de → 0.2.0-fdf0fc5
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/CONTRIBUTING.md +256 -0
- package/LICENSE +20 -20
- package/README.md +147 -89
- package/asur/README.md +2 -0
- package/asur/index.js +1262 -0
- package/byg/index.js +237 -0
- package/compiler/2c.js +317 -72
- package/compiler/{sections.js → assemble.js} +63 -15
- package/compiler/builtins/annexb_string.js +72 -0
- package/compiler/builtins/annexb_string.ts +18 -0
- package/compiler/builtins/array.ts +149 -0
- package/compiler/builtins/base64.ts +76 -0
- package/compiler/builtins/boolean.ts +20 -0
- package/compiler/builtins/crypto.ts +120 -0
- package/compiler/builtins/date.ts +2070 -0
- package/compiler/builtins/escape.ts +141 -0
- package/compiler/builtins/function.ts +7 -0
- package/compiler/builtins/int.ts +147 -0
- package/compiler/builtins/number.ts +534 -0
- package/compiler/builtins/object.ts +6 -0
- package/compiler/builtins/porffor.d.ts +59 -0
- package/compiler/builtins/set.ts +5 -0
- package/compiler/builtins/string.ts +1080 -0
- package/compiler/builtins.js +450 -270
- package/compiler/{codeGen.js → codegen.js} +1065 -414
- package/compiler/decompile.js +0 -1
- package/compiler/embedding.js +22 -22
- package/compiler/encoding.js +108 -10
- package/compiler/generated_builtins.js +1526 -0
- package/compiler/index.js +36 -34
- package/compiler/log.js +6 -3
- package/compiler/opt.js +50 -36
- package/compiler/parse.js +33 -23
- package/compiler/precompile.js +128 -0
- package/compiler/prefs.js +27 -0
- package/compiler/prototype.js +27 -42
- package/compiler/types.js +38 -0
- package/compiler/wasmSpec.js +28 -8
- package/compiler/wrap.js +51 -46
- package/package.json +9 -5
- package/porf +4 -0
- package/rhemyn/compile.js +46 -27
- package/rhemyn/parse.js +322 -320
- package/rhemyn/test/parse.js +58 -58
- package/runner/compare.js +34 -34
- package/runner/debug.js +122 -0
- package/runner/index.js +91 -11
- package/runner/profiler.js +102 -0
- package/runner/repl.js +42 -9
- package/runner/sizes.js +37 -37
- package/compiler/builtins/base64.js +0 -92
- package/r.js +0 -9
- package/runner/info.js +0 -89
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/runner/transform.js +0 -15
- package/util/enum.js +0 -20
@@ -0,0 +1,141 @@
|
|
1
|
+
// @porf --funsafe-no-unlikely-proto-checks --valtype=i32
|
2
|
+
|
3
|
+
import type {} from './porffor';
|
4
|
+
|
5
|
+
export const escape = (input: string|bytestring): bytestring => {
|
6
|
+
// we have no byte array yet so use bytestring with 0x00 and 0x01 via escape characters
|
7
|
+
// 0 = should escape, 1 = should not escape
|
8
|
+
// aka if in set 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@*+-./'
|
9
|
+
const lut: bytestring = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x01\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00';
|
10
|
+
|
11
|
+
const len: i32 = input.length;
|
12
|
+
let outLength: i32 = len; // at minimum, output length = input length
|
13
|
+
|
14
|
+
let i: i32 = Porffor.wasm`local.get ${input}`;
|
15
|
+
|
16
|
+
if (Porffor.wasm`local.get ${input+1}` == Porffor.TYPES.bytestring) {
|
17
|
+
const endPtr: i32 = i + len;
|
18
|
+
while (i < endPtr) {
|
19
|
+
const chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
|
20
|
+
|
21
|
+
if (chr < 128) {
|
22
|
+
if (Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${lut}` + chr, 0, 4)) {
|
23
|
+
continue;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
outLength += 2;
|
28
|
+
}
|
29
|
+
|
30
|
+
if (outLength == len) return input;
|
31
|
+
|
32
|
+
let output: bytestring = '';
|
33
|
+
output.length = outLength;
|
34
|
+
|
35
|
+
i = Porffor.wasm`local.get ${input}`;
|
36
|
+
let j: i32 = Porffor.wasm`local.get ${output}`;
|
37
|
+
while (i < endPtr) {
|
38
|
+
const chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
|
39
|
+
|
40
|
+
if (chr < 128) {
|
41
|
+
if (Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${lut}` + chr, 0, 4)) {
|
42
|
+
// append just character
|
43
|
+
Porffor.wasm.i32.store8(j++, chr, 0, 4);
|
44
|
+
continue;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
// %
|
49
|
+
Porffor.wasm.i32.store8(j++, 37, 0, 4);
|
50
|
+
|
51
|
+
// 8 bit integer to hex (0x12)
|
52
|
+
let lower: i32 = (chr & 0x0f) + 48;
|
53
|
+
if (lower > 57) lower += 7;
|
54
|
+
|
55
|
+
let upper: i32 = (chr >> 4) + 48;
|
56
|
+
if (upper > 57) upper += 7;
|
57
|
+
|
58
|
+
Porffor.wasm.i32.store8(j++, upper, 0, 4);
|
59
|
+
Porffor.wasm.i32.store8(j++, lower, 0, 4);
|
60
|
+
}
|
61
|
+
|
62
|
+
return output;
|
63
|
+
}
|
64
|
+
|
65
|
+
const endPtr: i32 = i + len * 2;
|
66
|
+
while (i < endPtr) {
|
67
|
+
const chr: i32 = Porffor.wasm.i32.load16_u(i, 0, 4);
|
68
|
+
i += 2;
|
69
|
+
|
70
|
+
if (chr < 128) {
|
71
|
+
if (Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${lut}` + chr, 0, 4)) {
|
72
|
+
continue;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
if (chr < 256) {
|
77
|
+
outLength += 2;
|
78
|
+
} else {
|
79
|
+
outLength += 5;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
if (outLength == len) return input;
|
84
|
+
|
85
|
+
let output: bytestring = '';
|
86
|
+
output.length = outLength;
|
87
|
+
|
88
|
+
i = Porffor.wasm`local.get ${input}`;
|
89
|
+
let j: i32 = Porffor.wasm`local.get ${output}`;
|
90
|
+
|
91
|
+
while (i < endPtr) {
|
92
|
+
const chr: i32 = Porffor.wasm.i32.load16_u(i, 0, 4);
|
93
|
+
i += 2;
|
94
|
+
|
95
|
+
if (chr < 128) {
|
96
|
+
if (Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${lut}` + chr, 0, 4)) {
|
97
|
+
// append just character
|
98
|
+
Porffor.wasm.i32.store8(j++, chr, 0, 4);
|
99
|
+
continue;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
if (chr < 256) {
|
104
|
+
// %
|
105
|
+
Porffor.wasm.i32.store8(j++, 37, 0, 4);
|
106
|
+
|
107
|
+
// 8 bit integer to hex (0x12)
|
108
|
+
let lower: i32 = (chr & 0x0f) + 48;
|
109
|
+
if (lower > 57) lower += 7;
|
110
|
+
|
111
|
+
let upper: i32 = (chr >> 4) + 48;
|
112
|
+
if (upper > 57) upper += 7;
|
113
|
+
|
114
|
+
Porffor.wasm.i32.store8(j++, upper, 0, 4);
|
115
|
+
Porffor.wasm.i32.store8(j++, lower, 0, 4);
|
116
|
+
} else {
|
117
|
+
// %u
|
118
|
+
Porffor.wasm.i32.store16(j, 29989, 0, 4);
|
119
|
+
j += 2;
|
120
|
+
|
121
|
+
// 16 bit integer to hex (0x1234)
|
122
|
+
let nibble: i32 = ((chr >> 12) & 0x0f) + 48;
|
123
|
+
if (nibble > 57) nibble += 7;
|
124
|
+
Porffor.wasm.i32.store8(j++, nibble, 0, 4);
|
125
|
+
|
126
|
+
nibble = ((chr >> 8) & 0x0f) + 48;
|
127
|
+
if (nibble > 57) nibble += 7;
|
128
|
+
Porffor.wasm.i32.store8(j++, nibble, 0, 4);
|
129
|
+
|
130
|
+
nibble = ((chr >> 4) & 0x0f) + 48;
|
131
|
+
if (nibble > 57) nibble += 7;
|
132
|
+
Porffor.wasm.i32.store8(j++, nibble, 0, 4);
|
133
|
+
|
134
|
+
nibble = (chr & 0x0f) + 48;
|
135
|
+
if (nibble > 57) nibble += 7;
|
136
|
+
Porffor.wasm.i32.store8(j++, nibble, 0, 4);
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
return output;
|
141
|
+
};
|
@@ -0,0 +1,147 @@
|
|
1
|
+
// @porf --funsafe-no-unlikely-proto-checks
|
2
|
+
|
3
|
+
// radix: number|any for rawType check
|
4
|
+
// export const parseInt = (input: string|bytestring, radix: number|any): f64 => {
|
5
|
+
export const parseInt = (input: string|bytestring, radix: number): f64 => {
|
6
|
+
// todo/perf: optimize this instead of doing a naive algo (https://kholdstare.github.io/technical/2020/05/26/faster-integer-parsing.html)
|
7
|
+
// todo/perf: use i32s here once that becomes not annoying
|
8
|
+
|
9
|
+
if (Porffor.rawType(radix) != Porffor.TYPES.number) {
|
10
|
+
// todo: string to number
|
11
|
+
radix = 10;
|
12
|
+
}
|
13
|
+
|
14
|
+
if (radix == 0) radix = 10;
|
15
|
+
if (radix < 2 || radix > 36) return NaN;
|
16
|
+
|
17
|
+
let nMax: f64 = 58;
|
18
|
+
if (radix < 10) nMax = 48 + radix;
|
19
|
+
|
20
|
+
// if (Porffor.rawType(input) == Porffor.TYPES.bytestring) input = __ByteString_prototype_trimStart(input);
|
21
|
+
// else input = __String_prototype_trimStart(input);
|
22
|
+
|
23
|
+
let n: f64 = NaN;
|
24
|
+
|
25
|
+
const inputPtr: f64 = Porffor.wasm`local.get ${input}`;
|
26
|
+
const len: f64 = Porffor.wasm.i32.load(inputPtr, 0, 0);
|
27
|
+
let i: f64 = inputPtr;
|
28
|
+
|
29
|
+
let negative: boolean = false;
|
30
|
+
|
31
|
+
if (Porffor.rawType(input) == Porffor.TYPES.bytestring) {
|
32
|
+
const endPtr: f64 = i + len;
|
33
|
+
|
34
|
+
// check start of string
|
35
|
+
const startChr: f64 = Porffor.wasm.i32.load8_u(i, 0, 4);
|
36
|
+
|
37
|
+
// +, ignore
|
38
|
+
if (startChr == 43) i++;
|
39
|
+
|
40
|
+
// -, switch to negative
|
41
|
+
if (startChr == 45) {
|
42
|
+
negative = true;
|
43
|
+
i++;
|
44
|
+
}
|
45
|
+
|
46
|
+
// 0, potential start of hex
|
47
|
+
if (startChr == 48) {
|
48
|
+
const second: f64 = Porffor.wasm.i32.load8_u(i + 1, 0, 4);
|
49
|
+
// 0x or 0X
|
50
|
+
if (second == 120 || second == 88) {
|
51
|
+
// set radix to 16 and skip leading 2 chars
|
52
|
+
i += 2;
|
53
|
+
radix = 16;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
while (i < endPtr) {
|
58
|
+
const chr: f64 = Porffor.wasm.i32.load8_u(i++, 0, 4);
|
59
|
+
|
60
|
+
if (chr >= 48 && chr < nMax) {
|
61
|
+
if (Number.isNaN(n)) n = 0;
|
62
|
+
|
63
|
+
n *= radix;
|
64
|
+
n += chr - 48;
|
65
|
+
} else if (radix > 10) {
|
66
|
+
if (chr >= 97 && chr < (87 + radix)) {
|
67
|
+
if (Number.isNaN(n)) n = 0;
|
68
|
+
|
69
|
+
n *= radix;
|
70
|
+
n += chr - 87;
|
71
|
+
} else if (chr >= 65 && chr < (55 + radix)) {
|
72
|
+
if (Number.isNaN(n)) n = 0;
|
73
|
+
|
74
|
+
n *= radix;
|
75
|
+
n += chr - 55;
|
76
|
+
} else {
|
77
|
+
if (negative) return -n;
|
78
|
+
return n;
|
79
|
+
}
|
80
|
+
} else {
|
81
|
+
if (negative) return -n;
|
82
|
+
return n;
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
if (negative) return -n;
|
87
|
+
return n;
|
88
|
+
}
|
89
|
+
|
90
|
+
const endPtr: f64 = i + len * 2;
|
91
|
+
|
92
|
+
// check start of string
|
93
|
+
const startChr: f64 = Porffor.wasm.i32.load16_u(i, 0, 4);
|
94
|
+
|
95
|
+
// +, ignore
|
96
|
+
if (startChr == 43) i += 2;
|
97
|
+
|
98
|
+
// -, switch to negative
|
99
|
+
if (startChr == 45) {
|
100
|
+
negative = true;
|
101
|
+
i += 2;
|
102
|
+
}
|
103
|
+
|
104
|
+
// 0, potential start of hex
|
105
|
+
if (startChr == 48) {
|
106
|
+
const second: f64 = Porffor.wasm.i32.load16_u(i + 2, 0, 4);
|
107
|
+
// 0x or 0X
|
108
|
+
if (second == 120 || second == 88) {
|
109
|
+
// set radix to 16 and skip leading 2 chars
|
110
|
+
i += 4;
|
111
|
+
radix = 16;
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
while (i < endPtr) {
|
116
|
+
const chr: f64 = Porffor.wasm.i32.load16_u(i, 0, 4);
|
117
|
+
i += 2;
|
118
|
+
|
119
|
+
if (chr >= 48 && chr < nMax) {
|
120
|
+
if (Number.isNaN(n)) n = 0;
|
121
|
+
|
122
|
+
n *= radix;
|
123
|
+
n += chr - 48;
|
124
|
+
} else if (radix > 10) {
|
125
|
+
if (chr >= 97 && chr < (87 + radix)) {
|
126
|
+
if (Number.isNaN(n)) n = 0;
|
127
|
+
|
128
|
+
n *= radix;
|
129
|
+
n += chr - 87;
|
130
|
+
} else if (chr >= 65 && chr < (55 + radix)) {
|
131
|
+
if (Number.isNaN(n)) n = 0;
|
132
|
+
|
133
|
+
n *= radix;
|
134
|
+
n += chr - 55;
|
135
|
+
} else {
|
136
|
+
if (negative) return -n;
|
137
|
+
return n;
|
138
|
+
}
|
139
|
+
} else {
|
140
|
+
if (negative) return -n;
|
141
|
+
return n;
|
142
|
+
}
|
143
|
+
}
|
144
|
+
|
145
|
+
if (negative) return -n;
|
146
|
+
return n;
|
147
|
+
};
|