porffor 0.20.7 → 0.20.9

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.
@@ -1,4 +1,4 @@
1
- import { Opcodes } from './wasmSpec.js';
1
+ import { Opcodes, Valtype } from './wasmSpec.js';
2
2
  import { read_unsignedLEB128 } from './encoding.js';
3
3
  import { TYPES } from './types.js';
4
4
 
@@ -24,7 +24,7 @@ const compile = async (file, _funcs) => {
24
24
  first = source.slice(0, source.indexOf('\n'));
25
25
  }
26
26
 
27
- let args = ['--bytestring', '--todo-time=compile', '--truthy=no_nan_negative', '--no-treeshake-wasm-imports', '--no-rm-unused-types', '--scoped-page-names', '--funsafe-no-unlikely-proto-checks', '--fast-length', '--fast-object', '--parse-types', '--opt-types'];
27
+ let args = ['--bytestring', '--todo-time=compile', '--truthy=no_nan_negative', '--no-treeshake-wasm-imports', '--no-rm-unused-types', '--scoped-page-names', '--funsafe-no-unlikely-proto-checks', '--fast-length', '--parse-types', '--opt-types'];
28
28
  if (first.startsWith('// @porf')) {
29
29
  args = first.slice('// @porf '.length).split(' ').concat(args);
30
30
  }
@@ -41,6 +41,16 @@ const compile = async (file, _funcs) => {
41
41
  return acc;
42
42
  }, {});
43
43
 
44
+ const returnOverrides = {
45
+ __Porffor_object_get: [ Valtype.f64, Valtype.i32 ],
46
+ __Porffor_object_set: [ Valtype.f64, Valtype.i32 ]
47
+ };
48
+
49
+ const paramOverrides = {
50
+ __Porffor_object_set: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.f64, Valtype.i32 ],
51
+ __Porffor_object_define: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.f64, Valtype.i32, Valtype.i32, Valtype.i32 ],
52
+ };
53
+
44
54
  const main = funcs.find(x => x.name === 'main');
45
55
  const exports = funcs.filter(x => x.export && x.name !== 'main');
46
56
  for (const x of exports) {
@@ -59,6 +69,9 @@ const compile = async (file, _funcs) => {
59
69
  }).filter(x => x);
60
70
  }
61
71
 
