porffor 0.2.0-536e463 → 0.2.0-5ac7ea0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -49
- package/asur/README.md +2 -0
- package/asur/index.js +978 -0
- package/compiler/2c.js +321 -71
- package/compiler/builtins/base64.ts +153 -0
- package/compiler/builtins/crypto.ts +132 -0
- package/compiler/builtins/porffor.d.ts +26 -0
- package/compiler/builtins.js +590 -255
- package/compiler/codeGen.js +641 -309
- package/compiler/decompile.js +4 -4
- package/compiler/encoding.js +2 -116
- package/compiler/generated_builtins.js +25 -0
- package/compiler/index.js +22 -22
- package/compiler/log.js +4 -1
- package/compiler/opt.js +61 -26
- package/compiler/parse.js +13 -12
- package/compiler/precompile.js +129 -0
- package/compiler/prefs.js +26 -0
- package/compiler/prototype.js +177 -21
- package/compiler/sections.js +8 -7
- package/compiler/wasmSpec.js +20 -6
- package/compiler/wrap.js +112 -11
- package/package.json +1 -1
- package/porf +2 -0
- package/rhemyn/compile.js +2 -1
- package/runner/index.js +26 -3
- package/runner/profiler.js +83 -0
- package/runner/repl.js +2 -2
- package/compiler/builtins/base64.js +0 -92
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/tmp.c +0 -69
package/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# Porffor <sup><sub>/ˈpɔrfɔr/ *(poor-for)*</sup></sub>
|
2
|
-
A from-scratch experimental **AOT** optimizing JS -> Wasm/C engine/compiler/runtime in JS. Not serious/intended for (real) use. (this is a straight forward, honest readme)<br>
|
2
|
+
A from-scratch experimental **AOT** optimizing JS/TS -> Wasm/C engine/compiler/runtime in JS. Not serious/intended for (real) use. (this is a straight forward, honest readme)<br>
|
3
3
|
Age: ~6 months (very on and off)
|
4
4
|
|
5
5
|
## Design
|
@@ -10,6 +10,65 @@ Porffor is a very unique JS engine, due many wildly different approaches. It is
|
|
10
10
|
|
11
11
|
Porffor is primarily built from scratch, the only thing that is not is the parser (using [Acorn](https://github.com/acornjs/acorn)). Binaryen/etc is not used, we make final wasm binaries ourself. You could imagine it as compiling a language which is a sub (some things unsupported) and super (new/custom apis) set of javascript. Not based on any particular spec version, focusing on function/working over spec compliance.
|
12
12
|
|
13
|
+
## Usage
|
14
|
+
Expect nothing to work! Only very limited JS is currently supported. See files in `bench` for examples.
|
15
|
+
|
16
|
+
### Setup
|
17
|
+
1. Clone this repo (`git clone https://github.com/CanadaHonk/porffor.git`)
|
18
|
+
2. `npm install` - for parser(s)
|
19
|
+
|
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`
|
24
|
+
|
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.
|
26
|
+
|
27
|
+
### Trying a REPL
|
28
|
+
**`./porf`**. Just run it with no script file argument.
|
29
|
+
|
30
|
+
### Compiling to native binaries
|
31
|
+
> [!WARNING]
|
32
|
+
> Compiling to native binaries uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
|
33
|
+
|
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.
|
35
|
+
|
36
|
+
### Compiling to C
|
37
|
+
> [!WARNING]
|
38
|
+
> Compiling to C uses [2c](#2c), Porffor's own Wasm -> C compiler, which is experimental.
|
39
|
+
|
40
|
+
**`./porf c path/to/script.js (out.c)`**. When not including an output file, it will be printed to stdout instead.
|
41
|
+
|
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
|
+
|
45
|
+
### 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
|
56
|
+
- `-O0` to disable opt
|
57
|
+
- `-O1` (default) to enable basic opt (simplify insts, treeshake wasm imports)
|
58
|
+
- `-O2` to enable advanced opt (inlining). unstable
|
59
|
+
- `-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?)
|
71
|
+
|
13
72
|
## Limitations
|
14
73
|
- No full object support yet
|
15
74
|
- Little built-ins/prototype
|
@@ -18,10 +77,15 @@ Porffor is primarily built from scratch, the only thing that is not is the parse
|
|
18
77
|
- Literal callees only in calls (eg `print()` works, `a = print; a()` does not)
|
19
78
|
- No `eval()` etc (since it is AOT)
|
20
79
|
|
21
|
-
##
|
80
|
+
## Sub-engines
|
81
|
+
|
82
|
+
### Asur
|
83
|
+
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.
|
84
|
+
|
85
|
+
### Rhemyn
|
22
86
|
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.
|
23
87
|
|
24
|
-
|
88
|
+
### 2c
|
25
89
|
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).
|
26
90
|
|
27
91
|
## Supported
|
@@ -118,10 +182,10 @@ No particular order and no guarentees, just what could happen soon™
|
|
118
182
|
- Objects
|
119
183
|
- Basic object expressions (eg `{}`, `{ a: 0 }`)
|
120
184
|
- Wasm
|
121
|
-
- *Basic* Wasm engine (interpreter) in
|
185
|
+
- *Basic* Wasm engine (interpreter) in JS
|
122
186
|
- More math operators (`**`, etc)
|
123
187
|
- `do { ... } while (...)`
|
124
|
-
-
|
188
|
+
- Typed export inputs (array)
|
125
189
|
- Exceptions
|
126
190
|
- Rewrite to use actual strings (optional?)
|
127
191
|
- `try { } finally { }`
|
@@ -130,12 +194,24 @@ No particular order and no guarentees, just what could happen soon™
|
|
130
194
|
- Rewrite local indexes per func for smallest local header and remove unused idxs
|
131
195
|
- Smarter inline selection (snapshots?)
|
132
196
|
- Remove const ifs (`if (true)`, etc)
|
133
|
-
-
|
197
|
+
- Memory alignment
|
198
|
+
- Runtime
|
199
|
+
- WASI target
|
200
|
+
- Run precompiled Wasm file if given
|
201
|
+
- Cool proposals
|
202
|
+
- [Optional Chaining Assignment](https://github.com/tc39/proposal-optional-chaining-assignment)
|
203
|
+
- [Modulus and Additional Integer Math](https://github.com/tc39/proposal-integer-and-modulus-math)
|
204
|
+
- [Array Equality](https://github.com/tc39/proposal-array-equality)
|
205
|
+
- [Declarations in Conditionals](https://github.com/tc39/proposal-Declarations-in-Conditionals)
|
206
|
+
- [Seeded Pseudo-Random Numbers](https://github.com/tc39/proposal-seeded-random)
|
207
|
+
- [`do` expressions](https://github.com/tc39/proposal-do-expressions)
|
208
|
+
- [String Trim Characters](https://github.com/Kingwl/proposal-string-trim-characters)
|
209
|
+
- Posts
|
210
|
+
- Inlining investigation
|
211
|
+
- Self hosted testing?
|
134
212
|
|
135
213
|
## Performance
|
136
|
-
*For the
|
137
|
-
|
138
|
-

|
214
|
+
*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.
|
139
215
|
|
140
216
|
## Optimizations
|
141
217
|
Mostly for reducing size. I do not really care about compiler perf/time as long as it is reasonable. We do not use/rely on external opt tools (`wasm-opt`, etc), instead doing optimization inside the compiler itself creating even smaller code sizes than `wasm-opt` itself can produce as we have more internal information.
|
@@ -157,10 +233,12 @@ Mostly for reducing size. I do not really care about compiler perf/time as long
|
|
157
233
|
- Remove unneeded blocks (no `br`s inside)
|
158
234
|
- Remove unused imports
|
159
235
|
- Use data segments for initing arrays/strings
|
236
|
+
- (Likely more not documented yet, todo)
|
160
237
|
|
161
238
|
### Wasm module
|
162
239
|
- Type cache/index (no repeated types)
|
163
240
|
- No main func if empty (and other exports)
|
241
|
+
- No tags if unused/optimized out
|
164
242
|
|
165
243
|
## Test262
|
166
244
|
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 >10% (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.
|
@@ -180,7 +258,7 @@ Porffor can run Test262 via some hacks/transforms which remove unsupported featu
|
|
180
258
|
- `wasmSpec.js`: "enums"/info from wasm spec
|
181
259
|
- `wrap.js`: wrapper for compiler which instantiates and produces nice exports
|
182
260
|
|
183
|
-
- `runner`: contains utils for running
|
261
|
+
- `runner`: contains utils for running JS with the compiler
|
184
262
|
- `index.js`: the main file, you probably want to use this
|
185
263
|
- `info.js`: runs with extra info printed
|
186
264
|
- `repl.js`: basic repl (uses `node:repl`)
|
@@ -193,57 +271,26 @@ Porffor can run Test262 via some hacks/transforms which remove unsupported featu
|
|
193
271
|
- `test262`: test262 runner and utils
|
194
272
|
|
195
273
|
## Usecases
|
196
|
-
Basically none (other than giving people headaches). Potential ideas
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
1. Clone repo
|
202
|
-
2. `npm install`
|
203
|
-
3. `node test` to run tests (some will fail)
|
204
|
-
4. `node runner path/to/code.js` to run a file (or `node runner` to use wip repl)
|
205
|
-
|
206
|
-
You can also use Deno (`deno run -A ...` instead of `node ...`), or Bun (`bun ...` instead of `node ...`).
|
207
|
-
|
208
|
-
### Options
|
209
|
-
- `-target=wasm|c|native` (default: `wasm`) to set target output (native compiles c output to binary, see args below)
|
210
|
-
- `-target=c|native` only:
|
211
|
-
- `-o=out.c|out.exe|out` to set file to output c or binary
|
212
|
-
- `-target=native` only:
|
213
|
-
- `-compiler=clang` to set compiler binary (path/name) to use to compile
|
214
|
-
- `-cO=O3` to set compiler opt argument
|
215
|
-
- `-valtype=i32|i64|f64` (default: `f64`) to set valtype
|
216
|
-
- `-O0` to disable opt
|
217
|
-
- `-O1` (default) to enable basic opt (simplify insts, treeshake wasm imports)
|
218
|
-
- `-O2` to enable advanced opt (inlining)
|
219
|
-
- `-O3` to enable advanceder opt (precompute const math)
|
220
|
-
- `-no-run` to not run wasm output, just compile
|
221
|
-
- `-opt-log` to log some opts
|
222
|
-
- `-code-log` to log some codegen (you probably want `-funcs`)
|
223
|
-
- `-regex-log` to log some regex
|
224
|
-
- `-funcs` to log funcs
|
225
|
-
- `-ast-log` to log AST
|
226
|
-
- `-opt-funcs` to log funcs after opt
|
227
|
-
- `-sections` to log sections as hex
|
228
|
-
- `-opt-no-inline` to not inline any funcs
|
229
|
-
- `-tail-call` to enable tail calls (experimental + not widely implemented)
|
230
|
-
- `-compile-hints` to enable V8 compilation hints (experimental + doesn't seem to do much?)
|
274
|
+
Basically none right now (other than giving people headaches). Potential ideas:
|
275
|
+
- Safety. As Porffor is written in JS, a memory-safe language\*, and compiles JS to Wasm, a fully sandboxed environment\*, it is quite safe. (\* These rely on the underlying implementations being secure. You could also run Wasm, or even Porffor itself, with an interpreter instead of a JIT for bonus security points too.)
|
276
|
+
- Compiling JS to native binaries. This is still very early!
|
277
|
+
- More in future probably?
|
231
278
|
|
232
279
|
## VSCode extension
|
233
|
-
There is a vscode extension in `
|
280
|
+
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).
|
234
281
|
|
235
282
|
## Isn't this the same as AssemblyScript/other Wasm langs?
|
236
283
|
No. they are not alike at all internally and have very different goals/ideals:
|
237
284
|
- Porffor is made as a generic JS engine, not for Wasm stuff specifically
|
238
|
-
- Porffor
|
239
|
-
- Porffor is
|
285
|
+
- Porffor primarily consumes JS
|
286
|
+
- Porffor is written in pure JS and compiles itself, not using Binaryen/etc
|
240
287
|
- (Also I didn't know it existed when I started this, lol)
|
241
288
|
|
242
289
|
## FAQ
|
243
290
|
|
244
291
|
### 1. Why the name?
|
245
292
|
`purple` in Welsh is `porffor`. Why purple?
|
246
|
-
- No other
|
293
|
+
- No other JS engine is purple colored
|
247
294
|
- Purple is pretty cool
|
248
295
|
- Purple apparently represents "ambition", which is.. one word to describe this project
|
249
296
|
- The hard to speak name is also the noise your brain makes in reaction to this idea!
|
package/asur/README.md
ADDED