porffor 0.2.0-eaee2da → 0.2.0-ef043de

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.
Files changed (52) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +159 -88
  3. package/asur/README.md +2 -0
  4. package/asur/index.js +1262 -0
  5. package/byg/index.js +237 -0
  6. package/compiler/2c.js +317 -72
  7. package/compiler/{sections.js → assemble.js} +63 -15
  8. package/compiler/builtins/annexb_string.js +72 -0
  9. package/compiler/builtins/annexb_string.ts +19 -0
  10. package/compiler/builtins/array.ts +145 -0
  11. package/compiler/builtins/base64.ts +151 -0
  12. package/compiler/builtins/crypto.ts +120 -0
  13. package/compiler/builtins/date.ts +1370 -0
  14. package/compiler/builtins/escape.ts +141 -0
  15. package/compiler/builtins/int.ts +147 -0
  16. package/compiler/builtins/number.ts +527 -0
  17. package/compiler/builtins/porffor.d.ts +42 -0
  18. package/compiler/builtins/string.ts +1055 -0
  19. package/compiler/builtins/tostring.ts +45 -0
  20. package/compiler/builtins.js +470 -269
  21. package/compiler/{codeGen.js → codegen.js} +999 -378
  22. package/compiler/embedding.js +22 -22
  23. package/compiler/encoding.js +108 -10
  24. package/compiler/generated_builtins.js +1262 -0
  25. package/compiler/index.js +36 -34
  26. package/compiler/log.js +6 -3
  27. package/compiler/opt.js +51 -36
  28. package/compiler/parse.js +35 -27
  29. package/compiler/precompile.js +123 -0
  30. package/compiler/prefs.js +26 -0
  31. package/compiler/prototype.js +13 -28
  32. package/compiler/types.js +37 -0
  33. package/compiler/wasmSpec.js +28 -8
  34. package/compiler/wrap.js +54 -46
  35. package/fib.js +7 -0
  36. package/package.json +9 -5
  37. package/porf +4 -0
  38. package/rhemyn/compile.js +5 -3
  39. package/rhemyn/parse.js +323 -320
  40. package/rhemyn/test/parse.js +58 -58
  41. package/runner/compare.js +34 -34
  42. package/runner/debug.js +122 -0
  43. package/runner/index.js +62 -10
  44. package/runner/profiler.js +102 -0
  45. package/runner/repl.js +40 -7
  46. package/runner/sizes.js +37 -37
  47. package/compiler/builtins/base64.js +0 -92
  48. package/runner/info.js +0 -89
  49. package/runner/profile.js +0 -46
  50. package/runner/results.json +0 -1
  51. package/runner/transform.js +0 -15
  52. package/util/enum.js +0 -20
