porffor 0.60.1 → 0.60.3

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/AGENT.md ADDED
@@ -0,0 +1,20 @@
1
+ you are an assistant coder for Porffor - an early javascript and typescript to webassembly and native ahead-of-time compiler. the codebase is written in js and ts.
2
+
3
+ - built-ins apis (Date, String.prototype, etc) are written in ts inside `compiler/builtins`
4
+ - once you update a file inside that directory, you MUST run `./porf precompile` to compile to make your changes effective
5
+ - test your work using the test262 tools available, iterate independently but do not get stuck on one chain of thought or approach. paths for test262 tools should be relative to the 'test262/test' directory (e.g., 'built-ins/RegExp' NOT 'test262/test/built-ins/RegExp').
6
+ - to get an overview of the most failing directories, use the command: `node test262/fails.cjs`
7
+ - to just evaluate code in the engine, use the command: `./porf -p "..."`
8
+ - after finishing, check if your work/diff could be simplified
9
+ - always use single quotes (unless double is required)
10
+ - 2-space indentation, LF line endings, no trailing whitespace
11
+ - built-in APIs in `compiler/builtins/` written in TypeScript
12
+ - inline code in built-ins, avoid helper functions
13
+ - use semicolons
14
+ - do not use trailing commas
15
+ - use const/let, avoid var
16
+ - follow existing naming: camelCase for variables, PascalCase for types
17
+ - when possible, do not concat strings and instead write to them directly using wasm memory
18
+ - there are no closures, you cannot use locals between functions. use arguments or globals if really necessary
19
+ - The user prefers that prototype functions not have explicit return types in this codebase.
20
+ - The user prefers not to use global variables in built-in functions.
package/compiler/2c.js CHANGED
@@ -220,7 +220,6 @@ export default ({ funcs, globals, data, pages }) => {
220
220
  if (pages.size > 0) {
221
221
  includes.set('stdlib.h', true);
222
222
  prepend.set('_memory', `char* _memory; u32 _memoryPages = ${Math.ceil((pages.size * pageSize) / PageSize)};\n`);
223
- prependMain.set('_initMemory', `_memory = malloc(_memoryPages * ${PageSize});\n`);
224
223
  prependMain.set('_initMemory', `_memory = calloc(1, _memoryPages * ${PageSize});\n`);
225
224
  if (Prefs['2cMemcpy']) includes.set('string.h', true);
226
225
  }
@@ -613,6 +612,21 @@ export default ({ funcs, globals, data, pages }) => {
613
612
  let func = funcs.find(x => x.index === i[1]);
614
613
  if (!func) {
615
614
  const importFunc = importFuncs[i[1]];
615
+ if (Prefs['2cWasmImports']) {
616
+ const name = '__porf_import_' + importFunc.name;
617
+ if (!prepend.has(name)) {
618
+ prepend.set(name, `
619
+ __attribute__((import_module(""), import_name("${importFunc.import}")))
620
+ extern ${importFunc.returns.length > 0 ? CValtype[importFunc.returns[0]] : 'void'} ${name}(${importFunc.params.map(x => CValtype[x]).join(', ')});`);
621
+ }
622
+
623
+ const call = `${name}(${importFunc.params.length > 0 ? vals.pop() : ''})`;
624
+ if (importFunc.returns.length > 0) vals.push(call);
625
+ else line(call);
626
+
627
+ break;
628
+ }
629
+
616
630
  switch (importFunc.name) {
617
631
  case 'print':
618
632
  line(`printf("${valtype === 'f64' ? '%.15g' : '%i'}", ${vals.pop()})`);
@@ -808,6 +822,8 @@ export default ({ funcs, globals, data, pages }) => {
808
822
  // todo: allow catching
809
823
  const type = vals.pop();
810
824
  const val = vals.pop();
825
+ if (Prefs['2cWasmImports']) break;
826
+
811
827
  line(`printf("Uncaught ")`);
812
828
 
813
829
  // const id = tmpId++;
@@ -887,7 +903,7 @@ export default ({ funcs, globals, data, pages }) => {
887
903
  line(`const u32 _oldPages${id} = _memoryPages`);
888
904
  line(`_memoryPages += ${vals.pop()}`);
889
905
  line(`_memory = realloc(_memory, _memoryPages * ${PageSize})`);
890
- line(`memset(_memory + (_memoryPages - 1) * ${PageSize}, 0, ${PageSize})`);
906
+ line(`memset(_memory + _oldPages${id} * ${PageSize}, 0, (_memoryPages - _oldPages${id}) * ${PageSize})`);
891
907
  vals.push(`_oldPages${id}`);
892
908
  break;
893
909
  }
@@ -268,12 +268,33 @@ export const __Porffor_print = (arg: any, colors: boolean = true, depth: number
268
268
  Porffor.printStatic('WeakRef {}');
269
269
  return;
270
270
 
271
- // case Porffor.TYPES.regexp:
272
- // // todo: we currently have no way of getting the source text, so this falls back
273
-
274
- // default:
275
- // __Porffor_printString(arg.toString());
276
- // return;
271
+ case Porffor.TYPES.error:
272
+ __Porffor_printString(__Error_prototype_toString(arg));
273
+ return;
274
+ case Porffor.TYPES.aggregateerror:
275
+ __Porffor_printString(__AggregateError_prototype_toString(arg));
276
+ return;
277
+ case Porffor.TYPES.typeerror:
278
+ __Porffor_printString(__TypeError_prototype_toString(arg));
279
+ return;
280
+ case Porffor.TYPES.referenceerror:
281
+ __Porffor_printString(__ReferenceError_prototype_toString(arg));
282
+ return;
283
+ case Porffor.TYPES.syntaxerror:
284
+ __Porffor_printString(__SyntaxError_prototype_toString(arg));
285
+ return;
286
+ case Porffor.TYPES.rangeerror:
287
+ __Porffor_printString(__RangeError_prototype_toString(arg));
288
+ return;
289
+ case Porffor.TYPES.evalerror:
290
+ __Porffor_printString(__EvalError_prototype_toString(arg));
291
+ return;
292
+ case Porffor.TYPES.urierror:
293
+ __Porffor_printString(__URIError_prototype_toString(arg));
294
+ return;
295
+ case Porffor.TYPES.test262error:
296
+ __Porffor_printString(__Test262Error_prototype_toString(arg));
297
+ return;
277
298
  }
278
299
  };
279
300
 
@@ -326,7 +326,7 @@ export const __JSON_parse = (_: bytestring) => {
326
326
  }
327
327
 
328
328
  while (true) {
329
- arr.push(parseValue());
329
+ Porffor.array.fastPush(arr, parseValue());
330
330
  skipWhitespace();
331
331
  if (pos >= len) throw new SyntaxError('Unterminated array');
332
332