porffor 0.60.0 → 0.60.1

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
@@ -221,6 +221,7 @@ export default ({ funcs, globals, data, pages }) => {
221
221
  includes.set('stdlib.h', true);
222
222
  prepend.set('_memory', `char* _memory; u32 _memoryPages = ${Math.ceil((pages.size * pageSize) / PageSize)};\n`);
223
223
  prependMain.set('_initMemory', `_memory = malloc(_memoryPages * ${PageSize});\n`);
224
+ prependMain.set('_initMemory', `_memory = calloc(1, _memoryPages * ${PageSize});\n`);
224
225
  if (Prefs['2cMemcpy']) includes.set('string.h', true);
225
226
  }
226
227
 
@@ -278,6 +279,9 @@ export default ({ funcs, globals, data, pages }) => {
278
279
  let ffiFuncs = {};
279
280
  const cified = new Set();
280
281
  const cify = f => {
282
+ if (cified.has(f.name)) return '';
283
+ cified.add(f.name);
284
+
281
285
  let out = '';
282
286
 
283
287
  let depth = 1;
@@ -457,6 +461,16 @@ export default ({ funcs, globals, data, pages }) => {
457
461
  case 0x03:
458
462
  vals.push(`(u32)(${vals.pop()})`);
459
463
  break;
464
+
465
+ // memory_copy
466
+ case 0x0a: {
467
+ const size = vals.pop();
468
+ const src = vals.pop();
469
+ const dst = vals.pop();
470
+ line(`memcpy(_memory + ${dst}, _memory + ${src}, ${size})`);
471
+ includes.set('string.h', true);
472
+ break;
473
+ }
460
474
  }
461
475
 
462
476
  lastCond = false;
@@ -609,26 +623,21 @@ export default ({ funcs, globals, data, pages }) => {
609
623
  includes.set('stdio.h', true);
610
624
  break;
611
625
 
612
- case 'time':
613
- line(`double _time_out`);
614
- platformSpecific(
615
- `LARGE_INTEGER _time_freq, _time_t;
616
- QueryPerformanceFrequency(&_time_freq);
617
- QueryPerformanceCounter(&_time_t);
618
- _time_out = ((double)_time_t.QuadPart / _time_freq.QuadPart) * 1000.;`,
619
- `struct timespec _time;
620
- clock_gettime(CLOCK_MONOTONIC, &_time);
621
- _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
622
- vals.push(`_time_out`);
623
-
624
- unixIncludes.set('time.h', true);
625
- winIncludes.set('windows.h', true);
626
+ case 'time': {
627
+ const id = tmpId++;
628
+ line(`time_t _time_t${id}`);
629
+ line(`time(&_time_t${id})`);
630
+ line(`f64 _time_out${id} = (f64)_time_t${id} * 1000.0`);
631
+ vals.push(`_time_out${id}`);
632
+
633
+ includes.set('time.h', true);
626
634
  break;
635
+ }
627
636
 
628
- case 'timeOrigin':
629
- // todo: actually implement
630
- vals.push('0');
637
+ case 'timeOrigin': {
638
+ vals.push('0.0');
631
639
  break;
640
+ }
632
641
 
633
642
  case '__Porffor_readArgv': {
634
643
  prepend.set('__Porffor_readArgv',
@@ -698,11 +707,8 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
698
707
  break;
699
708
  }
700
709
 
701
- if (!cified.has(func.name) && func.name !== f.name) {
702
- cified.add(func.name);
703
- if (!ffiFuncs[func.name]) {
704
- cify(func);
705
- }
710
+ if (!cified.has(func.name) && !ffiFuncs[func.name]) {
711
+ cify(func);
706
712
  }
707
713
 
708
714
  let args = [];
@@ -804,10 +810,10 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
804
810
  const val = vals.pop();
805
811
  line(`printf("Uncaught ")`);
806
812
 
807
- const id = tmpId++;
808
- line(`const struct ReturnValue _t${id} = __ecma262_ToString(${val}, ${type})`);
809
- line(`__Porffor_printString(_t${id}.value, _t${id}.type)`);
810
- line(`printf("\\n")`);
813
+ // const id = tmpId++;
814
+ // line(`const struct ReturnValue _t${id} = __ecma262_ToString(${val}, ${type})`);
815
+ // line(`__Porffor_printString(_t${id}.value, _t${id}.type)`);
816
+ // line(`printf("\\n")`);
811
817
  line(`exit(1)`);
812
818
 
813
819
  includes.set('stdio.h', true);
@@ -857,18 +863,31 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
857
863
  includes.set('math.h', true);
858
864
  break;
859
865
  case Opcodes.f64_trunc:
860
- vals.push(`(i32)(${removeBrackets(vals.pop())})`); // this is ~10x faster than math.h's trunc() with clang??
866
+ vals.push(`trunc(${vals.pop()})`);
867
+ includes.set('math.h', true);
861
868
  break;
862
869
  case Opcodes.f64_nearest:
863
870
  vals.push(`round(${vals.pop()})`);
864
871
  includes.set('math.h', true);
865
872
  break;
866
873
 
874
+ case Opcodes.i32_rotl: {
875
+ const shift = vals.pop();
876
+ const x = vals.pop();
877
+ const id = tmpId++;
878
+ line(`const i32 _x${id} = ${x}`);
879
+ line(`const i32 _shift${id} = ${shift}`);
880
+ line(`const i32 _out${id} = (_x${id} << _shift${id}) | (_x${id} >> (32 - _shift${id}))`);
881
+ vals.push(`_out${id}`);
882
+ break;
883
+ }
884
+
867
885
  case Opcodes.memory_grow: {
868
886
  const id = localTmpId++;
869
887
  line(`const u32 _oldPages${id} = _memoryPages`);
870
888
  line(`_memoryPages += ${vals.pop()}`);
871
889
  line(`_memory = realloc(_memory, _memoryPages * ${PageSize})`);
890
+ line(`memset(_memory + (_memoryPages - 1) * ${PageSize}, 0, ${PageSize})`);
872
891
  vals.push(`_oldPages${id}`);
873
892
  break;
874
893
  }
@@ -912,7 +931,9 @@ _time_out = _time.tv_nsec / 1000000. + _time.tv_sec * 1000.;`);
912
931
  globalThis.out = globalThis.out + out;
913
932
  };
914
933
 
915
- cify(funcs.find(x => x.name === '#main'));
934
+ for (const x of funcs) {
935
+ if (x.export || x.name === '#main') cify(x);
936
+ }
916
937
 
917
938
  const rawParams = f => {
918
939
  if (ffiFuncs[f.name]) return ffiFuncs[f.name].parameters;
@@ -427,15 +427,23 @@ export const __Math_asin = (x: number): number => {
427
427
  export const __Math_acos = (x: number): number => Math.asin(x) - Math.PI / 2;
428
428
 
429
429
  export const __Math_atan = (x: number): number => {
430
- if (x == Infinity) return Math.PI / 2
430
+ if (x == Infinity) return Math.PI / 2;
431
431
  if (x == -Infinity) return -Math.PI / 2;
432
+ if (x == 0) return x;
432
433
 
433
- // Taylor series
434
+ // atan(x) = π/2 - atan(1/x) for |x| > 1
435
+ if (Math.abs(x) > 1) {
436
+ const sign = x > 0 ? 1 : -1;
437
+ return sign * (Math.PI / 2 - __Math_atan(1 / Math.abs(x)));
438
+ }
439
+
440
+ // taylor series for |x| <= 1
434
441
  let sum: number = x;
435
442
  let term: number = x;
436
443
  let n: number = 1;
444
+ const maxIterations: number = 1000;
437
445
 
438
- while (Math.abs(term) > 1e-15) {
446
+ while (Math.abs(term) > 1e-15 && n < maxIterations) {
439
447
  term *= -x * x * (2 * n - 1) / ((2 * n) * (2 * n + 1));
440
448
  sum += term;
441
449
  n++;
@@ -448,7 +456,7 @@ export const __Math_atan2 = (y: number, x: number): number => {
448
456
  if (x == 0) {
449
457
  if (y > 0) return Math.PI / 2;
450
458
  if (y < 0) return -Math.PI / 2;
451
-
459
+ if (y == 0) return y;
452
460
  return NaN;
453
461
  }
454
462
 
@@ -2165,12 +2165,12 @@ params:[124,127],typedParams:1,returns:[124],returnType:1,jsLength:1,
2165
2165
  locals:[],localNames:["x","x#type"]
2166
2166
  }
2167
2167
  this.__Math_atan = {
2168
- wasm:(_,{builtin})=>eval("[[32,0],[68,\"Infinity\"],[97],[4,64],[68,3.141592653589793],[68,2],[163],[15],[26],[11],[32,0],[68,\"Infinity\"],[154],[97],[4,64],[68,3.141592653589793],[154],[68,2],[163],[15],[26],[11],[32,0],[33,2],[32,0],[33,3],[68,1],[33,4],[3,64],[32,3],[16,builtin('__Math_abs')],[68,1e-15],[100],[4,64],[32,3],[32,0],[154],[32,0],[162],[68,2],[32,4],[162],[68,1],[161],[162],[68,2],[32,4],[162],[68,2],[32,4],[162],[68,1],[160],[162],[163],[162],[33,3],[32,2],[32,3],[160],[33,2],[32,4],[68,1],[160],[33,4],[12,1],[11],[11],[32,2],[15]]"),
2168
+ wasm:(_,{builtin})=>eval("[[32,0],[68,\"Infinity\"],[97],[4,64],[68,3.141592653589793],[68,2],[163],[15],[26],[11],[32,0],[68,\"Infinity\"],[154],[97],[4,64],[68,3.141592653589793],[154],[68,2],[163],[15],[26],[11],[32,0],[68,0],[97],[4,64],[32,0],[15],[26],[11],[32,0],[16,builtin('__Math_abs')],[68,1],[100],[4,64],[32,0],[68,0],[100],[4,124],[68,1],[65,1],[33,4],[5],[68,-1],[65,1],[33,4],[11],[33,2],[32,4],[33,3],[32,2],[68,3.141592653589793],[68,2],[163],[68,1],[32,0],[16,builtin('__Math_abs')],[163],[65,1],[16,builtin('__Math_atan')],[161],[162],[15],[26],[11],[32,0],[33,5],[32,0],[33,6],[68,1],[33,7],[68,1000],[33,8],[3,64],[32,6],[16,builtin('__Math_abs')],[68,1e-15],[100],[34,9],[4,127],[32,7],[32,8],[99],[65,2],[33,4],[5],[32,9],[65,2],[33,4],[11],[4,64],[32,6],[32,0],[154],[32,0],[162],[68,2],[32,7],[162],[68,1],[161],[162],[68,2],[32,7],[162],[68,2],[32,7],[162],[68,1],[160],[162],[163],[162],[33,6],[32,5],[32,6],[160],[33,5],[32,7],[68,1],[160],[33,7],[12,1],[11],[11],[32,5],[15]]"),
2169
2169
  params:[124,127],typedParams:1,returns:[124],returnType:1,jsLength:1,
2170
- locals:[124,124,124],localNames:["x","x#type","sum","term","n"]
2170
+ locals:[124,127,127,124,124,124,124,127],localNames:["x","x#type","sign","sign#type","#last_type","sum","term","n","maxIterations","logictmpi"]
2171
2171
  }
2172
2172
  this.__Math_atan2 = {
2173
- wasm:(_,{builtin})=>eval("[[32,2],[68,0],[97],[4,64],[32,0],[68,0],[100],[4,64],[68,3.141592653589793],[68,2],[163],[15],[26],[11],[32,0],[68,0],[99],[4,64],[68,3.141592653589793],[154],[68,2],[163],[15],[26],[11],[68,\"NaN\"],[15],[26],[11],[32,0],[32,2],[163],[33,4],[65,1],[33,5],[32,2],[68,0],[100],[4,64],[32,4],[65,1],[16,builtin('__Math_atan')],[15],[26],[11],[32,0],[68,0],[102],[4,64],[32,4],[65,1],[16,builtin('__Math_atan')],[68,3.141592653589793],[160],[15],[26],[11],[32,4],[65,1],[16,builtin('__Math_atan')],[68,3.141592653589793],[161],[15]]"),
2173
+ wasm:(_,{builtin})=>eval("[[32,2],[68,0],[97],[4,64],[32,0],[68,0],[100],[4,64],[68,3.141592653589793],[68,2],[163],[15],[26],[11],[32,0],[68,0],[99],[4,64],[68,3.141592653589793],[154],[68,2],[163],[15],[26],[11],[32,0],[68,0],[97],[4,64],[32,0],[15],[26],[11],[68,\"NaN\"],[15],[26],[11],[32,0],[32,2],[163],[33,4],[65,1],[33,5],[32,2],[68,0],[100],[4,64],[32,4],[65,1],[16,builtin('__Math_atan')],[15],[26],[11],[32,0],[68,0],[102],[4,64],[32,4],[65,1],[16,builtin('__Math_atan')],[68,3.141592653589793],[160],[15],[26],[11],[32,4],[65,1],[16,builtin('__Math_atan')],[68,3.141592653589793],[161],[15]]"),
2174
2174
  params:[124,127,124,127],typedParams:1,returns:[124],returnType:1,jsLength:2,
2175
2175
  locals:[124,127],localNames:["y","y#type","x","x#type","ratio","ratio#type"]
2176
2176
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "An ahead-of-time JavaScript compiler",
4
- "version": "0.60.0",
4
+ "version": "0.60.1",
5
5
  "author": "Oliver Medhurst <honk@goose.icu>",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/runtime/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from 'node:fs';
3
- globalThis.version = '0.60.0';
3
+ globalThis.version = '0.60.1';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {