porffor 0.49.7 → 0.49.8

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
@@ -168,7 +168,8 @@ const removeBrackets = str => {
168
168
 
169
169
  for (const x in CValtype) {
170
170
  const p = `(${x})`;
171
- if (str.startsWith(p)) return p + removeBrackets(str.slice(p.length));
171
+ // if (str.startsWith(p)) return p + removeBrackets(str.slice(p.length));
172
+ if (str.startsWith(p)) return str;
172
173
  }
173
174
 
174
175
  return str.startsWith('(') && str.endsWith(')') && !str.startsWith('(*') ? str.slice(1, -1) : str;
@@ -192,7 +193,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
192
193
  return out;
193
194
  };
194
195
  const sanitize = str => {
195
- if (str === 'char') return '_' + str;
196
+ if (str === 'char' || str === 'main') return '_' + str;
196
197
 
197
198
  return str.replace(/[^0-9a-zA-Z_]/g, _ => codeToSanitizedStr(_.charCodeAt(0)));
198
199
  };
@@ -215,6 +216,7 @@ export default ({ funcs, globals, tags, data, exceptions, pages }) => {
215
216
  }
216
217
 
217
218
  if (pages.size > 0) {
219
+ includes.set('stdlib.h', true);
218
220
  prepend.set('_memory', `char* _memory; u32 _memoryPages = ${pages.size};\n`);
219
221
  prependMain.set('_initMemory', `_memory = malloc(_memoryPages * ${pageSize});\n`);
220
222
  if (Prefs['2cMemcpy']) includes.set('string.h', true);
@@ -719,7 +721,8 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
719
721
  break;
720
722
 
721
723
  case Opcodes.call_indirect:
722
- // stub, todo
724
+ // todo: stub
725
+ if (Prefs.d) log.warning('2c', `unimplemented op: ${invOpcodes[i[0]]} \x1b[90m(${f.name})`);
723
726
  vals.pop();
724
727
  vals.push('0', '0');
725
728
  break;
@@ -871,7 +874,7 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
871
874
  break;
872
875
  }
873
876
 
874
- if (Prefs.d) log.warning('2c', `unimplemented op: ${invOpcodes[i[0]]} \x1b[90m(${f.name})`);
877
+ if (Prefs.d) log.warning('2c', `unimplemented op: ${invOpcodes[i[0]] ?? invOpcodes[i[0] + ',' + i[1]]} \x1b[90m(${f.name})`);
875
878
  }
876
879
 
877
880
  lastCond = false;
@@ -259,7 +259,7 @@ export const __Porffor_print = (arg: any, colors: boolean = true, depth: number
259
259
 
260
260
  const buffer = new Uint8Array(arg);
261
261
  const bufferLen = buffer.length - 1;
262
- for (let i = 0; i <= bufferLen; i++) {
262
+ for (let i: i32 = 0; i <= bufferLen; i++) {
263
263
  const ele = buffer[i];
264
264
  __Porffor_printHexDigit((ele & 0xF0) / 16);
265
265
  __Porffor_printHexDigit(ele & 0xF);
@@ -340,7 +340,7 @@ export const __Porffor_print = (arg: any, colors: boolean = true, depth: number
340
340
 
341
341
  let tabLevel = 0;
342
342
  export const __Porffor_consoleIndent = () => {
343
- for (let i = 0; i < tabLevel; i++) {
343
+ for (let i: i32 = 0; i < tabLevel; i++) {
344
344
  Porffor.printStatic('\t');
345
345
  }
346
346
  };
@@ -376,7 +376,7 @@ export const __console_groupEnd = () => {
376
376
 
377
377
  export const __console_log = (...args: any[]) => {
378
378
  const argLen: i32 = args.length - 1;
379
- for (let i = 0; i <= argLen; i++) {
379
+ for (let i: i32 = 0; i <= argLen; i++) {
380
380
  __Porffor_consoleIndent();
381
381
  __Porffor_consolePrint(args[i]);
382
382
 
@@ -388,7 +388,7 @@ export const __console_log = (...args: any[]) => {
388
388
 
389
389
  export const __console_debug = (...args: any[]) => {
390
390
  const argLen: i32 = args.length - 1;
391
- for (let i = 0; i <= argLen; i++) {
391
+ for (let i: i32 = 0; i <= argLen; i++) {
392
392
  __Porffor_consoleIndent();
393
393
  __Porffor_consolePrint(args[i]);
394
394
 
@@ -400,7 +400,7 @@ export const __console_debug = (...args: any[]) => {
400
400
 
401
401
  export const __console_info = (...args: any[]) => {
402
402
  const argLen: i32 = args.length - 1;
403
- for (let i = 0; i <= argLen; i++) {
403
+ for (let i: i32 = 0; i <= argLen; i++) {
404
404
  __Porffor_consoleIndent();
405
405
  __Porffor_consolePrint(args[i]);
406
406
 
@@ -412,7 +412,7 @@ export const __console_info = (...args: any[]) => {
412
412
 
413
413
  export const __console_warn = (...args: any[]) => {
414
414
  const argLen: i32 = args.length - 1;
415
- for (let i = 0; i <= argLen; i++) {
415
+ for (let i: i32 = 0; i <= argLen; i++) {
416
416
  __Porffor_consoleIndent();
417
417
  __Porffor_consolePrint(args[i]);
418
418
 
@@ -424,7 +424,7 @@ export const __console_warn = (...args: any[]) => {
424
424
 
425
425
  export const __console_error = (...args: any[]) => {
426
426
  const argLen: i32 = args.length - 1;
427
- for (let i = 0; i <= argLen; i++) {
427
+ for (let i: i32 = 0; i <= argLen; i++) {
428
428
  __Porffor_consoleIndent();
429
429
  __Porffor_consolePrint(args[i]);
430
430
 
@@ -444,7 +444,7 @@ export const __console_assert = (assertion: any, ...args: any[]) => {
444
444
  }
445
445
 
446
446
  const argLen: i32 = args.length - 1;
447
- for (let i = 0; i <= argLen; i++) {
447
+ for (let i: i32 = 0; i <= argLen; i++) {
448
448
  __Porffor_consolePrint(args[i]);
449
449
  if (i != argLen) Porffor.printStatic(' ');
450
450
  }
@@ -684,7 +684,7 @@ export const __Date_parse = (string: bytestring): number => {
684
684
  // 'Sun May 12 2024 02:44:13 GMT+0000 (UTC)'
685
685
 
686
686
  // if first char is numerical, use DTSF parser
687
- const chr: i32 = Porffor.wasm.i32.load8_u(string, 0, 4);;
687
+ const chr: i32 = Porffor.wasm.i32.load8_u(string, 0, 4);
688
688
  if (Porffor.fastAnd(chr >= 48, chr <= 57)) { // 0-9
689
689
  return __ecma262_ParseDTSF(string);
690
690
  }
@@ -226,17 +226,22 @@ export const __Math_pow = (base: number, exponent: number): number => {
226
226
  if (base < 0) if (!Number.isInteger(exponent)) return NaN;
227
227
 
228
228
  // 13. Return an implementation-approximated Number value representing the result of raising ℝ(base) to the ℝ(exponent) power.
229
+ if (base == Math.E) {
230
+ return Math.exp(exponent);
231
+ }
232
+
229
233
  let currentBase: number = base;
230
234
  let currentExponent: number = Math.abs(exponent);
231
235
 
232
236
  let result: number = 1;
233
237
  while (currentExponent > 0) {
234
238
  if (currentExponent >= 1) {
235
- if (currentExponent % 2 != 0) {
239
+ if (currentExponent & 1) {
236
240
  result *= currentBase;
237
241
  }
242
+
238
243
  currentBase *= currentBase;
239
- currentExponent = Math.floor(currentExponent / 2);
244
+ currentExponent >>= 1;
240
245
  } else {
241
246
  // Handle fractional part
242
247
  result *= Math.exp(currentExponent * Math.log(Math.abs(currentBase)));
@@ -1078,13 +1078,13 @@ export const __ByteString_prototype_trim = (_this: bytestring) => {
1078
1078
  };
1079
1079
 
1080
1080
 
1081
- export const __String_prototype_concat = (_this: string, ...vals: any[]) => {
1082
- let out = Porffor.allocate();
1081
+ export const __String_prototype_concat = (_this: any, ...vals: any[]) => {
1082
+ let out: any = Porffor.allocate();
1083
1083
  Porffor.clone(_this, out);
1084
1084
 
1085
- // override type to string
1085
+ // copy _this type to out
1086
1086
  Porffor.wasm`
1087
- i32.const 67 ;; string
1087
+ local.get ${_this+1}
1088
1088
  local.set ${out+1}`;
1089
1089
 
1090
1090
  const valsLen: i32 = vals.length;
@@ -1117,13 +1117,13 @@ local.set ${out}`;
1117
1117
  return out;
1118
1118
  };
1119
1119
 
1120
- export const __ByteString_prototype_concat = (_this: bytestring, ...vals: any[]) => {
1121
- let out = Porffor.allocate();
1120
+ export const __ByteString_prototype_concat = (_this: any, ...vals: any[]) => {
1121
+ let out: any = Porffor.allocate();
1122
1122
  Porffor.clone(_this, out);
1123
1123
 
1124
- // override type to bytestring
1124
+ // copy _this type to out
1125
1125
  Porffor.wasm`
1126
- i32.const 195 ;; bytestring
1126
+ local.get ${_this+1}
1127
1127
  local.set ${out+1}`;
1128
1128
 
1129
1129
  const valsLen: i32 = vals.length;