porffor 0.2.0-c7b7423 → 0.2.0-dcc06c8

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.
@@ -1,5 +1,6 @@
1
- import { Blocktype, Opcodes, Valtype } from "./wasmSpec.js";
1
+ import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
2
2
  import { number, i32x4 } from "./embedding.js";
3
+ import Prefs from './prefs.js';
3
4
 
4
5
  export const importedFuncs = [
5
6
  {
@@ -29,6 +30,21 @@ for (let i = 0; i < importedFuncs.length; i++) {
29
30
 
30
31
  const char = c => number(c.charCodeAt(0));
31
32
 
33
+ const printStaticStr = str => {
34
+ const out = [];
35
+
36
+ for (let i = 0; i < str.length; i++) {
37
+ out.push(
38
+ // ...number(str.charCodeAt(i)),
39
+ ...number(str.charCodeAt(i), Valtype.i32),
40
+ Opcodes.i32_from_u,
41
+ [ Opcodes.call, importedFuncs.printChar ]
42
+ );
43
+ }
44
+
45
+ return out;
46
+ };
47
+
32
48
  // todo: somehow diff between these (undefined != null) while remaining falsey in wasm as a number value
33
49
  export const UNDEFINED = 0;
34
50
  export const NULL = 0;
@@ -155,25 +171,6 @@ export const BuiltinFuncs = function() {
155
171
  ]
156
172
  };
157
173
 
158
- // todo: return false for NaN
159
- this.Boolean = {
160
- params: [ valtypeBinary ],
161
- locals: [],
162
- returns: [ valtypeBinary ],
163
- returnType: 'boolean',
164
- wasm: [
165
- [ Opcodes.local_get, 0 ],
166
- ...(valtype === 'f64' ? [
167
- ...number(0),
168
- [ Opcodes.f64_ne ]
169
- ] : [
170
- ...Opcodes.eqz,
171
- [ Opcodes.i32_eqz ]
172
- ]),
173
- Opcodes.i32_from
174
- ]
175
- };
176
-
177
174
  // just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
178
175
  this.Object = {
179
176
  params: [ valtypeBinary ],
@@ -187,12 +184,125 @@ export const BuiltinFuncs = function() {
187
184
 
188
185
 
189
186
  this.__console_log = {
190
- params: [ valtypeBinary ],
191
- locals: [],
187
+ params: [ valtypeBinary, Valtype.i32 ],
188
+ typedParams: true,
189
+ locals: [ Valtype.i32, Valtype.i32 ],
192
190
  returns: [],
193
- wasm: [
194
- [ Opcodes.local_get, 0 ],
195
- [ Opcodes.call, importedFuncs.print ],
191
+ wasm: (scope, { TYPES, typeSwitch }) => [
192
+ ...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
193
+ [TYPES.number]: [
194
+ [ Opcodes.local_get, 0 ],
195
+ [ Opcodes.call, importedFuncs.print ],
196
+ ],
197
+ [TYPES.boolean]: [
198
+ [ Opcodes.local_get, 0 ],
199
+ Opcodes.i32_to_u,
200
+ [ Opcodes.if, Blocktype.void ],
201
+ ...printStaticStr('true'),
202
+ [ Opcodes.else ],
203
+ ...printStaticStr('false'),
204
+ [ Opcodes.end ]
205
+ ],
206
+ [TYPES.string]: [
207
+ // simply print a string :))
208
+ // cache input pointer as i32
209
+ [ Opcodes.local_get, 0 ],
210
+ Opcodes.i32_to_u,
211
+ [ Opcodes.local_tee, 2 ],
212
+
213
+ // make end pointer
214
+ [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
215
+ ...number(ValtypeSize.i16, Valtype.i32),
216
+ [ Opcodes.i32_mul ],
217
+
218
+ [ Opcodes.local_get, 2 ],
219
+ [ Opcodes.i32_add ],
220
+ [ Opcodes.local_set, 3 ],
221
+
222
+ [ Opcodes.loop, Blocktype.void ],
223
+
224
+ // print current char
225
+ [ Opcodes.local_get, 2 ],
226
+ [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
227
+ Opcodes.i32_from_u,
228
+ [ Opcodes.call, importedFuncs.printChar ],
229
+
230
+ // increment pointer by sizeof i16
231
+ [ Opcodes.local_get, 2 ],
232
+ ...number(ValtypeSize.i16, Valtype.i32),
233
+ [ Opcodes.i32_add ],
234
+ [ Opcodes.local_tee, 2 ],
235
+
236
+ // if pointer != end pointer, loop
237
+ [ Opcodes.local_get, 3 ],
238
+ [ Opcodes.i32_ne ],
239
+ [ Opcodes.br_if, 0 ],
240
+
241
+ [ Opcodes.end ]
242
+ ],
243
+ [TYPES._array]: [
244
+ ...printStaticStr('[ '),
245
+
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
+ ...number(ValtypeSize[valtype], Valtype.i32),
254
+ [ Opcodes.i32_mul ],
255
+
256
+ [ Opcodes.local_get, 2 ],
257
+ [ Opcodes.i32_add ],
258
+ [ Opcodes.local_set, 3 ],
259
+
260
+ [ Opcodes.loop, Blocktype.void ],
261
+
262
+ // print current char
263
+ [ Opcodes.local_get, 2 ],
264
+ [ Opcodes.load, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
265
+ [ Opcodes.call, importedFuncs.print ],
266
+
267
+ // increment pointer by sizeof valtype
268
+ [ Opcodes.local_get, 2 ],
269
+ ...number(ValtypeSize[valtype], Valtype.i32),
270
+ [ Opcodes.i32_add ],
271
+ [ Opcodes.local_tee, 2 ],
272
+
273
+ // if pointer != end pointer, print separator and loop
274
+ [ Opcodes.local_get, 3 ],
275
+ [ Opcodes.i32_ne ],
276
+ [ Opcodes.if, Blocktype.void ],
277
+ ...printStaticStr(', '),
278
+ [ Opcodes.br, 1 ],
279
+ [ Opcodes.end ],
280
+
281
+ [ Opcodes.end ],
282
+
283
+ ...printStaticStr(' ]'),
284
+ ],
285
+ [TYPES.undefined]: [
286
+ ...printStaticStr('undefined')
287
+ ],
288
+ [TYPES.function]: [
289
+ ...printStaticStr('function () {}')
290
+ ],
291
+ [TYPES.object]: [
292
+ [ Opcodes.local_get, 0 ],
293
+ Opcodes.i32_to_u,
294
+ [ Opcodes.if, Blocktype.void ],
295
+ ...printStaticStr('{}'),
296
+ [ Opcodes.else ],
297
+ ...printStaticStr('null'),
298
+ [ Opcodes.end ]
299
+ ],
300
+ default: [
301
+ [ Opcodes.local_get, 0 ],
302
+ [ Opcodes.call, importedFuncs.print ],
303
+ ]
304
+ }, Blocktype.void),
305
+
196
306
  ...char('\n'),
197
307
  [ Opcodes.call, importedFuncs.printChar ]
198
308
  ]
@@ -571,6 +681,49 @@ export const BuiltinFuncs = function() {
571
681
  };
572
682
 
573
683
 
684
+ this.__Porffor_type = {
685
+ params: [ valtypeBinary, Valtype.i32 ],
686
+ typedParams: true,
687
+ locals: [ Valtype.i32, Valtype.i32 ],
688
+ returns: [ valtypeBinary ],
689
+ returnType: Prefs.bytestring ? '_bytestring' : 'string',
690
+ wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
691
+ const bc = {};
692
+ for (const x in TYPE_NAMES) {
693
+ bc[x] = makeString(scope, TYPE_NAMES[x], false, '#Porffor_type_result');
694
+ }
695
+
696
+ return typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], bc);
697
+ }
698
+ };
699
+
700
+ const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
701
+ const out = [];
702
+
703
+ for (let i = 0; i < arr.length; i++) {
704
+ out.push(...getter, ...number(arr[i], valtype), valtype === Valtype.f64 ? [ Opcodes.f64_eq ] : [ Opcodes.i32_eq ]);
705
+ if (i !== 0) out.push([ Opcodes.i32_or ]);
706
+ }
707
+
708
+ return out;
709
+ };
710
+
711
+ this.__Porffor_pointer = {
712
+ params: [ valtypeBinary, Valtype.i32 ],
713
+ typedParams: true,
714
+ locals: [ Valtype.i32, Valtype.i32 ],
715
+ returns: [ valtypeBinary ],
716
+ wasm: (scope, { TYPES }) => [
717
+ ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
718
+ [ Opcodes.if, valtypeBinary ],
719
+ [ Opcodes.local_get, 0 ],
720
+ [ Opcodes.else ],
721
+ ...number(NaN),
722
+ [ Opcodes.end ]
723
+ ]
724
+ };
725
+
726
+
574
727
  this.__SIMD_i32x4_load = {
575
728
  params: [ Valtype.i32 ],
576
729
  locals: [],
@@ -738,39 +891,4 @@ export const BuiltinFuncs = function() {
738
891
  [ ...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)
739
892
  ]
740
893
  };
741
- };
742
-
743
- export const BuiltinPreludes = {
744
- btoa: `var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
745
- var btoa = function (input) {
746
- // todo: throw invalid character for unicode
747
-
748
- let output = "";
749
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
750
- let i = 0;
751
-
752
- while (i < input.length) {
753
- chr1 = input.charCodeAt(i++);
754
- chr2 = input.charCodeAt(i++);
755
- chr3 = input.charCodeAt(i++);
756
-
757
- enc1 = chr1 >> 2;
758
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
759
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
760
- enc4 = chr3 & 63;
761
-
762
- if (isNaN(chr2)) {
763
- enc3 = enc4 = 64;
764
- } else if (isNaN(chr3)) {
765
- enc4 = 64;
766
- }
767
-
768
- output += keyStr.charAt(enc1);
769
- output += keyStr.charAt(enc2);
770
- output += keyStr.charAt(enc3);
771
- output += keyStr.charAt(enc4);
772
- }
773
-
774
- return output;
775
- };`
776
894
  };