porffor 0.19.13 → 0.19.14

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/opt.js CHANGED
@@ -426,22 +426,6 @@ export default (funcs, globals, pages, tags, exceptions) => {
426
426
  i--;
427
427
  continue;
428
428
  }
429
-
430
- if (i < 2) continue;
431
- const lastLastInst = wasm[i - 2];
432
-
433
- if (lastLastInst[1] === inst[1] && inst[0] === Opcodes.local_get && lastInst[0] === Opcodes.local_tee && lastLastInst[0] === Opcodes.local_set) {
434
- // local.set x
435
- // local.tee y
436
- // local.get x
437
- // -->
438
- // <nothing>
439
-
440
- wasm.splice(i - 2, 3); // remove this, last, 2nd last insts
441
- if (Prefs.optLog) log('opt', `removed redundant inline param local handling`);
442
- i -= 3;
443
- continue;
444
- }
445
429
  }
446
430
 
447
431
  if (optLevel < 2) continue;
@@ -1,6 +1,9 @@
1
1
  import { Opcodes } from './wasmSpec.js';
2
2
  import { TYPES } from './types.js';
3
3
 
4
+ import process from 'node:process';
5
+ globalThis.process = process;
6
+
4
7
  import fs from 'node:fs';
5
8
  import { join } from 'node:path';
6
9
 
@@ -32,6 +35,11 @@ const compile = async (file, _funcs) => {
32
35
 
33
36
  const allocated = new Set();
34
37
 
38
+ const invGlobals = Object.keys(globals).reduce((acc, y) => {
39
+ acc[globals[y].idx] = { ...globals[y], name: y };
40
+ return acc;
41
+ }, {});
42
+
35
43
  const main = funcs.find(x => x.name === 'main');
36
44
  const exports = funcs.filter(x => x.export && x.name !== 'main');
37
45
  for (const x of exports) {
@@ -50,12 +58,12 @@ const compile = async (file, _funcs) => {
50
58
  }).filter(x => x);
51
59
  }
52
60
 
53
- const locals = Object.keys(x.locals).reduce((acc, y) => {
54
- acc[x.locals[y].idx] = { ...x.locals[y], name: y };
55
- return acc;
56
- }, {});
57
-
58
61
  const rewriteWasm = (x, wasm, rewriteLocals = false) => {
62
+ const locals = Object.keys(x.locals).reduce((acc, y) => {
63
+ acc[x.locals[y].idx] = { ...x.locals[y], name: y };
64
+ return acc;
65
+ }, {});
66
+
59
67
  for (let i = 0; i < wasm.length; i++) {
60
68
  const y = wasm[i];
61
69
  const n = wasm[i + 1];
@@ -67,10 +75,8 @@ const compile = async (file, _funcs) => {
67
75
  }
68
76
 
69
77
  if (y[0] === Opcodes.global_get || y[0] === Opcodes.global_set) {
70
- const global = Object.values(globals).findIndex(x => x.idx === y[1]);
71
- const name = Object.keys(globals)[global];
72
- const type = globals[name].type;
73
- y.splice(0, 10, 'global', y[0], name, type);
78
+ const global = invGlobals[y[1]];
79
+ y.splice(0, 10, 'global', y[0], global.name, global.type);
74
80
 
75
81
  if (!x.globalInits) {
76
82
  if (!main.rewrittenGlobalInits) {
@@ -85,11 +91,9 @@ const compile = async (file, _funcs) => {
85
91
  }
86
92
  }
87
93
 
88
- if (rewriteLocals && (y[0] === Opcodes.local_get || y[0] === Opcodes.local_set || y[0] === Opcodes.local_tee)) {
89
- const local = Object.values(x.locals).findIndex(x => x.idx === y[1]);
90
- const name = Object.keys(x.locals)[local];
91
- const type = x.locals[name].type;
92
- y.splice(1, 10, 'local', name, type);
94
+ if (rewriteLocals && typeof y[1] === 'number' && (y[0] === Opcodes.local_get || y[0] === Opcodes.local_set || y[0] === Opcodes.local_tee)) {
95
+ const local = locals[y[1]];
96
+ y.splice(1, 10, 'local', local.name, local.type);
93
97
  }
94
98
 
95
99
  if (y[0] === Opcodes.const && (n[0] === Opcodes.local_set || n[0] === Opcodes.local_tee)) {
@@ -105,6 +109,20 @@ const compile = async (file, _funcs) => {
105
109
  y.splice(0, 10, 'alloc', pageName, x.pages.get(pageName).type, valtypeBinary);
106
110
  }
107
111
 
112
+ if (y[0] === Opcodes.const && n[0] === Opcodes.global_set) {
113
+ const l = invGlobals[n[1]];
114
+ if (!l) continue;
115
+ if (![TYPES.string, TYPES.array, TYPES.bytestring].includes(l.metadata?.type)) continue;
116
+ if (!x.pages) continue;
117
+
118
+ const pageName = [...x.pages.keys()].find(z => z.endsWith(l.name));
119
+ if (!pageName || allocated.has(pageName)) continue;
120
+ allocated.add(pageName);
121
+
122
+ y.splice(0, 10, 'alloc', pageName, x.pages.get(pageName).type, valtypeBinary);
123
+ }
124
+
125
+
108
126
  if (y[0] === Opcodes.i32_const && n[0] === Opcodes.throw) {
109
127
  const id = y[1];
110
128
  y.splice(0, 10, 'throw', exceptions[id].constructor, exceptions[id].message);
@@ -126,18 +144,30 @@ const precompile = async () => {
126
144
 
127
145
  const dir = join(__dirname, 'builtins');
128
146
 
147
+ const t = performance.now();
148
+
129
149
  let funcs = [];
150
+ let fileCount = 0;
130
151
  for (const file of fs.readdirSync(dir)) {
131
152
  if (file.endsWith('.d.ts')) continue;
153
+ fileCount++;
132
154
 
133
- console.log(`${' '.repeat(12)}${file}`);
155
+ globalThis.precompile = file;
134
156
 
135
157
  const t = performance.now();
136
- await compile(join(dir, file), funcs);
137
158
 
138
- console.log(`\u001b[A${' '.repeat(100)}\r\u001b[90m${`[${(performance.now() - t).toFixed(2)}ms]`.padEnd(12, ' ')}\u001b[0m\u001b[92m${file}\u001b[0m`);
159
+ try {
160
+ await compile(join(dir, file), funcs);
161
+ } catch (e) {
162
+ console.log(`\r${' '.repeat(100)}\r${' '.repeat(12)}${file}`);
163
+ throw e;
164
+ }
165
+
166
+ process.stdout.write(`\r${' '.repeat(100)}\r\u001b[90m${`[${(performance.now() - t).toFixed(2)}ms]`.padEnd(16, ' ')}\u001b[0m\u001b[92m${file}\u001b[0m`);
139
167
  }
140
168
 
169
+ console.log(`\r${' '.repeat(100)}\r\u001b[90m${`[${(performance.now() - t).toFixed(2)}ms]`.padEnd(16, ' ')}\u001b[0m\u001b[92mcompiled ${fileCount} files (${funcs.length} funcs)\u001b[0m`);
170
+
141
171
  return `// autogenerated by compiler/precompile.js
142
172
  import { number } from './embedding.js';
143
173
 
@@ -148,7 +178,7 @@ ${funcs.map(x => {
148
178
  .replace(/\["alloc","(.*?)","(.*?)",(.*?)\]/g, (_, reason, type, valtype) => `...number(allocPage(scope, '${reason}', '${type}') * pageSize, ${valtype})`)
149
179
  .replace(/\["global",(.*?),"(.*?)",(.*?)\]/g, (_, opcode, name, valtype) => `...glbl(${opcode}, '${name}', ${valtype})`)
150
180
  .replace(/\"local","(.*?)",(.*?)\]/g, (_, name, valtype) => `loc('${name}', ${valtype})]`)
151
- .replace(/\[16,"(.*?)"]/g, (_, name) => `[16, builtin('${name}')]`)
181
+ .replace(/\[16,"(.*?)"]/g, (_, name) => `[16, ...builtin('${name}')]`)
152
182
  .replace(/\["throw","(.*?)","(.*?)"\]/g, (_, constructor, message) => `...internalThrow(scope, '${constructor}', \`${message}\`)`);
153
183
 
154
184
  return `(scope, {${`${str.includes('allocPage(') ? 'allocPage,' : ''}${str.includes('glbl(') ? 'glbl,' : ''}${str.includes('loc(') ? 'loc,' : ''}${str.includes('builtin(') ? 'builtin,' : ''}${str.includes('internalThrow(') ? 'internalThrow,' : ''}`.slice(0, -1)}}) => ` + str;
package/compiler/wrap.js CHANGED
@@ -114,7 +114,7 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
114
114
  }
115
115
 
116
116
  case TYPES.symbol: {
117
- const page = pages.get('bytestring: __Porffor_symbol_descStore/ptr');
117
+ const page = pages.get('array: symbol.ts/descStore');
118
118
  if (!page) return Symbol();
119
119
 
120
120
  const descStore = page.ind * pageSize;
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.19.13+800b4f2dc",
4
+ "version": "0.19.14+65794cc8d",
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.19.13+800b4f2dc';
3
+ globalThis.version = '0.19.14+65794cc8d';
4
4
 
5
5
  // deno compat
6
6
  if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
@@ -1,19 +0,0 @@
1
- // @porf --valtype=i32
2
- import type {} from './porffor.d.ts';
3
-
4
- export const __String_prototype_trimLeft = (_this: string) => {
5
- return __String_prototype_trimStart(_this);
6
- };
7
-
8
- export const __ByteString_prototype_trimLeft = (_this: bytestring) => {
9
- return __ByteString_prototype_trimStart(_this);
10
- };
11
-
12
-
13
- export const __String_prototype_trimRight = (_this: string) => {
14
- return __String_prototype_trimEnd(_this);
15
- };
16
-
17
- export const __ByteString_prototype_trimRight = (_this: bytestring) => {
18
- return __ByteString_prototype_trimEnd(_this);
19
- };