porffor 0.2.0-5ac7ea0 → 0.2.0-5ad562e

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 (49) hide show
  1. package/CONTRIBUTING.md +255 -0
  2. package/LICENSE +20 -20
  3. package/README.md +115 -82
  4. package/asur/index.js +624 -340
  5. package/byg/index.js +237 -0
  6. package/compiler/2c.js +1 -1
  7. package/compiler/{sections.js → assemble.js} +59 -12
  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/crypto.ts +29 -41
  13. package/compiler/builtins/date.ts +2071 -0
  14. package/compiler/builtins/escape.ts +141 -0
  15. package/compiler/builtins/int.ts +147 -0
  16. package/compiler/builtins/number.ts +527 -0
  17. package/compiler/builtins/porffor.d.ts +42 -9
  18. package/compiler/builtins/string.ts +1055 -0
  19. package/compiler/builtins/tostring.ts +45 -0
  20. package/compiler/builtins.js +58 -85
  21. package/compiler/{codeGen.js → codegen.js} +792 -279
  22. package/compiler/decompile.js +0 -1
  23. package/compiler/embedding.js +22 -22
  24. package/compiler/encoding.js +108 -10
  25. package/compiler/generated_builtins.js +1463 -7
  26. package/compiler/index.js +16 -14
  27. package/compiler/log.js +2 -2
  28. package/compiler/opt.js +23 -22
  29. package/compiler/parse.js +30 -22
  30. package/compiler/precompile.js +25 -26
  31. package/compiler/prefs.js +7 -6
  32. package/compiler/prototype.js +2 -18
  33. package/compiler/types.js +37 -0
  34. package/compiler/wasmSpec.js +11 -1
  35. package/compiler/wrap.js +41 -44
  36. package/package.json +9 -5
  37. package/porf +2 -0
  38. package/rhemyn/compile.js +44 -26
  39. package/rhemyn/parse.js +322 -320
  40. package/rhemyn/test/parse.js +58 -58
  41. package/runner/compare.js +34 -34
  42. package/runner/debug.js +122 -0
  43. package/runner/index.js +69 -12
  44. package/runner/profiler.js +45 -26
  45. package/runner/repl.js +42 -9
  46. package/runner/sizes.js +37 -37
  47. package/runner/info.js +0 -89
  48. package/runner/transform.js +0 -15
  49. package/util/enum.js +0 -20
@@ -0,0 +1,45 @@
1
+ // // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
+
3
+ export const __Boolean_prototype_toString = (_this: boolean) => {
4
+ let out: bytestring = '';
5
+ if (_this) out = 'true';
6
+ else out = 'false';
7
+
8
+ return out;
9
+ };
10
+
11
+ export const __String_prototype_toString = (_this: string) => {
12
+ let out: string = Porffor.s``;
13
+ Porffor.clone(_this, out);
14
+ return out;
15
+ };
16
+
17
+ export const ___bytestring_prototype_toString = (_this: bytestring) => {
18
+ let out: bytestring = Porffor.bs``;
19
+ Porffor.clone(_this, out);
20
+ return out;
21
+ };
22
+
23
+ // // export const __undefined_prototype_toString = (_this: number) => {
24
+
25
+ // // };
26
+
27
+ export const __Object_prototype_toString = (_this: object) => {
28
+ let out: bytestring = '[object Object]';
29
+ return out;
30
+ };
31
+
32
+ export const __Function_prototype_toString = (_this: Function) => {
33
+ // todo: actually use source
34
+ let out: bytestring = 'function () {}';
35
+ return out;
36
+ };
37
+
38
+
39
+ // // export const ___array_prototype_toString = (_this: any[]) => {
40
+ // // return _this.join();
41
+ // // };
42
+
43
+ // // export const ___regexp_prototype_toString = (_this: number) => {
44
+
45
+ // // };
@@ -2,6 +2,7 @@ import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
2
2
  import { number, i32x4 } from "./embedding.js";
3
3
  import Prefs from './prefs.js';
4
4
  import * as GeneratedBuiltins from './generated_builtins.js';
5
+ import { TYPES } from './types.js';
5
6
 
6
7
  export const importedFuncs = [
7
8
  {
@@ -23,12 +24,24 @@ export const importedFuncs = [
23
24
  returns: 1
24
25
  },
25
26
  {
26
- name: 'profile',
27
+ name: 'timeOrigin',
28
+ import: 'u',
29
+ params: 0,
30
+ returns: 1
31
+ },
32
+ {
33
+ name: 'profile1',
34
+ import: 'y',
35
+ params: 1,
36
+ returns: 0
37
+ },
38
+ {
39
+ name: 'profile2',
27
40
  import: 'z',
28
41
  params: 1,
29
42
  returns: 0
30
43
  }
31
- ].filter(x => x);
44
+ ];
32
45
 
33
46
  for (let i = 0; i < importedFuncs.length; i++) {
34
47
  const f = importedFuncs[i];
@@ -58,10 +71,10 @@ export const NULL = 0;
58
71
 
59
72
  export const BuiltinVars = function() {
60
73
  this.undefined = number(UNDEFINED);
61
- this.undefined.type = 'undefined';
74
+ this.undefined.type = TYPES.undefined;
62
75
 
63
76
  this.null = number(NULL);
64
- this.null.type = 'object';
77
+ this.null.type = TYPES.object;
65
78
 
66
79
  this.NaN = number(NaN);
67
80
  this.NaN.floatOnly = true;
@@ -128,7 +141,15 @@ export const BuiltinVars = function() {
128
141
 
129
142
  // wintercg(tm)
130
143
  this.__navigator_userAgent = (scope, { makeString }) => makeString(scope, `Porffor/0.2.0`, false, '__navigator_userAgent');
131
- this.__navigator_userAgent.type = Prefs.bytestring ? '_bytestring' : 'string';
144
+ this.__navigator_userAgent.type = Prefs.bytestring ? TYPES._bytestring : TYPES.string;
145
+
146
+ for (const x in TYPES) {
147
+ this['__Porffor_TYPES_' + x] = number(TYPES[x]);
148
+ }
149
+
150
+ this.__performance_timeOrigin = [
151
+ [ Opcodes.call, importedFuncs.timeOrigin ]
152
+ ];
132
153
  };
133
154
 
134
155
  export const BuiltinFuncs = function() {
@@ -187,19 +208,20 @@ export const BuiltinFuncs = function() {
187
208
  params: [ valtypeBinary ],
188
209
  locals: [],
189
210
  returns: [ valtypeBinary ],
190
- returnType: 'object',
211
+ returnType: TYPES.object,
191
212
  wasm: [
192
- [ Opcodes.local_get, 0 ]
213
+ // [ Opcodes.local_get, 0 ]
214
+ ...number(1)
193
215
  ]
194
216
  };
195
217
 
196
218
 
197
- this.__console_log = {
219
+ this.__Porffor_print = {
198
220
  params: [ valtypeBinary, Valtype.i32 ],
199
221
  typedParams: true,
200
222
  locals: [ Valtype.i32, Valtype.i32 ],
201
223
  returns: [],
202
- wasm: (scope, { TYPES, typeSwitch }) => [
224
+ wasm: (scope, { typeSwitch }) => [
203
225
  ...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
204
226
  [TYPES.number]: [
205
227
  [ Opcodes.local_get, 0 ],
@@ -346,10 +368,7 @@ export const BuiltinFuncs = function() {
346
368
  [ Opcodes.local_get, 0 ],
347
369
  [ Opcodes.call, importedFuncs.print ],
348
370
  ]
349
- }, Blocktype.void),
350
-
351
- ...char('\n'),
352
- [ Opcodes.call, importedFuncs.printChar ]
371
+ }, Blocktype.void)
353
372
  ]
354
373
  };
355
374
 
@@ -361,7 +380,7 @@ export const BuiltinFuncs = function() {
361
380
  params: [ valtypeBinary ],
362
381
  locals: [],
363
382
  returns: [ valtypeBinary ],
364
- returnType: 'boolean',
383
+ returnType: TYPES.boolean,
365
384
  wasm: [
366
385
  [ Opcodes.local_get, 0 ],
367
386
  [ Opcodes.local_get, 0 ],
@@ -376,7 +395,7 @@ export const BuiltinFuncs = function() {
376
395
  params: [ valtypeBinary ],
377
396
  locals: [ valtypeBinary ],
378
397
  returns: [ valtypeBinary ],
379
- returnType: 'boolean',
398
+ returnType: TYPES.boolean,
380
399
  wasm: [
381
400
  [ Opcodes.local_get, 0 ],
382
401
  [ Opcodes.local_get, 0 ],
@@ -395,7 +414,7 @@ export const BuiltinFuncs = function() {
395
414
  params: [ valtypeBinary ],
396
415
  locals: [],
397
416
  returns: [ valtypeBinary ],
398
- returnType: 'boolean',
417
+ returnType: TYPES.boolean,
399
418
  wasm: [
400
419
  [ Opcodes.local_get, 0 ],
401
420
  [ Opcodes.local_get, 0 ],
@@ -410,7 +429,7 @@ export const BuiltinFuncs = function() {
410
429
  params: [ valtypeBinary ],
411
430
  locals: [],
412
431
  returns: [ valtypeBinary ],
413
- returnType: 'boolean',
432
+ returnType: TYPES.boolean,
414
433
  wasm: [
415
434
  [ Opcodes.local_get, 0 ],
416
435
  [ Opcodes.local_get, 0 ],
@@ -896,28 +915,7 @@ export const BuiltinFuncs = function() {
896
915
  ]
897
916
  };
898
917
 
899
- this.__Porffor_i32_random = {
900
- params: [],
901
- locals: prng.locals,
902
- localNames: [ 's1', 's0' ],
903
- globals: prng.globals,
904
- globalNames: [ 'state0', 'state1' ],
905
- globalInits: [ prngSeed0, prngSeed1 ],
906
- returns: [ Valtype.i32 ],
907
- wasm: [
908
- ...prng.wasm,
909
-
910
- ...(prng.returns === Valtype.i64 ? [
911
- // the lowest bits of the output generated by xorshift128+ have low quality
912
- ...number(56, Valtype.i64),
913
- [ Opcodes.i64_shr_u ],
914
-
915
- [ Opcodes.i32_wrap_i64 ],
916
- ] : []),
917
- ]
918
- };
919
-
920
- this.__Porffor_i32_randomByte = {
918
+ this.__Porffor_randomByte = {
921
919
  params: [],
922
920
  locals: prng.locals,
923
921
  localNames: [ 's1', 's0' ],
@@ -1016,7 +1014,7 @@ export const BuiltinFuncs = function() {
1016
1014
  params: [ valtypeBinary ],
1017
1015
  locals: [],
1018
1016
  returns: [ valtypeBinary ],
1019
- returnType: 'boolean',
1017
+ returnType: TYPES.boolean,
1020
1018
  wasm: [
1021
1019
  [ Opcodes.local_get, 0 ],
1022
1020
  ...number(0),
@@ -1041,7 +1039,7 @@ export const BuiltinFuncs = function() {
1041
1039
  typedParams: true,
1042
1040
  locals: [ Valtype.i32, Valtype.i32 ],
1043
1041
  returns: [ valtypeBinary ],
1044
- returnType: Prefs.bytestring ? '_bytestring' : 'string',
1042
+ returnType: Prefs.bytestring ? TYPES._bytestring : TYPES.string,
1045
1043
  wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
1046
1044
  const bc = {};
1047
1045
  for (const x in TYPE_NAMES) {
@@ -1052,60 +1050,35 @@ export const BuiltinFuncs = function() {
1052
1050
  }
1053
1051
  };
1054
1052
 
1055
- const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
1056
- const out = [];
1057
-
1058
- for (let i = 0; i < arr.length; i++) {
1059
- out.push(...getter, ...number(arr[i], valtype), valtype === Valtype.f64 ? [ Opcodes.f64_eq ] : [ Opcodes.i32_eq ]);
1060
- if (i !== 0) out.push([ Opcodes.i32_or ]);
1061
- }
1062
-
1063
- return out;
1064
- };
1065
-
1066
- this.__Porffor_ptr = {
1053
+ this.__Porffor_rawType = {
1067
1054
  params: [ valtypeBinary, Valtype.i32 ],
1068
1055
  typedParams: true,
1069
- locals: [ Valtype.i32, Valtype.i32 ],
1056
+ locals: [],
1070
1057
  returns: [ valtypeBinary ],
1071
- wasm: (scope, { TYPES }) => [
1072
- ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
1073
- [ Opcodes.if, valtypeBinary ],
1074
- [ Opcodes.local_get, 0 ],
1075
- [ Opcodes.else ],
1076
- ...number(NaN),
1077
- [ Opcodes.end ]
1078
- ]
1079
- };
1080
-
1081
- this.__Porffor_i32_ptr = {
1082
- params: [ Valtype.i32, Valtype.i32 ],
1083
- typedParams: true,
1084
- locals: [ Valtype.i32, Valtype.i32 ],
1085
- returns: [ Valtype.i32 ],
1086
- wasm: (_scope, { TYPES }) => [
1087
- ...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
1088
- [ Opcodes.if, Valtype.i32 ],
1089
- [ Opcodes.local_get, 0 ],
1090
- [ Opcodes.else ],
1091
- ...number(-1, Valtype.i32),
1092
- [ Opcodes.end ]
1058
+ wasm: [
1059
+ [ Opcodes.local_get, 1 ],
1060
+ Opcodes.i32_from_u
1093
1061
  ]
1094
1062
  };
1095
1063
 
1096
- // unsafe: does not check type just ~casts to number
1097
- this.__Porffor_i32_ptrUnsafe = {
1098
- params: [ Valtype.i32 ],
1064
+ this.__Porffor_clone = {
1065
+ params: [ valtypeBinary, valtypeBinary ],
1099
1066
  locals: [],
1100
- returns: [ Valtype.i32 ],
1067
+ returns: [],
1101
1068
  wasm: [
1069
+ // dst
1070
+ [ Opcodes.local_get, 1 ],
1071
+ Opcodes.i32_to_u,
1072
+
1073
+ // src
1102
1074
  [ Opcodes.local_get, 0 ],
1075
+ Opcodes.i32_to_u,
1076
+
1077
+ // size = pageSize
1078
+ ...number(pageSize, Valtype.i32),
1079
+ [ ...Opcodes.memory_copy, 0x00, 0x00 ],
1103
1080
  ]
1104
1081
  };
1105
1082
 
1106
-
1107
- const generated = new GeneratedBuiltins.BuiltinFuncs();
1108
- for (const x in generated) {
1109
- this[x] = generated[x];
1110
- }
1083
+ GeneratedBuiltins.BuiltinFuncs.call(this);
1111
1084
  };