porffor 0.17.0-cab4904b8 → 0.17.0-cbb73d209
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/README.md +6 -4
- package/compiler/2c.js +28 -11
- package/compiler/assemble.js +19 -4
- package/compiler/builtins/array.ts +79 -9
- package/compiler/builtins/date.ts +104 -106
- package/compiler/builtins/error.js +2 -5
- package/compiler/builtins/set.ts +6 -14
- package/compiler/builtins/string_f64.ts +4 -6
- package/compiler/builtins/symbol.ts +2 -1
- package/compiler/builtins/typedarray.js +94 -0
- package/compiler/builtins/z_ecma262.ts +1 -0
- package/compiler/builtins.js +12 -1
- package/compiler/codegen.js +747 -140
- package/compiler/generated_builtins.js +2752 -669
- package/compiler/opt.js +3 -0
- package/compiler/pgo.js +9 -1
- package/compiler/precompile.js +4 -2
- package/compiler/prototype.js +0 -69
- package/compiler/types.js +31 -5
- package/compiler/wasmSpec.js +2 -0
- package/compiler/wrap.js +20 -5
- package/package.json +1 -1
- package/rhemyn/README.md +7 -4
- package/rhemyn/compile.js +170 -76
- package/runner/debug.js +1 -1
- package/runner/index.js +3 -3
- package/runner/profile.js +1 -1
- package/runner/repl.js +16 -11
package/compiler/opt.js
CHANGED
@@ -203,6 +203,9 @@ export default (funcs, globals, pages, tags, exceptions) => {
|
|
203
203
|
if (type === 'Array') missing = !pages.hasArray;
|
204
204
|
if (type === 'String') missing = !pages.hasString;
|
205
205
|
if (type === 'ByteString') missing = !pages.hasByteString;
|
206
|
+
if (['Set', 'Uint8Array', 'Int8Array', 'Uint8ClampedArray', 'Uint16Array', 'Int16Array', 'Uint32Array', 'Int32Array', 'Float32Array', 'Float64Array'].includes(type)) {
|
207
|
+
missing = funcs.find(x => x.name === type) == null;
|
208
|
+
}
|
206
209
|
|
207
210
|
if (missing) {
|
208
211
|
let j = i, depth = 0;
|
package/compiler/pgo.js
CHANGED
@@ -91,7 +91,9 @@ export const run = obj => {
|
|
91
91
|
},
|
92
92
|
w: (ind, outPtr) => { // readArgv
|
93
93
|
const pgoInd = process.argv.indexOf('--pgo');
|
94
|
-
|
94
|
+
let args = process.argv.slice(pgoInd);
|
95
|
+
args = args.slice(args.findIndex(x => !x.startsWith('-')) + 1);
|
96
|
+
|
95
97
|
const str = args[ind - 1];
|
96
98
|
if (pgoInd === -1 || !str) {
|
97
99
|
if (Prefs.pgoLog) console.log('\nPGO warning: script was expecting arguments, please specify args to use for PGO after --pgo arg');
|
@@ -100,6 +102,9 @@ export const run = obj => {
|
|
100
102
|
|
101
103
|
writeByteStr(exports.$, outPtr, str);
|
102
104
|
return str.length;
|
105
|
+
},
|
106
|
+
q: (pathPtr, outPtr) => {
|
107
|
+
return -1;
|
103
108
|
}
|
104
109
|
}, () => {});
|
105
110
|
|
@@ -179,6 +184,9 @@ export const run = obj => {
|
|
179
184
|
|
180
185
|
log = '';
|
181
186
|
for (const x of funcs) {
|
187
|
+
// skip pgo opt for main()
|
188
|
+
if (x.name === 'main') continue;
|
189
|
+
|
182
190
|
const wasmFunc = wasmFuncs.find(y => y.name === x.name);
|
183
191
|
|
184
192
|
let targets = [];
|
package/compiler/precompile.js
CHANGED
@@ -6,6 +6,8 @@ import { join } from 'node:path';
|
|
6
6
|
|
7
7
|
import { fileURLToPath } from 'node:url';
|
8
8
|
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
9
|
+
globalThis.precompileCompilerPath = __dirname;
|
10
|
+
globalThis.precompile = true;
|
9
11
|
|
10
12
|
const argv = process.argv.slice();
|
11
13
|
|
@@ -14,7 +16,7 @@ const compile = async (file, [ _funcs, _globals ]) => {
|
|
14
16
|
let first = source.slice(0, source.indexOf('\n'));
|
15
17
|
|
16
18
|
if (first.startsWith('export default')) {
|
17
|
-
source = (await import(file)).default();
|
19
|
+
source = await (await import(file)).default();
|
18
20
|
first = source.slice(0, source.indexOf('\n'));
|
19
21
|
}
|
20
22
|
|
@@ -117,7 +119,7 @@ ${funcs.map(x => {
|
|
117
119
|
locals: ${JSON.stringify(Object.values(x.locals).slice(x.params.length).map(x => x.type))},
|
118
120
|
localNames: ${JSON.stringify(Object.keys(x.locals))},
|
119
121
|
${x.data && x.data.length > 0 ? ` data: ${JSON.stringify(x.data)},` : ''}
|
120
|
-
${x.table ? ` table: true` : ''}
|
122
|
+
${x.table ? ` table: true,` : ''}${x.constr ? ` constr: true,` : ''}
|
121
123
|
};`.replaceAll('\n\n', '\n').replaceAll('\n\n', '\n').replaceAll('\n\n', '\n');
|
122
124
|
}).join('\n')}
|
123
125
|
};`;
|
package/compiler/prototype.js
CHANGED
@@ -208,72 +208,6 @@ export const PrototypeFuncs = function() {
|
|
208
208
|
// ...number(1, Valtype.i32),
|
209
209
|
// [ Opcodes.i32_sub ]
|
210
210
|
// ]),
|
211
|
-
],
|
212
|
-
|
213
|
-
fill: (pointer, length, wElement, wType, iTmp, iTmp2) => [
|
214
|
-
...wElement,
|
215
|
-
[ Opcodes.local_set, iTmp ],
|
216
|
-
|
217
|
-
// use cached length i32 as pointer
|
218
|
-
...length.getCachedI32(),
|
219
|
-
|
220
|
-
// length - 1 for indexes
|
221
|
-
...number(1, Valtype.i32),
|
222
|
-
[ Opcodes.i32_sub ],
|
223
|
-
|
224
|
-
// * sizeof value
|
225
|
-
...number(ValtypeSize[valtype] + 1, Valtype.i32),
|
226
|
-
[ Opcodes.i32_mul ],
|
227
|
-
|
228
|
-
...length.setCachedI32(),
|
229
|
-
|
230
|
-
...(noUnlikelyChecks ? [] : [
|
231
|
-
...length.getCachedI32(),
|
232
|
-
...number(0, Valtype.i32),
|
233
|
-
[ Opcodes.i32_lt_s ],
|
234
|
-
[ Opcodes.if, Blocktype.void ],
|
235
|
-
...pointer,
|
236
|
-
Opcodes.i32_from_u,
|
237
|
-
[ Opcodes.br, 1 ],
|
238
|
-
[ Opcodes.end ]
|
239
|
-
]),
|
240
|
-
|
241
|
-
[ Opcodes.loop, Blocktype.void ],
|
242
|
-
|
243
|
-
// calculate offset
|
244
|
-
...length.getCachedI32(),
|
245
|
-
...pointer,
|
246
|
-
[ Opcodes.i32_add ],
|
247
|
-
[ Opcodes.local_set, iTmp2 ],
|
248
|
-
|
249
|
-
// store value
|
250
|
-
[ Opcodes.local_get, iTmp2 ],
|
251
|
-
[ Opcodes.local_get, iTmp ],
|
252
|
-
[ Opcodes.store, 0, ValtypeSize.i32 ],
|
253
|
-
|
254
|
-
// store type
|
255
|
-
[ Opcodes.local_get, iTmp2 ],
|
256
|
-
...wType,
|
257
|
-
[ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
258
|
-
|
259
|
-
// pointer - sizeof value
|
260
|
-
...length.getCachedI32(),
|
261
|
-
...number(ValtypeSize[valtype] + 1, Valtype.i32),
|
262
|
-
[ Opcodes.i32_sub ],
|
263
|
-
|
264
|
-
...length.setCachedI32(),
|
265
|
-
|
266
|
-
// if pointer >= 0, loop
|
267
|
-
...length.getCachedI32(),
|
268
|
-
...number(0, Valtype.i32),
|
269
|
-
[ Opcodes.i32_ge_s ],
|
270
|
-
[ Opcodes.br_if, 0 ],
|
271
|
-
|
272
|
-
[ Opcodes.end ],
|
273
|
-
|
274
|
-
// return this array
|
275
|
-
...pointer,
|
276
|
-
Opcodes.i32_from_u,
|
277
211
|
]
|
278
212
|
};
|
279
213
|
|
@@ -282,9 +216,6 @@ export const PrototypeFuncs = function() {
|
|
282
216
|
this[TYPES.array].push.noArgRetLength = true;
|
283
217
|
this[TYPES.array].push.local = Valtype.i32;
|
284
218
|
this[TYPES.array].pop.local = Valtype.i32;
|
285
|
-
this[TYPES.array].fill.local = valtypeBinary;
|
286
|
-
this[TYPES.array].fill.local2 = Valtype.i32;
|
287
|
-
this[TYPES.array].fill.returnType = TYPES.array;
|
288
219
|
|
289
220
|
this[TYPES.string] = {
|
290
221
|
at: (pointer, length, wIndex, wType, iTmp, _, arrayShell) => {
|
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,
|
package/compiler/wrap.js
CHANGED
@@ -2,7 +2,7 @@ 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
|
|
@@ -115,6 +115,19 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
|
115
115
|
return Symbol(desc);
|
116
116
|
}
|
117
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
|
+
|
118
131
|
default: return value;
|
119
132
|
}
|
120
133
|
};
|
@@ -127,7 +140,7 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
|
|
127
140
|
|
128
141
|
globalThis.porfDebugInfo = { funcs, globals };
|
129
142
|
|
130
|
-
if (process.argv[1].includes('/runner') && source.includes?.('export ')) flags.push('module');
|
143
|
+
// if (process.argv[1].includes('/runner') && source.includes?.('export ')) flags.push('module');
|
131
144
|
|
132
145
|
// fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
133
146
|
|
@@ -235,8 +248,10 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
|
|
235
248
|
y: () => {},
|
236
249
|
z: () => {},
|
237
250
|
w: (ind, outPtr) => { // readArgv
|
238
|
-
|
239
|
-
|
251
|
+
let args = process.argv.slice(2);
|
252
|
+
args = args.slice(args.findIndex(x => !x.startsWith('-')) + 1);
|
253
|
+
|
254
|
+
const str = args[ind - 1];
|
240
255
|
if (!str) return -1;
|
241
256
|
|
242
257
|
writeByteStr(memory, outPtr, str);
|
@@ -244,7 +259,7 @@ export default (source, flags = [ 'module' ], customImports = {}, print = str =>
|
|
244
259
|
},
|
245
260
|
q: (pathPtr, outPtr) => { // readFile
|
246
261
|
try {
|
247
|
-
const path = readByteStr(memory, pathPtr);
|
262
|
+
const path = pathPtr === 0 ? 0 : readByteStr(memory, pathPtr);
|
248
263
|
const contents = fs.readFileSync(path, 'utf8');
|
249
264
|
writeByteStr(memory, outPtr, contents);
|
250
265
|
return contents.length;
|
package/package.json
CHANGED
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$`)
|
package/rhemyn/compile.js
CHANGED
@@ -6,75 +6,78 @@ import { TYPES } from '../compiler/types.js';
|
|
6
6
|
|
7
7
|
// local indexes
|
8
8
|
const BasePointer = 0; // base string pointer
|
9
|
-
const
|
10
|
-
const
|
11
|
-
const
|
12
|
-
const
|
13
|
-
const
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
const Counter = 2; // what char we are running on
|
10
|
+
const Pointer = 3; // next char pointer
|
11
|
+
const Length = 4;
|
12
|
+
const Tmp = 5;
|
13
|
+
const QuantifierTmp = 6; // the temporary variable used for quanitifers
|
14
|
+
|
15
|
+
const doesSucceedZero = (node) => {
|
16
|
+
for (const n of node.body) {
|
17
|
+
if (n.type === "Group") {
|
18
|
+
if (!doesSucceedZero(node)) return false;
|
19
|
+
}
|
20
|
+
if (!n.quantifier || n.quantifier[0] > 0) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return true;
|
25
|
+
}
|
26
|
+
|
17
27
|
const generate = (node, negated = false, get = true, stringSize = 2, func = 'test') => {
|
18
28
|
let out = [];
|
19
29
|
switch (node.type) {
|
20
30
|
case 'Expression':
|
21
|
-
|
31
|
+
let succeedsZero = doesSucceedZero(node);
|
32
|
+
|
22
33
|
out = [
|
23
34
|
// set length local
|
24
35
|
[ Opcodes.local_get, BasePointer ],
|
25
36
|
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
26
|
-
[ Opcodes.
|
37
|
+
[ Opcodes.local_tee, Length ],
|
27
38
|
|
28
|
-
|
39
|
+
...number(0, Valtype.i32),
|
40
|
+
[ Opcodes.i32_eq ],
|
41
|
+
[ Opcodes.if, Blocktype.void ],
|
42
|
+
...number(succeedsZero ? 1 : 0, Valtype.i32),
|
43
|
+
[ Opcodes.return ],
|
44
|
+
[ Opcodes.end ],
|
45
|
+
|
46
|
+
// pointer = base + sizeof i32
|
29
47
|
[ Opcodes.local_get, BasePointer ],
|
30
48
|
...number(ValtypeSize.i32, Valtype.i32),
|
31
49
|
[ Opcodes.i32_add ],
|
32
|
-
[ Opcodes.local_set, IterPointer ],
|
33
|
-
|
34
|
-
[ Opcodes.loop, Blocktype.void ],
|
35
|
-
|
36
|
-
// reset pointer as iter pointer
|
37
|
-
[ Opcodes.local_get, IterPointer ],
|
38
50
|
[ Opcodes.local_set, Pointer ],
|
39
51
|
|
40
|
-
[ Opcodes.
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
[ Opcodes.
|
53
|
-
]
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
[ Opcodes.local_get, Counter ],
|
67
|
-
...number(1, Valtype.i32),
|
68
|
-
[ Opcodes.i32_add ],
|
69
|
-
[ Opcodes.local_tee, Counter ],
|
70
|
-
|
71
|
-
[ Opcodes.local_get, Length ],
|
72
|
-
[ Opcodes.i32_ne ],
|
73
|
-
|
74
|
-
[ Opcodes.br_if, 0 ],
|
52
|
+
[ Opcodes.loop, Blocktype.void ],
|
53
|
+
[ Opcodes.block, Blocktype.void ],
|
54
|
+
// generate checks
|
55
|
+
...node.body.flatMap(x => generate(x, negated, true, stringSize, func)),
|
56
|
+
|
57
|
+
// reached end without branching out, successful match
|
58
|
+
...({
|
59
|
+
test: number(1, Valtype.i32),
|
60
|
+
search: [
|
61
|
+
[ Opcodes.local_get, Counter ]
|
62
|
+
]
|
63
|
+
})[func],
|
64
|
+
[ Opcodes.return ],
|
65
|
+
[ Opcodes.end ],
|
66
|
+
|
67
|
+
// counter++, if length > counter, loop
|
68
|
+
[ Opcodes.local_get, Length ],
|
69
|
+
|
70
|
+
[ Opcodes.local_get, Counter ],
|
71
|
+
...number(1, Valtype.i32),
|
72
|
+
[ Opcodes.i32_add ],
|
73
|
+
[ Opcodes.local_tee, Counter ],
|
74
|
+
|
75
|
+
[ Opcodes.i32_gt_s ],
|
76
|
+
|
77
|
+
[ Opcodes.br_if, 0 ],
|
75
78
|
[ Opcodes.end ],
|
76
79
|
|
77
|
-
// no match
|
80
|
+
// no match
|
78
81
|
...number(({
|
79
82
|
test: 0,
|
80
83
|
search: -1
|
@@ -110,12 +113,12 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
|
|
110
113
|
return out;
|
111
114
|
};
|
112
115
|
|
113
|
-
const getNextChar = (stringSize) => [
|
116
|
+
const getNextChar = (stringSize, peek = false) => [
|
114
117
|
// get char from pointer
|
115
118
|
[ Opcodes.local_get, Pointer ],
|
116
119
|
[ stringSize == 2 ? Opcodes.i32_load16_u : Opcodes.i32_load8_u, 0, 0 ],
|
117
120
|
|
118
|
-
...(
|
121
|
+
...(peek ? [] : [
|
119
122
|
// pointer += string size
|
120
123
|
[ Opcodes.local_get, Pointer ],
|
121
124
|
...number(stringSize, Valtype.i32),
|
@@ -134,37 +137,123 @@ const checkFailure = () => [
|
|
134
137
|
[ Opcodes.br_if, 0 ]
|
135
138
|
];
|
136
139
|
|
137
|
-
const
|
140
|
+
const wrapQuantifier = (node, method, get, stringSize) => {
|
141
|
+
const [ min, max ] = node.quantifier;
|
138
142
|
return [
|
139
|
-
|
143
|
+
// initalize our temp value (number of matched characters)
|
144
|
+
...number(0, Valtype.i32),
|
145
|
+
[Opcodes.local_set, QuantifierTmp],
|
146
|
+
|
147
|
+
// if len - counter == 0, if min == 0, succeed, else fail
|
148
|
+
[ Opcodes.local_get, Length ],
|
149
|
+
[ Opcodes.local_get, Counter ],
|
150
|
+
[ Opcodes.i32_sub ],
|
151
|
+
...number(0, Valtype.i32),
|
152
|
+
[ Opcodes.i32_eq ],
|
153
|
+
...(min == 0 ? [
|
154
|
+
[ Opcodes.if, Blocktype.void ],
|
155
|
+
] : [
|
156
|
+
[ Opcodes.br_if, 0 ],
|
157
|
+
]),
|
158
|
+
|
159
|
+
// start loop
|
160
|
+
[Opcodes.loop, Blocktype.void],
|
161
|
+
[ Opcodes.block, Blocktype.void ],
|
162
|
+
// if counter + tmp == length, break
|
163
|
+
[ Opcodes.local_get, Counter ],
|
164
|
+
[ Opcodes.local_get, QuantifierTmp ],
|
165
|
+
[ Opcodes.i32_add ],
|
166
|
+
[ Opcodes.local_get, Length ],
|
167
|
+
[ Opcodes.i32_eq ],
|
168
|
+
[ Opcodes.br_if, 0 ],
|
169
|
+
|
170
|
+
// if doesn't match, break
|
171
|
+
...method,
|
172
|
+
[Opcodes.br_if, 0 ],
|
173
|
+
...(get ? [
|
174
|
+
// pointer += stringSize
|
175
|
+
[ Opcodes.local_get, Pointer ],
|
176
|
+
...number(stringSize, Valtype.i32),
|
177
|
+
[ Opcodes.i32_add ],
|
178
|
+
[ Opcodes.local_set, Pointer ]
|
179
|
+
] : []),
|
180
|
+
|
181
|
+
// if maximum was reached, break
|
182
|
+
...(max ? [
|
183
|
+
[ Opcodes.local_get, QuantifierTmp ],
|
184
|
+
...number(max, Valtype.i32),
|
185
|
+
[ Opcodes.i32_eq ],
|
186
|
+
[ Opcodes.br_if, 0 ]
|
187
|
+
] : []),
|
188
|
+
|
189
|
+
[ Opcodes.local_get, QuantifierTmp ],
|
190
|
+
...number(1, Valtype.i32),
|
191
|
+
[ Opcodes.i32_add ],
|
192
|
+
[ Opcodes.local_set, QuantifierTmp ],
|
193
|
+
[ Opcodes.br, 1 ],
|
194
|
+
[ Opcodes.end ],
|
195
|
+
[ Opcodes.end ],
|
196
|
+
|
197
|
+
// if less than minimum, fail
|
198
|
+
[Opcodes.local_get, QuantifierTmp],
|
199
|
+
...number(min, Valtype.i32),
|
200
|
+
[Opcodes.i32_lt_s],
|
201
|
+
...(get ? checkFailure(): []),
|
202
|
+
|
203
|
+
...(min == 0 ? [ [ Opcodes.end ] ] : []),
|
204
|
+
];
|
205
|
+
}
|
206
|
+
|
207
|
+
const generateChar = (node, negated, get, stringSize) => {
|
208
|
+
const hasQuantifier = !!node.quantifier;
|
209
|
+
const out = [
|
210
|
+
...(get ? getNextChar(stringSize, hasQuantifier) : []),
|
140
211
|
...number(node.char.charCodeAt(0), Valtype.i32),
|
141
212
|
negated ? [ Opcodes.i32_eq ] : [ Opcodes.i32_ne ],
|
142
|
-
|
213
|
+
];
|
214
|
+
|
215
|
+
if (node.quantifier) {
|
216
|
+
return wrapQuantifier(node, out, get, stringSize);
|
217
|
+
}
|
218
|
+
|
219
|
+
return [
|
220
|
+
...out,
|
221
|
+
...(get ? checkFailure(): []),
|
143
222
|
];
|
144
223
|
};
|
145
224
|
|
146
225
|
const generateSet = (node, negated, get, stringSize) => {
|
147
226
|
// for a single char we do not need a tmp, it is like just
|
148
227
|
const singleChar = node.body.length === 1 && node.body[0].type === 'Character';
|
228
|
+
if (singleChar) return generateChar(node.body[0], negated, get, stringSize)
|
149
229
|
|
150
|
-
|
151
|
-
|
152
|
-
|
230
|
+
const hasQuantifier = !!node.quantifier;
|
231
|
+
|
232
|
+
const out = [
|
233
|
+
...(get ? getNextChar(stringSize, hasQuantifier) : []),
|
234
|
+
[ Opcodes.local_set, Tmp ],
|
153
235
|
];
|
154
236
|
|
155
237
|
for (const x of node.body) {
|
156
|
-
out
|
157
|
-
|
158
|
-
...(singleChar ? [] : [ [ Opcodes.local_get, Tmp ] ]),
|
238
|
+
out.push(
|
239
|
+
[ Opcodes.local_get, Tmp ],
|
159
240
|
...generate(x, negated, false, stringSize)
|
160
|
-
|
241
|
+
);
|
161
242
|
}
|
162
243
|
|
163
|
-
if (node.body.length > 0)
|
244
|
+
if (node.body.length > 0) {
|
245
|
+
for (let i = 0; i < node.body.length - 1; i++) {
|
246
|
+
out.push(negated ? [ Opcodes.i32_or ] : [ Opcodes.i32_and ])
|
247
|
+
}
|
248
|
+
};
|
249
|
+
|
250
|
+
if (hasQuantifier) {
|
251
|
+
return wrapQuantifier(node, out, get, stringSize);
|
252
|
+
}
|
164
253
|
|
165
254
|
return [
|
166
255
|
...out,
|
167
|
-
...checkFailure()
|
256
|
+
...checkFailure(),
|
168
257
|
];
|
169
258
|
};
|
170
259
|
|
@@ -206,28 +295,33 @@ const wrapFunc = (regex, func, name, index) => {
|
|
206
295
|
// bytestring
|
207
296
|
...generate(parsed, false, true, 1, func),
|
208
297
|
[ Opcodes.end ]
|
209
|
-
], name, index);
|
298
|
+
], name, index, types[func]);
|
210
299
|
};
|
211
300
|
|
212
301
|
export const test = (regex, index = 0, name = 'regex_test_' + regex) => wrapFunc(regex, 'test', name, index);
|
213
302
|
export const search = (regex, index = 0, name = 'regex_search_' + regex) => wrapFunc(regex, 'search', name, index);
|
214
303
|
|
215
|
-
const
|
304
|
+
export const types = {
|
305
|
+
test: TYPES.boolean,
|
306
|
+
search: TYPES.number
|
307
|
+
};
|
308
|
+
|
309
|
+
const outputFunc = (wasm, name, index, returnType) => ({
|
216
310
|
name,
|
217
311
|
index,
|
218
312
|
wasm,
|
313
|
+
returnType,
|
219
314
|
|
220
315
|
export: true,
|
221
316
|
params: [ Valtype.i32, Valtype.i32 ],
|
222
317
|
returns: [ Valtype.i32 ],
|
223
|
-
returnType: TYPES.boolean,
|
224
318
|
locals: {
|
225
319
|
basePointer: { idx: 0, type: Valtype.i32 },
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
320
|
+
inputType: { idx: 1, type: Valtype.i32 },
|
321
|
+
counter: { idx: 2, type: Valtype.i32 },
|
322
|
+
pointer: { idx: 3, type: Valtype.i32 },
|
323
|
+
length: { idx: 4, type: Valtype.i32 },
|
324
|
+
tmp: { idx: 5, type: Valtype.i32 },
|
325
|
+
quantifierTmp: { idx: 6, type: Valtype.i32 },
|
232
326
|
}
|
233
327
|
});
|
package/runner/debug.js
CHANGED
@@ -43,7 +43,7 @@ let lastLine;
|
|
43
43
|
let output = '';
|
44
44
|
|
45
45
|
try {
|
46
|
-
const { exports } =
|
46
|
+
const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
|
47
47
|
y: n => {
|
48
48
|
if (callStarts[callStarts.length - 1] === n - 1) {
|
49
49
|
// end of call
|
package/runner/index.js
CHANGED
@@ -130,14 +130,14 @@ const print = str => {
|
|
130
130
|
let runStart;
|
131
131
|
try {
|
132
132
|
if (process.argv.includes('-b')) {
|
133
|
-
const { wasm, exports } =
|
133
|
+
const { wasm, exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
|
134
134
|
|
135
135
|
runStart = performance.now();
|
136
136
|
if (!process.argv.includes('--no-run')) exports.main();
|
137
137
|
|
138
138
|
console.log(`\n\nwasm size: ${wasm.byteLength} bytes`);
|
139
139
|
} else {
|
140
|
-
const { exports } =
|
140
|
+
const { exports } = compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
|
141
141
|
|
142
142
|
runStart = performance.now();
|
143
143
|
if (!process.argv.includes('--no-run')) exports.main();
|
@@ -146,7 +146,7 @@ try {
|
|
146
146
|
} catch (e) {
|
147
147
|
// if (cache) process.stdout.write(cache);
|
148
148
|
let out = e;
|
149
|
-
if (!process.argv.includes('-i') && e.
|
149
|
+
if (!process.argv.includes('-i') && Object.getPrototypeOf(e).message != null) out = `${e.constructor.name}${e.message != null ? `: ${e.message}` : ''}`;
|
150
150
|
console.error(out);
|
151
151
|
}
|
152
152
|
|