@@ -1,59 +1,59 @@
1
- import util from 'node:util';
2
-
3
- import parse from '../parse.js';
4
-
5
- const tests = {
6
- 'a': {},
7
- 'a(b)': {},
8
- 'a(b(c))': {},
9
- 'ab': {},
10
- '[ab]': {},
11
- '[a-z]': {},
12
- 'a*': {},
13
- 'a+': {},
14
- 'a?': {},
15
- 'a(b)+': {},
16
- '[^a]': {},
17
- '[a^]': {},
18
- '[^ab]': {},
19
- '.': {},
20
-
21
- // not range
22
- '[-]': {},
23
- '[0-]': {},
24
- '[-0]': {},
25
- '[\\s-\\S]': {},
26
- '[\\s-.]': {},
27
-
28
- '[\\S]': {},
29
-
30
- '\\c': {},
31
- '\\c0': {},
32
- '\\cJ': {},
33
-
34
- '\\x': {},
35
- '\\x0': {},
36
- '\\x0g': {},
37
- '\\x0a': {},
38
-
39
- '\\u': {},
40
- '\\u0': {},
41
- '\\u000': {},
42
- '\\u000g': {},
43
- '\\u000a': {},
44
-
45
- /*
46
- // email regexes
47
- '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$': {},
48
-
49
- // input type=email from HTML spec
50
- // https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)
51
- // simpler form
52
- '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$': {},
53
- // full/complex form
54
- '^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$': {}*/
55
- };
56
-
57
- for (const str in tests) {
58
- console.log(str, util.inspect(parse(str), false, null, true));
1
+ import util from 'node:util';
2
+
3
+ import parse from '../parse.js';
4
+
5
+ const tests = {
6
+ 'a': {},
7
+ 'a(b)': {},
8
+ 'a(b(c))': {},
9
+ 'ab': {},
10
+ '[ab]': {},
11
+ '[a-z]': {},
12
+ 'a*': {},
13
+ 'a+': {},
14
+ 'a?': {},
15
+ 'a(b)+': {},
16
+ '[^a]': {},
17
+ '[a^]': {},
18
+ '[^ab]': {},
19
+ '.': {},
20
+
21
+ // not range
22
+ '[-]': {},
23
+ '[0-]': {},
24
+ '[-0]': {},
25
+ '[\\s-\\S]': {},
26
+ '[\\s-.]': {},
27
+
28
+ '[\\S]': {},
29
+
30
+ '\\c': {},
31
+ '\\c0': {},
32
+ '\\cJ': {},
33
+
34
+ '\\x': {},
35
+ '\\x0': {},
36
+ '\\x0g': {},
37
+ '\\x0a': {},
38
+
39
+ '\\u': {},
40
+ '\\u0': {},
41
+ '\\u000': {},
42
+ '\\u000g': {},
43
+ '\\u000a': {},
44
+
45
+ /*
46
+ // email regexes
47
+ '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$': {},
48
+
49
+ // input type=email from HTML spec
50
+ // https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)
51
+ // simpler form
52
+ '^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$': {},
53
+ // full/complex form
54
+ '^[a-zA-Z0-9.!#$%&\'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$': {}*/
55
+ };
56
+
57
+ for (const str in tests) {
58
+ console.log(str, util.inspect(parse(str), false, null, true));
59
59
  }
package/runner/compare.js CHANGED
@@ -1,35 +1,35 @@
1
- import { execSync } from 'node:child_process';
2
- import { pathToFileURL } from 'node:url';
3
- import fs from 'node:fs';
4
-
5
- const [ commitsAgo, file ] = process.argv.slice(2);
6
- const source = fs.readFileSync(file, 'utf8');
7
-
8
- const compileNow = (await import('../compiler/index.js')).default;
9
- const wasmNow = compileNow(source).byteLength;
10
-
11
- execSync(`git checkout HEAD~${commitsAgo}`);
12
-
13
- let failed = false;
14
-
15
- fs.writeFileSync('tmp.js', source);
16
- try {
17
- execSync(`node runner/index.js tmp.js`);
18
- } catch {
19
- failed = true;
20
- } finally {
21
- fs.rmSync('tmp.js');
22
- execSync(`git checkout main`);
23
- }
24
-
25
- console.log();
26
-
27
- if (failed) {
28
- console.log(`!! failed to compile then`);
29
- process.exit();
30
- }
31
-
32
- const wasmThen = Buffer.byteLength(fs.readFileSync('out.wasm'));
33
-
34
- console.log(`now: ${wasmNow} bytes`);
1
+ import { execSync } from 'node:child_process';
2
+ import { pathToFileURL } from 'node:url';
3
+ import fs from 'node:fs';
4
+
5
+ const [ commitsAgo, file ] = process.argv.slice(2);
6
+ const source = fs.readFileSync(file, 'utf8');
7
+
8
+ const compileNow = (await import('../compiler/index.js')).default;
9
+ const wasmNow = compileNow(source).byteLength;
10
+
11
+ execSync(`git checkout HEAD~${commitsAgo}`);
12
+
13
+ let failed = false;
14
+
15
+ fs.writeFileSync('tmp.js', source);
16
+ try {
17
+ execSync(`node runner/index.js tmp.js`);
18
+ } catch {
19
+ failed = true;
20
+ } finally {
21
+ fs.rmSync('tmp.js');
22
+ execSync(`git checkout main`);
23
+ }
24
+
25
+ console.log();
26
+
27
+ if (failed) {
28
+ console.log(`!! failed to compile then`);
29
+ process.exit();
30
+ }
31
+
32
+ const wasmThen = Buffer.byteLength(fs.readFileSync('out.wasm'));
33
+
34
+ console.log(`now: ${wasmNow} bytes`);
35
35
  console.log(`${commitsAgo} commit${commitsAgo > 1 ? 's' : ''} ago: ${wasmThen} bytes`);
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env node
2
+
3
+ import compile from '../compiler/wrap.js';
4
+ import fs from 'node:fs';
5
+
6
+ import Byg from '../byg/index.js';
7
+
8
+ const file = process.argv.slice(2).find(x => x[0] !== '-');
9
+ let source = fs.readFileSync(file, 'utf8');
10
+
11
+ const originalLines = source.split('\n');
12
+
13
+ let funcs = {}, funcId = 0;
14
+ source = source.replace(/^\s*(function|const)\s*([a-zA-Z0-9]+)(\s*=\s*)?\([^)]*\)\s*(=>)?\s*\{$/gm, (x, _, n) => {
15
+ const id = funcId++;
16
+ funcs[funcId] = n;
17
+ return `${x}profile2(Porffor.wasm.i32.const(${id}))`;
18
+ });
19
+
20
+ const lines = source.split('\n');
21
+ for (let i = 0; i < lines.length; i++) {
22
+ // lines[line] = lines[line].replace(/^[^\n}]*;$/, _ => `profile(${line});${_}`);
23
+ if (lines[i].trim().replace('}', '') !== '') lines[i] = `profile1(Porffor.wasm.i32.const(${i}));` + lines[i];
24
+ }
25
+ source = lines.join('\n');
26
+
27
+ const breakpoints = new Array(lines.length);
28
+
29
+ let paused = true;
30
+ const byg = Byg({
31
+ lines: originalLines,
32
+ pause: () => { paused = true; },
33
+ breakpoint: (line, breakpoint) => {
34
+ breakpoints[line] = breakpoint;
35
+ }
36
+ });
37
+
38
+ // console.log(source);
39
+
40
+ let stepIn = false, stepOut = false;
41
+ const callStack = [];
42
+
43
+ let _paused;
44
+ let callStarts = [];
45
+ let lastLine;
46
+
47
+ let output = '';
48
+
49
+ try {
50
+ const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
51
+ y: n => {
52
+ if (callStarts[callStarts.length - 1] === n - 1) {
53
+ // end of call
54
+
55
+ callStarts.pop();
56
+ callStack.pop();
57
+
58
+ paused = _paused;
59
+ }
60
+
61
+ lastLine = n;
62
+
63
+ if (breakpoints[n]) paused = true;
64
+
65
+ if (paused) {
66
+ stepIn = false; stepOut = false;
67
+
68
+ switch (byg(
69
+ paused,
70
+ n,
71
+ `\x1b[1mporffor debugger\x1b[22m: ${file}@${n + 1} ${callStack.join('->')}`,
72
+ [
73
+ {
74
+ x: termWidth - 1 - 40 - 6,
75
+ // y: () => termHeight - 20 - 1 - 4,
76
+ y: () => 4,
77
+ width: 40,
78
+ height: 20,
79
+ title: 'console',
80
+ content: output.split('\n')
81
+ }
82
+ ]
83
+ )) {
84
+ case 'resume': {
85
+ paused = false;
86
+ break;
87
+ }
88
+
89
+ case 'stepOver': {
90
+ break;
91
+ }
92
+
93
+ case 'stepIn': {
94
+ stepIn = true;
95
+ // paused = false;
96
+ break;
97
+ }
98
+
99
+ case 'stepOut': {
100
+ stepOut = true;
101
+ paused = false;
102
+ break;
103
+ }
104
+ }
105
+ }
106
+ },
107
+ z: n => {
108
+ // start of call
109
+ callStack.push(funcs[n]);
110
+
111
+ callStarts.push(lastLine);
112
+
113
+ _paused = paused;
114
+ if (!stepIn) paused = false;
115
+ else paused = true;
116
+ }
117
+ }, s => output += s);
118
+
119
+ exports.main();
120
+ } catch (e) {
121
+ console.error(e);
122
+ }
package/runner/index.js CHANGED
@@ -3,6 +3,8 @@
3
3
  import compile from '../compiler/wrap.js';
4
4
  import fs from 'node:fs';
5
5
 
6
+ const start = performance.now();
7
+
6
8
  if (process.argv.includes('-compile-hints')) {
7
9
  const v8 = await import('node:v8');
8
10
  v8.setFlagsFromString(`--experimental-wasm-compilation-hints`);
@@ -15,9 +17,51 @@ if (process.argv.includes('-compile-hints')) {
15
17
  // --experimental-wasm-return-call (on by default)
16
18
  }
17
19
 
18
- const file = process.argv.slice(2).find(x => x[0] !== '-');
20
+ if (process.argv.includes('--help')) {
21
+ // description + version
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
+
24
+ // basic usage
25
+ console.log(`\x1B[1mporf [command] path/to/script.js [...prefs] [...args]\x1B[0m`);
26
+
27
+ // commands
28
+ console.log(`\x1B[1mporf [subcommand] path/to/script.js [...prefs] [...args]\x1B[0m`);
29
+
30
+ process.exit(0);
31
+ }
32
+
33
+ let file = process.argv.slice(2).find(x => x[0] !== '-');
34
+ if (['run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm'].includes(file)) {
35
+ if (file === 'profile') {
36
+ process.argv.splice(process.argv.indexOf(file), 1);
37
+ await import('./profiler.js');
38
+ await new Promise(() => {}); // do nothing for the rest of this file
39
+ }
40
+
41
+ if (['wasm', 'native', 'c'].includes(file)) {
42
+ process.argv.push(`-target=${file}`);
43
+ }
44
+
45
+ if (file === 'debug-wasm') {
46
+ process.argv.push('-asur', '-wasm-debug');
47
+ }
48
+
49
+ if (file === 'debug') {
50
+ process.argv.splice(process.argv.indexOf(file), 1);
51
+ await import('./debug.js');
52
+ await new Promise(() => {}); // do nothing for the rest of this file
53
+ }
54
+
55
+ file = process.argv.slice(process.argv.indexOf(file) + 1).find(x => x[0] !== '-');
56
+
57
+ const nonOptOutFile = process.argv.slice(process.argv.indexOf(file) + 1).find(x => x[0] !== '-');
58
+ if (nonOptOutFile) {
59
+ process.argv.push(`-o=${nonOptOutFile}`);
60
+ }
61
+ }
62
+
19
63
  if (!file) {
20
- if (process.argv.includes('-v')) {
64
+ if (process.argv.includes('-v') || process.argv.includes('--version')) {
21
65
  // just print version
22
66
  console.log((await import('./version.js')).default);
23
67
  process.exit(0);
@@ -25,9 +69,7 @@ if (!file) {
25
69
 
26
70
  // run repl if no file given
27
71
  await import('./repl.js');
28
-
29
- // do nothing for the rest of this file
30
- await new Promise(() => {});
72
+ await new Promise(() => {}); // do nothing for the rest of this file
31
73
  }
32
74
 
33
75
  const source = fs.readFileSync(file, 'utf8');
@@ -45,11 +87,21 @@ const print = str => {
45
87
  };
46
88
 
47
89
  try {
48
- const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
90
+ if (process.argv.includes('-b')) {
91
+ const { wasm, exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
92
+
93
+ if (!process.argv.includes('-no-run')) exports.main();
94
+
95
+ console.log(`\n\nwasm size: ${wasm.byteLength} bytes`);
96
+ } else {
97
+ const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {}, print);
49
98
 
50
- if (!process.argv.includes('-no-run')) exports.main();
51
- if (cache) process.stdout.write(cache);
99
+ if (!process.argv.includes('-no-run')) exports.main();
100
+ }
101
+ // if (cache) process.stdout.write(cache);
52
102
  } catch (e) {
53
- if (cache) process.stdout.write(cache);
103
+ // if (cache) process.stdout.write(cache);
54
104
  console.error(process.argv.includes('-i') ? e : `${e.constructor.name}: ${e.message}`);
55
- }
105
+ }
106
+
107
+ if (process.argv.includes('-t')) console.log(`${process.argv.includes('-b') ? '' : '\n\n'}total time: ${(performance.now() - start).toFixed(2)}ms`);
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env node
2
+
3
+ import compile from '../compiler/wrap.js';
4
+ import fs from 'node:fs';
5
+
6
+ import Prefs from '../compiler/prefs.js';
7
+
8
+ // const fast = Prefs.profiler === 'fast';
9
+ const fast = !Prefs.experimentalProfiler;
10
+
11
+ const file = process.argv.slice(2).find(x => x[0] !== '-');
12
+ let source = fs.readFileSync(file, 'utf8');
13
+
14
+ let profileId = 0;
15
+ // source = fast ? source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});${_}profile(${profileId++});`) : source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});profile(${profileId++});${_}profile(${profileId++});`);
16
+ // source = fast ? source.replace(/^[^\n}]*;$/mg, _ => `profile(Porffor.wasm.i32.const(${profileId++}));${_}profile(Porffor.wasm.i32.const(${profileId++}));`) : source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});profile(${profileId++});${_}profile(${profileId++});`);
17
+ source = fast ? source.replace(/^[^\n}]*;$/mg, _ => `profile1(Porffor.wasm.i32.const(${profileId}));${_}profile2(Porffor.wasm.i32.const(${profileId++}));`) : source.replace(/^[^\n}]*;$/mg, _ => `profile(${profileId++});profile(${profileId++});${_}profile(${profileId++});`);
18
+
19
+ // console.log(source);
20
+
21
+ // let tmp = new Array(profileId).fill(0);
22
+ let tmp = new Array(profileId).fill(0);
23
+ let times = new Array(profileId).fill(0);
24
+ let samples = 0;
25
+
26
+ const percents = process.argv.includes('-%');
27
+
28
+ const spinner = ['-', '\\', '|', '/'];
29
+ let spin = 0;
30
+ let last = 0;
31
+
32
+ try {
33
+ const { exports } = await compile(source, process.argv.includes('--module') ? [ 'module' ] : [], {
34
+ y: fast ? n => {
35
+ tmp[n] = performance.now();
36
+ } : n => { /* todo */ },
37
+ z: fast ? n => {
38
+ times[n] += performance.now() - tmp[n];
39
+ } : n => { /* todo */ }
40
+ // z: fast ? n => {
41
+ // if (n % 2) {
42
+ // tmp[n] += performance.now() - tmp[n - 1];
43
+ // } else {
44
+ // tmp[n] = performance.now();
45
+ // }
46
+ // } : n => {
47
+ // if (n % 3 === 2) {
48
+ // tmp[n] += (performance.now() - tmp[n - 1]) - (tmp[n - 1] - tmp[n - 2]);
49
+ // samples++;
50
+
51
+ // if (performance.now() > last) {
52
+ // process.stdout.write(`\r${spinner[spin++ % 4]} running: collected ${samples} samples...`);
53
+ // last = performance.now() + 100;
54
+ // }
55
+ // } else {
56
+ // tmp[n] = performance.now();
57
+ // }
58
+ // }
59
+ });
60
+
61
+ const start = performance.now();
62
+
63
+ exports.main();
64
+
65
+ const total = performance.now() - start;
66
+
67
+ console.log(`\n${fast ? '' : `samples: ${samples}\n`}total: ${total}ms\n\n` + source.split('\n').map(x => {
68
+ let time = 0;
69
+ if (x.startsWith('profile')) {
70
+ // const id = parseInt(x.slice(8, x.indexOf(')')));
71
+ // const id = parseInt(x.slice(31, x.indexOf(')') + 1));
72
+ const id = parseInt(x.slice(32, x.indexOf(')')));
73
+ // time = fast ? tmp[id + 1] : tmp[id + 2];
74
+ time = fast ? times[id] : tmp[id + 2];
75
+ }
76
+
77
+ let color = [ 0, 0, 0 ];
78
+ if (time) {
79
+ let relativeTime = time / total;
80
+ if (percents) time = relativeTime;
81
+
82
+ relativeTime = Math.sqrt(relativeTime);
83
+ color = [ (relativeTime * 250) | 0, (Math.sin(relativeTime * Math.PI) * 50) | 0, 0 ];
84
+ }
85
+
86
+ const ansiColor = `2;${color[0]};${color[1]};${color[2]}m`;
87
+
88
+ // const line = x.replace(/profile\([0-9]+\);/g, '');
89
+ // const line = x.replace(/profile\(Porffor.wasm.i32.const\([0-9]+\)\);/g, '');
90
+ const line = x.replace(/profile[0-9]\(Porffor.wasm.i32.const\([0-9]+\)\);/g, '');
91
+
92
+ if (percents) return `\x1b[48;${ansiColor}\x1b[97m${time ? ((time * 100).toFixed(0).padStart(4, ' ') + '%') : ' '}\x1b[0m\x1b[38;${ansiColor}▌\x1b[0m ${line}`;
93
+
94
+ let digits = 2;
95
+ if (time >= 100) digits = 1;
96
+ if (time >= 1000) digits = 0;
97
+
98
+ return `\x1b[48;${ansiColor}\x1b[97m${time ? time.toFixed(digits).padStart(6, ' ') : ' '}\x1b[0m\x1b[38;${ansiColor}▌\x1b[0m ${line}`;
99
+ }).join('\n'));
100
+ } catch (e) {
101
+ console.error(e);
102
+ }
package/runner/repl.js CHANGED
@@ -1,7 +1,21 @@
1
1
  import compile from '../compiler/wrap.js';
2
2
  import rev from './version.js';
3
3
 
4
- import repl from 'node:repl';
4
+ // import repl from 'node:repl';
5
+ // import repl from '../../node-repl-polyfill/index.js';
6
+
7
+ let repl;
8
+ try {
9
+ // try importing node:repl
10
+ repl = await import('node:repl');
11
+
12
+ // check it is not just a mock with REPLServer prototype
13
+ if (repl.REPLServer.prototype.defineCommand == null)
14
+ throw 'mock node:repl detected';
15
+ } catch {
16
+ // it failed, import the polyfill
17
+ repl = (await import('node-repl-polyfill')).default;
18
+ }
5
19
 
6
20
  // process.argv.push('-O0'); // disable opts
7
21
 
@@ -10,8 +24,16 @@ globalThis.valtype = 'f64';
10
24
  const valtypeOpt = process.argv.find(x => x.startsWith('-valtype='));
11
25
  if (valtypeOpt) valtype = valtypeOpt.split('=')[1];
12
26
 
13
- console.log(`welcome to porffor rev ${rev.slice(0, 7)}`);
14
- console.log(`info: using opt ${process.argv.find(x => x.startsWith('-O')) ?? '-O1'} and valtype ${valtype}`);
27
+ let host = globalThis?.navigator?.userAgent;
28
+ if (typeof process !== 'undefined' && process.argv0 === 'node') host = 'Node/' + process.versions.node;
29
+ host ??= 'Unknown';
30
+
31
+ if (host.startsWith('Node')) host = '\x1B[92m' + host;
32
+ if (host.startsWith('Deno')) host = '\x1B[97m' + host;
33
+ if (host.startsWith('Bun')) host = '\x1B[93m' + host;
34
+
35
+ console.log(`Welcome to \x1B[1m\x1B[35mPorffor\x1B[0m \x1B[90m(${rev.slice(0, 7)})\x1B[0m running on \x1B[1m${host.replace('/', ' \x1B[0m\x1B[90m(')})\x1B[0m`);
36
+ console.log(`\x1B[90musing opt ${process.argv.find(x => x.startsWith('-O')) ?? '-O1'}, parser ${parser}, valtype ${valtype}\x1B[0m`);
15
37
  console.log();
16
38
 
17
39
  let lastMemory, lastPages;
@@ -21,16 +43,27 @@ const memoryToString = mem => {
21
43
  const pages = lastPages.length;
22
44
  const wasmPages = mem.buffer.byteLength / PageSize;
23
45
 
24
- out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KB\x1B[0m for ${pages} thing${pages === 1 ? '' : 's'} using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'}\n`;
46
+ out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KB\x1B[0m for ${pages} thing${pages === 1 ? '' : 's'} using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'}\n\n`;
25
47
 
26
48
  const buf = new Uint8Array(mem.buffer);
27
49
 
50
+ let longestType = 0, longestName = 0;
51
+ for (const x of lastPages) {
52
+ const [ type, name ] = x.split(': ');
53
+ if (type.length > longestType) longestType = type.length;
54
+ if (name.length > longestName) longestName = name.length;
55
+ }
56
+
57
+ out += `\x1B[0m\x1B[1m name${' '.repeat(longestName - 4)} \x1B[0m\x1B[90m│\x1B[0m\x1B[1m type${' '.repeat(longestType - 4)} \x1B[0m\x1B[90m│\x1B[0m\x1B[1m memory\x1B[0m\n`; // ─
28
58
  for (let i = 0; i < pages; i++) {
29
- out += `\x1B[36m${lastPages[i]}\x1B[2m | \x1B[0m`;
59
+ const [ type, name ] = lastPages[i].split(': ');
60
+ // out += `\x1B[36m${lastPages[i].replace(':', '\x1B[90m:\x1B[34m')}\x1B[90m${' '.repeat(longestName - lastPages[i].length)} | \x1B[0m`;
61
+ out += ` \x1B[34m${name}${' '.repeat(longestName - name.length)} \x1B[90m│\x1B[0m \x1B[36m${type}${' '.repeat(longestType - type.length)} \x1B[90m│\x1B[0m `;
30
62
 
31
- for (let j = 0; j < 50; j++) {
63
+ for (let j = 0; j < 40; j++) {
32
64
  const val = buf[i * pageSize + j];
33
- if (val === 0) out += '\x1B[2m';
65
+ // if (val === 0) out += '\x1B[2m';
66
+ if (val === 0) out += '\x1B[90m';
34
67
  out += val.toString(16).padStart(2, '0');
35
68
  if (val === 0) out += '\x1B[0m';
36
69
  out += ' ';
package/runner/sizes.js CHANGED
@@ -1,38 +1,38 @@
1
- import fs from 'node:fs';
2
- import compile from '../compiler/index.js';
3
-
4
- // deno compat
5
- const textEncoder = new TextEncoder();
6
- if (typeof process === 'undefined') globalThis.process = { argv: ['', '', ...Deno.args], stdout: { write: str => Deno.writeAllSync(Deno.stdout, textEncoder.encode(str)) } };
7
-
8
- let csv = `file,porffor i32 -O0,porffor i32 -O1,porffor i32 -O2,porffor i32 -O3,porffor i64 -O0,porffor i64 -O1,porffor i64 -O2,porffor i64 -O3,porffor f64 -O0, porffor f64 -O1, porffor f64 -O2, porffor f64 -O3\n`;
9
- const perform = async (file, args) => {
10
- process.argv = process.argv.slice(0, 2).concat(args);
11
- const source = fs.readFileSync(file, 'utf8');
12
-
13
- const { wasm } = compile(source, []);
14
- const size = wasm.byteLength;
15
-
16
- const label = `${file} ${args.join(' ')}`;
17
- csv += `${size},`;
18
- console.log(label, ' '.repeat(40 - label.length), `${size}b`);
19
- };
20
-
21
- const argsValtypes = [ '-valtype=i32', '-valtype=i64', '-valtype=f64' ];
22
- const argsOptlevels = [ '-O0', '-O1', '-O2', '-O3' ];
23
-
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' ]) {
25
- const niceFile = file.split('/')[1].slice(0, -3);
26
- csv += `${niceFile},`;
27
-
28
- for (const x of argsValtypes) {
29
- for (const y of argsOptlevels) {
30
- await perform(file, [ x, y ]);
31
- }
32
- }
33
-
34
- csv = csv.slice(0, -1) + '\n';
35
- }
36
-
37
- fs.writeFileSync('sizes.csv', csv);
1
+ import fs from 'node:fs';
2
+ import compile from '../compiler/index.js';
3
+
4
+ // deno compat
5
+ const textEncoder = new TextEncoder();
6
+ if (typeof process === 'undefined') globalThis.process = { argv: ['', '', ...Deno.args], stdout: { write: str => Deno.writeAllSync(Deno.stdout, textEncoder.encode(str)) } };
7
+
8
+ let csv = `file,porffor i32 -O0,porffor i32 -O1,porffor i32 -O2,porffor i32 -O3,porffor i64 -O0,porffor i64 -O1,porffor i64 -O2,porffor i64 -O3,porffor f64 -O0, porffor f64 -O1, porffor f64 -O2, porffor f64 -O3\n`;
9
+ const perform = async (file, args) => {
10
+ process.argv = process.argv.slice(0, 2).concat(args);
11
+ const source = fs.readFileSync(file, 'utf8');
12
+
13
+ const { wasm } = compile(source, []);
14
+ const size = wasm.byteLength;
15
+
16
+ const label = `${file} ${args.join(' ')}`;
17
+ csv += `${size},`;
18
+ console.log(label, ' '.repeat(40 - label.length), `${size}b`);
19
+ };
20
+
21
+ const argsValtypes = [ '-valtype=i32', '-valtype=i64', '-valtype=f64' ];
22
+ const argsOptlevels = [ '-O0', '-O1', '-O2', '-O3' ];
23
+
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' ]) {
25
+ const niceFile = file.split('/')[1].slice(0, -3);
26
+ csv += `${niceFile},`;
27
+
28
+ for (const x of argsValtypes) {
29
+ for (const y of argsOptlevels) {
30
+ await perform(file, [ x, y ]);
31
+ }
32
+ }
33
+
34
+ csv = csv.slice(0, -1) + '\n';
35
+ }
36
+
37
+ fs.writeFileSync('sizes.csv', csv);
38
38
  console.log(csv);