porffor 0.2.0-fde989a → 0.14.0-4057a18e9

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 (60) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/LICENSE +20 -20
  3. package/README.md +131 -86
  4. package/asur/README.md +2 -0
  5. package/asur/index.js +1262 -0
  6. package/byg/index.js +216 -0
  7. package/compiler/2c.js +2 -53
  8. package/compiler/{sections.js → assemble.js} +84 -21
  9. package/compiler/builtins/annexb_string.js +72 -0
  10. package/compiler/builtins/annexb_string.ts +18 -0
  11. package/compiler/builtins/array.ts +145 -0
  12. package/compiler/builtins/base64.ts +76 -0
  13. package/compiler/builtins/boolean.ts +18 -0
  14. package/compiler/builtins/crypto.ts +120 -0
  15. package/compiler/builtins/date.ts +2067 -0
  16. package/compiler/builtins/escape.ts +141 -0
  17. package/compiler/builtins/function.ts +5 -0
  18. package/compiler/builtins/int.ts +145 -0
  19. package/compiler/builtins/number.ts +529 -0
  20. package/compiler/builtins/object.ts +4 -0
  21. package/compiler/builtins/porffor.d.ts +60 -0
  22. package/compiler/builtins/set.ts +187 -0
  23. package/compiler/builtins/string.ts +1080 -0
  24. package/compiler/builtins.js +436 -283
  25. package/compiler/{codeGen.js → codegen.js} +1027 -482
  26. package/compiler/decompile.js +2 -3
  27. package/compiler/embedding.js +22 -22
  28. package/compiler/encoding.js +94 -10
  29. package/compiler/expression.js +1 -1
  30. package/compiler/generated_builtins.js +1625 -0
  31. package/compiler/index.js +25 -36
  32. package/compiler/log.js +6 -3
  33. package/compiler/opt.js +55 -41
  34. package/compiler/parse.js +38 -30
  35. package/compiler/precompile.js +120 -0
  36. package/compiler/prefs.js +27 -0
  37. package/compiler/prototype.js +31 -46
  38. package/compiler/types.js +38 -0
  39. package/compiler/wasmSpec.js +33 -8
  40. package/compiler/wrap.js +88 -70
  41. package/package.json +9 -5
  42. package/porf +2 -0
  43. package/rhemyn/compile.js +46 -27
  44. package/rhemyn/parse.js +322 -320
  45. package/rhemyn/test/parse.js +58 -58
  46. package/runner/compare.js +33 -34
  47. package/runner/debug.js +117 -0
  48. package/runner/index.js +78 -11
  49. package/runner/profiler.js +75 -0
  50. package/runner/repl.js +40 -13
  51. package/runner/sizes.js +37 -37
  52. package/runner/version.js +10 -8
  53. package/compiler/builtins/base64.js +0 -92
  54. package/filesize.cmd +0 -2
  55. package/runner/info.js +0 -89
  56. package/runner/profile.js +0 -46
  57. package/runner/results.json +0 -1
  58. package/runner/transform.js +0 -15
  59. package/tmp.c +0 -661
  60. package/util/enum.js +0 -20
@@ -0,0 +1,141 @@
1
+ // @porf --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,5 @@
1
+ export const __Function_prototype_toString = (_this: Function) => {
2
+ // todo: actually use source
3
+ let out: bytestring = 'function () {}';
4
+ return out;
5
+ };
@@ -0,0 +1,145 @@
1
+ // radix: number|any for rawType check
2
+ // export const parseInt = (input: string|bytestring, radix: number|any): f64 => {
3
+ export const parseInt = (input: string|bytestring, radix: number): f64 => {
4
+ // todo/perf: optimize this instead of doing a naive algo (https://kholdstare.github.io/technical/2020/05/26/faster-integer-parsing.html)
5
+ // todo/perf: use i32s here once that becomes not annoying
6
+
7
+ if (Porffor.rawType(radix) != Porffor.TYPES.number) {
8
+ // todo: string to number
9
+ radix = 10;
10
+ }
11
+
12
+ if (radix == 0) radix = 10;
13
+ if (radix < 2 || radix > 36) return NaN;
14
+
15
+ let nMax: f64 = 58;
16
+ if (radix < 10) nMax = 48 + radix;
17
+
18
+ // if (Porffor.rawType(input) == Porffor.TYPES.bytestring) input = __ByteString_prototype_trimStart(input);
19
+ // else input = __String_prototype_trimStart(input);
20
+
21
+ let n: f64 = NaN;
22
+
23
+ const inputPtr: f64 = Porffor.wasm`local.get ${input}`;
24
+ const len: f64 = Porffor.wasm.i32.load(inputPtr, 0, 0);
25
+ let i: f64 = inputPtr;
26
+
27
+ let negative: boolean = false;
28
+
29
+ if (Porffor.rawType(input) == Porffor.TYPES.bytestring) {
30
+ const endPtr: f64 = i + len;
31
+
32
+ // check start of string
33
+ const startChr: f64 = Porffor.wasm.i32.load8_u(i, 0, 4);
34
+
35
+ // +, ignore
36
+ if (startChr == 43) i++;
37
+
38
+ // -, switch to negative
39
+ if (startChr == 45) {
40
+ negative = true;
41
+ i++;
42
+ }
43
+
44
+ // 0, potential start of hex
45
+ if (startChr == 48) {
46
+ const second: f64 = Porffor.wasm.i32.load8_u(i + 1, 0, 4);
47
+ // 0x or 0X
48
+ if (second == 120 || second == 88) {
49
+ // set radix to 16 and skip leading 2 chars
50
+ i += 2;
51
+ radix = 16;
52
+ }
53
+ }
54
+
55
+ while (i < endPtr) {
56
+ const chr: f64 = Porffor.wasm.i32.load8_u(i++, 0, 4);
57
+
58
+ if (chr >= 48 && chr < nMax) {
59
+ if (Number.isNaN(n)) n = 0;
60
+
61
+ n *= radix;
62
+ n += chr - 48;
63
+ } else if (radix > 10) {
64
+ if (chr >= 97 && chr < (87 + radix)) {
65
+ if (Number.isNaN(n)) n = 0;
66
+
67
+ n *= radix;
68
+ n += chr - 87;
69
+ } else if (chr >= 65 && chr < (55 + radix)) {
70
+ if (Number.isNaN(n)) n = 0;
71
+
72
+ n *= radix;
73
+ n += chr - 55;
74
+ } else {
75
+ if (negative) return -n;
76
+ return n;
77
+ }
78
+ } else {
79
+ if (negative) return -n;
80
+ return n;
81
+ }
82
+ }
83
+
84
+ if (negative) return -n;
85
+ return n;
86
+ }
87
+
88
+ const endPtr: f64 = i + len * 2;
89
+
90
+ // check start of string
91
+ const startChr: f64 = Porffor.wasm.i32.load16_u(i, 0, 4);
92
+
93
+ // +, ignore
94
+ if (startChr == 43) i += 2;
95
+
96
+ // -, switch to negative
97
+ if (startChr == 45) {
98
+ negative = true;
99
+ i += 2;
100
+ }
101
+
102
+ // 0, potential start of hex
103
+ if (startChr == 48) {
104
+ const second: f64 = Porffor.wasm.i32.load16_u(i + 2, 0, 4);
105
+ // 0x or 0X
106
+ if (second == 120 || second == 88) {
107
+ // set radix to 16 and skip leading 2 chars
108
+ i += 4;
109
+ radix = 16;
110
+ }
111
+ }
112
+
113
+ while (i < endPtr) {
114
+ const chr: f64 = Porffor.wasm.i32.load16_u(i, 0, 4);
115
+ i += 2;
116
+
117
+ if (chr >= 48 && chr < nMax) {
118
+ if (Number.isNaN(n)) n = 0;
119
+
120
+ n *= radix;
121
+ n += chr - 48;
122
+ } else if (radix > 10) {
123
+ if (chr >= 97 && chr < (87 + radix)) {
124
+ if (Number.isNaN(n)) n = 0;
125
+
126
+ n *= radix;
127
+ n += chr - 87;
128
+ } else if (chr >= 65 && chr < (55 + radix)) {
129
+ if (Number.isNaN(n)) n = 0;
130
+
131
+ n *= radix;
132
+ n += chr - 55;
133
+ } else {
134
+ if (negative) return -n;
135
+ return n;
136
+ }
137
+ } else {
138
+ if (negative) return -n;
139
+ return n;
140
+ }
141
+ }
142
+
143
+ if (negative) return -n;
144
+ return n;
145
+ };