porffor 0.2.0-4035760 → 0.2.0-4b72c49

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.
@@ -0,0 +1,153 @@
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
+ // }
82
+
83
+ export const btoa = (input: bytestring): bytestring => {
84
+ const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
85
+ const keyStrPtr: i32 = Porffor.i32.ptrUnsafe(keyStr);
86
+
87
+ let len: i32 = input.length;
88
+ let output: bytestring = '';
89
+ output.length = 4 * (len / 3 + !!(len % 3));
90
+
91
+ let i: i32 = Porffor.i32.ptrUnsafe(input),
92
+ j: i32 = Porffor.i32.ptrUnsafe(output);
93
+
94
+ const endPtr = i + len;
95
+ while (i < endPtr) {
96
+ const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
97
+ const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
98
+ const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
99
+
100
+ const enc1: i32 = chr1 >> 2;
101
+ const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
102
+ let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
103
+ let enc4: i32 = chr3 & 63;
104
+
105
+ if (chr2 == -1) {
106
+ enc3 = 64;
107
+ enc4 = 64;
108
+ } else if (chr3 == -1) {
109
+ enc4 = 64;
110
+ }
111
+
112
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
113
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
114
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
115
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
116
+ }
117
+
118
+ return output;
119
+ };
120
+
121
+ /* var atob = function (input) {
122
+ const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
123
+
124
+ let output = "";
125
+ let chr1, chr2, chr3;
126
+ let enc1, enc2, enc3, enc4;
127
+ let i = 0;
128
+
129
+ while (i < input.length) {
130
+ enc1 = keyStr.indexOf(input.charAt(i++));
131
+ enc2 = keyStr.indexOf(input.charAt(i++));
132
+ enc3 = keyStr.indexOf(input.charAt(i++));
133
+ enc4 = keyStr.indexOf(input.charAt(i++));
134
+
135
+ chr1 = (enc1 << 2) | (enc2 >> 4);
136
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
137
+ chr3 = ((enc3 & 3) << 6) | enc4;
138
+
139
+ // output += String.fromCharCode(chr1);
140
+ Porffor.bytestring.appendCharCode(output, chr1);
141
+
142
+ if (enc3 != 64) {
143
+ // output += String.fromCharCode(chr2);
144
+ Porffor.bytestring.appendCharCode(output, chr2);
145
+ }
146
+ if (enc4 != 64) {
147
+ // output += String.fromCharCode(chr3);
148
+ Porffor.bytestring.appendCharCode(output, chr3);
149
+ }
150
+ }
151
+
152
+ return output;
153
+ }; */
@@ -0,0 +1,23 @@
1
+ export type i32 = number;
2
+ export type i64 = number;
3
+ export type bytestring = string;
4
+
5
+ type PorfforGlobal = {
6
+ wasm: {
7
+ (...args: any[]): void;
8
+ i32: {
9
+ load8_u: (pointer: i32, align: i32, offset: i32) => i32;
10
+ store8: (pointer: i32, value: i32, align: i32, offset: i32) => i32;
11
+ }
12
+ }
13
+ ptr: (obj: any) => i32;
14
+
15
+ i32: {
16
+ ptr: (obj: any) => i32;
17
+ ptrUnsafe: (obj: any) => i32;
18
+ }
19
+ };
20
+
21
+ declare global {
22
+ const Porffor: PorfforGlobal;
23
+ }
@@ -1,5 +1,7 @@
1
1
  import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
2
2
  import { number, i32x4 } from "./embedding.js";
3
+ import Prefs from './prefs.js';
4
+ import * as GeneratedBuiltins from './generated_builtins.js';
3
5
 
4
6
  export const importedFuncs = [
5
7
  {
@@ -170,25 +172,6 @@ export const BuiltinFuncs = function() {
170
172
  ]
171
173
  };
172
174
 
173
- // todo: return false for NaN
174
- this.Boolean = {
175
- params: [ valtypeBinary ],
176
- locals: [],
177
- returns: [ valtypeBinary ],
178
- returnType: 'boolean',
179
- wasm: [
180
- [ Opcodes.local_get, 0 ],
181
- ...(valtype === 'f64' ? [
182
- ...number(0),
183
- [ Opcodes.f64_ne ]
184
- ] : [
185
- ...Opcodes.eqz,
186
- [ Opcodes.i32_eqz ]
187
- ]),
188
- Opcodes.i32_from
189
- ]
190
- };
191
-
192
175
  // just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
193
176
  this.Object = {
194
177
  params: [ valtypeBinary ],
@@ -258,6 +241,40 @@ export const BuiltinFuncs = function() {
258
241
 
259
242
  [ Opcodes.end ]
260
243
  ],
244
+ [TYPES._bytestring]: [
245
+ // simply print a (byte)string :))
246
+ // cache input pointer as i32
247
+ [ Opcodes.local_get, 0 ],
248
+ Opcodes.i32_to_u,
249
+ [ Opcodes.local_tee, 2 ],
250
+
251
+ // make end pointer
252
+ [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
253
+ [ Opcodes.local_get, 2 ],
254
+ [ Opcodes.i32_add ],
255
+ [ Opcodes.local_set, 3 ],
256
+
257
+ [ Opcodes.loop, Blocktype.void ],
258
+
259
+ // print current char
260
+ [ Opcodes.local_get, 2 ],
261
+ [ Opcodes.i32_load8_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
262
+ Opcodes.i32_from_u,
263
+ [ Opcodes.call, importedFuncs.printChar ],
264
+
265
+ // increment pointer
266
+ [ Opcodes.local_get, 2 ],
267
+ [ Opcodes.i32_const, 1 ],
268
+ [ Opcodes.i32_add ],
269
+ [ Opcodes.local_tee, 2 ],
270
+
271
+ // if pointer != end pointer, loop
272
+ [ Opcodes.local_get, 3 ],
273
+ [ Opcodes.i32_ne ],
274
+ [ Opcodes.br_if, 0 ],
275
+
276
+ [ Opcodes.end ]
277
+ ],
261
278
  [TYPES._array]: [
262
279
  ...printStaticStr('[ '),
263
280
 
@@ -699,206 +716,76 @@ export const BuiltinFuncs = function() {
699
716
  };
700
717
 
701
718
 
702
- this.__SIMD_i32x4_load = {
703
- params: [ Valtype.i32 ],
704
- locals: [],
705
- returns: [ Valtype.v128 ],
706
- wasm: [
707
- [ Opcodes.local_get, 0 ],
708
- [ ...Opcodes.v128_load, 0, 0 ]
709
- ]
710
- };
711
-
712
- this.__SIMD_i32x4_splat = {
713
- params: [ Valtype.i32 ],
714
- locals: [],
715
- returns: [ Valtype.v128 ],
716
- wasm: [
717
- [ Opcodes.local_get, 0 ],
718
- [ ...Opcodes.i32x4_splat ],
719
- ]
720
- };
721
-
722
- this.__SIMD_i16x8_create = {
723
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
724
- locals: [],
725
- returns: [ Valtype.v128 ],
726
- wasm: [
727
- ...i32x4(0, 0, 0, 0),
728
- [ Opcodes.local_get, 0 ],
729
- [ ...Opcodes.i16x8_replace_lane, 0 ],
730
- [ Opcodes.local_get, 1 ],
731
- [ ...Opcodes.i16x8_replace_lane, 1 ],
732
- [ Opcodes.local_get, 2 ],
733
- [ ...Opcodes.i16x8_replace_lane, 2 ],
734
- [ Opcodes.local_get, 3 ],
735
- [ ...Opcodes.i16x8_replace_lane, 3 ],
736
- [ Opcodes.local_get, 4 ],
737
- [ ...Opcodes.i16x8_replace_lane, 4 ],
738
- [ Opcodes.local_get, 5 ],
739
- [ ...Opcodes.i16x8_replace_lane, 5 ],
740
- [ Opcodes.local_get, 6 ],
741
- [ ...Opcodes.i16x8_replace_lane, 6 ],
742
- [ Opcodes.local_get, 7 ],
743
- [ ...Opcodes.i16x8_replace_lane, 7 ],
744
- ]
745
- };
746
-
747
- this.__SIMD_i32x4_dot_i16x8 = {
748
- params: [ Valtype.v128, Valtype.v128 ],
749
- locals: [],
750
- returns: [ Valtype.v128 ],
751
- wasm: [
752
- [ Opcodes.local_get, 0 ],
753
- [ Opcodes.local_get, 1 ],
754
- [ ...Opcodes.i32x4_dot_i16x8_s ]
755
- ]
719
+ this.__Porffor_type = {
720
+ params: [ valtypeBinary, Valtype.i32 ],
721
+ typedParams: true,
722
+ locals: [ Valtype.i32, Valtype.i32 ],
723
+ returns: [ valtypeBinary ],
724
+ returnType: Prefs.bytestring ? '_bytestring' : 'string',
725
+ wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
726
+ const bc = {};
727
+ for (const x in TYPE_NAMES) {
728
+ bc[x] = makeString(scope, TYPE_NAMES[x], false, '#Porffor_type_result');
729
+ }
730
+
731
+ return typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], bc);
732
+ }
756
733
  };
757
734
 
758
- this.__SIMD_i32x4_create = {
759
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
760
- locals: [],
761
- returns: [ Valtype.v128 ],
762
- wasm: [
763
- ...i32x4(0, 0, 0, 0),
764
- [ Opcodes.local_get, 0 ],
765
- [ ...Opcodes.i32x4_replace_lane, 0 ],
766
- [ Opcodes.local_get, 1 ],
767
- [ ...Opcodes.i32x4_replace_lane, 1 ],
768
- [ Opcodes.local_get, 2 ],
769
- [ ...Opcodes.i32x4_replace_lane, 2 ],
770
- [ Opcodes.local_get, 3 ],
771
- [ ...Opcodes.i32x4_replace_lane, 3 ],
772
- ]
773
- };
735
+ const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
736
+ const out = [];
774
737
 
775
- this.__SIMD_i32x4_add = {
776
- params: [ Valtype.v128, Valtype.v128 ],
777
- locals: [],
778
- returns: [ Valtype.v128 ],
779
- wasm: [
780
- [ Opcodes.local_get, 0 ],
781
- [ Opcodes.local_get, 1 ],
782
- [ ...Opcodes.i32x4_add ]
783
- ]
784
- };
738
+ for (let i = 0; i < arr.length; i++) {
739
+ out.push(...getter, ...number(arr[i], valtype), valtype === Valtype.f64 ? [ Opcodes.f64_eq ] : [ Opcodes.i32_eq ]);
740
+ if (i !== 0) out.push([ Opcodes.i32_or ]);
741
+ }
785
742
 
786
- this.__SIMD_i32x4_sub = {
787
- params: [ Valtype.v128, Valtype.v128 ],
788
- locals: [],
789
- returns: [ Valtype.v128 ],
790
- wasm: [
791
- [ Opcodes.local_get, 0 ],
792
- [ Opcodes.local_get, 1 ],
793
- [ ...Opcodes.i32x4_sub ]
794
- ]
743
+ return out;
795
744
  };
796
745
 
797
- this.__SIMD_i32x4_mul = {
798
- params: [ Valtype.v128, Valtype.v128 ],
799
- locals: [],
800
- returns: [ Valtype.v128 ],
801
- wasm: [
746
+ this.__Porffor_ptr = {
747
+ params: [ valtypeBinary, Valtype.i32 ],
748
+ typedParams: true,
749
+ locals: [ Valtype.i32, Valtype.i32 ],
750
+ returns: [ valtypeBinary ],
751
+ wasm: (scope, { TYPES }) => [
752
+ ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
753
+ [ Opcodes.if, valtypeBinary ],
802
754
  [ Opcodes.local_get, 0 ],
803
- [ Opcodes.local_get, 1 ],
804
- [ ...Opcodes.i32x4_mul ]
755
+ [ Opcodes.else ],
756
+ ...number(NaN),
757
+ [ Opcodes.end ]
805
758
  ]
806
759
  };
807
760
 
808
- this.__SIMD_i32x4_get0 = {
809
- params: [ Valtype.v128 ],
810
- locals: [],
811
- returns: [ Valtype.i32 ],
812
- wasm: [
813
- [ Opcodes.local_get, 0 ],
814
- [ ...Opcodes.i32x4_extract_lane, 0 ],
815
- ],
816
- },
817
-
818
- this.__SIMD_i32x4_get1 = {
819
- params: [ Valtype.v128 ],
820
- locals: [],
821
- returns: [ Valtype.i32 ],
822
- wasm: [
823
- [ Opcodes.local_get, 0 ],
824
- [ ...Opcodes.i32x4_extract_lane, 1 ],
825
- ],
826
- };
827
-
828
- this.__SIMD_i32x4_get2 = {
829
- params: [ Valtype.v128 ],
830
- locals: [],
831
- returns: [ Valtype.i32 ],
832
- wasm: [
833
- [ Opcodes.local_get, 0 ],
834
- [ ...Opcodes.i32x4_extract_lane, 2 ],
835
- ],
836
- };
837
-
838
- this.__SIMD_i32x4_get3 = {
839
- params: [ Valtype.v128 ],
840
- locals: [],
761
+ this.__Porffor_i32_ptr = {
762
+ params: [ Valtype.i32, Valtype.i32 ],
763
+ typedParams: true,
764
+ locals: [ Valtype.i32, Valtype.i32 ],
841
765
  returns: [ Valtype.i32 ],
842
- wasm: [
843
- [ Opcodes.local_get, 0 ],
844
- [ ...Opcodes.i32x4_extract_lane, 3 ],
845
- ],
846
- };
847
-
848
- this.__SIMD_i32x4_shuffle_000c = {
849
- params: [ Valtype.v128 ],
850
- locals: [],
851
- returns: [ Valtype.v128 ],
852
- wasm: [
766
+ wasm: (scope, { TYPES }) => [
767
+ ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
768
+ [ Opcodes.if, Valtype.i32 ],
853
769
  [ Opcodes.local_get, 0 ],
854
- ...i32x4(0, 0, 0, 0),
855
- [ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 9, 10, 11 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, 0, c)
770
+ [ Opcodes.else ],
771
+ ...number(-1, Valtype.i32),
772
+ [ Opcodes.end ]
856
773
  ]
857
774
  };
858
775
 
859
- this.__SIMD_i32x4_shuffle_00ab = {
860
- params: [ Valtype.v128 ],
776
+ // unsafe: does not check type just ~casts to number
777
+ this.__Porffor_i32_ptrUnsafe = {
778
+ params: [ Valtype.i32 ],
861
779
  locals: [],
862
- returns: [ Valtype.v128 ],
780
+ returns: [ Valtype.i32 ],
863
781
  wasm: [
864
782
  [ Opcodes.local_get, 0 ],
865
- ...i32x4(0, 0, 0, 0),
866
- [ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, a, b)
867
783
  ]
868
784
  };
869
- };
870
785
 
871
- export const BuiltinPreludes = {
872
- btoa: `var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
873
- var btoa = function (input) {
874
- // todo: throw invalid character for unicode
875
-
876
- let output = "";
877
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
878
- let i = 0;
879
-
880
- while (i < input.length) {
881
- chr1 = input.charCodeAt(i++);
882
- chr2 = input.charCodeAt(i++);
883
- chr3 = input.charCodeAt(i++);
884
-
885
- enc1 = chr1 >> 2;
886
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
887
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
888
- enc4 = chr3 & 63;
889
-
890
- if (isNaN(chr2)) {
891
- enc3 = enc4 = 64;
892
- } else if (isNaN(chr3)) {
893
- enc4 = 64;
894
- }
895
786
 
896
- output += keyStr.charAt(enc1);
897
- output += keyStr.charAt(enc2);
898
- output += keyStr.charAt(enc3);
899
- output += keyStr.charAt(enc4);
787
+ const generated = new GeneratedBuiltins.BuiltinFuncs();
788
+ for (const x in generated) {
789
+ this[x] = generated[x];
900
790
  }
901
-
902
- return output;
903
- };`
904
791
  };