porffor 0.2.0-f2bbe1f → 0.2.0-f647e42

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.
Files changed (53) hide show
  1. package/CONTRIBUTING.md +239 -0
  2. package/LICENSE +20 -20
  3. package/README.md +154 -89
  4. package/asur/README.md +2 -0
  5. package/asur/index.js +1262 -0
  6. package/byg/index.js +237 -0
  7. package/compiler/2c.js +317 -72
  8. package/compiler/{sections.js → assemble.js} +63 -15
  9. package/compiler/builtins/annexb_string.js +72 -0
  10. package/compiler/builtins/annexb_string.ts +19 -0
  11. package/compiler/builtins/array.ts +145 -0
  12. package/compiler/builtins/base64.ts +151 -0
  13. package/compiler/builtins/crypto.ts +120 -0
  14. package/compiler/builtins/date.ts +1858 -0
  15. package/compiler/builtins/escape.ts +141 -0
  16. package/compiler/builtins/int.ts +147 -0
  17. package/compiler/builtins/number.ts +527 -0
  18. package/compiler/builtins/porffor.d.ts +59 -0
  19. package/compiler/builtins/string.ts +1055 -0
  20. package/compiler/builtins/tostring.ts +45 -0
  21. package/compiler/builtins.js +449 -269
  22. package/compiler/{codeGen.js → codegen.js} +1153 -418
  23. package/compiler/decompile.js +0 -1
  24. package/compiler/embedding.js +22 -22
  25. package/compiler/encoding.js +108 -10
  26. package/compiler/generated_builtins.js +1454 -0
  27. package/compiler/index.js +36 -34
  28. package/compiler/log.js +6 -3
  29. package/compiler/opt.js +51 -36
  30. package/compiler/parse.js +33 -23
  31. package/compiler/precompile.js +128 -0
  32. package/compiler/prefs.js +27 -0
  33. package/compiler/prototype.js +177 -37
  34. package/compiler/types.js +37 -0
  35. package/compiler/wasmSpec.js +30 -7
  36. package/compiler/wrap.js +56 -40
  37. package/package.json +9 -5
  38. package/porf +4 -0
  39. package/rhemyn/compile.js +46 -27
  40. package/rhemyn/parse.js +322 -320
  41. package/rhemyn/test/parse.js +58 -58
  42. package/runner/compare.js +34 -34
  43. package/runner/debug.js +122 -0
  44. package/runner/index.js +91 -11
  45. package/runner/profiler.js +102 -0
  46. package/runner/repl.js +42 -9
  47. package/runner/sizes.js +37 -37
  48. package/compiler/builtins/base64.js +0 -92
  49. package/runner/info.js +0 -89
  50. package/runner/profile.js +0 -46
  51. package/runner/results.json +0 -1
  52. package/runner/transform.js +0 -15
  53. 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
+ };