porffor 0.28.5 → 0.28.7

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/compiler/2c.js CHANGED
@@ -335,10 +335,15 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
335
335
  // this just does local.get via the variable name
336
336
  // nice but does not get the value at this moment
337
337
  // so breaks some wasm principles :(
338
- vals.push(`${invLocals[i[1]]}`);
338
+ vals.push(`${invLocals[idx]}`);
339
339
  } else {
340
340
  const id = localTmpId++;
341
- line(`const ${invLocalTypes[idx]} _get${id} = ${invLocals[idx]}`);
341
+
342
+ const line2 = out.indexOf('{\n') + 2;
343
+ out = out.slice(0, line2) + ` ${invLocalTypes[idx]} _get${id};\n` + out.slice(line2);
344
+ // line(`const ${invLocalTypes[idx]} _get${id} = ${invLocals[idx]}`);
345
+
346
+ line(`_get${id} = ${invLocals[idx]}`);
342
347
  vals.push(`_get${id}`);
343
348
  }
344
349
  };
@@ -823,7 +828,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
823
828
  lastCond = false;
824
829
  }
825
830
 
826
- if (vals.length === 1 && returns) {
831
+ if (vals.length === 1 && f.returns.length === 1) {
827
832
  line(`return ${vals.pop()}`);
828
833
  }
829
834
 
@@ -170,14 +170,18 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
170
170
  const func = funcs[i];
171
171
  let name = func.name;
172
172
 
173
+ // real argc
173
174
  let argc = func.params.length;
174
175
  if (func.constr) argc -= 4;
175
176
  if (!func.internal || func.typedParams) argc = Math.floor(argc / 2);
177
+ bytes.push(argc % 256, (argc / 256 | 0) % 256);
176
178
 
177
- // hack: argc-- for prototype methods to remove _this hack from count
178
- if (name.includes('_prototype_')) argc--;
179
+ // userland exposed .length
180
+ let length = argc;
181
+ // remove _this from internal prototype funcs
182
+ if (func.internal && name.includes('_prototype_')) length--;
179
183
 
180
- bytes.push(argc % 256, (argc / 256 | 0) % 256);
184
+ bytes.push(length % 256, (length / 256 | 0) % 256);
181
185
 
182
186
  let flags = 0b00000000; // 8 flag bits
183
187
  if (func.returnType != null) flags |= 0b01;
@@ -189,7 +193,7 @@ export default (funcs, globals, tags, pages, data, flags, noTreeshake = false) =
189
193
 
190
194
  bytes.push(...new Uint8Array(new Int32Array([ name.length ]).buffer));
191
195
 
192
- for (let i = 0; i < (128 - 3 - 4); i++) {
196
+ for (let i = 0; i < (128 - 5 - 4); i++) {
193
197
  const c = name.charCodeAt(i);
194
198
  bytes.push((c || 0) % 256);
195
199
  }
@@ -1,10 +1,32 @@
1
1
  import type {} from './porffor.d.ts';
2
2
 
3
- export const Number = function (argument: any): any {
4
- // todo: actually do prim objects
5
- new.target; // trick compiler into allowing as constructor
3
+ // 21.1.1.1 Number (value)
4
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number-constructor-number-value
5
+ export const Number = function (value: any): any {
6
+ let n: number = 0;
7
+
8
+ // 1. If value is present, then
9
+ // todo: handle undefined (NaN) and not present (0) args differently
10
+ if (Porffor.rawType(value) != Porffor.TYPES.undefined) {
11
+ // a. Let prim be ? ToNumeric(value).
12
+ // b. If prim is a BigInt, let n be 𝔽(ℝ(prim)).
13
+ // todo: handle when bigints exist
14
+ // c. Otherwise, let n be prim.
15
+ n = ecma262.ToNumeric(value);
16
+ }
17
+
18
+ // 2. Else,
19
+ // a. Let n be +0𝔽.
20
+ // n is already 0 (from init value)
6
21
 
7
- return ecma262.ToNumeric(argument);
22
+ // 3. If NewTarget is undefined, return n.
23
+ if (!new.target) return n;
24
+
25
+ // 4. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%Number.prototype%", « [[NumberData]] »).
26
+ // 5. Set O.[[NumberData]] to n.
27
+ // 6. Return O.
28
+ // todo: actual prim objects
29
+ return n;
8
30
  };
9
31
 
10
32
  // radix: number|any for rawType check
@@ -60,6 +60,8 @@ export const __ecma262_StringToNumber = (str: unknown): number => {
60
60
  // trim whitespace
61
61
  str = str.trim();
62
62
 
63
+ if (str.length == 0) return 0;
64
+
63
65
  // check 0x, 0o, 0b prefixes
64
66
  const first: i32 = str.charCodeAt(0);
65
67
  const second: i32 = str.charCodeAt(1);
@@ -1,15 +1,22 @@
1
1
  import type {} from './porffor.d.ts';
2
2
 
3
- export const __WeakMap_prototype_has = (_this: WeakMap, key: any) => __Map_prototype_has(_this, key);
3
+ export const __WeakMap_prototype_has = (_this: WeakMap, key: any) => {
4
+ const map: Map = _this;
5
+ return __Map_prototype_has(map, key);
6
+ };
4
7
 
5
8
  export const __WeakMap_prototype_set = (_this: WeakMap, key: any, value: any) => {
6
9
  if (!Porffor.object.isObjectOrSymbol(key)) throw new TypeError('Value in WeakSet needs to be an object or symbol');
7
10
 
8
- __Map_prototype_set(_this, key, value);
11
+ const map: Map = _this;
12
+ __Map_prototype_set(map, key, value);
9
13
  return _this;
10
14
  };
11
15
 
12
- export const __WeakMap_prototype_delete = (_this: WeakMap, key: any) => __Map_prototype_delete(_this, key);
16
+ export const __WeakMap_prototype_delete = (_this: WeakMap, key: any) => {
17
+ const map: Map = _this;
18
+ return __Map_prototype_delete(map, key);
19
+ };
13
20
 
14
21
  export const WeakMap = function (iterable: any): WeakMap {
15
22
  if (!new.target) throw new TypeError("Constructor WeakMap requires 'new'");
@@ -1,15 +1,22 @@
1
1
  import type {} from './porffor.d.ts';
2
2
 
3
- export const __WeakSet_prototype_has = (_this: WeakSet, value: any) => __Set_prototype_has(_this, value);
3
+ export const __WeakSet_prototype_has = (_this: WeakSet, value: any) => {
4
+ const set: Set = _this;
5
+ return __Set_prototype_has(set, value);
6
+ };
4
7
 
5
8
  export const __WeakSet_prototype_add = (_this: WeakSet, value: any) => {
6
9
  if (!Porffor.object.isObjectOrSymbol(value)) throw new TypeError('Value in WeakSet needs to be an object or symbol');
7
10
 
8
- __Set_prototype_add(_this, value);
11
+ const set: Set = _this;
12
+ __Set_prototype_add(set, value);
9
13
  return _this;
10
14
  };
11
15
 
12
- export const __WeakSet_prototype_delete = (_this: WeakSet, value: any) => __Set_prototype_delete(_this, value);
16
+ export const __WeakSet_prototype_delete = (_this: WeakSet, value: any) => {
17
+ const set: Set = _this;
18
+ return __Set_prototype_delete(set, value);
19
+ };
13
20
 
14
21
  export const WeakSet = function (iterable: any): WeakSet {
15
22
  if (!new.target) throw new TypeError("Constructor WeakSet requires 'new'");
@@ -1052,7 +1052,7 @@ export const BuiltinFuncs = function() {
1052
1052
  [ Opcodes.local_get, 0 ],
1053
1053
  ...number(128, Valtype.i32),
1054
1054
  [ Opcodes.i32_mul ],
1055
- ...number(2, Valtype.i32),
1055
+ ...number(4, Valtype.i32),
1056
1056
  [ Opcodes.i32_add ],
1057
1057
  [ Opcodes.i32_load8_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut') * pageSize) ]
1058
1058
  ],
@@ -1067,6 +1067,8 @@ export const BuiltinFuncs = function() {
1067
1067
  [ Opcodes.local_get, 0 ],
1068
1068
  ...number(128, Valtype.i32),
1069
1069
  [ Opcodes.i32_mul ],
1070
+ ...number(2, Valtype.i32),
1071
+ [ Opcodes.i32_add ],
1070
1072
  [ Opcodes.i32_load16_u, 0, ...unsignedLEB128(allocPage(scope, 'func lut') * pageSize) ]
1071
1073
  ],
1072
1074
  table: true
@@ -1080,7 +1082,7 @@ export const BuiltinFuncs = function() {
1080
1082
  [ Opcodes.local_get, 0 ],
1081
1083
  ...number(128, Valtype.i32),
1082
1084
  [ Opcodes.i32_mul ],
1083
- ...number(3, Valtype.i32),
1085
+ ...number(5, Valtype.i32),
1084
1086
  [ Opcodes.i32_add ],
1085
1087
  ...number(allocPage(scope, 'func lut') * pageSize, Valtype.i32),
1086
1088
  [ Opcodes.i32_add ]