porffor 0.16.0-fe07da0f4 → 0.17.0-05070e1f0
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/CONTRIBUTING.md +2 -2
- package/README.md +5 -17
- package/compiler/2c.js +146 -81
- package/compiler/allocators.js +128 -0
- package/compiler/assemble.js +12 -5
- package/compiler/builtins/array.ts +94 -5
- package/compiler/builtins/base64.ts +28 -24
- package/compiler/builtins/date.ts +3 -30
- package/compiler/builtins/math.ts +6 -2
- package/compiler/builtins/number.ts +10 -21
- package/compiler/builtins/porffor.d.ts +10 -0
- package/compiler/builtins/set.ts +9 -14
- package/compiler/builtins/string_f64.ts +10 -0
- package/compiler/builtins/typedarray.js +42 -0
- package/compiler/builtins/z_ecma262.ts +62 -0
- package/compiler/builtins.js +51 -6
- package/compiler/codegen.js +824 -409
- package/compiler/cyclone.js +535 -0
- package/compiler/decompile.js +3 -1
- package/compiler/generated_builtins.js +420 -162
- package/compiler/havoc.js +93 -0
- package/compiler/index.js +104 -7
- package/compiler/opt.js +10 -44
- package/compiler/parse.js +2 -8
- package/compiler/pgo.js +220 -0
- package/compiler/precompile.js +12 -7
- package/compiler/prefs.js +8 -4
- package/compiler/prototype.js +34 -43
- package/compiler/types.js +31 -5
- package/compiler/wasmSpec.js +4 -2
- package/compiler/wrap.js +120 -21
- package/package.json +3 -5
- package/rhemyn/README.md +7 -4
- package/rhemyn/compile.js +138 -66
- package/runner/debug.js +1 -1
- package/runner/index.js +31 -14
- package/runner/{profiler.js → profile.js} +1 -1
- package/runner/repl.js +16 -11
package/compiler/prefs.js
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
const onByDefault = [ 'bytestring', 'treeshakeWasmImports', 'alwaysMemory', 'indirectCalls', 'optUnused' ];
|
1
|
+
const onByDefault = [ 'bytestring', 'treeshakeWasmImports', 'alwaysMemory', 'indirectCalls', 'optUnused', 'data', 'rmUnusedTypes' ];
|
2
2
|
|
3
3
|
let cache = {};
|
4
4
|
const obj = new Proxy({}, {
|
5
5
|
get(_, p) {
|
6
|
-
|
7
|
-
if (cache[p]) return cache[p];
|
6
|
+
if (cache[p] != null) return cache[p];
|
8
7
|
|
9
8
|
const ret = (() => {
|
10
9
|
// fooBar -> foo-bar
|
@@ -23,9 +22,14 @@ const obj = new Proxy({}, {
|
|
23
22
|
// do not cache in web demo as args are changed live
|
24
23
|
if (!globalThis.document) cache[p] = ret;
|
25
24
|
return ret;
|
25
|
+
},
|
26
|
+
|
27
|
+
set(_, p, v) {
|
28
|
+
cache[p] = v;
|
29
|
+
return true;
|
26
30
|
}
|
27
31
|
});
|
28
32
|
|
29
|
-
|
33
|
+
export const uncache = () => cache = {};
|
30
34
|
|
31
35
|
export default obj;
|
package/compiler/prototype.js
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { Opcodes, Blocktype, Valtype, ValtypeSize } from './wasmSpec.js';
|
2
2
|
import { number } from './embedding.js';
|
3
|
-
import { unsignedLEB128 } from './encoding.js';
|
4
3
|
import { UNDEFINED } from './builtins.js';
|
5
4
|
import { TYPES } from './types.js';
|
6
5
|
import Prefs from './prefs.js';
|
@@ -55,10 +54,10 @@ export const PrototypeFuncs = function() {
|
|
55
54
|
|
56
55
|
// read from memory
|
57
56
|
[ Opcodes.local_get, iTmp ],
|
58
|
-
[ Opcodes.load, 0,
|
57
|
+
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
59
58
|
|
60
59
|
[ Opcodes.local_get, iTmp ],
|
61
|
-
[ Opcodes.i32_load8_u, 0,
|
60
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
|
62
61
|
],
|
63
62
|
|
64
63
|
// todo: only for 1 argument
|
@@ -74,12 +73,12 @@ export const PrototypeFuncs = function() {
|
|
74
73
|
// store value
|
75
74
|
[ Opcodes.local_get, iTmp ],
|
76
75
|
...wNewMember,
|
77
|
-
[ Opcodes.store, 0,
|
76
|
+
[ Opcodes.store, 0, ValtypeSize.i32 ],
|
78
77
|
|
79
78
|
// store type
|
80
79
|
[ Opcodes.local_get, iTmp ],
|
81
80
|
...wType,
|
82
|
-
[ Opcodes.i32_store8, 0,
|
81
|
+
[ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
83
82
|
|
84
83
|
// bump array length by 1 and return it
|
85
84
|
...length.setI32([
|
@@ -137,10 +136,10 @@ export const PrototypeFuncs = function() {
|
|
137
136
|
[ Opcodes.local_set, iTmp ],
|
138
137
|
|
139
138
|
[ Opcodes.local_get, iTmp ],
|
140
|
-
[ Opcodes.load, 0,
|
139
|
+
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
141
140
|
|
142
141
|
[ Opcodes.local_get, iTmp ],
|
143
|
-
[ Opcodes.i32_load8_u, 0,
|
142
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
|
144
143
|
])
|
145
144
|
],
|
146
145
|
|
@@ -168,10 +167,10 @@ export const PrototypeFuncs = function() {
|
|
168
167
|
// load first element
|
169
168
|
// todo/perf: unusedValue opt
|
170
169
|
...pointer,
|
171
|
-
[ Opcodes.load, 0,
|
170
|
+
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
172
171
|
|
173
172
|
...pointer,
|
174
|
-
[ Opcodes.i32_load8_u, 0,
|
173
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
175
174
|
|
176
175
|
// offset page by -1 ind
|
177
176
|
// ...number(pointer + ValtypeSize.i32, Valtype.i32), // dst = base array index + length size
|
@@ -250,12 +249,12 @@ export const PrototypeFuncs = function() {
|
|
250
249
|
// store value
|
251
250
|
[ Opcodes.local_get, iTmp2 ],
|
252
251
|
[ Opcodes.local_get, iTmp ],
|
253
|
-
[ Opcodes.store, 0,
|
252
|
+
[ Opcodes.store, 0, ValtypeSize.i32 ],
|
254
253
|
|
255
254
|
// store type
|
256
255
|
[ Opcodes.local_get, iTmp2 ],
|
257
256
|
...wType,
|
258
|
-
[ Opcodes.i32_store8, 0,
|
257
|
+
[ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
259
258
|
|
260
259
|
// pointer - sizeof value
|
261
260
|
...length.getCachedI32(),
|
@@ -292,11 +291,8 @@ export const PrototypeFuncs = function() {
|
|
292
291
|
const [ newOut, newPointer ] = arrayShell(1, 'i16');
|
293
292
|
|
294
293
|
return [
|
295
|
-
// setup new/out array
|
294
|
+
// setup new/out array and use pointer for store
|
296
295
|
...newOut,
|
297
|
-
[ Opcodes.drop ],
|
298
|
-
|
299
|
-
...number(0, Valtype.i32), // base 0 for store later
|
300
296
|
|
301
297
|
...wIndex,
|
302
298
|
Opcodes.i32_to_u,
|
@@ -335,13 +331,14 @@ export const PrototypeFuncs = function() {
|
|
335
331
|
[ Opcodes.i32_add ],
|
336
332
|
|
337
333
|
// load current string ind {arg}
|
338
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1,
|
334
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
339
335
|
|
340
336
|
// store to new string ind 0
|
341
|
-
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1,
|
337
|
+
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
342
338
|
|
343
339
|
// return new string (pointer)
|
344
|
-
...
|
340
|
+
...newPointer,
|
341
|
+
Opcodes.i32_from_u
|
345
342
|
];
|
346
343
|
},
|
347
344
|
|
@@ -350,11 +347,8 @@ export const PrototypeFuncs = function() {
|
|
350
347
|
const [ newOut, newPointer ] = arrayShell(1, 'i16');
|
351
348
|
|
352
349
|
return [
|
353
|
-
// setup new/out array
|
350
|
+
// setup new/out array and use as pointer for store
|
354
351
|
...newOut,
|
355
|
-
[ Opcodes.drop ],
|
356
|
-
|
357
|
-
...number(0, Valtype.i32), // base 0 for store later
|
358
352
|
|
359
353
|
...wIndex,
|
360
354
|
Opcodes.i32_to,
|
@@ -366,13 +360,14 @@ export const PrototypeFuncs = function() {
|
|
366
360
|
[ Opcodes.i32_add ],
|
367
361
|
|
368
362
|
// load current string ind {arg}
|
369
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1,
|
363
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
370
364
|
|
371
365
|
// store to new string ind 0
|
372
|
-
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1,
|
366
|
+
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
373
367
|
|
374
368
|
// return new string (page)
|
375
|
-
...
|
369
|
+
...newPointer,
|
370
|
+
Opcodes.i32_from_u
|
376
371
|
];
|
377
372
|
},
|
378
373
|
|
@@ -411,7 +406,7 @@ export const PrototypeFuncs = function() {
|
|
411
406
|
[ Opcodes.i32_add ],
|
412
407
|
|
413
408
|
// load current string ind {arg}
|
414
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1,
|
409
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
415
410
|
Opcodes.i32_from_u
|
416
411
|
],
|
417
412
|
|
@@ -433,7 +428,7 @@ export const PrototypeFuncs = function() {
|
|
433
428
|
[ Opcodes.block, Blocktype.void ],
|
434
429
|
|
435
430
|
[ Opcodes.local_get, iTmp ],
|
436
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1,
|
431
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
437
432
|
[ Opcodes.local_set, iTmp2 ],
|
438
433
|
|
439
434
|
// if not surrogate, continue
|
@@ -455,7 +450,7 @@ export const PrototypeFuncs = function() {
|
|
455
450
|
|
456
451
|
// if not followed by trailing surrogate, return false
|
457
452
|
[ Opcodes.local_get, iTmp ],
|
458
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1,
|
453
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 + ValtypeSize.i16 ],
|
459
454
|
...number(0xFC00, Valtype.i32),
|
460
455
|
[ Opcodes.i32_and ],
|
461
456
|
...number(0xDC00, Valtype.i32),
|
@@ -507,11 +502,8 @@ export const PrototypeFuncs = function() {
|
|
507
502
|
const [ newOut, newPointer ] = arrayShell(1, 'i8');
|
508
503
|
|
509
504
|
return [
|
510
|
-
// setup new/out array
|
505
|
+
// setup new/out array and use pointer for store later
|
511
506
|
...newOut,
|
512
|
-
[ Opcodes.drop ],
|
513
|
-
|
514
|
-
...number(0, Valtype.i32), // base 0 for store later
|
515
507
|
|
516
508
|
...wIndex,
|
517
509
|
Opcodes.i32_to_u,
|
@@ -548,13 +540,14 @@ export const PrototypeFuncs = function() {
|
|
548
540
|
[ Opcodes.i32_add ],
|
549
541
|
|
550
542
|
// load current string ind {arg}
|
551
|
-
[ Opcodes.i32_load8_u, 0,
|
543
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
552
544
|
|
553
545
|
// store to new string ind 0
|
554
|
-
[ Opcodes.i32_store8, 0,
|
546
|
+
[ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
|
555
547
|
|
556
548
|
// return new string (pointer)
|
557
|
-
...
|
549
|
+
...newPointer,
|
550
|
+
Opcodes.i32_from_u
|
558
551
|
];
|
559
552
|
},
|
560
553
|
|
@@ -563,11 +556,8 @@ export const PrototypeFuncs = function() {
|
|
563
556
|
const [ newOut, newPointer ] = arrayShell(1, 'i8');
|
564
557
|
|
565
558
|
return [
|
566
|
-
// setup new/out array
|
559
|
+
// setup new/out array and use pointer for later
|
567
560
|
...newOut,
|
568
|
-
[ Opcodes.drop ],
|
569
|
-
|
570
|
-
...number(0, Valtype.i32), // base 0 for store later
|
571
561
|
|
572
562
|
...wIndex,
|
573
563
|
Opcodes.i32_to,
|
@@ -576,13 +566,14 @@ export const PrototypeFuncs = function() {
|
|
576
566
|
[ Opcodes.i32_add ],
|
577
567
|
|
578
568
|
// load current string ind {arg}
|
579
|
-
[ Opcodes.i32_load8_u, 0,
|
569
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
580
570
|
|
581
571
|
// store to new string ind 0
|
582
|
-
[ Opcodes.i32_store8, 0,
|
572
|
+
[ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
|
583
573
|
|
584
574
|
// return new string (page)
|
585
|
-
...
|
575
|
+
...newPointer,
|
576
|
+
Opcodes.i32_from_u
|
586
577
|
];
|
587
578
|
},
|
588
579
|
|
@@ -618,7 +609,7 @@ export const PrototypeFuncs = function() {
|
|
618
609
|
[ Opcodes.i32_add ],
|
619
610
|
|
620
611
|
// load current string ind {arg}
|
621
|
-
[ Opcodes.i32_load8_u, 0,
|
612
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
622
613
|
Opcodes.i32_from_u
|
623
614
|
],
|
624
615
|
|
package/compiler/types.js
CHANGED
@@ -20,10 +20,26 @@ export const TYPE_NAMES = {
|
|
20
20
|
[TYPES.bigint]: 'BigInt'
|
21
21
|
};
|
22
22
|
|
23
|
+
// flags
|
24
|
+
export const TYPE_FLAGS = {
|
25
|
+
// iterable: 0b10000000,
|
26
|
+
length: 0b01000000,
|
27
|
+
};
|
28
|
+
|
29
|
+
// TYPES.string |= TYPE_FLAGS.iterable;
|
30
|
+
TYPES.string |= TYPE_FLAGS.length;
|
31
|
+
|
32
|
+
export const typeHasFlag = (type, flag) => (type & flag) !== 0;
|
33
|
+
|
23
34
|
export const INTERNAL_TYPE_BASE = 0x10;
|
24
35
|
let internalTypeIndex = INTERNAL_TYPE_BASE;
|
25
|
-
const registerInternalType = name => {
|
26
|
-
|
36
|
+
const registerInternalType = (name, flags = []) => {
|
37
|
+
let n = internalTypeIndex++;
|
38
|
+
|
39
|
+
for (const x of flags) {
|
40
|
+
if (TYPE_FLAGS[x]) n |= TYPE_FLAGS[x];
|
41
|
+
}
|
42
|
+
|
27
43
|
TYPES[name.toLowerCase()] = n;
|
28
44
|
TYPE_NAMES[n] = name;
|
29
45
|
};
|
@@ -31,8 +47,18 @@ const registerInternalType = name => {
|
|
31
47
|
// note: when adding a new internal type, please also add a deserializer to wrap.js
|
32
48
|
// (it is okay to add a throw todo deserializer for wips)
|
33
49
|
|
34
|
-
registerInternalType('Array');
|
50
|
+
registerInternalType('Array', ['iterable', 'length']);
|
35
51
|
registerInternalType('RegExp');
|
36
|
-
registerInternalType('ByteString');
|
52
|
+
registerInternalType('ByteString', ['iterable', 'length']);
|
37
53
|
registerInternalType('Date');
|
38
|
-
registerInternalType('Set');
|
54
|
+
registerInternalType('Set', ['iterable']);
|
55
|
+
|
56
|
+
registerInternalType('Uint8Array', ['iterable', 'length']);
|
57
|
+
registerInternalType('Int8Array', ['iterable', 'length']);
|
58
|
+
registerInternalType('Uint8ClampedArray', ['iterable', 'length']);
|
59
|
+
registerInternalType('Uint16Array', ['iterable', 'length']);
|
60
|
+
registerInternalType('Int16Array', ['iterable', 'length']);
|
61
|
+
registerInternalType('Uint32Array', ['iterable', 'length']);
|
62
|
+
registerInternalType('Int32Array', ['iterable', 'length']);
|
63
|
+
registerInternalType('Float32Array', ['iterable', 'length']);
|
64
|
+
registerInternalType('Float64Array', ['iterable', 'length']);
|
package/compiler/wasmSpec.js
CHANGED
@@ -68,6 +68,7 @@ export const Opcodes = {
|
|
68
68
|
|
69
69
|
i32_load: 0x28,
|
70
70
|
i64_load: 0x29,
|
71
|
+
f32_load: 0x2a,
|
71
72
|
f64_load: 0x2b,
|
72
73
|
|
73
74
|
i32_load8_s: 0x2c,
|
@@ -82,6 +83,7 @@ export const Opcodes = {
|
|
82
83
|
|
83
84
|
i32_store: 0x36,
|
84
85
|
i64_store: 0x37,
|
86
|
+
f32_store: 0x38,
|
85
87
|
f64_store: 0x39,
|
86
88
|
|
87
89
|
i32_store8: 0x3a,
|
@@ -191,6 +193,8 @@ export const Opcodes = {
|
|
191
193
|
i32_trunc_sat_f64_s: [ 0xfc, 0x02 ],
|
192
194
|
i32_trunc_sat_f64_u: [ 0xfc, 0x03 ],
|
193
195
|
|
196
|
+
memory_init: [ 0xfc, 0x08 ],
|
197
|
+
data_drop: [ 0xfc, 0x09 ],
|
194
198
|
memory_copy: [ 0xfc, 0x0a ],
|
195
199
|
|
196
200
|
// simd insts are 0xFD simdop: varuint32
|
@@ -209,8 +213,6 @@ export const Opcodes = {
|
|
209
213
|
i32x4_add: [ 0xfd, 0xae, 0x01 ],
|
210
214
|
i32x4_sub: [ 0xfd, 0xb1, 0x01 ],
|
211
215
|
i32x4_mul: [ 0xfd, 0xb5, 0x01 ],
|
212
|
-
|
213
|
-
i32x4_dot_i16x8_s: [ 0xfd, 0xba, 0x01 ],
|
214
216
|
};
|
215
217
|
|
216
218
|
export const FuncType = 0x60;
|
package/compiler/wrap.js
CHANGED
@@ -2,12 +2,29 @@ import { encodeVector, encodeLocal } from './encoding.js';
|
|
2
2
|
import { importedFuncs } from './builtins.js';
|
3
3
|
import compile from './index.js';
|
4
4
|
import decompile from './decompile.js';
|
5
|
-
import { TYPES } from './types.js';
|
5
|
+
import { TYPES, TYPE_NAMES } 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);
|
@@ -87,42 +104,59 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
|
87
104
|
|
88
105
|
case TYPES.symbol: {
|
89
106
|
const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
|
107
|
+
if (!descStore) return Symbol();
|
108
|
+
|
90
109
|
const offset = descStore + 4 + ((value - 1) * 9);
|
91
110
|
|
92
111
|
const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
|
93
112
|
const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
|
94
113
|
|
95
114
|
const desc = porfToJSValue({ memory, funcs, pages }, v, t);
|
96
|
-
|
97
115
|
return Symbol(desc);
|
98
116
|
}
|
99
117
|
|
118
|
+
case TYPES.uint8array:
|
119
|
+
case TYPES.int8array:
|
120
|
+
case TYPES.uint8clampedarray:
|
121
|
+
case TYPES.uint16array:
|
122
|
+
case TYPES.int16array:
|
123
|
+
case TYPES.uint32array:
|
124
|
+
case TYPES.int32array:
|
125
|
+
case TYPES.float32array:
|
126
|
+
case TYPES.float64array: {
|
127
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
128
|
+
return new globalThis[TYPE_NAMES[type]](memory.buffer, value + 4, length);
|
129
|
+
}
|
130
|
+
|
100
131
|
default: return value;
|
101
132
|
}
|
102
133
|
};
|
103
134
|
|
104
|
-
export default
|
135
|
+
export default (source, flags = [ 'module' ], customImports = {}, print = str => process.stdout.write(str)) => {
|
105
136
|
const times = [];
|
106
137
|
|
107
138
|
const t1 = performance.now();
|
108
|
-
const { wasm, funcs, globals, tags, exceptions, pages, c } = compile(source, flags);
|
139
|
+
const { wasm, funcs, globals, tags, exceptions, pages, c } = typeof source === 'object' ? source : compile(source, flags);
|
109
140
|
|
110
141
|
globalThis.porfDebugInfo = { funcs, globals };
|
111
142
|
|
112
|
-
if (source.includes('export
|
143
|
+
// if (process.argv[1].includes('/runner') && source.includes?.('export ')) flags.push('module');
|
113
144
|
|
114
|
-
//
|
145
|
+
// fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
115
146
|
|
116
147
|
times.push(performance.now() - t1);
|
117
148
|
if (Prefs.profileCompiler) console.log(bold(`compiled in ${times[0].toFixed(2)}ms`));
|
118
149
|
|
119
150
|
const backtrace = (funcInd, blobOffset) => {
|
120
|
-
if (funcInd == null || blobOffset == null
|
151
|
+
if (funcInd == null || blobOffset == null ||
|
152
|
+
Number.isNaN(funcInd) || Number.isNaN(blobOffset)) return false;
|
121
153
|
|
122
154
|
// convert blob offset -> function wasm offset.
|
123
155
|
// this is not good code and is somewhat duplicated
|
124
156
|
// I just want it to work for debugging, I don't care about perf/yes
|
125
157
|
const func = funcs.find(x => x.index === funcInd);
|
158
|
+
if (!func) return false;
|
159
|
+
|
126
160
|
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
161
|
|
128
162
|
let localDecl = [], typeCount = 0, lastType;
|
@@ -197,13 +231,15 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
197
231
|
|
198
232
|
let instance;
|
199
233
|
try {
|
200
|
-
let wasmEngine = WebAssembly;
|
201
|
-
if (Prefs.asur) {
|
202
|
-
|
203
|
-
|
204
|
-
}
|
205
|
-
|
206
|
-
0, { instance } = await wasmEngine.instantiate(wasm, {
|
234
|
+
// let wasmEngine = WebAssembly;
|
235
|
+
// if (Prefs.asur) {
|
236
|
+
// log.warning('wrap', 'using our !experimental! asur wasm engine instead of host to run');
|
237
|
+
// wasmEngine = await import('../asur/index.js');
|
238
|
+
// }
|
239
|
+
|
240
|
+
// 0, { instance } = await wasmEngine.instantiate(wasm, {
|
241
|
+
const module = new WebAssembly.Module(wasm);
|
242
|
+
instance = new WebAssembly.Instance(module, {
|
207
243
|
'': {
|
208
244
|
p: valtype === 'i64' ? i => print(Number(i).toString()) : i => print(i.toString()),
|
209
245
|
c: valtype === 'i64' ? i => print(String.fromCharCode(Number(i))) : i => print(String.fromCharCode(i)),
|
@@ -211,12 +247,33 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
211
247
|
u: () => performance.timeOrigin,
|
212
248
|
y: () => {},
|
213
249
|
z: () => {},
|
250
|
+
w: (ind, outPtr) => { // readArgv
|
251
|
+
let args = process.argv.slice(2);
|
252
|
+
args = args.slice(args.findIndex(x => !x.startsWith('-')) + 1);
|
253
|
+
|
254
|
+
const str = args[ind - 1];
|
255
|
+
if (!str) return -1;
|
256
|
+
|
257
|
+
writeByteStr(memory, outPtr, str);
|
258
|
+
return str.length;
|
259
|
+
},
|
260
|
+
q: (pathPtr, outPtr) => { // readFile
|
261
|
+
try {
|
262
|
+
const path = pathPtr === 0 ? 0 : readByteStr(memory, pathPtr);
|
263
|
+
const contents = fs.readFileSync(path, 'utf8');
|
264
|
+
writeByteStr(memory, outPtr, contents);
|
265
|
+
return contents.length;
|
266
|
+
} catch {
|
267
|
+
return -1;
|
268
|
+
}
|
269
|
+
},
|
214
270
|
...customImports
|
215
271
|
}
|
216
272
|
});
|
217
273
|
} catch (e) {
|
218
274
|
// only backtrace for runner, not test262/etc
|
219
275
|
if (!process.argv[1].includes('/runner')) throw e;
|
276
|
+
if (!(e instanceof WebAssembly.CompileError)) throw e;
|
220
277
|
|
221
278
|
const funcInd = parseInt(e.message.match(/function #([0-9]+) /)?.[1]);
|
222
279
|
const blobOffset = parseInt(e.message.split('@')?.[1]);
|
@@ -255,16 +312,58 @@ export default async (source, flags = [ 'module' ], customImports = {}, print =
|
|
255
312
|
return porfToJSValue({ memory, funcs, pages }, ret[0], ret[1]);
|
256
313
|
} catch (e) {
|
257
314
|
if (e.is && e.is(exceptTag)) {
|
258
|
-
const
|
259
|
-
|
315
|
+
const exceptionMode = Prefs.exceptionMode ?? 'lut';
|
316
|
+
if (exceptionMode === 'lut') {
|
317
|
+
const exceptId = e.getArg(exceptTag, 0);
|
318
|
+
const exception = exceptions[exceptId];
|
319
|
+
|
320
|
+
const constructorName = exception.constructor;
|
321
|
+
|
322
|
+
// no constructor, just throw message
|
323
|
+
if (!constructorName) throw exception.message;
|
324
|
+
|
325
|
+
const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
|
326
|
+
throw new constructor(exception.message);
|
327
|
+
}
|
328
|
+
|
329
|
+
if (exceptionMode === 'stack') {
|
330
|
+
const value = e.getArg(exceptTag, 0);
|
331
|
+
const type = e.getArg(exceptTag, 1);
|
332
|
+
|
333
|
+
throw porfToJSValue({ memory, funcs, pages }, value, type);
|
334
|
+
}
|
335
|
+
|
336
|
+
if (exceptionMode === 'stackest') {
|
337
|
+
const constructorIdx = e.getArg(exceptTag, 0);
|
338
|
+
const constructorName = constructorIdx == -1 ? null : funcs.find(x => ((x.originalIndex ?? x.index) - importedFuncs.length) === constructorIdx)?.name;
|
339
|
+
|
340
|
+
const value = e.getArg(exceptTag, 1);
|
341
|
+
const type = e.getArg(exceptTag, 2);
|
342
|
+
const message = porfToJSValue({ memory, funcs, pages }, value, type);
|
343
|
+
|
344
|
+
// no constructor, just throw message
|
345
|
+
if (!constructorName) throw message;
|
346
|
+
|
347
|
+
const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
|
348
|
+
throw new constructor(message);
|
349
|
+
}
|
350
|
+
|
351
|
+
if (exceptionMode === 'partial') {
|
352
|
+
const exceptId = e.getArg(exceptTag, 0);
|
353
|
+
const exception = exceptions[exceptId];
|
354
|
+
|
355
|
+
const constructorName = exception.constructor;
|
260
356
|
|
261
|
-
|
357
|
+
const value = e.getArg(exceptTag, 1);
|
358
|
+
const type = e.getArg(exceptTag, 2);
|
359
|
+
const message = porfToJSValue({ memory, funcs, pages }, value, type);
|
262
360
|
|
263
|
-
|
264
|
-
|
361
|
+
// no constructor, just throw message
|
362
|
+
if (!constructorName) throw message;
|
265
363
|
|
266
|
-
|
267
|
-
|
364
|
+
const constructor = globalThis[constructorName] ?? eval(`class ${constructorName} extends Error { constructor(message) { super(message); this.name = "${constructorName}"; } }; ${constructorName}`);
|
365
|
+
throw new constructor(message);
|
366
|
+
}
|
268
367
|
}
|
269
368
|
|
270
369
|
if (e instanceof WebAssembly.RuntimeError) {
|
package/package.json
CHANGED
@@ -1,12 +1,10 @@
|
|
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.
|
4
|
+
"version": "0.17.0-05070e1f0",
|
5
5
|
"author": "CanadaHonk",
|
6
6
|
"license": "MIT",
|
7
|
-
"scripts": {
|
8
|
-
"precompile": "node ./compiler/precompile.js"
|
9
|
-
},
|
7
|
+
"scripts": {},
|
10
8
|
"dependencies": {
|
11
9
|
"acorn": "^8.11.3",
|
12
10
|
"node-repl-polyfill": "^0.1.1"
|
@@ -28,5 +26,5 @@
|
|
28
26
|
"bugs": {
|
29
27
|
"url": "https://github.com/CanadaHonk/porffor/issues"
|
30
28
|
},
|
31
|
-
"homepage": "https://porffor.
|
29
|
+
"homepage": "https://porffor.dev"
|
32
30
|
}
|
package/rhemyn/README.md
CHANGED
@@ -24,13 +24,16 @@ Made for use with Porffor but could possibly be adapted, implementation/library
|
|
24
24
|
- 🟢 digit, not digit (eg `\d\D`)
|
25
25
|
- 🟢 word, not word (eg `\w\W`)
|
26
26
|
- 🟢 whitespace, not whitespace (eg `\s\S`)
|
27
|
-
-
|
28
|
-
-
|
29
|
-
-
|
30
|
-
-
|
27
|
+
- 🟡 quantifiers
|
28
|
+
- 🟡 star (eg `a*`)
|
29
|
+
- 🟡 plus (eg `a+`)
|
30
|
+
- 🟡 optional (eg `a?`)
|
31
31
|
- 🟠 lazy modifier (eg `a*?`)
|
32
32
|
- 🔴 n repetitions (eg `a{4}`)
|
33
33
|
- 🔴 n-m repetitions (eg `a{2,4}`)
|
34
|
+
- 🟠 groups
|
35
|
+
- 🟠 capturing groups (`(a)`)
|
36
|
+
- 🔴 non-capturing groups (`(?:a)`)
|
34
37
|
- 🔴 assertions
|
35
38
|
- 🔴 beginning (eg `^a`)
|
36
39
|
- 🔴 end (eg `a$`)
|