Home

Awesome

Porffor  <sup><sub>/ˈpɔrfɔr/  (poor-for)</sup></sub>

A from-scratch experimental AOT optimizing JS/TS -> Wasm/C engine/compiler/runtime in JS. Research project, not yet intended for serious use.<br>

<img src="https://github.com/CanadaHonk/porffor/assets/19228318/de8ad753-8ce3-4dcd-838e-f4d49452f8f8" alt="Screenshot of terminal showing Porffor running and compiling a hello world" width="60%">

Design

Porffor is a very unique JS engine, due many wildly different approaches. It is seriously limited, but what it can do, it does pretty well. Key differences:

Porffor is primarily built from scratch, the only thing that is not is the parser (using 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.

Usage

Expect nothing to work! Only very limited JS is currently supported. See files in bench for examples.

Install

npm install -g porffor@latest. It's that easy (hopefully) :)

Trying a REPL

porf. Just run it with no script file argument.

Running a JS file

porf path/to/script.js

Compiling to Wasm

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.

Compiling to native binaries

[!WARNING] Compiling to native binaries uses 2c, Porffor's own Wasm -> C compiler, which is experimental.

porf native path/to/script.js out(.exe). You can specify the compiler with --compiler=clang|gcc|zig (clang by default), and which optimization level to use with --cO=Ofast|O3|O2|O1|O0 (Ofast by default). Output binaries are also stripped by default.

Compiling to C

[!WARNING] Compiling to C uses 2c, Porffor's own Wasm -> C compiler, which is experimental.

porf c path/to/script.js (out.c). When not including an output file, it will be printed to stdout instead.

Profiling a JS file

[!WARNING] Very experimental WIP feature!

porf hotlines path/to/script.js

Debugging a JS file

[!WARNING] Very experimental WIP feature!

porf debug path/to/script.js

Debugging the compiled Wasm of a JS file

[!WARNING] Very experimental WIP feature!

porf dissect path/to/script.js

Options

Current limitations

Sub-engines

Asur

Asur is Porffor's own Wasm engine; it is an intentionally simple interpreter written in JS. It is very WIP. See its readme for more details.

Rhemyn

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 for more details.

2c

2c is Porffor's own Wasm -> C compiler, using generated Wasm bytecode and internal info to generate specific and efficient C code. Little boilerplate/preluded code or required external files, just for CLI binaries (not like wasm2c very much).

Versioning

Porffor uses a unique versioning system, here's an example: 0.48.7. Let's break it down:

  1. 0 - major, always 0 as Porffor is not ready yet
  2. 48 - minor, total Test262 pass percentage (rounded half down, eg 49.4% -> 48, 49.5% -> 49)
  3. 7 - micro, build number for that minor (incremented each git push)

Performance

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.

Test262

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.

image

Codebase

Usecases

Currently, Porffor is seriously limited in features and functionality, however it has some key benefits:

Todo

No particular order and no guarantees, just what could happen soon™

VSCode extension

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).

Wasm proposals used

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.

FAQ

1. Why the name?

purple in Welsh is porffor. Why purple?

2. Why at all?

Yes!

3. Isn't this the same as AssemblyScript/other Wasm langs?

No. they are not alike at all internally and have very different goals/ideals: