porffor 0.2.0-fde989a → 0.14.0-032e4ad08
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +262 -0
- package/LICENSE +20 -20
- package/README.md +135 -94
- package/asur/README.md +2 -0
- package/asur/index.js +1262 -0
- package/byg/index.js +216 -0
- package/compiler/2c.js +66 -54
- package/compiler/{sections.js → assemble.js} +109 -21
- package/compiler/builtins/annexb_string.js +72 -0
- package/compiler/builtins/annexb_string.ts +19 -0
- package/compiler/builtins/array.ts +225 -0
- package/compiler/builtins/base64.ts +77 -0
- package/compiler/builtins/boolean.ts +20 -0
- package/compiler/builtins/crypto.ts +121 -0
- package/compiler/builtins/date.ts +2069 -0
- package/compiler/builtins/error.js +22 -0
- package/compiler/builtins/escape.ts +140 -0
- package/compiler/builtins/function.ts +7 -0
- package/compiler/builtins/int.ts +147 -0
- package/compiler/builtins/math.ts +410 -0
- package/compiler/builtins/number.ts +531 -0
- package/compiler/builtins/object.ts +6 -0
- package/compiler/builtins/porffor.d.ts +60 -0
- package/compiler/builtins/set.ts +199 -0
- package/compiler/builtins/string.ts +1081 -0
- package/compiler/builtins/symbol.ts +62 -0
- package/compiler/builtins.js +466 -284
- package/compiler/{codeGen.js → codegen.js} +1573 -656
- package/compiler/decompile.js +3 -4
- package/compiler/embedding.js +22 -22
- package/compiler/encoding.js +94 -10
- package/compiler/expression.js +1 -1
- package/compiler/generated_builtins.js +2110 -0
- package/compiler/index.js +29 -44
- package/compiler/log.js +6 -3
- package/compiler/opt.js +55 -41
- package/compiler/parse.js +38 -30
- package/compiler/precompile.js +121 -0
- package/compiler/prefs.js +31 -0
- package/compiler/prototype.js +209 -201
- package/compiler/types.js +38 -0
- package/compiler/wasmSpec.js +33 -8
- package/compiler/wrap.js +154 -89
- package/package.json +9 -5
- package/porf +2 -0
- package/porffor_tmp.c +202 -0
- package/rhemyn/compile.js +46 -27
- package/rhemyn/parse.js +322 -320
- package/rhemyn/test/parse.js +58 -58
- package/runner/compare.js +33 -34
- package/runner/debug.js +117 -0
- package/runner/index.js +80 -12
- package/runner/profiler.js +75 -0
- package/runner/repl.js +58 -15
- package/runner/sizes.js +37 -37
- package/runner/version.js +10 -8
- package/compiler/builtins/base64.js +0 -92
- package/filesize.cmd +0 -2
- package/runner/info.js +0 -89
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/runner/transform.js +0 -15
- package/tmp.c +0 -661
- package/util/enum.js +0 -20
package/CONTRIBUTING.md
ADDED
@@ -0,0 +1,262 @@
|
|
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.**
|
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
|
+
- You cannot use other functions in the file not exported, or variables not inside the current function.
|
207
|
+
- `if (...)` uses a fast truthy implementation which is not spec-compliant as most conditions should be strictly checked. To use spec-compliant behavior, use `if (Boolean(...))`.
|
208
|
+
|
209
|
+
<br>
|
210
|
+
|
211
|
+
## Formatting/linting
|
212
|
+
|
213
|
+
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 :^)
|
214
|
+
|
215
|
+
<br>
|
216
|
+
|
217
|
+
## Commit (message) style
|
218
|
+
|
219
|
+
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.~~
|
220
|
+
|
221
|
+
Examples:
|
222
|
+
```
|
223
|
+
builtins/date: impl toJSON
|
224
|
+
builtins/date: fix ToIntegerOrInfinity returning -0
|
225
|
+
codegen: fix inline wasm for unreachable
|
226
|
+
builtins/array: wip toReversed
|
227
|
+
builtins/tostring_number: impl radix
|
228
|
+
```
|
229
|
+
|
230
|
+
<br>
|
231
|
+
|
232
|
+
## Test262
|
233
|
+
|
234
|
+
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.
|
235
|
+
|
236
|
+
Run `node test262` to run all the tests and get an output of total overall test results.
|
237
|
+
|
238
|
+
Warning: this will consume 1-6GB of memory and ~90% of all CPU cores while running (depending on thread count), it should take 15-120s depending on machine. You can specify how many threads with `--threads=N`, it will use the number of CPU threads by default.
|
239
|
+
|
240
|
+
The main thing you want to pay attention to is the emoji summary (lol):
|
241
|
+
```
|
242
|
+
🧪 50005 | 🤠 7007 (-89) | ❌ 1914 (-32) | 💀 13904 (-61) | 📝 23477 (-120) | ⏰ 2 | 🏗 2073 (+302) | 💥 1628
|
243
|
+
```
|
244
|
+
|
245
|
+
To break this down:
|
246
|
+
🧪 total 🤠 pass ❌ fail 💀 runtime error 📝 todo (error) ⏰ timeout 🏗️ wasm compile error 💥 compile error
|
247
|
+
|
248
|
+
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.
|
249
|
+
|
250
|
+
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 :)
|
251
|
+
|
252
|
+
### Debugging tips
|
253
|
+
|
254
|
+
- Use `node test262 path/to/tests` to run specific test262 dirs/files (eg `node test262 built-ins/Date`).
|
255
|
+
- Use `--log-errors` to log the errors of individual tests.
|
256
|
+
- Use `--debug-asserts` to log expected/actual of assertion failures (experimental).
|
257
|
+
|
258
|
+
<br>
|
259
|
+
|
260
|
+
[^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).
|
261
|
+
|
262
|
+
[^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/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2023 CanadaHonk
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 CanadaHonk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
@@ -13,61 +13,69 @@ Porffor is primarily built from scratch, the only thing that is not is the parse
|
|
13
13
|
## Usage
|
14
14
|
Expect nothing to work! Only very limited JS is currently supported. See files in `bench` for examples.
|
15
15
|
|
16
|
-
###
|
17
|
-
|
18
|
-
2. `npm install` - for parser(s)
|
16
|
+
### Install
|
17
|
+
**`npm install -g porffor`**. It's that easy (hopefully) :)
|
19
18
|
|
20
|
-
###
|
21
|
-
|
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
|
-
|
22
|
+
### Running a JS file
|
23
|
+
**`porf path/to/script.js`**
|
26
24
|
|
27
|
-
###
|
28
|
-
|
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
|
-
|
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
|
-
|
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
|
-
-
|
47
|
-
- `-
|
48
|
-
|
49
|
-
-
|
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
|
-
-
|
61
|
-
-
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
65
|
-
-
|
66
|
-
-
|
67
|
-
-
|
68
|
-
-
|
69
|
-
-
|
70
|
-
-
|
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
|
@@ -77,10 +85,15 @@ Please note that further examples below will just use `./porf`, you need to use
|
|
77
85
|
- Literal callees only in calls (eg `print()` works, `a = print; a()` does not)
|
78
86
|
- No `eval()` etc (since it is AOT)
|
79
87
|
|
80
|
-
##
|
88
|
+
## Sub-engines
|
89
|
+
|
90
|
+
### Asur
|
91
|
+
Asur is Porffor's own Wasm engine; it is an intentionally simple interpreter written in JS. It is very WIP. See [its readme](asur/README.md) for more details.
|
92
|
+
|
93
|
+
### Rhemyn
|
81
94
|
Rhemyn is Porffor's own regex engine; it compiles literal regex to Wasm bytecode AOT (remind you of anything?). It is quite basic and WIP. See [its readme](rhemyn/README.md) for more details.
|
82
95
|
|
83
|
-
|
96
|
+
### 2c
|
84
97
|
2c is Porffor's own Wasm -> C compiler, using generated Wasm bytecode and internal info to generate specific and efficient/fast C code. Little boilerplate/preluded code or required external files, just for CLI binaries (not like wasm2c very much).
|
85
98
|
|
86
99
|
## Supported
|
@@ -145,20 +158,29 @@ These include some early (stage 1/0) and/or dead (last commit years ago) proposa
|
|
145
158
|
- `for...of` (arrays and strings)
|
146
159
|
- Array member setting (`arr[0] = 2`, `arr[0] += 2`, etc)
|
147
160
|
- Array constructor (`Array(5)`, `new Array(1, 2, 3)`)
|
161
|
+
- Labelled statements (`foo: while (...)`)
|
162
|
+
- `do...while` loops
|
148
163
|
|
149
164
|
### Built-ins
|
150
165
|
|
151
|
-
- `NaN` and `Infinity`
|
152
|
-
- `isNaN()` and `isFinite()`
|
153
|
-
- Most of `Number` (`MAX_VALUE`, `MIN_VALUE`, `MAX_SAFE_INTEGER`, `MIN_SAFE_INTEGER`, `POSITIVE_INFINITY`, `NEGATIVE_INFINITY`, `EPSILON`, `NaN`, `isNaN`, `isFinite`, `isInteger`, `isSafeInteger`)
|
154
|
-
- Some `Math` funcs (`
|
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`)
|
155
170
|
- Basic `globalThis` support
|
156
171
|
- Basic `Boolean` and `Number`
|
157
172
|
- Basic `eval` for literals
|
158
173
|
- `Math.random()` using self-made xorshift128+ PRNG
|
159
|
-
- Some of `performance` (`now()`)
|
160
|
-
- Some of `Array.prototype` (`at`, `push`, `pop`, `shift`, `fill`)
|
161
|
-
- Some of `
|
174
|
+
- Some of `performance` (`now()`, `timeOrigin`)
|
175
|
+
- Some of `Array.prototype` (`at`, `push`, `pop`, `shift`, `fill`, `slice`, `indexOf`, `lastIndexOf`, `includes`, `with`, `reverse`, `toReversed`)
|
176
|
+
- Some of `Array` (`of`, `isArray`)
|
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`, )
|
178
|
+
- Some of `crypto` (`randomUUID`)
|
179
|
+
- `escape`
|
180
|
+
- `btoa`
|
181
|
+
- Most of `Number.prototype` (`toString`, `toFixed`, `toExponential`)
|
182
|
+
- `parseInt`
|
183
|
+
- Spec-compliant `Date`
|
162
184
|
|
163
185
|
### Custom
|
164
186
|
|
@@ -167,43 +189,12 @@ These include some early (stage 1/0) and/or dead (last commit years ago) proposa
|
|
167
189
|
- Intrinsic functions (see below)
|
168
190
|
- Inlining wasm via ``asm`...``\` "macro"
|
169
191
|
|
170
|
-
##
|
171
|
-
|
172
|
-
|
173
|
-
-
|
174
|
-
|
175
|
-
|
176
|
-
- Destructuring
|
177
|
-
- Objects
|
178
|
-
- Basic object expressions (eg `{}`, `{ a: 0 }`)
|
179
|
-
- Wasm
|
180
|
-
- *Basic* Wasm engine (interpreter) in JS
|
181
|
-
- More math operators (`**`, etc)
|
182
|
-
- `do { ... } while (...)`
|
183
|
-
- Typed export inputs (array)
|
184
|
-
- Exceptions
|
185
|
-
- Rewrite to use actual strings (optional?)
|
186
|
-
- `try { } finally { }`
|
187
|
-
- Rethrowing inside catch
|
188
|
-
- Optimizations
|
189
|
-
- Rewrite local indexes per func for smallest local header and remove unused idxs
|
190
|
-
- Smarter inline selection (snapshots?)
|
191
|
-
- Remove const ifs (`if (true)`, etc)
|
192
|
-
- Memory alignment
|
193
|
-
- Runtime
|
194
|
-
- WASI target
|
195
|
-
- Run precompiled Wasm file if given
|
196
|
-
- Cool proposals
|
197
|
-
- [Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
|
198
|
-
- [Modulus and Additional Integer Math](https://github.com/tc39/proposal-integer-and-modulus-math)
|
199
|
-
- [Array Equality](https://github.com/tc39/proposal-array-equality)
|
200
|
-
- [Declarations in Conditionals](https://github.com/tc39/proposal-Declarations-in-Conditionals)
|
201
|
-
- [Seeded Pseudo-Random Numbers](https://github.com/tc39/proposal-seeded-random)
|
202
|
-
- [`do` expressions](https://github.com/tc39/proposal-do-expressions)
|
203
|
-
- [String Trim Characters](https://github.com/Kingwl/proposal-string-trim-characters)
|
204
|
-
- Posts
|
205
|
-
- Inlining investigation
|
206
|
-
- Self hosted testing?
|
192
|
+
## Versioning
|
193
|
+
Porffor uses a unique versioning system, here's an example: `0.14.0-15cb49f07`. Let's break it down:
|
194
|
+
1. `0` - major, always `0` as Porffor is not ready yet
|
195
|
+
2. `14` - minor, total Test262 pass percentage (floored to nearest int)
|
196
|
+
3. `0` - micro, always `0` as unused
|
197
|
+
4. `15cb49f07` - commit hash
|
207
198
|
|
208
199
|
## Performance
|
209
200
|
*For the features it supports most of the time*, Porffor is *blazingly fast* compared to most interpreters and common engines running without JIT. For those with JIT, it is usually slower by default, but can catch up with compiler arguments and typed input, even more so when compiling to native binaries.
|
@@ -214,7 +205,7 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
|
|
214
205
|
### Traditional opts
|
215
206
|
- Inlining functions (WIP, limited)
|
216
207
|
- Inline const math ops
|
217
|
-
- Tail calls (behind flag
|
208
|
+
- Tail calls (behind flag `--tail-call`)
|
218
209
|
|
219
210
|
### Wasm transforms
|
220
211
|
- `local.set`, `local.get` -> `local.tee`
|
@@ -236,12 +227,12 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
|
|
236
227
|
- No tags if unused/optimized out
|
237
228
|
|
238
229
|
## Test262
|
239
|
-
Porffor can run Test262 via some hacks/transforms which remove unsupported features whilst still doing the same asserts (eg simpler error messages using literals only). It currently passes >
|
230
|
+
Porffor can run Test262 via some hacks/transforms which remove unsupported features whilst still doing the same asserts (eg simpler error messages using literals only). It currently passes >14% (see latest commit desc for latest and details). Use `node test262` to test, it will also show a difference of overall results between the last commit and current results.
|
240
231
|
|
241
232
|
## Codebase
|
242
233
|
- `compiler`: contains the compiler itself
|
243
234
|
- `builtins.js`: all built-ins of the engine (spec, custom. vars, funcs)
|
244
|
-
- `
|
235
|
+
- `codegen.js`: code (wasm) generation, ast -> wasm. The bulk of the effort
|
245
236
|
- `decompile.js`: basic wasm decompiler for debug info
|
246
237
|
- `embedding.js`: utils for embedding consts
|
247
238
|
- `encoding.js`: utils for encoding things as bytes as wasm expects
|
@@ -249,7 +240,7 @@ Porffor can run Test262 via some hacks/transforms which remove unsupported featu
|
|
249
240
|
- `index.js`: doing all the compiler steps, takes code in, wasm out
|
250
241
|
- `opt.js`: self-made wasm bytecode optimizer
|
251
242
|
- `parse.js`: parser simply wrapping acorn
|
252
|
-
- `
|
243
|
+
- `assemble.js`: assembles wasm ops and metadata into a wasm module/file
|
253
244
|
- `wasmSpec.js`: "enums"/info from wasm spec
|
254
245
|
- `wrap.js`: wrapper for compiler which instantiates and produces nice exports
|
255
246
|
|
@@ -271,15 +262,61 @@ Basically none right now (other than giving people headaches). Potential ideas:
|
|
271
262
|
- Compiling JS to native binaries. This is still very early!
|
272
263
|
- More in future probably?
|
273
264
|
|
265
|
+
## Todo
|
266
|
+
No particular order and no guarentees, just what could happen soon™
|
267
|
+
|
268
|
+
- Arrays
|
269
|
+
- Destructuring
|
270
|
+
- Objects
|
271
|
+
- Basic object expressions (eg `{}`, `{ a: 0 }`)
|
272
|
+
- Asur
|
273
|
+
- Support memory
|
274
|
+
- Support exceptions
|
275
|
+
- More math operators (`**`, etc)
|
276
|
+
- Typed export inputs (array)
|
277
|
+
- Exceptions
|
278
|
+
- Rewrite to use actual strings (optional?)
|
279
|
+
- `try { } finally { }`
|
280
|
+
- Rethrowing inside catch
|
281
|
+
- Optimizations
|
282
|
+
- Rewrite local indexes per func for smallest local header and remove unused idxs
|
283
|
+
- Smarter inline selection (snapshots?)
|
284
|
+
- Remove const ifs (`if (true)`, etc)
|
285
|
+
- Memory alignment
|
286
|
+
- Add general pref for always using "fast" (non-short circuiting) or/and
|
287
|
+
- Runtime
|
288
|
+
- WASI target
|
289
|
+
- Run precompiled Wasm file if given
|
290
|
+
- Docs
|
291
|
+
- Update codebase readme section
|
292
|
+
- Cool proposals
|
293
|
+
- [Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
|
294
|
+
- [Modulus and Additional Integer Math](https://github.com/tc39/proposal-integer-and-modulus-math)
|
295
|
+
- [Array Equality](https://github.com/tc39/proposal-array-equality)
|
296
|
+
- [Declarations in Conditionals](https://github.com/tc39/proposal-Declarations-in-Conditionals)
|
297
|
+
- [Seeded Pseudo-Random Numbers](https://github.com/tc39/proposal-seeded-random)
|
298
|
+
- [`do` expressions](https://github.com/tc39/proposal-do-expressions)
|
299
|
+
- [String Trim Characters](https://github.com/Kingwl/proposal-string-trim-characters)
|
300
|
+
- Posts
|
301
|
+
- Inlining investigation
|
302
|
+
- JS -> Native
|
303
|
+
- Precompiled TS built-ins
|
304
|
+
- Asur
|
305
|
+
- `escape()` optimization
|
306
|
+
- Self hosted testing?
|
307
|
+
|
274
308
|
## VSCode extension
|
275
|
-
There is a vscode extension in `
|
309
|
+
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).
|
310
|
+
|
311
|
+
## Wasm proposals used
|
312
|
+
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.
|
313
|
+
|
314
|
+
- Multi-value **(required)**
|
315
|
+
- Non-trapping float-to-int conversions **(required)**
|
316
|
+
- Bulk memory operations (optional, can get away without sometimes)
|
317
|
+
- Exception handling (optional, only for errors)
|
318
|
+
- Tail calls (opt-in, off by default)
|
276
319
|
|
277
|
-
## Isn't this the same as AssemblyScript/other Wasm langs?
|
278
|
-
No. they are not alike at all internally and have very different goals/ideals:
|
279
|
-
- Porffor is made as a generic JS engine, not for Wasm stuff specifically
|
280
|
-
- Porffor primarily consumes JS
|
281
|
-
- Porffor is written in pure JS and compiles itself, not using Binaryen/etc
|
282
|
-
- (Also I didn't know it existed when I started this, lol)
|
283
320
|
|
284
321
|
## FAQ
|
285
322
|
|
@@ -293,5 +330,9 @@ No. they are not alike at all internally and have very different goals/ideals:
|
|
293
330
|
### 2. Why at all?
|
294
331
|
Yes!
|
295
332
|
|
296
|
-
|
297
|
-
|
333
|
+
## 3. Isn't this the same as AssemblyScript/other Wasm langs?
|
334
|
+
No. they are not alike at all internally and have very different goals/ideals:
|
335
|
+
- Porffor is made as a generic JS engine, not for Wasm stuff specifically
|
336
|
+
- Porffor primarily consumes JS
|
337
|
+
- Porffor is written in pure JS and compiles itself, not using Binaryen/etc
|
338
|
+
- (Also I didn't know it existed when I started this, lol)
|
package/asur/README.md
ADDED