porffor 0.14.0-320727338 → 0.14.0-3905158d3

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.
package/CONTRIBUTING.md CHANGED
@@ -198,13 +198,11 @@ Store the character code into the `out` pointer variable, and increment it.
198
198
 
199
199
  ## Porffor-specific TS notes
200
200
 
201
- - For declaring variables, you must use explicit type annotations currently (eg `let a: number = 1`, not `let a = 1`).
201
+ - For declaring variables, you must use explicit type annotations currently (eg `let a: number = 1`, not `let a = 1`)
202
202
  - You might spot `Porffor.fastOr`/`Porffor.fastAnd`, these are non-short circuiting versions of `||`/`&&`, taking any number of conditions as arguments. You shouldn't don't need to use or worry about these.
203
- - **There are ~no objects, you cannot use them.**
203
+ - **There are ~no objects, you cannot use them/literals.**
204
204
  - Attempt to avoid string/array-heavy code and use more variables instead if possible, easier on memory and CPU/perf.
205
205
  - Do not set a return type for prototype methods, it can cause errors/unexpected results.
206
- - You cannot use other functions in the file not exported, or variables not inside the current function.
207
- - `if (...)` uses a fast truthy implementation which is not spec-compliant as most conditions should be strictly checked. To use spec-compliant behavior, use `if (Boolean(...))`.
208
206
 
209
207
  <br>
210
208
 
package/compiler/2c.js CHANGED
@@ -119,9 +119,6 @@ const removeBrackets = str => {
119
119
  };
120
120
 
121
121
  export default ({ funcs, globals, tags, data, exceptions, pages }) => {
122
- // fix declaring order for c
123
- funcs.reverse();
124
-
125
122
  const invOperatorOpcode = Object.values(operatorOpcode).reduce((acc, x) => {
126
123
  for (const k in x) {
127
124
  acc[x[k]] = k;
@@ -1,5 +1,4 @@
1
1
  // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
3
2
 
4
3
  export const __String_prototype_trimLeft = (_this: string) => {
5
4
  return __String_prototype_trimStart(_this);
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  export const __Array_isArray = (x: unknown): boolean =>
4
2
  // Porffor.wasm`local.get ${x+1}` == Porffor.TYPES.array;
5
3
  Porffor.rawType(x) == Porffor.TYPES.array;
@@ -164,7 +162,7 @@ export const __Array_prototype_filter = (_this: any[], callbackFn: any) => {
164
162
  let i: i32 = 0;
165
163
  while (i < len) {
166
164
  const el: any = _this[i];
167
- if (Boolean(callbackFn(el, i++, _this))) out.push(el);
165
+ if (callbackFn(el, i++, _this)) out.push(el);
168
166
  }
169
167
 
170
168
  return out;
@@ -187,7 +185,7 @@ export const __Array_prototype_find = (_this: any[], callbackFn: any) => {
187
185
  let i: i32 = 0;
188
186
  while (i < len) {
189
187
  const el: any = _this[i];
190
- if (Boolean(callbackFn(el, i++, _this))) return el;
188
+ if (callbackFn(el, i++, _this)) return el;
191
189
  }
192
190
  };
193
191
 
@@ -195,7 +193,7 @@ export const __Array_prototype_findLast = (_this: any[], callbackFn: any) => {
195
193
  let i: i32 = _this.length;
196
194
  while (i > 0) {
197
195
  const el: any = _this[--i];
198
- if (Boolean(callbackFn(el, i, _this))) return el;
196
+ if (callbackFn(el, i, _this)) return el;
199
197
  }
200
198
  };
201
199
 
@@ -203,14 +201,14 @@ export const __Array_prototype_findIndex = (_this: any[], callbackFn: any) => {
203
201
  const len: i32 = _this.length;
204
202
  let i: i32 = 0;
205
203
  while (i < len) {
206
- if (Boolean(callbackFn(_this[i], i++, _this))) return i;
204
+ if (callbackFn(_this[i], i++, _this)) return i;
207
205
  }
208
206
  };
209
207
 
210
208
  export const __Array_prototype_findLastIndex = (_this: any[], callbackFn: any) => {
211
209
  let i: i32 = _this.length;
212
210
  while (i > 0) {
213
- if (Boolean(callbackFn(_this[--i], i, _this))) return i;
211
+ if (callbackFn(_this[--i], i, _this)) return i;
214
212
  }
215
213
  };
216
214
 
@@ -218,7 +216,7 @@ export const __Array_prototype_every = (_this: any[], callbackFn: any) => {
218
216
  const len: i32 = _this.length;
219
217
  let i: i32 = 0;
220
218
  while (i < len) {
221
- if (!Boolean(callbackFn(_this[i], i++, _this))) return false;
219
+ if (!callbackFn(_this[i], i++, _this)) return false;
222
220
  }
223
221
 
224
222
  return true;
@@ -1,5 +1,4 @@
1
1
  // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
3
2
 
4
3
  export const btoa = (input: bytestring): bytestring => {
5
4
  const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
@@ -1,12 +1,10 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // 20.3.3.2 Boolean.prototype.toString ()
4
2
  // https://tc39.es/ecma262/#sec-boolean.prototype.tostring
5
3
  export const __Boolean_prototype_toString = (_this: boolean) => {
6
4
  // 1. Let b be ? ThisBooleanValue(this value).
7
5
  // 2. If b is true, return "true"; else return "false".
8
6
  let out: bytestring = '';
9
- if (_this) out = 'true';
7
+ if (_this == true) out = 'true';
10
8
  else out = 'false';
11
9
 
12
10
  return out;
@@ -1,5 +1,4 @@
1
1
  // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
3
2
 
4
3
  export const __crypto_randomUUID = (): bytestring => {
5
4
  let bytes: bytestring = '................';
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // 21.4.1.3 Day (t)
4
2
  // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-day
5
3
  // 1. Return 𝔽(floor(ℝ(t / msPerDay))).
@@ -1,5 +1,6 @@
1
1
  // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
2
+
3
+ import type {} from './porffor';
3
4
 
4
5
  export const escape = (input: string|bytestring): bytestring => {
5
6
  // we have no byte array yet so use bytestring with 0x00 and 0x01 via escape characters
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  export const __Function_prototype_toString = (_this: Function) => {
4
2
  // todo: actually use source
5
3
  let out: bytestring = 'function () {}';
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // radix: number|any for rawType check
4
2
  // export const parseInt = (input: string|bytestring, radix: number|any): f64 => {
5
3
  export const parseInt = (input: string|bytestring, radix: number): f64 => {
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // todo: use any and Number(x) in all these later
4
2
  // todo: specify the rest of this file later
5
3
  // todo/perf: make i32 variants later
@@ -19,7 +17,7 @@ export const __Math_exp = (x: number): number => {
19
17
  const k: number = Math.floor(x / Math.LN2);
20
18
  const r: number = x - k * Math.LN2;
21
19
 
22
- // Taylor series via Horner's method
20
+ // Horner's method
23
21
  let term: number = r;
24
22
  let sum: number = 1 + r;
25
23
  let i: number = 2;
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // radix: number|any for rawType check
4
2
  export const __Number_prototype_toString = (_this: number, radix: number|any) => {
5
3
  let out: bytestring = '';
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  export const __Object_prototype_toString = (_this: object) => {
4
2
  let out: bytestring = '[object Object]';
5
3
  return out;
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  // dark wasm magic for dealing with memory, sorry.
4
2
  export const __Porffor_allocate = (): number => {
5
3
  Porffor.wasm`i32.const 1
@@ -1,5 +1,4 @@
1
1
  // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
3
2
 
4
3
  export const __String_fromCharCode = (code: i32) => {
5
4
  // todo: support >1 arg
@@ -1,5 +1,3 @@
1
- import type {} from './porffor.d.ts';
2
-
3
1
  export const __Porffor_symbol_descStore = (op: boolean, value: any): any => {
4
2
  const ptr: bytestring = '';
5
3
 
@@ -668,7 +668,7 @@ const compareStrings = (scope, left, right, bytestrings = false) => {
668
668
  ];
669
669
  };
670
670
 
671
- const truthy = (scope, wasm, type, intIn = false, intOut = false, forceTruthyMode = undefined) => {
671
+ const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
672
672
  if (isIntToFloatOp(wasm[wasm.length - 1])) return [
673
673
  ...wasm,
674
674
  ...(!intIn && intOut ? [ Opcodes.i32_to_u ] : [])
@@ -703,13 +703,18 @@ const truthy = (scope, wasm, type, intIn = false, intOut = false, forceTruthyMod
703
703
  ...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
704
704
  ...(!intOut || (intIn && intOut) ? [] : [ Opcodes.i32_to_u ])
705
705
  ];
706
- })(forceTruthyMode ?? Prefs.truthy ?? 'full');
706
+ })(Prefs.truthy ?? 'full');
707
707
 
708
708
  return [
709
709
  ...wasm,
710
710
  ...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
711
711
 
712
712
  ...typeSwitch(scope, type, {
713
+ // [TYPES.number]: def,
714
+ // [TYPES.array]: [
715
+ // // arrays are always truthy
716
+ // ...number(1, intOut ? Valtype.i32 : valtypeBinary)
717
+ // ],
713
718
  [TYPES.string]: [
714
719
  ...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
715
720
  ...(intIn ? [] : [ Opcodes.i32_to_u ]),
@@ -745,6 +750,10 @@ const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
745
750
  ...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
746
751
 
747
752
  ...typeSwitch(scope, type, {
753
+ // [TYPES.array]: [
754
+ // // arrays are always truthy
755
+ // ...number(0, intOut ? Valtype.i32 : valtypeBinary)
756
+ // ],
748
757
  [TYPES.string]: [
749
758
  ...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
750
759
  ...(intIn ? [] : [ Opcodes.i32_to_u ]),
@@ -2579,7 +2588,6 @@ const generateUnary = (scope, decl) => {
2579
2588
  ];
2580
2589
 
2581
2590
  case '!':
2582
- // todo/perf: optimize !!
2583
2591
  // !=
2584
2592
  return falsy(scope, generate(scope, decl.argument), getNodeType(scope, decl.argument), false, false);
2585
2593
 
@@ -3839,7 +3847,7 @@ const internalConstrs = {
3839
3847
  generate: (scope, decl) => {
3840
3848
  // todo: boolean object when used as constructor
3841
3849
  const arg = decl.arguments[0] ?? DEFAULT_VALUE;
3842
- return truthy(scope, generate(scope, arg), getNodeType(scope, arg), false, false, 'full');
3850
+ return truthy(scope, generate(scope, arg), getNodeType(scope, arg));
3843
3851
  },
3844
3852
  type: TYPES.boolean,
3845
3853
  length: 1