porffor 0.2.0-6aff0fa → 0.2.0-6bc63ef

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 (54) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/LICENSE +20 -20
  3. package/README.md +115 -82
  4. package/asur/index.js +624 -340
  5. package/byg/index.js +216 -0
  6. package/compiler/2c.js +2 -53
  7. package/compiler/{sections.js → assemble.js} +60 -14
  8. package/compiler/builtins/annexb_string.js +72 -0
  9. package/compiler/builtins/annexb_string.ts +18 -0
  10. package/compiler/builtins/array.ts +145 -0
  11. package/compiler/builtins/base64.ts +7 -84
  12. package/compiler/builtins/boolean.ts +18 -0
  13. package/compiler/builtins/crypto.ts +120 -0
  14. package/compiler/builtins/date.ts +2067 -0
  15. package/compiler/builtins/escape.ts +141 -0
  16. package/compiler/builtins/function.ts +5 -0
  17. package/compiler/builtins/int.ts +145 -0
  18. package/compiler/builtins/number.ts +529 -0
  19. package/compiler/builtins/object.ts +4 -0
  20. package/compiler/builtins/porffor.d.ts +44 -7
  21. package/compiler/builtins/set.ts +187 -0
  22. package/compiler/builtins/string.ts +1080 -0
  23. package/compiler/builtins.js +400 -120
  24. package/compiler/{codeGen.js → codegen.js} +850 -402
  25. package/compiler/decompile.js +2 -3
  26. package/compiler/embedding.js +22 -22
  27. package/compiler/encoding.js +94 -10
  28. package/compiler/expression.js +1 -1
  29. package/compiler/generated_builtins.js +1613 -3
  30. package/compiler/index.js +16 -16
  31. package/compiler/log.js +2 -2
  32. package/compiler/opt.js +28 -27
  33. package/compiler/parse.js +36 -30
  34. package/compiler/precompile.js +37 -46
  35. package/compiler/prefs.js +7 -6
  36. package/compiler/prototype.js +20 -36
  37. package/compiler/types.js +38 -0
  38. package/compiler/wasmSpec.js +14 -1
  39. package/compiler/wrap.js +79 -69
  40. package/package.json +9 -5
  41. package/porf +2 -0
  42. package/rhemyn/compile.js +44 -26
  43. package/rhemyn/parse.js +322 -320
  44. package/rhemyn/test/parse.js +58 -58
  45. package/runner/compare.js +33 -34
  46. package/runner/debug.js +117 -0
  47. package/runner/index.js +69 -12
  48. package/runner/profiler.js +22 -30
  49. package/runner/repl.js +40 -13
  50. package/runner/sizes.js +37 -37
  51. package/runner/version.js +3 -3
  52. package/runner/info.js +0 -89
  53. package/runner/transform.js +0 -15
  54. package/util/enum.js +0 -20
@@ -0,0 +1,145 @@
1
+ export const __Array_isArray = (x: unknown): boolean =>
2
+ // Porffor.wasm`local.get ${x+1}` == Porffor.TYPES.array;
3
+ Porffor.rawType(x) == Porffor.TYPES.array;
4
+
5
+ export const __Array_prototype_slice = (_this: any[], start: number, end: number) => {
6
+ const len: i32 = _this.length;
7
+ if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
8
+
9
+ start |= 0;
10
+ end |= 0;
11
+
12
+ if (start < 0) {
13
+ start = len + start;
14
+ if (start < 0) start = 0;
15
+ }
16
+ if (start > len) start = len;
17
+ if (end < 0) {
18
+ end = len + end;
19
+ if (end < 0) end = 0;
20
+ }
21
+ if (end > len) end = len;
22
+
23
+ let out: any[] = [];
24
+
25
+ if (start > end) return out;
26
+
27
+ let outPtr: i32 = Porffor.wasm`local.get ${out}`;
28
+ let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
29
+
30
+ const thisPtrEnd: i32 = thisPtr + end * 8;
31
+
32
+ thisPtr += start * 8;
33
+
34
+ while (thisPtr < thisPtrEnd) {
35
+ Porffor.wasm.f64.store(outPtr, Porffor.wasm.f64.load(thisPtr, 0, 4), 0, 4);
36
+ thisPtr += 8;
37
+ outPtr += 8;
38
+ }
39
+
40
+ out.length = end - start;
41
+
42
+ return out;
43
+ };
44
+
45
+ export const __Array_prototype_indexOf = (_this: any[], searchElement: any, position: number) => {
46
+ const len: i32 = _this.length;
47
+ if (position > 0) {
48
+ if (position > len) position = len;
49
+ else position |= 0;
50
+ } else position = 0;
51
+
52
+ for (let i: i32 = position; i < len; i++) {
53
+ if (_this[i] == searchElement) return i;
54
+ }
55
+
56
+ return -1;
57
+ };
58
+
59
+ export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any, position: number) => {
60
+ const len: i32 = _this.length;
61
+ if (position > 0) {
62
+ if (position > len) position = len;
63
+ else position |= 0;
64
+ } else position = 0;
65
+
66
+ for (let i: i32 = len - 1; i >= position; i--) {
67
+ if (_this[i] == searchElement) return i;
68
+ }
69
+
70
+ return -1;
71
+ };
72
+
73
+ export const __Array_prototype_includes = (_this: any[], searchElement: any, position: number) => {
74
+ const len: i32 = _this.length;
75
+ if (position > 0) {
76
+ if (position > len) position = len;
77
+ else position |= 0;
78
+ } else position = 0;
79
+
80
+ for (let i: i32 = position; i < len; i++) {
81
+ if (_this[i] == searchElement) return true;
82
+ }
83
+
84
+ return false;
85
+ };
86
+
87
+ export const __Array_prototype_with = (_this: any[], index: number, value: any) => {
88
+ const len: i32 = _this.length;
89
+ if (index < 0) {
90
+ index = len + index;
91
+ if (index < 0) {
92
+ throw new RangeError('Invalid index');
93
+ }
94
+ }
95
+
96
+ if (index > len) {
97
+ throw new RangeError('Invalid index');
98
+ }
99
+
100
+ // todo: allocator is bad here?
101
+ let out: any[] = [];
102
+
103
+ Porffor.clone(_this, out);
104
+
105
+ out[index] = value;
106
+
107
+ return out;
108
+ };
109
+
110
+ export const __Array_prototype_reverse = (_this: any[]) => {
111
+ const len: i32 = _this.length;
112
+
113
+ let start: i32 = 0;
114
+ let end: i32 = len - 1;
115
+
116
+ while (start < end) {
117
+ const tmp: i32 = _this[start];
118
+ _this[start++] = _this[end];
119
+ _this[end--] = tmp;
120
+ }
121
+
122
+ return _this;
123
+ };
124
+
125
+ // todo: this has memory/allocation bugs so sometimes crashes :(
126
+ export const __Array_prototype_toReversed = (_this: any[]) => {
127
+ const len: i32 = _this.length;
128
+
129
+ let start: i32 = 0;
130
+ let end: i32 = len - 1;
131
+
132
+ let out: any[] = [];
133
+ out.length = len;
134
+
135
+ while (start < end) {
136
+ out[start] = _this[end];
137
+ out[end--] = _this[start++];
138
+ }
139
+
140
+ return out;
141
+ };
142
+
143
+ export const __Array_prototype_valueOf = (_this: any[]) => {
144
+ return _this;
145
+ };
@@ -1,95 +1,17 @@
1
- // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
-
3
- import type { i32, bytestring } from './porffor.d.ts';
4
-
5
- // while (len >= 8) {
6
- // Porffor.wasm`
7
- // local tmp i64
8
- // local.get ${i}
9
- // i64.load 0 4
10
- // local.set tmp
11
-
12
- // local k i64
13
- // i64.const 0
14
- // local.set k
15
-
16
- // loop 64
17
- // local.get ${j}
18
-
19
- // local.get ${keyStrPtr}
20
-
21
- // local.get tmp
22
-
23
- // ;; k * 6
24
- // i64.const 58
25
-
26
- // local.get k
27
- // i64.const 6
28
- // i64.mul
29
-
30
- // i64.sub
31
-
32
- // ;; tmp >> (58 - (k * 6))
33
- // i64.shr_u
34
-
35
- // ;; (tmp >> (58 - (k * 6))) & 0x3f
36
- // i64.const 63
37
- // i64.and
38
-
39
- // i32.wrap_i64
40
-
41
- // ;; keyStrPtr + ...
42
- // i32.add
43
-
44
- // ;; load character from keyStr
45
- // i32.load8_u 0 4
46
-
47
- // ;; store in output at j
48
- // i32.store8 0 4
49
-
50
- // local.get ${j}
51
- // i32.const 1
52
- // i32.add
53
- // local.set ${j}
54
-
55
- // local.get k
56
- // i64.const 1
57
- // i64.add
58
- // local.tee k
59
-
60
- // i64.const 8
61
- // i64.lt_s
62
- // br_if 0
63
- // end
64
-
65
- // `;
66
-
67
- // // len -= 6;
68
- // i += 6;
69
- // }
70
-
71
- // // while (k < 8) {
72
- // // Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + Porffor.wasm.i32.wrap_i64(Porffor.wasm.i64.and(
73
- // // Porffor.wasm.i64.shr_u(tmp, Porffor.wasm.i64.extend_i32_u(58 - k * 6)),
74
- // // Porffor.wasm.i64.const(0x3f)
75
- // // )), 0, 4), 0, 4);
76
- // // k += 1;
77
- // // }
78
-
79
- // i += 6;
80
- // len -= 6;
81
- // }
1
+ // @porf --valtype=i32
82
2
 
83
3
  export const btoa = (input: bytestring): bytestring => {
84
4
  const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
85
- const keyStrPtr: i32 = Porffor.i32.ptrUnsafe(keyStr);
5
+ const keyStrPtr: i32 = Porffor.wasm`local.get ${keyStr}`;
86
6
 
87
7
  let len: i32 = input.length;
88
8
  let output: bytestring = '';
89
9
  output.length = 4 * (len / 3 + !!(len % 3));
90
10
 
91
- let i: i32 = Porffor.i32.ptrUnsafe(input),
92
- j: i32 = Porffor.i32.ptrUnsafe(output);
11
+ let i: i32 = Porffor.wasm`local.get ${input}`,
12
+ j: i32 = Porffor.wasm`local.get ${output}`;
13
+
14
+ // todo/perf: add some per 6 char variant using bitwise magic
93
15
 
94
16
  const endPtr = i + len;
95
17
  while (i < endPtr) {
@@ -118,6 +40,7 @@ export const btoa = (input: bytestring): bytestring => {
118
40
  return output;
119
41
  };
120
42
 
43
+ // todo: impl atob by converting below to "porf ts"
121
44
  /* var atob = function (input) {
122
45
  const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
123
46
 
@@ -0,0 +1,18 @@
1
+ // 20.3.3.2 Boolean.prototype.toString ()
2
+ // https://tc39.es/ecma262/#sec-boolean.prototype.tostring
3
+ export const __Boolean_prototype_toString = (_this: boolean) => {
4
+ // 1. Let b be ? ThisBooleanValue(this value).
5
+ // 2. If b is true, return "true"; else return "false".
6
+ let out: bytestring = '';
7
+ if (_this) out = 'true';
8
+ else out = 'false';
9
+
10
+ return out;
11
+ };
12
+
13
+ // 20.3.3.3 Boolean.prototype.valueOf ()
14
+ // https://tc39.es/ecma262/#sec-boolean.prototype.valueof
15
+ export const __Boolean_prototype_valueOf = (_this: boolean) => {
16
+ // 1. Return ? ThisBooleanValue(this value).
17
+ return _this;
18
+ };
@@ -0,0 +1,120 @@
1
+ // @porf --valtype=i32
2
+
3
+ export const __crypto_randomUUID = (): bytestring => {
4
+ let bytes: bytestring = '................';
5
+
6
+ const bytesPtr: i32 = Porffor.wasm`local.get ${bytes}`;
7
+
8
+ let a: i32 = bytesPtr;
9
+ let aEndPtr: i32 = a + 16;
10
+ while (a < aEndPtr) {
11
+ Porffor.wasm.i32.store8(a++, Porffor.randomByte(), 0, 4);
12
+ }
13
+
14
+ // bytes[6] = (bytes[6] & 0b00001111) | 0b01000000
15
+ Porffor.wasm.i32.store8(
16
+ bytesPtr,
17
+ (Porffor.wasm.i32.load8_u(bytesPtr, 0, 10) & 0b00001111) | 0b01000000,
18
+ 0,
19
+ 10 // 4 + 6
20
+ );
21
+
22
+ // bytes[8] = (bytes[8] & 0b00111111) | 0b10000000
23
+ Porffor.wasm.i32.store8(
24
+ bytesPtr,
25
+ (Porffor.wasm.i32.load8_u(bytesPtr, 0, 12) & 0b00111111) | 0b10000000,
26
+ 0,
27
+ 12 // 4 + 8
28
+ );
29
+
30
+ let output: bytestring = '------------------------------------';
31
+
32
+ let i: i32 = Porffor.wasm`local.get ${output}`;
33
+ let j: i32 = bytesPtr;
34
+
35
+ // bytes[0..4]-bytes[4..6]-bytes[6..8]-bytes[8..10]-bytes[10..15]
36
+ // 00112233-4455-6677-8899-aabbccddeeff
37
+
38
+ // bytes[0..4]-
39
+ let endPtr: i32 = i + 8;
40
+ while (i < endPtr) {
41
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
42
+
43
+ let lower: i32 = (byte & 0x0f) + 48;
44
+ if (lower > 57) lower += 39;
45
+
46
+ let upper: i32 = (byte >> 4) + 48;
47
+ if (upper > 57) upper += 39;
48
+
49
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
50
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
51
+ }
52
+ i++;
53
+
54
+ // bytes[4..6]-
55
+ endPtr = i + 4;
56
+ while (i < endPtr) {
57
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
58
+
59
+ let lower: i32 = (byte & 0x0f) + 48;
60
+ if (lower > 57) lower += 39;
61
+
62
+ let upper: i32 = (byte >> 4) + 48;
63
+ if (upper > 57) upper += 39;
64
+
65
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
66
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
67
+ }
68
+ i++;
69
+
70
+ // bytes[6..8]-
71
+ endPtr = i + 4;
72
+ while (i < endPtr) {
73
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
74
+
75
+ let lower: i32 = (byte & 0x0f) + 48;
76
+ if (lower > 57) lower += 39;
77
+
78
+ let upper: i32 = (byte >> 4) + 48;
79
+ if (upper > 57) upper += 39;
80
+
81
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
82
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
83
+ }
84
+ i++;
85
+
86
+ // bytes[8..10]-
87
+ endPtr = i + 4;
88
+ while (i < endPtr) {
89
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
90
+
91
+ let lower: i32 = (byte & 0x0f) + 48;
92
+ if (lower > 57) lower += 39;
93
+
94
+ let upper: i32 = (byte >> 4) + 48;
95
+ if (upper > 57) upper += 39;
96
+
97
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
98
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
99
+ }
100
+ i++;
101
+
102
+ // bytes[10..15]
103
+ endPtr = i + 12;
104
+ while (i < endPtr) {
105
+ const byte: i32 = Porffor.wasm.i32.load8_u(j++, 0, 4);
106
+
107
+ let lower: i32 = (byte & 0x0f) + 48;
108
+ if (lower > 57) lower += 39;
109
+
110
+ let upper: i32 = (byte >> 4) + 48;
111
+ if (upper > 57) upper += 39;
112
+
113
+ Porffor.wasm.i32.store8(i++, upper, 0, 4);
114
+ Porffor.wasm.i32.store8(i++, lower, 0, 4);
115
+ }
116
+ i++;
117
+
118
+
119
+ return output;
120
+ };