72
+ if (returnOverrides[x.name]) x.returns = returnOverrides[x.name];
73
+ if (paramOverrides[x.name]) x.params = paramOverrides[x.name];
74
+
62
75
  const rewriteWasm = (x, wasm, rewriteLocals = false) => {
63
76
  const locals = Object.keys(x.locals).reduce((acc, y) => {
64
77
  acc[x.locals[y].idx] = { ...x.locals[y], name: y };
@@ -101,7 +114,7 @@ const compile = async (file, _funcs) => {
101
114
  if (y[0] === Opcodes.const && (n[0] === Opcodes.local_set || n[0] === Opcodes.local_tee)) {
102
115
  const l = locals[n[1]];
103
116
  if (!l) continue;
104
- if (![TYPES.string, TYPES.array, TYPES.bytestring].includes(l.metadata?.type)) continue;
117
+ if (!['#member_prop'].includes(l.name) && ![TYPES.string, TYPES.array, TYPES.bytestring].includes(l.metadata?.type)) continue;
105
118
  if (!x.pages) continue;
106
119
 
107
120
  const pageName = [...x.pages.keys()].find(z => z.endsWith(l.name));
package/compiler/types.js CHANGED
@@ -1,3 +1,5 @@
1
+ import Prefs from './prefs.js';
2
+
1
3
  export const TYPE_FLAGS = {
2
4
  parity: 0b10000000,
3
5
  length: 0b01000000,
@@ -68,4 +70,23 @@ registerInternalType('Float64Array', ['iterable', 'length']);
68
70
 
69
71
  registerInternalType('WeakRef');
70
72
  registerInternalType('WeakSet');
71
- registerInternalType('WeakMap');
73
+ registerInternalType('WeakMap');
74
+
75
+ if (Prefs.largestTypes) {
76
+ const typeKeys = Object.keys(TYPES);
77
+ const typeVals = Object.values(TYPES);
78
+
79
+ const largestType = (vals, keys) => {
80
+ const val = Math.max(...vals);
81
+ const key = keys[vals.indexOf(val)];
82
+ return [ val, key ];
83
+ };
84
+
85
+ const unflag = val => val & 0b00111111;
86
+
87
+ const logType = (label, val, key) => console.log(`${label} ${key} - ${val} (0x${val.toString(16)}, 0b${val.toString(2).padStart(8, '0')})`);
88
+
89
+ logType(`largest type: `, ...largestType(typeVals.map(unflag), typeKeys));
90
+ logType(`largest type w/ flags:`, ...largestType(typeVals, typeKeys));
91
+ console.log();
92
+ }
package/compiler/wrap.js CHANGED
@@ -45,24 +45,44 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
45
45
  case TYPES.object: {
46
46
  if (value === 0 || checkOOB(memory, value)) return null;
47
47
 
48
- const [ keysPtr, valsPtr ] = read(Uint32Array, memory, value, 2);
49
- if (checkOOB(memory, keysPtr) || checkOOB(memory, valsPtr)) return null;
50
-
51
- const size = read(Uint32Array, memory, keysPtr, 1);
48
+ const size = read(Uint32Array, memory, value, 1)[0];
52
49
 
53
50
  const out = {};
54
51
  for (let i = 0; i < size; i++) {
55
- const offset = 4 + (i * 9);
52
+ const offset = 4 + (i * 14);
56
53
 
57
- const kValue = read(Float64Array, memory, keysPtr + offset, 1)[0];
58
- const kType = read(Uint8Array, memory, keysPtr + offset + 8, 1)[0];
54
+ const kRaw = read(Uint32Array, memory, value + offset, 1)[0];
55
+ const kType = kRaw >>> 31 ? TYPES.string : TYPES.bytestring;
56
+ const kValue = kRaw & 0x7fffffff;
59
57
  const k = porfToJSValue({ memory, funcs, pages }, kValue, kType);
60
58
 
61
- const vValue = read(Float64Array, memory, valsPtr + offset, 1)[0];
62
- const vType = read(Uint8Array, memory, valsPtr + offset + 8, 1)[0];
59
+ const tail = read(Uint16Array, memory, value + offset + 12, 1)[0];
60
+
61
+ const vValue = read(Float64Array, memory, value + offset + 4, 1)[0];
62
+ const vType = tail >>> 8;
63
63
  const v = porfToJSValue({ memory, funcs, pages }, vValue, vType);
64
64
 
65
- out[k] = v;
65
+ const flags = tail & 0xff;
66
+
67
+ if (Prefs.d) {
68
+ console.log(`\x1b[4m\x1b[1m${k}\x1b[0m \x1B[90m(${TYPE_NAMES[kType]})\x1B[0m
69
+ value: \x1B[92m${v}\x1B[0m \x1B[90m(${TYPE_NAMES[vType]})\x1B[0m
70
+ flags: 0b\x1B[93m${flags.toString(2).padStart(4, '0')}\x1B[0m\x1B[90m
71
+ accessor: ${!!(flags & 0b0001)}
72
+ configurable: ${!!(flags & 0b0010)}
73
+ enumerable: ${!!(flags & 0b0100)}
74
+ writable: ${!!(flags & 0b1000)}
75
+ \x1B[0m`);
76
+ }
77
+
78
+ const configurable = flags & 0b0010;
79
+ const enumerable = flags & 0b0100;
80
+
81
+ Object.defineProperty(out, k, {
82
+ value: v,
83
+ configurable,
84
+ enumerable,
85
+ });
66
86
  }
67
87
 
68
88
  return out;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.20.7+2834584b7",
4
+ "version": "0.20.9+891a17fe5",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/runner/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import fs from 'node:fs';
3
- globalThis.version = '0.20.7+2834584b7';
3
+ globalThis.version = '0.20.9+891a17fe5';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {