porffor 0.2.0-ef043de → 0.2.0-f435128

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/parse.js CHANGED
@@ -9,8 +9,10 @@ if (typeof process === 'undefined' && typeof Deno !== 'undefined') {
9
9
  globalThis.process = { argv: ['', '', ...Deno.args], stdout: { write: str => Deno.writeAllSync(Deno.stdout, textEncoder.encode(str)) } };
10
10
  }
11
11
 
12
+ const file = process.argv.slice(2).find(x => x[0] !== '-');
13
+
12
14
  // should we try to support types (while parsing)
13
- const types = Prefs.parseTypes;
15
+ const types = Prefs.parseTypes || file?.endsWith('.ts');
14
16
  globalThis.typedInput = types && Prefs.optTypes;
15
17
 
16
18
  // todo: review which to use by default
@@ -23,7 +25,7 @@ globalThis.typedInput = types && Prefs.optTypes;
23
25
  globalThis.parser = '';
24
26
  let parse;
25
27
  const loadParser = async (fallbackParser = 'acorn', forceParser) => {
26
- parser = forceParser ?? process.argv.find(x => x.startsWith('-parser='))?.split('=')?.[1] ?? fallbackParser;
28
+ parser = forceParser ?? process.argv.find(x => x.startsWith('--parser='))?.split('=')?.[1] ?? fallbackParser;
27
29
  0, { parse } = (await import((globalThis.document || globalThis.Deno ? 'https://esm.sh/' : '') + parser));
28
30
  };
29
31
  globalThis._porf_loadParser = loadParser;
@@ -21,7 +21,7 @@ const compile = async (file, [ _funcs, _globals ]) => {
21
21
  first = source.slice(0, source.indexOf('\n'));
22
22
  }
23
23
 
24
- let args = ['-bytestring', '-todo-time=compile', '-no-aot-pointer-opt', '-no-treeshake-wasm-imports', '-scoped-page-names', '-parse-types', '-opt-types'];
24
+ let args = ['--bytestring', '--todo-time=compile', '--no-aot-pointer-opt', '--no-treeshake-wasm-imports', '--scoped-page-names', '--parse-types', '--opt-types'];
25
25
  if (first.startsWith('// @porf')) {
26
26
  args = args.concat(first.slice('// @porf '.length).split(' '));
27
27
  }
@@ -93,9 +93,14 @@ const precompile = async () => {
93
93
  let funcs = [], globals = [];
94
94
  for (const file of fs.readdirSync(dir)) {
95
95
  if (file.endsWith('.d.ts')) continue;
96
+ console.log(file);
97
+
96
98
  await compile(join(dir, file), [ funcs, globals ]);
97
99
  }
98
100
 
101
+ // const a = funcs.find(x => x.name === '__ecma262_ToUTCDTSF');
102
+ // console.log(Object.values(a.locals).slice(a.params.length));
103
+
99
104
  // ${x.pages && x.pages.size > 0 ? ` pages: ${JSON.stringify(Object.fromEntries(x.pages.entries()))},` : ''}
100
105
  // ${x.used && x.used.length > 0 ? ` used: ${JSON.stringify(x.used)},` : ''}
101
106
 
package/compiler/prefs.js CHANGED
@@ -9,11 +9,12 @@ const obj = new Proxy({}, {
9
9
  return cache[p] = (() => {
10
10
  // fooBar -> foo-bar
11
11
  const name = p[0] === '_' ? p : p.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
12
- if (process.argv.includes('-' + name)) return true;
13
- if (process.argv.includes('-no-' + name)) return false;
12
+ const prefix = name.length === 1 ? '-' : '--';
13
+ if (process.argv.includes(prefix + name)) return true;
14
+ if (process.argv.includes(prefix + 'no-' + name)) return false;
14
15
 
15
- const valArg = process.argv.find(x => x.startsWith(`-${name}=`));
16
- if (valArg) return valArg.slice(name.length + 2);
16
+ const valArg = process.argv.find(x => x.startsWith(`${prefix}${name}=`));
17
+ if (valArg) return valArg.slice(name.length + 1 + prefix.length);
17
18
 
18
19
  if (onByDefault.includes(p)) return true;
19
20
  return undefined;
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.2.0-ef043de",
4
+ "version": "0.2.0-f435128",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {
package/rhemyn/compile.js CHANGED
@@ -1,8 +1,8 @@
1
- import { Blocktype, Opcodes, Valtype, PageSize, ValtypeSize } from '../compiler/wasmSpec.js';
1
+ import { Blocktype, Opcodes, Valtype, ValtypeSize } from '../compiler/wasmSpec.js';
2
2
  import { number } from '../compiler/embedding.js';
3
- import { signedLEB128, unsignedLEB128 } from '../compiler/encoding.js';
4
3
  import parse from './parse.js';
5
4
  import Prefs from '../compiler/prefs.js';
5
+ import { TYPES } from '../compiler/types.js';
6
6
 
7
7
  // local indexes
8
8
  const BasePointer = 0; // base string pointer
@@ -14,7 +14,7 @@ const Length = 5;
14
14
  const Tmp = 6;
15
15
 
16
16
  let exprLastGet = false;
17
- const generate = (node, negated = false, get = true, func = 'test') => {
17
+ const generate = (node, negated = false, get = true, stringSize = 2, func = 'test') => {
18
18
  let out = [];
19
19
  switch (node.type) {
20
20
  case 'Expression':
@@ -42,7 +42,7 @@ const generate = (node, negated = false, get = true, func = 'test') => {
42
42
  // generate checks
43
43
  ...node.body.flatMap((x, i) => {
44
44
  exprLastGet = x.type !== 'Group' && i === (node.body.length - 1);
45
- return generate(x, negated);
45
+ return generate(x, negated, true, stringSize, func);
46
46
  }),
47
47
 
48
48
  // reached end without branching out, successful match
@@ -56,9 +56,9 @@ const generate = (node, negated = false, get = true, func = 'test') => {
56
56
 
57
57
  [ Opcodes.end ],
58
58
 
59
- // increment iter pointer by sizeof i16
59
+ // increment iter pointer by string size
60
60
  [ Opcodes.local_get, IterPointer ],
61
- ...number(ValtypeSize.i16, Valtype.i32),
61
+ ...number(stringSize, Valtype.i32),
62
62
  [ Opcodes.i32_add ],
63
63
  [ Opcodes.local_set, IterPointer ],
64
64
 
@@ -91,34 +91,34 @@ const generate = (node, negated = false, get = true, func = 'test') => {
91
91
  break;
92
92
 
93
93
  case 'Character':
94
- out = generateChar(node, node.negated ^ negated, get);
94
+ out = generateChar(node, node.negated ^ negated, get, stringSize);
95
95
  break;
96
96
 
97
97
  case 'Set':
98
- out = generateSet(node, node.negated, get);
98
+ out = generateSet(node, node.negated, get, stringSize);
99
99
  break;
100
100
 
101
101
  case 'Group':
102
- out = generateGroup(node, negated, get);
102
+ out = generateGroup(node, negated, get, stringSize);
103
103
  break;
104
104
 
105
105
  case 'Range':
106
- out = generateRange(node, negated, get);
106
+ out = generateRange(node, negated, get, stringSize);
107
107
  break;
108
108
  }
109
109
 
110
110
  return out;
111
111
  };
112
112
 
113
- const getNextChar = () => [
113
+ const getNextChar = (stringSize) => [
114
114
  // get char from pointer
115
115
  [ Opcodes.local_get, Pointer ],
116
- [ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(0) ],
116
+ [ stringSize == 2 ? Opcodes.i32_load16_u : Opcodes.i32_load8_u, 0, 0 ],
117
117
 
118
118
  ...(exprLastGet ? [] : [
119
- // pointer += sizeof i16
119
+ // pointer += string size
120
120
  [ Opcodes.local_get, Pointer ],
121
- ...number(ValtypeSize.i16, Valtype.i32),
121
+ ...number(stringSize, Valtype.i32),
122
122
  [ Opcodes.i32_add ],
123
123
  [ Opcodes.local_set, Pointer ]
124
124
  ])
@@ -134,21 +134,21 @@ const checkFailure = () => [
134
134
  [ Opcodes.br_if, 0 ]
135
135
  ];
136
136
 
137
- const generateChar = (node, negated, get) => {
137
+ const generateChar = (node, negated, get, stringSize) => {
138
138
  return [
139
- ...(get ? getNextChar() : []),
139
+ ...(get ? getNextChar(stringSize) : []),
140
140
  ...number(node.char.charCodeAt(0), Valtype.i32),
141
141
  negated ? [ Opcodes.i32_eq ] : [ Opcodes.i32_ne ],
142
142
  ...(get ? checkFailure(): [])
143
143
  ];
144
144
  };
145
145
 
146
- const generateSet = (node, negated, get) => {
146
+ const generateSet = (node, negated, get, stringSize) => {
147
147
  // for a single char we do not need a tmp, it is like just
148
148
  const singleChar = node.body.length === 1 && node.body[0].type === 'Character';
149
149
 
150
150
  let out = [
151
- ...(get ? getNextChar() : []),
151
+ ...(get ? getNextChar(stringSize) : []),
152
152
  ...(singleChar ? [] : [ [ Opcodes.local_set, Tmp ] ]),
153
153
  ];
154
154
 
@@ -156,7 +156,7 @@ const generateSet = (node, negated, get) => {
156
156
  out = [
157
157
  ...out,
158
158
  ...(singleChar ? [] : [ [ Opcodes.local_get, Tmp ] ]),
159
- ...generate(x, negated, false)
159
+ ...generate(x, negated, false, stringSize)
160
160
  ];
161
161
  }
162
162
 
@@ -168,9 +168,9 @@ const generateSet = (node, negated, get) => {
168
168
  ];
169
169
  };
170
170
 
171
- const generateRange = (node, negated, get) => {
171
+ const generateRange = (node, negated, get, stringSize) => {
172
172
  return [
173
- ...(get ? getNextChar() : []),
173
+ ...(get ? getNextChar(stringSize) : []),
174
174
  ...(get ? [ [ Opcodes.local_tee, Tmp ] ] : []),
175
175
 
176
176
  ...number(node.from.charCodeAt(0), Valtype.i32),
@@ -192,8 +192,25 @@ const generateGroup = (node, negated, get) => {
192
192
  return [];
193
193
  };
194
194
 
195
- export const test = (regex, index = 0, name = 'regex_test_' + regex) => outputFunc(generate(parse(regex), false, true, 'test'), name, index);
196
- export const search = (regex, index = 0, name = 'regex_search_' + regex) => outputFunc(generate(parse(regex), false, true, 'search'), name, index);
195
+ const wrapFunc = (regex, func, name, index) => {
196
+ const parsed = parse(regex);
197
+
198
+ return outputFunc([
199
+ [ Opcodes.local_get, 1 ],
200
+ ...number(TYPES.string, Valtype.i32),
201
+ [ Opcodes.i32_eq ],
202
+ [ Opcodes.if, Valtype.i32 ],
203
+ // string
204
+ ...generate(parsed, false, true, 2, func),
205
+ [ Opcodes.else ],
206
+ // bytestring
207
+ ...generate(parsed, false, true, 1, func),
208
+ [ Opcodes.end ]
209
+ ], name, index);
210
+ };
211
+
212
+ export const test = (regex, index = 0, name = 'regex_test_' + regex) => wrapFunc(regex, 'test', name, index);
213
+ export const search = (regex, index = 0, name = 'regex_search_' + regex) => wrapFunc(regex, 'search', name, index);
197
214
 
198
215
  const outputFunc = (wasm, name, index) => ({
199
216
  name,
@@ -201,9 +218,9 @@ const outputFunc = (wasm, name, index) => ({
201
218
  wasm,
202
219
 
203
220
  export: true,
204
- params: [ Valtype.i32 ],
221
+ params: [ Valtype.i32, Valtype.i32 ],
205
222
  returns: [ Valtype.i32 ],
206
- returnType: 0xffffffffffff1, // boolean - todo: do not hardcode this
223
+ returnType: TYPES.boolean,
207
224
  locals: {
208
225
  basePointer: { idx: 0, type: Valtype.i32 },
209
226
  iterPointer: { idx: 1, type: Valtype.i32 },
package/rhemyn/parse.js CHANGED
@@ -11,7 +11,7 @@ const Quantifiers = {
11
11
  const QuantifierKeys = Object.keys(Quantifiers);
12
12
 
13
13
  const getArg = (name, def) => {
14
- const arg = (typeof process !== 'undefined' ? process.argv : Deno.args).find(x => x.startsWith(`-${name}=`));
14
+ const arg = (typeof process !== 'undefined' ? process.argv : Deno.args).find(x => x.startsWith(`--${name}=`));
15
15
  if (arg) return arg.split('=')[0];
16
16
 
17
17
  return def;
@@ -20,8 +20,7 @@ const getArg = (name, def) => {
20
20
  // full is spec-compliant but slower. not needed most of the time. (evil)
21
21
  const DotChars = () => ({
22
22
  full: [ '\n', '\r', '\u2028', '\u2029' ],
23
- simple: [ '\n', '\r' ],
24
- fast: [ '\n' ]
23
+ fast: [ '\n', '\r' ]
25
24
  })[getArg('regex-dot', 'fast')];
26
25
 
27
26
  const WordChars = () => ({
@@ -31,8 +30,8 @@ const WordChars = () => ({
31
30
 
32
31
  const WhitespaceChars = () => ({
33
32
  full: [ ' ', '\t', '\n', '\r', '\u2028', '\u2029' ],
34
- simple: [ ' ', '\t', '\n', '\r' ]
35
- })[getArg('regex-ws', 'simple')];
33
+ fast: [ ' ', '\t', '\n', '\r' ]
34
+ })[getArg('regex-ws', 'fast')];
36
35
 
37
36
  const _Metachars = () => ({
38
37
  unescaped: {
package/runner/index.js CHANGED
@@ -5,7 +5,7 @@ import fs from 'node:fs';
5
5
 
6
6
  const start = performance.now();
7
7
 
8
- if (process.argv.includes('-compile-hints')) {
8
+ if (process.argv.includes('--compile-hints')) {
9
9
  const v8 = await import('node:v8');
10
10
  v8.setFlagsFromString(`--experimental-wasm-compilation-hints`);
11
11
 
@@ -22,11 +22,39 @@ if (process.argv.includes('--help')) {
22
22
  console.log(`\x1B[1m\x1B[35mPorffor\x1B[0m is a JavaScript engine/runtime/compiler. \x1B[90m(${(await import('./version.js')).default})\x1B[0m`);
23
23
 
24
24
  // basic usage
25
- console.log(`\x1B[1mporf [command] path/to/script.js [...prefs] [...args]\x1B[0m`);
25
+ console.log(`Usage: \x1B[1mporf [command] path/to/script.js [...prefs] [...args]\x1B[0m`);
26
26
 
27
27
  // commands
28
- console.log(`\x1B[1mporf [subcommand] path/to/script.js [...prefs] [...args]\x1B[0m`);
28
+ console.log(`\n\u001b[4mCommands\x1B[0m`);
29
+ for (const [ cmd, [ color, desc ] ] of Object.entries({
30
+ run: [ 34, 'Run a JS file' ],
31
+ wasm: [ 34, 'Compile a JS file to a Wasm binary\n' ],
32
+ c: [ 31, 'Compile a JS file to C source code' ],
33
+ native: [ 31, 'Compile a JS file to a native binary\n' ],
34
+ profile: [ 33, 'Profile a JS file' ],
35
+ debug: [ 33, 'Debug a JS file' ],
36
+ 'debug-wasm': [ 33, 'Debug the compiled Wasm of a JS file' ]
37
+ })) {
38
+ console.log(` \x1B[1m\x1B[${color}m${cmd}\x1B[0m${' '.repeat(20 - cmd.length - (desc.startsWith('🧪') ? 3 : 0))}${desc}`);
39
+ }
29
40
 
41
+ // console.log();
42
+
43
+ // // options
44
+ // console.log(`\n\u001b[4mCommands\x1B[0m`);
45
+ // for (const [ cmd, [ color, desc ] ] of Object.entries({
46
+ // run: [ 34, 'Run a JS file' ],
47
+ // wasm: [ 34, 'Compile a JS file to a Wasm binary\n' ],
48
+ // c: [ 31, 'Compile a JS file to C source code' ],
49
+ // native: [ 31, 'Compile a JS file to a native binary\n' ],
50
+ // profile: [ 33, 'Profile a JS file' ],
51
+ // debug: [ 33, 'Debug a JS file' ],
52
+ // 'debug-wasm': [ 33, 'Debug the compiled Wasm of a JS file' ]
53
+ // })) {
54
+ // console.log(` \x1B[1m\x1B[${color}m${cmd}\x1B[0m${' '.repeat(20 - cmd.length - (desc.startsWith('🧪') ? 3 : 0))}${desc}`);
55
+ // }
56
+
57
+ console.log();
30
58
  process.exit(0);
31
59
  }
32
60
 
@@ -39,11 +67,11 @@ if (['run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm'].includes(fi
39
67
  }
40
68
 
41
69
  if (['wasm', 'native', 'c'].includes(file)) {
42
- process.argv.push(`-target=${file}`);
70
+ process.argv.push(`--target=${file}`);
43
71
  }
44
72
 
45
73
  if (file === 'debug-wasm') {
46
- process.argv.push('-asur', '-wasm-debug');
74
+ process.argv.push('--asur', '--wasm-debug');
47
75
  }
48
76
 
49
77
  if (file === 'debug') {
@@ -90,7 +118,7 @@ try {
90
118
  if (process.argv.includes('-b')) {
91
119
  const { wasm, exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
92
120
 
93
- if (!process.argv.includes('-no-run')) exports.main();
121
+ if (!process.argv.includes('--no-run')) exports.main();
94
122
 
95
123
  console.log(`\n\nwasm size: ${wasm.byteLength} bytes`);
96
124
  } else {
package/runner/repl.js CHANGED
@@ -21,7 +21,7 @@ try {
21
21
 
22
22
  globalThis.valtype = 'f64';
23
23
 
24
- const valtypeOpt = process.argv.find(x => x.startsWith('-valtype='));
24
+ const valtypeOpt = process.argv.find(x => x.startsWith('--valtype='));
25
25
  if (valtypeOpt) valtype = valtypeOpt.split('=')[1];
26
26
 
27
27
  let host = globalThis?.navigator?.userAgent;
@@ -116,7 +116,7 @@ replServer.defineCommand('asm', {
116
116
  this.clearBufferedCommand();
117
117
 
118
118
  try {
119
- process.argv.push('-opt-funcs');
119
+ process.argv.push('--opt-funcs');
120
120
  await run('', null, null, () => {}, false);
121
121
  process.argv.pop();
122
122
  } catch { }
package/runner/sizes.js CHANGED
@@ -18,7 +18,7 @@ const perform = async (file, args) => {
18
18
  console.log(label, ' '.repeat(40 - label.length), `${size}b`);
19
19
  };
20
20
 
21
- const argsValtypes = [ '-valtype=i32', '-valtype=i64', '-valtype=f64' ];
21
+ const argsValtypes = [ '--valtype=i32', '--valtype=i64', '--valtype=f64' ];
22
22
  const argsOptlevels = [ '-O0', '-O1', '-O2', '-O3' ];
23
23
 
24
24
  for (const file of [ 'bench/prime_basic.js', 'bench/fib_iter.js', 'test/math_1.js', 'test/math_3.js', 'test/while_1.js', 'test/for_2.js', 'test/unary_3.js', 'test/updateexp_1.js', 'test/eq_3.js', 'test/empty.js' ]) {
package/fib.js DELETED
@@ -1,7 +0,0 @@
1
- export const fib = n => {
2
- if (n < 2) {
3
- return n;
4
- } else {
5
- return fib(n - 1) + fib(n - 2);
6
- }
7
- };