porffor 0.2.0-69d30a8 → 0.2.0-6bc63ef

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 (50) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/README.md +64 -44
  3. package/asur/index.js +2 -2
  4. package/byg/index.js +3 -24
  5. package/compiler/2c.js +2 -53
  6. package/compiler/assemble.js +6 -7
  7. package/compiler/builtins/annexb_string.js +12 -12
  8. package/compiler/builtins/annexb_string.ts +5 -6
  9. package/compiler/builtins/array.ts +15 -15
  10. package/compiler/builtins/base64.ts +4 -79
  11. package/compiler/builtins/boolean.ts +18 -0
  12. package/compiler/builtins/crypto.ts +1 -1
  13. package/compiler/builtins/date.ts +2067 -0
  14. package/compiler/builtins/escape.ts +2 -2
  15. package/compiler/builtins/function.ts +5 -0
  16. package/compiler/builtins/int.ts +2 -4
  17. package/compiler/builtins/number.ts +11 -9
  18. package/compiler/builtins/object.ts +4 -0
  19. package/compiler/builtins/porffor.d.ts +28 -10
  20. package/compiler/builtins/set.ts +187 -0
  21. package/compiler/builtins/string.ts +47 -22
  22. package/compiler/builtins.js +19 -36
  23. package/compiler/codegen.js +239 -199
  24. package/compiler/decompile.js +2 -3
  25. package/compiler/embedding.js +2 -2
  26. package/compiler/encoding.js +0 -14
  27. package/compiler/expression.js +1 -1
  28. package/compiler/generated_builtins.js +1138 -208
  29. package/compiler/index.js +0 -2
  30. package/compiler/opt.js +7 -7
  31. package/compiler/parse.js +5 -5
  32. package/compiler/precompile.js +21 -24
  33. package/compiler/prefs.js +6 -5
  34. package/compiler/prototype.js +19 -19
  35. package/compiler/types.js +3 -2
  36. package/compiler/wasmSpec.js +1 -0
  37. package/compiler/wrap.js +70 -54
  38. package/package.json +1 -1
  39. package/rhemyn/compile.js +42 -25
  40. package/rhemyn/parse.js +4 -5
  41. package/runner/compare.js +0 -1
  42. package/runner/debug.js +1 -6
  43. package/runner/index.js +45 -4
  44. package/runner/profiler.js +15 -42
  45. package/runner/repl.js +3 -9
  46. package/runner/sizes.js +2 -2
  47. package/runner/version.js +3 -3
  48. package/.vscode/launch.json +0 -18
  49. package/compiler/builtins/tostring.ts +0 -45
  50. package/test262_changes_from_1afe9b87d2_to_04-09.md +0 -270
@@ -0,0 +1,256 @@
1
+ # Contributing to Porffor
2
+
3
+ Hello! Thanks for your potential interest in contributing to Porffor :)
4
+
5
+ This document hopes to help you understand Porffor-specific TS, specifically for writing built-ins (inside `compiler/builtins/*.ts` eg `btoa`, `String.prototype.trim`, ...). This guide isn't really meant for modifying the compiler itself yet (eg `compiler/codegen.js`), as built-ins are ~easier to implement and more useful at the moment.
6
+
7
+ I mostly presume decent JS knowledge, with some basic TS too but nothing complicated. Knowing low-level stuff generally (pointers, etc) and/or Wasm (bytecode) is also a plus but hopefully not required.
8
+
9
+ If you have any questions you can ask in [the Porffor Discord](https://discord.gg/6crs9Znx9R), please feel free to ask anything if you get stuck :)
10
+
11
+ Please read this entire document before beginning as there are important things throughout.
12
+
13
+ <br>
14
+
15
+ ## Setup
16
+
17
+ 1. Clone the repo and enter the repo (`git clone https://github.com/CanadaHonk/porffor.git`)
18
+ 2. `npm install`
19
+
20
+ The repo comes with easy alias scripts for Unix and Windows, which you can use like so:
21
+ - Unix: `./porf path/to/script.js`
22
+ - Windows: `.\porf path/to/script.js`
23
+
24
+ You can also swap out `node` in the alias to use another runtime like Deno (`deno run -A ...`) or Bun (`bun ...`), or just use it yourself (eg `node runner/index.js ...`, `bun runner/index.js ...`). Node, Deno, Bun should work.
25
+
26
+
27
+ ### Precompile
28
+
29
+ **If you update any file inside `compiler/builtins` you will need to do this for it to update inside Porffor otherwise your changes will have no effect.** Run `node compiler/precompile.js` to precompile. It may error during this, if so, you might have an error in your code or there could be a compiler error with Porffor (feel free to ask for help as soon as you encounter any errors with it).
30
+
31
+ <br>
32
+
33
+ ## Types
34
+
35
+ Porffor has usual JS types (or at least the ones it supports), but also internal types for various reasons.
36
+
37
+ ### ByteString
38
+
39
+ The most important and widely used internal type is ByteString. Regular strings in Porffor are UTF-16 encoded, so each character uses 2 bytes. ByteStrings are special strings which are used when the characters in a string only use ASCII/LATIN-1 characters, so the lower byte of the UTF-16 characters are unused. Instead of wasting memory with all the unused memory, ByteStrings instead use 1 byte per character. This halves memory usage of such strings and also makes operating on them faster. The downside is that many Porffor built-ins have to be written twice, slightly different, for both `String` and `ByteString` types.
40
+
41
+ ### i32
42
+
43
+ This is complicated internally but essentially, only use it for pointers. (This is not signed or unsigned, instead it is the Wasm valtype `i32` so the signage is ~instruction dependant).
44
+
45
+ <br>
46
+
47
+ ## Pointers
48
+
49
+ Pointers are the main (and most difficult) unique feature you ~need to understand when dealing with objects (arrays, strings, ...).
50
+
51
+ We'll explain things per common usage you will likely need to know:
52
+
53
+ ## Commonly used Wasm code
54
+
55
+ ### Get a pointer
56
+
57
+ ```js
58
+ Porffor.wasm`local.get ${foobar}`
59
+ ```
60
+
61
+ Gets the pointer to the variable `foobar`. You don't really need to worry about how it works in detail, but essentially it gets the pointer as a number (type) instead of as the object it is.
62
+
63
+ ### Store a character in a ByteString
64
+
65
+ ```js
66
+ Porffor.wasm.i32.store8(pointer, characterCode, 0, 4)
67
+ ```
68
+
69
+ Stores the character code `characterCode` at the pointer `pointer` **for a ByteString**.[^1]
70
+
71
+ ### Store a character in a String
72
+
73
+ ```js
74
+ Porffor.wasm.i32.store16(pointer, characterCode, 0, 4)
75
+ ```
76
+
77
+ Stores the character code `characterCode` at the pointer `pointer` **for a String**.[^1]
78
+
79
+ ### Load a character from a ByteString
80
+
81
+ ```js
82
+ Porffor.wasm.i32.load8_u(pointer, 0, 4)
83
+ ```
84
+
85
+ Loads the character code at the pointer `pointer` **for a ByteString**.[^1]
86
+
87
+ ### Load a character from a String
88
+
89
+ ```js
90
+ Porffor.wasm.i32.load16_u(pointer, 0, 4)
91
+ ```
92
+
93
+ Loads the character code at the pointer `pointer` **for a String**.[^1]
94
+
95
+ ### Manually store the length of an object
96
+
97
+ ```js
98
+ Porffor.wasm.i32.store(pointer, length, 0, 0)
99
+ ```
100
+
101
+ Stores the length `length` at pointer `pointer`, setting the length of an object. This is mostly unneeded today as you can just do `obj.length = length`. [^2]
102
+
103
+ <br>
104
+
105
+ ## Example
106
+
107
+ Here is the code for `ByteString.prototype.toUpperCase()`:
108
+
109
+ ```ts
110
+ export const __ByteString_prototype_toUpperCase = (_this: bytestring) => {
111
+ const len: i32 = _this.length;
112
+
113
+ let out: bytestring = '';
114
+ Porffor.wasm.i32.store(out, len, 0, 0);
115
+
116
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
117
+ j: i32 = Porffor.wasm`local.get ${out}`;
118
+
119
+ const endPtr: i32 = i + len;
120
+ while (i < endPtr) {
121
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
122
+
123
+ if (chr >= 97) if (chr <= 122) chr -= 32;
124
+
125
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
126
+ }
127
+
128
+ return out;
129
+ };
130
+ ```
131
+
132
+ Now let's go through it section by section:
133
+
134
+ ```ts
135
+ export const __ByteString_prototype_toUpperCase = (_this: bytestring) => {
136
+ ```
137
+
138
+ Here we define a built-in for Porffor. Notably:
139
+ - We do not use `a.b.c`, instead we use `__a_b_c`
140
+ - We use a `_this` argument, as `this` does not exist in Porffor yet
141
+ - We use an arrow function
142
+ - We do not set a return type as prototype methods cannot use them currently or errors can happen.
143
+
144
+ ---
145
+
146
+ ```ts
147
+ const len: i32 = _this.length;
148
+
149
+ let out: bytestring = '';
150
+ Porffor.wasm.i32.store(out, len, 0, 0);
151
+ ```
152
+
153
+ This sets up the `out` variable we are going to write to for the output of this function. We set the length in advance to be the same as `_this`, as `foo.length == foo.toLowerCase().length`, because we will later be manually writing to it using Wasm intrinsics, which will not update the length themselves.
154
+
155
+ ---
156
+
157
+ ```ts
158
+ let i: i32 = Porffor.wasm`local.get ${_this}`,
159
+ j: i32 = Porffor.wasm`local.get ${out}`;
160
+ ```
161
+
162
+ Get the pointers for `_this` and `out` as `i32`s (~`number`s).
163
+
164
+ ---
165
+
166
+ ```ts
167
+ const endPtr: i32 = i + len;
168
+ while (i < endPtr) {
169
+ ```
170
+
171
+ Set up an end target pointer as the pointer variable for `_this` plus the length of it. Loop below until that pointer reaches the end target, so we iterate through the entire string.
172
+
173
+ ---
174
+
175
+ ```ts
176
+ let chr: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
177
+ ```
178
+
179
+ Read the character (code) from the current `_this` pointer variable, and increment it so next iteration it reads the next character, etc.
180
+
181
+ ---
182
+
183
+ ```ts
184
+ if (chr >= 97) if (chr <= 122) chr -= 32;
185
+ ```
186
+
187
+ If the character code is >= 97 (`a`) and <= 122 (`z`), decrease it by 32, making it an upper case character. eg: 97 (`a`) - 32 = 65 (`A`).
188
+
189
+ ---
190
+
191
+ ```ts
192
+ Porffor.wasm.i32.store8(j++, chr, 0, 4);
193
+ ```
194
+
195
+ Store the character code into the `out` pointer variable, and increment it.
196
+
197
+ <br>
198
+
199
+ ## Porffor-specific TS notes
200
+
201
+ - For declaring variables, you must use explicit type annotations currently (eg `let a: number = 1`, not `let a = 1`)
202
+ - You might spot `Porffor.fastOr`/`Porffor.fastAnd`, these are non-short circuiting versions of `||`/`&&`, taking any number of conditions as arguments. You shouldn't don't need to use or worry about these.
203
+ - **There are ~no objects, you cannot use them/literals.**
204
+ - Attempt to avoid string/array-heavy code and use more variables instead if possible, easier on memory and CPU/perf.
205
+ - Do not set a return type for prototype methods, it can cause errors/unexpected results.
206
+
207
+ <br>
208
+
209
+ ## Formatting/linting
210
+
211
+ There is 0 setup for this (right now). You can try looking through the other built-ins files but do not worry about it a lot, I honestly do not mind going through and cleaning up after a PR as long as the code itself is good :^)
212
+
213
+ <br>
214
+
215
+ ## Commit (message) style
216
+
217
+ You should ideally have one commit per notable change (using amend/force push). Commit messages should be like `${file}: ${description}`. Don't be afraid to use long titles if needed, but try and be short if possible. Bonus points for detail in commit description. ~~Gold star for jokes in description too.~~
218
+
219
+ Examples:
220
+ ```
221
+ builtins/date: impl toJSON
222
+ builtins/date: fix ToIntegerOrInfinity returning -0
223
+ codegen: fix inline wasm for unreachable
224
+ builtins/array: wip toReversed
225
+ builtins/tostring_number: impl radix
226
+ ```
227
+
228
+ <br>
229
+
230
+ ## Test262
231
+
232
+ Make sure you have Test262 cloned already **inside of `test262/`** (`git clone https://github.com/tc39/test262.git test262/test262`) and run `npm install` inside `test262/` too.
233
+
234
+ Run `node test262` to run all the tests and get an output of total overall test results. The main thing you want to pay attention to is the emoji summary (lol):
235
+ ```
236
+ 🧪 50005 | 🤠 7007 (-89) | ❌ 1914 (-32) | 💀 13904 (-61) | 📝 23477 (-120) | ⏰ 2 | 🏗 2073 (+302) | 💥 1628
237
+ ```
238
+
239
+ To break this down:
240
+ 🧪 total 🤠 pass ❌ fail 💀 runtime error 📝 todo (error) ⏰ timeout 🏗️ wasm compile error 💥 compile error
241
+
242
+ The diff compared to the last commit (with test262 data) is shown in brackets. Basically, you can passes 🤠 up, and errors 💀📝🏗💥 down. It is fine if some errors change balance/etc, as long as they are not new failures.
243
+
244
+ It will also log new passes/fails. Be careful as sometimes the overall passes can increase, but other files have also regressed into failures which you might miss. Also keep in mind some tests may have been false positives before, but we can investigate the diff together :)
245
+
246
+ ### Debugging tips
247
+
248
+ - Use `node test262 path/to/tests` to run specific test262 dirs/files (eg `node test262 built-ins/Date`).
249
+ - Use `--log-errors` to log the errors of individual tests.
250
+ - Use `--debug-asserts` to log expected/actual of assertion failures (experimental).
251
+
252
+ <br>
253
+
254
+ [^1]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `4` byte offset for length).
255
+
256
+ [^2]: The `0, 4` args are necessary for the Wasm instruction, but you don't need to worry about them (`0` alignment, `0` byte offset).
package/README.md CHANGED
@@ -14,60 +14,68 @@ Porffor is primarily built from scratch, the only thing that is not is the parse
14
14
  Expect nothing to work! Only very limited JS is currently supported. See files in `bench` for examples.
15
15
 
16
16
  ### Setup
17
- 1. Clone this repo (`git clone https://github.com/CanadaHonk/porffor.git`)
18
- 2. `npm install` - for parser(s)
17
+ **`npm install -g porffor`**. It's that easy (hopefully) :)
19
18
 
20
- ### Running a file
21
- The repos comes with easy alias files for Unix and Windows, which you can use like so:
22
- - Unix: `./porf path/to/script.js`
23
- - Windows: `.\porf path/to/script.js`
19
+ ### Trying a REPL
20
+ **`porf`**. Just run it with no script file argument.
24
21
 
25
- Please note that further examples below will just use `./porf`, you need to use `.\porf` on Windows. You can also swap out `node` in the alias to use another runtime like Deno (`deno run -A`) or Bun (`bun ...`), or just use it yourself (eg `node runner/index.js ...`, `bun runner/index.js ...`). Node and Bun should work great, Deno support is WIP.
22
+ ### Running a JS file
23
+ **`porf path/to/script.js`**
26
24
 
27
- ### Trying a REPL
28
- **`./porf`**. Just run it with no script file argument.
25
+ ### Compiling to Wasm
26
+ **`porf wasm path/to/script.js out.wasm`**. Currently it does not use an import standard like WASI, so it is mostly unusable on its own.
29
27
 
30
28
  ### Compiling to native binaries
31
29
  > [!WARNING]
32
30
  > Compiling to native binaries uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
33
31
 
34
- **`./porf native path/to/script.js out(.exe)`**. You can specify the compiler with `-compiler=clang/zig/gcc`, and which opt level to use with `-cO=O3` (`Ofast` by default). Output binaries are also stripped by default.
32
+ **`porf native path/to/script.js out(.exe)`**. You can specify the compiler with `--compiler=clang/zig/gcc`, and which opt level to use with `--cO=O3` (`Ofast` by default). Output binaries are also stripped by default.
35
33
 
36
34
  ### Compiling to C
37
35
  > [!WARNING]
38
36
  > Compiling to C uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
39
37
 
40
- **`./porf c path/to/script.js (out.c)`**. When not including an output file, it will be printed to stdout instead.
38
+ **`porf c path/to/script.js (out.c)`**. When not including an output file, it will be printed to stdout instead.
39
+
40
+ ### Profiling a JS file
41
+ > [!WARNING]
42
+ > Very experimental WIP feature!
43
+
44
+ **`porf profile path/to/script.js`**
45
+
46
+ ### Debugging a JS file
47
+ > [!WARNING]
48
+ > Very experimental WIP feature!
49
+
50
+ **`porf debug path/to/script.js`**
51
+
52
+ ### Profiling the generated Wasm of a JS file
53
+ > [!WARNING]
54
+ > Very experimental WIP feature!
55
+
56
+ **`porf debug-wasm path/to/script.js`**
41
57
 
42
- ### Compiling to a Wasm binary
43
- **`./porf compile path/to/script.js out.wasm`**. Currently it does not use an import standard like WASI, so it is mostly unusable.
44
58
 
45
59
  ### Options
46
- - `-target=wasm|c|native` (default: `wasm`) to set target output (native compiles c output to binary, see args below)
47
- - `-target=c|native` only:
48
- - `-o=out.c|out.exe|out` to set file to output c or binary
49
- - `-target=native` only:
50
- - `-compiler=clang` to set compiler binary (path/name) to use to compile
51
- - `-cO=O3` to set compiler opt argument
52
- - `-parser=acorn|@babel/parser|meriyah|hermes-parser` (default: `acorn`) to set which parser to use
53
- - `-parse-types` to enable parsing type annotations/typescript. if `-parser` is unset, changes default to `@babel/parser`. does not type check
54
- - `-opt-types` to perform optimizations using type annotations as compiler hints. does not type check
55
- - `-valtype=i32|i64|f64` (default: `f64`) to set valtype
60
+ - `--parser=acorn|@babel/parser|meriyah|hermes-parser` (default: `acorn`) to set which parser to use
61
+ - `--parse-types` to enable parsing type annotations/typescript. if `-parser` is unset, changes default to `@babel/parser`. does not type check
62
+ - `--opt-types` to perform optimizations using type annotations as compiler hints. does not type check
63
+ - `--valtype=i32|i64|f64` (default: `f64`) to set valtype
56
64
  - `-O0` to disable opt
57
65
  - `-O1` (default) to enable basic opt (simplify insts, treeshake wasm imports)
58
66
  - `-O2` to enable advanced opt (inlining). unstable
59
67
  - `-O3` to enable advanceder opt (precompute const math). unstable
60
- - `-no-run` to not run wasm output, just compile
61
- - `-opt-log` to log some opts
62
- - `-code-log` to log some codegen (you probably want `-funcs`)
63
- - `-regex-log` to log some regex
64
- - `-funcs` to log funcs
65
- - `-ast-log` to log AST
66
- - `-opt-funcs` to log funcs after opt
67
- - `-sections` to log sections as hex
68
- - `-opt-no-inline` to not inline any funcs
69
- - `-tail-call` to enable tail calls (experimental + not widely implemented)
70
- - `-compile-hints` to enable V8 compilation hints (experimental + doesn't seem to do much?)
68
+ - `--no-run` to not run wasm output, just compile
69
+ - `--opt-log` to log some opts
70
+ - `--code-log` to log some codegen (you probably want `-funcs`)
71
+ - `--regex-log` to log some regex
72
+ - `--funcs` to log funcs
73
+ - `--ast-log` to log AST
74
+ - `--opt-funcs` to log funcs after opt
75
+ - `--sections` to log sections as hex
76
+ - `--opt-no-inline` to not inline any funcs
77
+ - `--tail-call` to enable tail calls (experimental + not widely implemented)
78
+ - `--compile-hints` to enable V8 compilation hints (experimental + doesn't seem to do much?)
71
79
 
72
80
  ## Limitations
73
81
  - No full object support yet
@@ -151,23 +159,28 @@ These include some early (stage 1/0) and/or dead (last commit years ago) proposa
151
159
  - Array member setting (`arr[0] = 2`, `arr[0] += 2`, etc)
152
160
  - Array constructor (`Array(5)`, `new Array(1, 2, 3)`)
153
161
  - Labelled statements (`foo: while (...)`)
162
+ - `do...while` loops
154
163
 
155
164
  ### Built-ins
156
165
 
157
- - `NaN` and `Infinity` (f64 only)
158
- - `isNaN()` and `isFinite()` (f64 only)
159
- - Most of `Number` (`MAX_VALUE`, `MIN_VALUE`, `MAX_SAFE_INTEGER`, `MIN_SAFE_INTEGER`, `POSITIVE_INFINITY`, `NEGATIVE_INFINITY`, `EPSILON`, `NaN`, `isNaN`, `isFinite`, `isInteger`, `isSafeInteger`) (some f64 only)
160
- - Some `Math` funcs (`sqrt`, `abs`, `floor`, `sign`, `round`, `trunc`, `clz32`, `fround`, `random`) (f64 only)
166
+ - `NaN` and `Infinity`
167
+ - `isNaN()` and `isFinite()`
168
+ - Most of `Number` (`MAX_VALUE`, `MIN_VALUE`, `MAX_SAFE_INTEGER`, `MIN_SAFE_INTEGER`, `POSITIVE_INFINITY`, `NEGATIVE_INFINITY`, `EPSILON`, `NaN`, `isNaN`, `isFinite`, `isInteger`, `isSafeInteger`)
169
+ - Some `Math` funcs (`sqrt`, `abs`, `floor`, `sign`, `round`, `trunc`, `clz32`, `fround`, `random`)
161
170
  - Basic `globalThis` support
162
171
  - Basic `Boolean` and `Number`
163
172
  - Basic `eval` for literals
164
173
  - `Math.random()` using self-made xorshift128+ PRNG
165
- - Some of `performance` (`now()`)
166
- - Some of `Array.prototype` (`at`, `push`, `pop`, `shift`, `fill`)
174
+ - Some of `performance` (`now()`, `timeOrigin`)
175
+ - Some of `Array.prototype` (`at`, `push`, `pop`, `shift`, `fill`, `slice`, `indexOf`, `lastIndexOf`, `includes`, `with`, `reverse`, `toReversed`)
167
176
  - Some of `Array` (`of`, `isArray`)
168
- - Some of `String.prototype` (`at`, `charAt`, `charCodeAt`)
177
+ - Most of `String.prototype` (`at`, `charAt`, `charCodeAt`, `toUpperCase`, `toLowerCase`, `startsWith`, `endsWith`, `indexOf`, `lastIndexOf`, `includes`, `padStart`, `padEnd`, `substring`, `substr`, `slice`, `trimStart`, `trimEnd`, `trim`, `toString`, `big`, `blink`, `bold`, `fixed`, `italics`, `small`, `strike`, `sub`, `sup`, `trimLeft`, `trimRight`, )
169
178
  - Some of `crypto` (`randomUUID`)
170
179
  - `escape`
180
+ - `btoa`
181
+ - Most of `Number.prototype` (`toString`, `toFixed`, `toExponential`)
182
+ - `parseInt`
183
+ - Spec-compliant `Date`
171
184
 
172
185
  ### Custom
173
186
 
@@ -185,7 +198,7 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
185
198
  ### Traditional opts
186
199
  - Inlining functions (WIP, limited)
187
200
  - Inline const math ops
188
- - Tail calls (behind flag `-tail-call`)
201
+ - Tail calls (behind flag `--tail-call`)
189
202
 
190
203
  ### Wasm transforms
191
204
  - `local.set`, `local.get` -> `local.tee`
@@ -271,8 +284,6 @@ No particular order and no guarentees, just what could happen soon™
271
284
  - Run precompiled Wasm file if given
272
285
  - Docs
273
286
  - Update codebase readme section
274
- - REPL
275
- - Basic polyfill of `node:repl` for non-Node runtimes to work
276
287
  - Cool proposals
277
288
  - [Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
278
289
  - [Modulus and Additional Integer Math](https://github.com/tc39/proposal-integer-and-modulus-math)
@@ -292,6 +303,15 @@ No particular order and no guarentees, just what could happen soon™
292
303
  ## VSCode extension
293
304
  There is a vscode extension in `vscode-ext` which tweaks JS syntax highlighting to be nicer with porffor features (eg highlighting wasm inside of inline asm).
294
305
 
306
+ ## Wasm proposals used
307
+ Porffor intentionally does not use Wasm proposals which are not commonly implemented yet (eg GC) so it can be used in as many places as possible.
308
+
309
+ - Multi-value **(required)**
310
+ - Non-trapping float-to-int conversions **(required)**
311
+ - Bulk memory operations (required, but uncommonly used)
312
+ - Exception handling (optional, for errors)
313
+ - Tail calls (opt-in, off by default)
314
+
295
315
  ## Isn't this the same as AssemblyScript/other Wasm langs?
296
316
  No. they are not alike at all internally and have very different goals/ideals:
297
317
  - Porffor is made as a generic JS engine, not for Wasm stuff specifically
package/asur/index.js CHANGED
@@ -985,7 +985,7 @@ const stringifyOp = ({ types, funcs, codes, imports, globals }, op, porfFunc = o
985
985
  if (!porfFunc.invLocals) porfFunc.invLocals = inv(porfFunc.locals, x => x.idx);
986
986
 
987
987
  if (op.func != null) {
988
- str += ` \x1b[90m${op.func >= imports.length ? `${noHighlight(codes[op.func - imports.length].bc.porfFunc.name)}` : `${({ p: 'print', c: 'printChar', t: 'time', y: 'profile1', z: 'profile2' })[imports[op.func].name]}`}`;
988
+ str += ` \x1b[90m${op.func >= imports.length ? `${noHighlight(codes[op.func - imports.length].bc.porfFunc.name)}` : `${({ p: 'print', c: 'printChar', t: 'time', u: 'timeOrigin', y: 'profile1', z: 'profile2' })[imports[op.func].name]}`}`;
989
989
  const type = types[op.func >= imports.length ? funcs[op.func - imports.length] : imports[op.func].typeIdx];
990
990
  str += ` (${type.params.map(x => noHighlight(invValtype[x])).join(', ')}) -> (${type.returns.map(x => noHighlight(invValtype[x])).join(', ')})`;
991
991
  str += '\x1b[0m';
@@ -1244,7 +1244,7 @@ paused = _paused;`);
1244
1244
  });
1245
1245
 
1246
1246
  export const instantiate = async (binary, importImpls) => {
1247
- const _vm = process?.argv?.includes('-wasm-debug') ? await wasmDebugVm() : vm;
1247
+ const _vm = process?.argv?.includes('--wasm-debug') ? await wasmDebugVm() : vm;
1248
1248
 
1249
1249
  const parsed = parse(binary);
1250
1250
  const exports = {};
package/byg/index.js CHANGED
@@ -2,7 +2,6 @@ import fs from 'node:fs';
2
2
 
3
3
  const noAnsi = s => s.replace(/\u001b\[[0-9]+m/g, '');
4
4
  const printLine = (line, number, breakpoint = false, current = false, selected = false) => {
5
- // console.log(`\x1b[${breakpoint ? (selected ? '106' : '46') : (selected ? '47' : '100')}m\x1b[${selected ? '30' : '97'}m${number.toFixed(0).padStart(4, ' ')}\x1b[${breakpoint ? (selected ? '96' : '36') : (selected ? '37' : '90')}m\x1b[${current ? '47' : '40'}m▌ \x1b[${current ? '47' : '40'}m\x1b[${current ? '30' : '37'}m${current ? noAnsi(line) : line}\x1b[0K`);
6
5
  console.log(`\x1b[${breakpoint ? (selected ? '43' : '103') : (selected ? '47' : '100')}m\x1b[${selected || breakpoint ? '30' : '97'}m${number.toFixed(0).padStart(4, ' ')}\x1b[${breakpoint ? (selected ? '33' : '93') : (selected ? '37' : '90')}m\x1b[${current ? '47' : '40'}m▌ \x1b[${current ? '47' : '40'}m\x1b[${current ? '30' : '37'}m${current ? noAnsi(line) : line}\x1b[0K`);
7
6
  };
8
7
 
@@ -13,13 +12,10 @@ const box = (x, y, width, height, title = '', content = [], color = ['90', '100'
13
12
 
14
13
  // top
15
14
  process.stdout.write(`\x1b[48m\x1b[${y + 1};${x + 1}H\x1b[${color[0]}m` + '▄'.repeat(width));
16
-
17
15
  // bottom
18
16
  process.stdout.write(`\x1b[${y + height + 1};${x + 1}H▝` + '▀'.repeat(width - 1) + '▘');
19
-
20
17
  // left
21
18
  process.stdout.write(`\x1b[${y + 1};${x + 1}H▗` + '\x1b[1B\x1b[1D▐'.repeat(height - 1));
22
-
23
19
  // right
24
20
  process.stdout.write(`\x1b[${y + 1};${x + width + 1}H▖` + '\x1b[1B\x1b[1D▌'.repeat(height - 1));
25
21
 
@@ -33,8 +29,6 @@ const box = (x, y, width, height, title = '', content = [], color = ['90', '100'
33
29
  process.stdout.write(`\x1b[${y + 1};${x + 1}H\x1b[${color[1]}m` + ' '.repeat(width) + (`\x1b[1B\x1b[${width}D` + ' '.repeat(width)).repeat(Math.max(0, height - 1)));
34
30
 
35
31
  // title
36
- // if (title) process.stdout.write(`\x1b[${y + 1};${x + ((width - title.length) / 2 | 0) + 1}H\x1b[${color[1]}m\x1b[${color[2]}m\x1b[1m${title}\x1b[22m`);
37
- // if (title) process.stdout.write(`\x1b[${y};${x}H\x1b[${color[3]}▗\x1b[${color[4]}m\x1b[${color[2]}m\x1b[1m${' '.repeat((width - title.length) / 2 | 0)}${title}${' '.repeat(width - (((width - title.length) / 2 | 0)) - title.length)}\x1b[0m\x1b[${color[4]}m▖`);
38
32
  if (title) process.stdout.write(`\x1b[${y};${x}H\x1b[0m\x1b[${color[3]}m▐\x1b[${color[4]}m\x1b[${color[2]}m\x1b[1m${' '.repeat((width - title.length) / 2 | 0)}${title}${' '.repeat(width - (((width - title.length) / 2 | 0)) - title.length)}\x1b[0m\x1b[${color[3]}m▌`);
39
33
 
40
34
  // content
@@ -66,8 +60,6 @@ export default ({ lines, pause, breakpoint }) => {
66
60
  process.exit();
67
61
  }
68
62
 
69
- // process.stdout.write(s);
70
-
71
63
  if (!paused) pause();
72
64
  });
73
65
 
@@ -83,7 +75,6 @@ export default ({ lines, pause, breakpoint }) => {
83
75
 
84
76
  process.on('exit', () => {
85
77
  process.stdout.write('\x1b[0m');
86
- // console.clear();
87
78
  });
88
79
 
89
80
  console.clear();
@@ -130,18 +121,9 @@ export default ({ lines, pause, breakpoint }) => {
130
121
 
131
122
  for (const x of boxes) {
132
123
  const y = x.y({ currentLinePos });
133
- const height = x.height;
134
124
  if (y < 0 || y >= termHeight) continue;
135
125
 
136
- // crop box if > termHeight
137
- // if (y + height >= termHeight) {
138
- // const excess = y + height - termHeight;
139
- // height -= excess;
140
-
141
- // content = content.slice(0, height - 2);
142
- // }
143
-
144
- box(x.x, y, x.width, height, x.title, x.content);
126
+ box(x.x, y, x.width, x.height, x.title, x.content);
145
127
  }
146
128
 
147
129
  // text += ` | rss: ${(process.memoryUsage.rss() / 1024 / 1024).toFixed(2)}mb`;
@@ -179,7 +161,8 @@ export default ({ lines, pause, breakpoint }) => {
179
161
  }
180
162
 
181
163
  case 'b': {
182
- if (!lastSpecial) { // b pressed normally
164
+ if (!lastSpecial) {
165
+ // b pressed normally
183
166
  breakpoints[currentLine + scrollOffset] = !breakpoints[currentLine + scrollOffset];
184
167
  draw();
185
168
 
@@ -188,7 +171,6 @@ export default ({ lines, pause, breakpoint }) => {
188
171
  }
189
172
 
190
173
  // arrow down
191
- // if (screenOffset + termHeight <= lines.length) scrollOffset++;
192
174
  if (scrollOffset < lines.length - currentLine - 1) scrollOffset++;
193
175
  draw();
194
176
  break;
@@ -198,7 +180,6 @@ export default ({ lines, pause, breakpoint }) => {
198
180
  if (!lastSpecial) break;
199
181
 
200
182
  // arrow up
201
- // if (screenOffset > 0) scrollOffset--;
202
183
  if (scrollOffset > -currentLine) scrollOffset--;
203
184
  draw();
204
185
 
@@ -209,7 +190,6 @@ export default ({ lines, pause, breakpoint }) => {
209
190
  if (!lastSpecial) break;
210
191
 
211
192
  // page up
212
- // scrollOffset -= Math.min(screenOffset, termHeight - 1);
213
193
  scrollOffset -= Math.min(scrollOffset + currentLine, termHeight - 1);
214
194
  draw();
215
195
  break;
@@ -219,7 +199,6 @@ export default ({ lines, pause, breakpoint }) => {
219
199
  if (!lastSpecial) break;
220
200
 
221
201
  // page down
222
- // scrollOffset += Math.min((lines.length + 1) - (screenOffset + termHeight), termHeight - 1);
223
202
  scrollOffset += Math.min(lines.length - (scrollOffset + currentLine) - 1, termHeight - 1);
224
203
  draw();
225
204
  break;