porffor 0.2.0-1afe9b8 → 0.2.0-240f1c8

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,141 @@
1
+ // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
2
+
3
+ import type { i32, i64, bytestring } from './porffor.d.ts';
4
+
5
+ // while (len >= 8) {
6
+ // // Porffor.wasm`local tmp i64`;
7
+ // // const tmp: i64 = Porffor.wasm.i64.load(i, 0, 4);
8
+
9
+ // Porffor.wasm`
10
+ // local tmp i64
11
+ // local k i32
12
+
13
+ // local.get ${i}
14
+ // i64.load 0 4
15
+ // local.set tmp
16
+
17
+ // loop 64
18
+ // local.get ${j}
19
+ // local.get ${j}
20
+ // i32.const 1
21
+ // i32.add
22
+ // local.set ${j}
23
+
24
+ // local.get ${keyStrPtr}
25
+ // local.get tmp
26
+
27
+ // i32.const 58
28
+
29
+ // local.get k
30
+ // i32.const 6
31
+ // i32.mul
32
+
33
+ // i32.sub
34
+
35
+ // i64.extend_i32_u
36
+
37
+ // i64.shr_u
38
+
39
+ // i64.const 63
40
+ // i64.and
41
+
42
+ // i32.wrap_i64
43
+ // i32.add
44
+
45
+ // i32.load8_u 0 4
46
+ // i32.store8 0 4
47
+
48
+ // local.get k
49
+ // i32.const 1
50
+ // i32.add
51
+ // local.tee k
52
+
53
+ // i32.const 8
54
+ // i32.lt_s
55
+ // br_if 1
56
+ // end
57
+
58
+ // `;
59
+ // // while (k < 8) {
60
+ // // Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + Porffor.wasm.i32.wrap_i64(Porffor.wasm.i64.and(
61
+ // // Porffor.wasm.i64.shr_u(tmp, Porffor.wasm.i64.extend_i32_u(58 - k * 6)),
62
+ // // Porffor.wasm.i64.const(0x3f)
63
+ // // )), 0, 4), 0, 4);
64
+ // // k += 1;
65
+ // // }
66
+
67
+ // i += 6;
68
+ // len -= 6;
69
+ // }
70
+
71
+ export const btoa = (input: bytestring): bytestring => {
72
+ const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
73
+ const keyStrPtr: i32 = Porffor.i32.ptrUnsafe(keyStr);
74
+
75
+ let len: i32 = input.length;
76
+ let output: bytestring = '';
77
+ output.length = 4 * (len / 3 + !!(len % 3));
78
+
79
+ let i: i32 = Porffor.i32.ptrUnsafe(input),
80
+ j: i32 = Porffor.i32.ptrUnsafe(output);
81
+
82
+ const endPtr = i + len;
83
+ while (i < endPtr) {
84
+ const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
85
+ const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
86
+ const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
87
+
88
+ const enc1: i32 = chr1 >> 2;
89
+ const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
90
+ let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
91
+ let enc4: i32 = chr3 & 63;
92
+
93
+ if (chr2 == -1) {
94
+ enc3 = 64;
95
+ enc4 = 64;
96
+ } else if (chr3 == -1) {
97
+ enc4 = 64;
98
+ }
99
+
100
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
101
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
102
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
103
+ Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
104
+ }
105
+
106
+ return output;
107
+ };
108
+
109
+ /* var atob = function (input) {
110
+ const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
111
+
112
+ let output = "";
113
+ let chr1, chr2, chr3;
114
+ let enc1, enc2, enc3, enc4;
115
+ let i = 0;
116
+
117
+ while (i < input.length) {
118
+ enc1 = keyStr.indexOf(input.charAt(i++));
119
+ enc2 = keyStr.indexOf(input.charAt(i++));
120
+ enc3 = keyStr.indexOf(input.charAt(i++));
121
+ enc4 = keyStr.indexOf(input.charAt(i++));
122
+
123
+ chr1 = (enc1 << 2) | (enc2 >> 4);
124
+ chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
125
+ chr3 = ((enc3 & 3) << 6) | enc4;
126
+
127
+ // output += String.fromCharCode(chr1);
128
+ Porffor.bytestring.appendCharCode(output, chr1);
129
+
130
+ if (enc3 != 64) {
131
+ // output += String.fromCharCode(chr2);
132
+ Porffor.bytestring.appendCharCode(output, chr2);
133
+ }
134
+ if (enc4 != 64) {
135
+ // output += String.fromCharCode(chr3);
136
+ Porffor.bytestring.appendCharCode(output, chr3);
137
+ }
138
+ }
139
+
140
+ return output;
141
+ }; */
@@ -0,0 +1,35 @@
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
+ wrap_i64: (x: i64) => i32;
12
+ }
13
+ i64: {
14
+ load: (pointer: i32, align: i32, offset: i32) => i64;
15
+ shr_u: (a: i64, b: i64) => i64;
16
+ extend_i32_u: (x: i32) => i64;
17
+ and: (a: i64, b: i64) => i64;
18
+ const: (val: number) => i64;
19
+ }
20
+ }
21
+ ptr: (obj: any) => i32;
22
+
23
+ i32: {
24
+ ptr: (obj: any) => i32;
25
+ ptrUnsafe: (obj: any) => i32;
26
+
27
+ bytestring: {
28
+ setCharAt: (target: bytestring, targetIndex: i32, lookup: bytestring, lookupIndex: i32) => void;
29
+ }
30
+ }
31
+ };
32
+
33
+ declare global {
34
+ const Porffor: PorfforGlobal;
35
+ }
@@ -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
  {
@@ -239,6 +241,40 @@ export const BuiltinFuncs = function() {
239
241
 
240
242
  [ Opcodes.end ]
241
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
+ ],
242
278
  [TYPES._array]: [
243
279
  ...printStaticStr('[ '),
244
280
 
@@ -685,7 +721,7 @@ export const BuiltinFuncs = function() {
685
721
  typedParams: true,
686
722
  locals: [ Valtype.i32, Valtype.i32 ],
687
723
  returns: [ valtypeBinary ],
688
- returnType: process.argv.includes('-bytestring') ? '_bytestring' : 'string',
724
+ returnType: Prefs.bytestring ? '_bytestring' : 'string',
689
725
  wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
690
726
  const bc = {};
691
727
  for (const x in TYPE_NAMES) {
@@ -696,207 +732,60 @@ export const BuiltinFuncs = function() {
696
732
  }
697
733
  };
698
734
 
735
+ const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
736
+ const out = [];
699
737
 
700
- this.__SIMD_i32x4_load = {
701
- params: [ Valtype.i32 ],
702
- locals: [],
703
- returns: [ Valtype.v128 ],
704
- wasm: [
705
- [ Opcodes.local_get, 0 ],
706
- [ ...Opcodes.v128_load, 0, 0 ]
707
- ]
708
- };
709
-
710
- this.__SIMD_i32x4_splat = {
711
- params: [ Valtype.i32 ],
712
- locals: [],
713
- returns: [ Valtype.v128 ],
714
- wasm: [
715
- [ Opcodes.local_get, 0 ],
716
- [ ...Opcodes.i32x4_splat ],
717
- ]
718
- };
719
-
720
- this.__SIMD_i16x8_create = {
721
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
722
- locals: [],
723
- returns: [ Valtype.v128 ],
724
- wasm: [
725
- ...i32x4(0, 0, 0, 0),
726
- [ Opcodes.local_get, 0 ],
727
- [ ...Opcodes.i16x8_replace_lane, 0 ],
728
- [ Opcodes.local_get, 1 ],
729
- [ ...Opcodes.i16x8_replace_lane, 1 ],
730
- [ Opcodes.local_get, 2 ],
731
- [ ...Opcodes.i16x8_replace_lane, 2 ],
732
- [ Opcodes.local_get, 3 ],
733
- [ ...Opcodes.i16x8_replace_lane, 3 ],
734
- [ Opcodes.local_get, 4 ],
735
- [ ...Opcodes.i16x8_replace_lane, 4 ],
736
- [ Opcodes.local_get, 5 ],
737
- [ ...Opcodes.i16x8_replace_lane, 5 ],
738
- [ Opcodes.local_get, 6 ],
739
- [ ...Opcodes.i16x8_replace_lane, 6 ],
740
- [ Opcodes.local_get, 7 ],
741
- [ ...Opcodes.i16x8_replace_lane, 7 ],
742
- ]
743
- };
744
-
745
- this.__SIMD_i32x4_dot_i16x8 = {
746
- params: [ Valtype.v128, Valtype.v128 ],
747
- locals: [],
748
- returns: [ Valtype.v128 ],
749
- wasm: [
750
- [ Opcodes.local_get, 0 ],
751
- [ Opcodes.local_get, 1 ],
752
- [ ...Opcodes.i32x4_dot_i16x8_s ]
753
- ]
754
- };
755
-
756
- this.__SIMD_i32x4_create = {
757
- params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
758
- locals: [],
759
- returns: [ Valtype.v128 ],
760
- wasm: [
761
- ...i32x4(0, 0, 0, 0),
762
- [ Opcodes.local_get, 0 ],
763
- [ ...Opcodes.i32x4_replace_lane, 0 ],
764
- [ Opcodes.local_get, 1 ],
765
- [ ...Opcodes.i32x4_replace_lane, 1 ],
766
- [ Opcodes.local_get, 2 ],
767
- [ ...Opcodes.i32x4_replace_lane, 2 ],
768
- [ Opcodes.local_get, 3 ],
769
- [ ...Opcodes.i32x4_replace_lane, 3 ],
770
- ]
771
- };
772
-
773
- this.__SIMD_i32x4_add = {
774
- params: [ Valtype.v128, Valtype.v128 ],
775
- locals: [],
776
- returns: [ Valtype.v128 ],
777
- wasm: [
778
- [ Opcodes.local_get, 0 ],
779
- [ Opcodes.local_get, 1 ],
780
- [ ...Opcodes.i32x4_add ]
781
- ]
782
- };
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
+ }
783
742
 
784
- this.__SIMD_i32x4_sub = {
785
- params: [ Valtype.v128, Valtype.v128 ],
786
- locals: [],
787
- returns: [ Valtype.v128 ],
788
- wasm: [
789
- [ Opcodes.local_get, 0 ],
790
- [ Opcodes.local_get, 1 ],
791
- [ ...Opcodes.i32x4_sub ]
792
- ]
743
+ return out;
793
744
  };
794
745
 
795
- this.__SIMD_i32x4_mul = {
796
- params: [ Valtype.v128, Valtype.v128 ],
797
- locals: [],
798
- returns: [ Valtype.v128 ],
799
- 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 ],
800
754
  [ Opcodes.local_get, 0 ],
801
- [ Opcodes.local_get, 1 ],
802
- [ ...Opcodes.i32x4_mul ]
755
+ [ Opcodes.else ],
756
+ ...number(NaN),
757
+ [ Opcodes.end ]
803
758
  ]
804
759
  };
