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)
Age: ~6 months (very on and off)
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:
- 100% AOT compiled (no JIT)
- No constant runtime/preluded code
- Least Wasm imports possible (only I/O)
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, focusing on function/working over spec compliance.
- No full object support yet
- Little built-ins/prototype
- No async/promise/await
- No variables between scopes (except args and globals)
- Literal callees only in calls (eg
print()works,a = print; a()does not) - No
eval()etc (since it is AOT)
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 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).
See optimizations for opts implemented/supported.
These include some early (stage 1/0) and/or dead (last commit years ago) proposals but I think they are pretty neat, so.
Math.clampProposal:Math.clamp(stage 0 - last commit april 2023)MathExtensions Proposal:Math.scale,Math.radians,Math.degrees,Math.RAD_PER_DEG,Math.DEG_PER_RAD(stage 1 - last commit september 2020)Math.signbitProposal:Math.signbit(stage 1 - last commit february 2020)
- Number literals
- Declaring functions
- Calling functions literal callees only
returnlet/const/varbasic declarations- Some basic integer operators (
+-/*%) - Some basic integer bitwise operators (
&|) - Equality operators (
==,!=, etc) - GT/LT operators (
>,<,>=, etc) - Some unary operators (
!,+,-) - Logical operators (
&&,||) - Declaring multiple variables in one (
let a, b = 0) - Global variables (
var/none in top scope) - Functions returning 1 number
- Bool literals as ints (not real type)
ifandif ... else- Anonymous functions
- Setting functions using vars (
const foo = function() { ... }) - Arrow functions
undefined/nullas ints (hack)- Update expressions (
a++,++b,c--, etc) forloops (for (let i = 0; i < N; i++), etc)- Basic objects (hack)
console.log(hack)whileloopsbreakandcontinue- Named export funcs
- IIFE support
- Assignment operators (
+=,-=,>>=,&&=, etc) - Conditional/ternary operator (
cond ? a : b) - Recursive functions
- Bare returns (
return) throw(literals only)- Basic
try { ... } catch { ... }(no error given) - Calling functions with non-matching arguments (eg
f(a, b); f(0); f(1, 2, 3);) typeof- Runtime errors for undeclared variables (
ReferenceError), not functions (TypeError) - Array creation via
[](eglet arr = [ 1, 2, 3 ]) - Array member access via
arr[ind](egarr[0]) - String literals (
'hello world') - String member (char) access via
str[ind](egstr[0]) - String concat (
+) (eg'a' + 'b') - Truthy/falsy (eg
!'' == true) - String comparison (eg
'a' == 'a','a' != 'b') - Nullish coalescing operator (
??) for...of(arrays and strings)- Array member setting (
arr[0] = 2,arr[0] += 2, etc) - Array constructor (
Array(5),new Array(1, 2, 3))
NaNandInfinity(f64 only)isNaN()andisFinite()(f64 only)- 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) - Some
Mathfuncs (Math.sqrt,Math.abs,Math.floor,Math.sign,Math.round,Math.trunc,Math.clz32,Math.fround,Math.random) (f64 only) - Basic
globalThissupport - Basic
BooleanandNumber - Basic
evalfor literals Math.random()using self-made xorshift128+ PRNG- Some of
performance(now()) - Some of
Array.prototype(at,push,pop,shift,fill) - Some of
String.prototype(at,charAt,charCodeAt)
- Supports i32, i64, and f64 for valtypes
- Start of a SIMD api (docs needed)
- Intrinsic functions (see below)
- Inlining wasm via
asm`...` "macro"
No particular order and no guarentees, just what could happen soon™
- Arrays
- More of
Arrayprototype - Arrays/strings inside arrays
- Destructuring
- More of
- Objects
- Basic object expressions (eg
{},{ a: 0 })
- Basic object expressions (eg
- Wasm
- Basic Wasm engine (interpreter) in JS
- More math operators (
**, etc) do { ... } while (...)- Rewrite
console.logto work with strings/arrays - Exceptions
- Rewrite to use actual strings (optional?)
try { } finally { }- Rethrowing inside catch
- Optimizations
- Rewrite local indexes per func for smallest local header and remove unused idxs
- Smarter inline selection (snapshots?)
- Remove const ifs (
if (true), etc) - Experiment with byte strings?
- Runtime
- WASI target
- Run precompiled Wasm file if given
- Cool proposals
- Posts
- Inlining investigation
- Self hosted testing?
For the things 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 not that much slower like a traditional interpreter would be; mostly the same or a bit faster/slower depending on what.
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.
- Inlining functions (WIP, limited)
- Inline const math ops
- Tail calls (behind flag
-tail-call)
local.set,local.get->local.teei32.const 0,i32.eq->i32.eqzi64.extend_i32_s,i32.wrap_i64-> ``f64.convert_i32_u,i32.trunc_sat_f64_s-> ``return,end->end- Change const, convert to const of converted valtype (eg
f64.const,i32.trunc_sat_f64_s ->i32.const`) - Remove some redundant sets/gets
- Remove unneeded single just used vars
- Remove unneeded blocks (no
brs inside) - Remove unused imports
- Use data segments for initing arrays/strings
- (Likely more not documented yet, todo)
- Type cache/index (no repeated types)
- No main func if empty (and other exports)
- No tags if unused/optimized out
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.
-
compiler: contains the compiler itselfbuiltins.js: all built-ins of the engine (spec, custom. vars, funcs)codeGen.js: code (wasm) generation, ast -> wasm. The bulk of the effortdecompile.js: basic wasm decompiler for debug infoembedding.js: utils for embedding constsencoding.js: utils for encoding things as bytes as wasm expectsexpression.js: mapping most operators to an opcode (advanced are as built-ins egf64_%)index.js: doing all the compiler steps, takes code in, wasm outopt.js: self-made wasm bytecode optimizerparse.js: parser simply wrapping acornsections.js: assembles wasm ops and metadata into a wasm module/filewasmSpec.js: "enums"/info from wasm specwrap.js: wrapper for compiler which instantiates and produces nice exports
-
runner: contains utils for running JS with the compilerindex.js: the main file, you probably want to use thisinfo.js: runs with extra info printedrepl.js: basic repl (usesnode:repl)
-
rhemyn: contains Rhemyn - our regex engine (used by Porffor)compile.js: compiles regex ast into wasm bytecodeparse.js: own regex parser
-
test: contains many test files for majority of supported features -
test262: test262 runner and utils
Basically none (other than giving people headaches). Potential ideas to come?
Basically nothing will work :). See files in test for examples.
- Clone repo
npm installnode testto run tests (some will fail)node runner path/to/code.jsto run a file (ornode runnerto use wip repl)
You can also use Deno (deno run -A ... instead of node ...), or Bun (bun ... instead of node ...).
-target=wasm|c|native(default:wasm) to set target output (native compiles c output to binary, see args below)-target=c|nativeonly:-o=out.c|out.exe|outto set file to output c or binary
-target=nativeonly:-compiler=clangto set compiler binary (path/name) to use to compile-cO=O3to set compiler opt argument
-parser=acorn|@babel/parser|meriyah|hermes-parser(default:acorn) to set which parser to use-parse-typesto enable parsing type annotations/typescript. if-parseris unset, changes default to@babel/parser. does not type check-opt-typesto perform optimizations using type annotations as compiler hints. does not type check-valtype=i32|i64|f64(default:f64) to set valtype-O0to disable opt-O1(default) to enable basic opt (simplify insts, treeshake wasm imports)-O2to enable advanced opt (inlining). unstable-O3to enable advanceder opt (precompute const math). unstable-no-runto not run wasm output, just compile-opt-logto log some opts-code-logto log some codegen (you probably want-funcs)-regex-logto log some regex-funcsto log funcs-ast-logto log AST-opt-funcsto log funcs after opt-sectionsto log sections as hex-opt-no-inlineto not inline any funcs-tail-callto enable tail calls (experimental + not widely implemented)-compile-hintsto enable V8 compilation hints (experimental + doesn't seem to do much?)
There is a vscode extension in porffor-for-vscode which tweaks JS syntax highlighting to be nicer with porffor features (eg highlighting wasm inside of inline asm).
No. they are not alike at all internally and have very different goals/ideals:
- Porffor is made as a generic JS engine, not for Wasm stuff specifically
- Porffor primarily consumes JS
- Porffor is written in pure JS and compiles itself, not using Binaryen/etc
- (Also I didn't know it existed when I started this, lol)
purple in Welsh is porffor. Why purple?
- No other JS engine is purple colored
- Purple is pretty cool
- Purple apparently represents "ambition", which is.. one word to describe this project
- The hard to speak name is also the noise your brain makes in reaction to this idea!
Yes!
Lol, no. (sorry.)
