$ show CanadaHonk/porffor
Porffor is an ahead-of-time (AOT) JavaScript compiler and lightweight runtime, written primarily in JavaScript and TypeScript. It compiles JS/TS source code into C, then to native binaries via an external C compiler (e.g., TCC, GCC, or Clang). The system includes a full ECMAScript built-in library, a self-hosted compiler pipeline (the compiler compiles itself), and a custom intermediate representation (IR) with constant folding, effect tracking, and type inference. It supports modern JS features including async/await, generators, Promises, classes, modules, and most built-in objects, with a particular focus on performance through techniques like coroutine lowering, inline caching, and compile-time evaluation. Architecturally, the compiler is a classic three-stage pipeline: parser (custom ESTree-compatible parser with full TypeScript support), code generator (AST-to-IR lowering with closure analysis and type inference), and renderer (IR-to-C code generation with runtime embedding). The runtime is split into a small C core (providing garbage collection, coroutine support, and native fetch) and JS-implemented built-ins that leverage an explicit memory model and low-level IR intrinsics. The project also includes a comprehensive benchmark suite (based on the V8 benchmark suite) and a test262 runner for ECMAScript conformance tracking. A notable aspect is the self-hosting infrastructure: the compiler can compile its own source code, and the repository includes build scripts, a profile-guided optimization pipeline in CI, and cross-platform binary releases. While primarily an application (compiler/runtime toolchain), it also contains non-trivial performance measurement and conformance tracking code, giving it a slight hybrid character. The codebase is early-stage but shows high engineering quality with many carefully optimized components, though some files are monolithic and tightly coupled.
The LLM identified 2 files worth highlighting for reuse:
A compact, well-structured preference system that manages compiler flags (e.g., optimisation switches, debug mode, caching) as a global object. Provides CLI argument parsing from process.argv, on-by-default flags with overrides, and an uncache() function to invalidate cached builds when preferences change. The pattern is self-contained, easily extractable, and demonstrates a clean separation of configuration concerns that can be reused in any command-line tool.
A standalone benchmark framework derived from the V8 benchmark suite, providing `Benchmark`, `BenchmarkResult`, and `BenchmarkSuite` classes. Features deterministic timing via a seeded Math.random replacement, geometric mean scoring, and a `RunStep` function that uses setTimeout to yield between tests (browser-friendly but easily adaptable to Node). The modular structure allows any compute-intensive function to be wrapped as a benchmark with optional setup/teardown, validation, and reference scoring. Fully reusable for performance testing in any JavaScript environment.