805
760
 
806
- this.__SIMD_i32x4_get0 = {
807
- params: [ Valtype.v128 ],
808
- locals: [],
809
- returns: [ Valtype.i32 ],
810
- wasm: [
811
- [ Opcodes.local_get, 0 ],
812
- [ ...Opcodes.i32x4_extract_lane, 0 ],
813
- ],
814
- },
815
-
816
- this.__SIMD_i32x4_get1 = {
817
- params: [ Valtype.v128 ],
818
- locals: [],
819
- returns: [ Valtype.i32 ],
820
- wasm: [
821
- [ Opcodes.local_get, 0 ],
822
- [ ...Opcodes.i32x4_extract_lane, 1 ],
823
- ],
824
- };
825
-
826
- this.__SIMD_i32x4_get2 = {
827
- params: [ Valtype.v128 ],
828
- locals: [],
829
- returns: [ Valtype.i32 ],
830
- wasm: [
831
- [ Opcodes.local_get, 0 ],
832
- [ ...Opcodes.i32x4_extract_lane, 2 ],
833
- ],
834
- };
835
-
836
- this.__SIMD_i32x4_get3 = {
837
- params: [ Valtype.v128 ],
838
- locals: [],
761
+ this.__Porffor_i32_ptr = {
762
+ params: [ Valtype.i32, Valtype.i32 ],
763
+ typedParams: true,
764
+ locals: [ Valtype.i32, Valtype.i32 ],
839
765
  returns: [ Valtype.i32 ],
840
- wasm: [
766
+ wasm: (scope, { TYPES }) => [
767
+ ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
768
+ [ Opcodes.if, Valtype.i32 ],
841
769
  [ Opcodes.local_get, 0 ],
842
- [ ...Opcodes.i32x4_extract_lane, 3 ],
843
- ],
844
- };
845
-
846
- this.__SIMD_i32x4_shuffle_000c = {
847
- params: [ Valtype.v128 ],
848
- locals: [],
849
- returns: [ Valtype.v128 ],
850
- wasm: [
851
- [ Opcodes.local_get, 0 ],
852
- ...i32x4(0, 0, 0, 0),
853
- [ ...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 ]
854
773
  ]
855
774
  };
856
775
 
857
- this.__SIMD_i32x4_shuffle_00ab = {
858
- params: [ Valtype.v128 ],
776
+ // unsafe: does not check type just ~casts to number
777
+ this.__Porffor_i32_ptrUnsafe = {
778
+ params: [ Valtype.i32 ],
859
779
  locals: [],
860
- returns: [ Valtype.v128 ],
780
+ returns: [ Valtype.i32 ],
861
781
  wasm: [
862
782
  [ Opcodes.local_get, 0 ],
863
- ...i32x4(0, 0, 0, 0),
864
- [ ...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)
865
783
  ]
866
784
  };
867
- };
868
785
 
869
- export const BuiltinPreludes = {
870
- btoa: `var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
871
- var btoa = function (input) {
872
- // todo: throw invalid character for unicode
873
-
874
- let output = "";
875
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
876
- let i = 0;
877
-
878
- while (i < input.length) {
879
- chr1 = input.charCodeAt(i++);
880
- chr2 = input.charCodeAt(i++);
881
- chr3 = input.charCodeAt(i++);
882
-
883
- enc1 = chr1 >> 2;
884
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
885
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
886
- enc4 = chr3 & 63;
887
-
888
- if (isNaN(chr2)) {
889
- enc3 = enc4 = 64;
890
- } else if (isNaN(chr3)) {
891
- enc4 = 64;
892
- }
893
786
 
894
- output += keyStr.charAt(enc1);
895
- output += keyStr.charAt(enc2);
896
- output += keyStr.charAt(enc3);
897
- output += keyStr.charAt(enc4);
787
+ const generated = new GeneratedBuiltins.BuiltinFuncs();
788
+ for (const x in generated) {
789
+ this[x] = generated[x];
898
790
  }
899
-
900
- return output;
901
- };`
902
791
  };