porffor 0.16.0-e5b6b94c8 → 0.16.0-f9dde1759

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/wrap.js CHANGED
@@ -6,8 +6,25 @@ import { TYPES } from './types.js';
6
6
  import { log } from './log.js';
7
7
  import Prefs from './prefs.js';
8
8
 
9
+ const fs = (typeof process?.version !== 'undefined' ? (await import('node:fs')) : undefined);
10
+
9
11
  const bold = x => `\u001b[1m${x}\u001b[0m`;
10
12
 
13
+ export const readByteStr = (memory, ptr) => {
14
+ const length = (new Int32Array(memory.buffer, ptr, 1))[0];
15
+ return Array.from(new Uint8Array(memory.buffer, ptr + 4, length)).map(x => String.fromCharCode(x)).join('');
16
+ };
17
+
18
+ export const writeByteStr = (memory, ptr, str) => {
19
+ const length = str.length;
20
+ (new Int32Array(memory.buffer, ptr, 1))[0] = length;
21
+
22
+ const arr = new Uint8Array(memory.buffer, ptr + 4, length);
23
+ for (let i = 0; i < length; i++) {
24
+ arr[i] = str.charCodeAt(i);
25
+ }
26
+ };
27
+
11
28
  const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
12
29
  switch (type) {
13
30
  case TYPES.boolean: return Boolean(value);
@@ -101,28 +118,31 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
101
118
  }
102
119
  };
103
120
 
104
- export default async (source, flags = [ 'module' ], customImports = {}, print = str => process.stdout.write(str)) => {
121
+ export default (source, flags = [ 'module' ], customImports = {}, print = str => process.stdout.write(str)) => {
105
122
  const times = [];
106
123
 
107
124
  const t1 = performance.now();
108
- const { wasm, funcs, globals, tags, exceptions, pages, c } = compile(source, flags);
125
+ const { wasm, funcs, globals, tags, exceptions, pages, c } = typeof source === 'object' ? source : compile(source, flags);
109
126
 
110
127
  globalThis.porfDebugInfo = { funcs, globals };
111
128
 
112
- if (source.includes('export function')) flags.push('module');
129
+ if (source.includes?.('export ')) flags.push('module');
113
130
 
114
- // (await import('node:fs')).writeFileSync('out.wasm', Buffer.from(wasm));
131
+ // fs.writeFileSync('out.wasm', Buffer.from(wasm));
115
132
 
116
133
  times.push(performance.now() - t1);
117
134
  if (Prefs.profileCompiler) console.log(bold(`compiled in ${times[0].toFixed(2)}ms`));
118
135
 
119
136
  const backtrace = (funcInd, blobOffset) => {
120
- if (funcInd == null || blobOffset == null) return false;
137
+ if (funcInd == null || blobOffset == null ||
138
+ Number.isNaN(funcInd) || Number.isNaN(blobOffset)) return false;
121
139
 
122
140
  // convert blob offset -> function wasm offset.
123
141
  // this is not good code and is somewhat duplicated
124
142
  // I just want it to work for debugging, I don't care about perf/yes
125
143
  const func = funcs.find(x => x.index === funcInd);
144
+ if (!func) return false;
145
+
126
146
  const locals = Object.values(func.locals).sort((a, b) => a.idx - b.idx).slice(func.params.length).sort((a, b) => a.idx - b.idx);
127
147
 
128
148
  let localDecl = [], typeCount = 0, lastType;
@@ -197,13 +217,15 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
197
217
 
198
218
  let instance;
199
219
  try {
200
- let wasmEngine = WebAssembly;
201
- if (Prefs.asur) {
202
- log.warning('wrap', 'using our !experimental! asur wasm engine instead of host to run');
203
- wasmEngine = await import('../asur/index.js');
204
- }
205
-
206
- 0, { instance } = await wasmEngine.instantiate(wasm, {
220
+ // let wasmEngine = WebAssembly;
221
+ // if (Prefs.asur) {
222
+ // log.warning('wrap', 'using our !experimental! asur wasm engine instead of host to run');
223
+ // wasmEngine = await import('../asur/index.js');
224
+ // }
225
+
226
+ // 0, { instance } = await wasmEngine.instantiate(wasm, {
227
+ const module = new WebAssembly.Module(wasm);
228
+ instance = new WebAssembly.Instance(module, {
207
229
  '': {
208
230
  p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
209
231
  c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
@@ -211,12 +233,31 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
211
233
  u: () => performance.timeOrigin,
212
234
  y: () => {},
213
235
  z: () => {},
236
+ w: (ind, outPtr) => { // readArgv
237
+ const args = process.argv.slice(2).filter(x => !x.startsWith('-'));
238
+ const str = args[ind];
239
+ if (!str) return -1;
240
+
241
+ writeByteStr(memory, outPtr, str);
242
+ return str.length;
243
+ },
244
+ q: (pathPtr, outPtr) => { // readFile
245
+ try {
246
+ const path = readByteStr(memory, pathPtr);
247
+ const contents = fs.readFileSync(path, 'utf8');
248
+ writeByteStr(memory, outPtr, contents);
249
+ return contents.length;
250
+ } catch {
251
+ return -1;
252
+ }
253
+ },
214
254
  ...customImports
215
255
  }
216
256
  });
217
257
  } catch (e) {
218
258
  // only backtrace for runner, not test262/etc
219
259
  if (!process.argv[1].includes('/runner')) throw e;
260
+ if (!(e instanceof WebAssembly.CompileError)) throw e;
220
261
 
221
262
  const funcInd = parseInt(e.message.match(/function #([0-9]+) /)?.[1]);
222
263
  const blobOffset = parseInt(e.message.split('@')?.[1]);