Skip to content
wlejonPublic

About

Ahead-of-time & JIT JavaScript compiler and runtime on Brass, with profile-guided speculation, moving GC, compartments, and SMT verification.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

bronze

CI Nightly CodeQL

An ahead-of-time compiler that turns JavaScript into a native executable.

bronze build app.js -o app
./app

bronze compiles JavaScript to machine code through Brass. It follows imports from your entry file, compiles the whole program, and writes a native program — no linker, no toolchain, nothing on the machine but bronze itself. The test suite compiles three.js r160 and pixi.js v8 from unmodified source.

What you can build

A native program. bronze build app.js -o app writes three files in one directory: app, the compiled program as a loadable module beside it (app.dll / app.so / app.dylib), and bronze's shared runtime. The executable is a copy of bronze's program host, which opens the module named after itself; the three travel together.

Direct in-memory execution / JIT. bronze run app.js compiles and runs scripts and module graphs directly in memory with zero disk I/O, and bronze eval "2 + 2" (or -e) evaluates expressions on the fly.

An app embedded in a larger program. --emit-obj produces an object file for your own build to link, and --host-globals lists the globals your host provides (document, requestAnimationFrame, and so on). --emit-shared produces a module your host opens at run time instead, linked against a shared bronze runtime so one process can load several. src/embed is the C++ API a host uses to drive the compiled program, register globals, and hold GC-safe handles (Persistent, HandleScope, Local<T>). A host can also compile a script ahead of time through the embedded evaluator (eval::buildModule): the same program the JIT would run, written as a loadable module with a record of what it was built from, which eval::loadModule checks before running it, so a stale module falls back to the JIT instead of running. The bro engine compiles its apps this way, in process: an installed app's scripts are built on the harvest of its first run (EvalOptions::harvestOutPath / feedbackInPath), and its scene graph and render loop run as machine code while the engine supplies the browser surface.

How it works

bronze compiles JavaScript cleanly through one harvest-guided speculative AOT pipeline (--harvest-out followed by --feedback-in) and one Auto multi-tier Brass JIT (src/eval):

  • Speculative AOT ($spec / $opt with generic G fallback): whole-program type/shape analysis (bronze types app.js) pairs with runtime inline-cache harvests to emit direct native loops, unboxed numeric arithmetic, guarded slot accesses, and inlined methods/constructors. Any guard miss exits cleanly to the generic G twin via resume points without corrupting state.
  • In-memory JIT (bronze run / bronze eval / host eval): runs on the Auto multi-tier Brass pipeline (Fast Tier-0 interpreter with background Tier-1, Tier-2, and OSR), with compartment-isolated abort handling so host runtimes stay protected.
  • Shared memory & atomics: full process-wide SharedArrayBuffer backing and Atomics.wait/notify/waitAsync support across worker threads.

TypeScript annotations are accepted and treated as hints. .ts, .mts and .cts files are read as TypeScript: types, generics, assertions and overloads are erased, and enums, namespaces and parameter properties become the JavaScript node's TypeScript transform emits for them. A .js file is read as JavaScript, so tokens TypeScript would read differently (a < b > (c), type, namespace) mean what JavaScript says; plain annotations (let x: number, function f(a: T): R) are still accepted there.

The performance target is concrete: harvest-guided compiled output beats Node.js on the same program, and benchmarks that fall short are treated as bugs.

Status

bronze passes 1,099 ECMA-262 differential oracle cases across run, aot, profile, switch, and adversarial modes (0 blocked cases remain) and compiles unmodified three.js r160 and pixi.js v8 from source. Ahead-of-time compilation turns the full module graph into native machine code. Dynamic constructs like eval and new Function (including generator and async functions) are evaluated dynamically in-memory via the Brass JIT engine (src/eval). When a program uses a language feature bronze hasn't implemented yet, compilation stops with an error naming the feature.

Verification

src/check provides static verification and int32 bitvector constraint solving over straight-line JavaScript functions (backed by Brass's BVSolver):

  • bronze check <entry.js>: abstract-interpretation and bitvector hazard checks (null/undefined dereference, division by zero, typed-array bounds, and, with --deterministic, a replay audit: wall clock, unseeded random, for..in order, outer state).
  • bronze prove-equiv <a.js> <b.js> --fn <name>: bitvector equivalence check of each pair of same-named functions (int32 or float inputs, bounded loops), comparing return values and what each leaves in its object parameters and this, under every aliasing of its objects (the receiver, parameters used as objects or compared with one, and object fields such as this.p: v is this, a is b, v is this.p), and reporting a counterexample, confirmed by a concrete replay, that names the fewest objects that are one. A class's methods are inlined where this.m() calls one, this taken as an instance of the class as declared (a subclass overriding m is not considered); a function using this in no class is taken to be called on an instance of the module's one class only where it has one, and a parameter is never taken for an instance of a class. A function value a function hands on (returns, leaves in a field, gives to a call outside) is compared as that function, so the pair is proven only where that function is the same in both versions or its own pair is proven; otherwise it is undecided ("it hands on the function f"). A call to a function outside the module (a host API, an unmodelled builtin, a function value from the input) is taken to act the same in both versions given the same calls before it, the same arguments and the same heap it can reach (what the function allocated and kept to itself excepted), and not to call back into the module but into a function it is given (r.map((v) => v * 2)), which is compared as that function (one left in an object it is given too), whose call's key holds what each variable of the run holds then, and after which a variable it may set is unknown, and the objects its scopes hold are the call's: its result is only passed on (to another call outside too), stored, returned, thrown, tested for truth or nullishness, compared (each operator its own uninterpreted test: == is not ===), read for a field, made a number, an int32 or a string (r + 1, r | 0, 'id:' + r), or given to a builtin that reads it (Math.* but random, Number, parseInt, isNaN, JSON.stringify, ...); a read is a value of what the calls before it left (an object's valueOf or getter may see them) and is taken to change nothing, an ordering, a conversion or a builtin may throw (a Symbol, a BigInt), and Math.* and Number make a number, any double where the pair is compared over doubles. A builtin that makes a new object of it each call (Array.from, JSON.parse, Object.keys) is a call outside, its result its own. Every field such a call can reach is unknown after it. x instanceof C on an object not the module's own is an uninterpreted test of x, C and the calls before it. Where on some arguments the versions make different calls outside whatever those calls return (one calls f where the other calls g, or no more), the way each takes decided by the arguments alone, the calls before it the same function given the same values, the pair has a counterexample, not replayed, in those calls ("old calls .h where new makes no more calls outside the module", callsDiffer in the JSON in place of the results): a host whose calls return and throw nothing tells them apart. A call is named by its path, globalThis./window. dropped; one named otherwise (o["h"](), f.call(o), an operation on a value from outside), or a builtin's that may call back as it runs (Float64Array.from(v)), leaves it undecided, and a builtin's that cannot (new WeakRef(o), or one given nothing) is not one of the calls compared. A proof that rests on such calls names them; a function passing one a function, or a name the module defines, is undecided. A method of a host global (__bro_native.x.f()) is the host's, not the module's f, unless the module hands an object on that path to something (a call, a field, a return) or sets f on it, when it is undecided as any call by a name the module defines. Where an integer result could pass 2^53 the pair is compared over doubles, as JS computes them (int32 arguments, any double in a field), so x * x + y * y is decided and a reassociated sum that rounds differently is a counterexample. Where both versions throw, what each wrote and the calls outside it made before the throw are compared (what is thrown is not), and a throw from a call is one: what the caller writes after the call, on the paths where it did not throw, does not change what the throw left. An object module init made (const st = {}) outlives a call, and what a function changes in it is compared (not what init put there), the object known by the variable or field that holds it; where the versions' inits hold their objects differently (made in another order), a function writing one is undecided. One a function returns is compared in what it, and the module's objects it reaches, hold. Module init is followed through forEach of an array it knows, given one of its functions, and through its calls outside the module (a function's first call outside is after them; one making none is not compared in them). A regular expression literal, and an empty Map/Set/WeakMap/WeakSet init makes, is an object from outside, the n-th of its kind (a builtin given one is undecided). Object.defineProperty of a data field (value, all three flags true) is that field set; of an accessor, or a prototype set, it is kept under the object for the calls outside that reach it, and a function reading or setting that name (or the object's keys) is undecided; Object.freeze/seal/preventExtensions return the object, whose fields read as before, and a set, delete or builtin given it is undecided. A set of a field of a global not the language's (bro.mesh.Mesh = Mesh, globalThis.X = x, through a variable holding it too) is a call outside given the value, and a read of that global after is the world's; a field the module set on a global it holds reads as set. An operation on a value from outside of no known type (r[k], r + 1, x instanceof R) is a call outside given its operands. A parameter's (or this's) fields, before the function's first call outside, are the caller's, whatever init's calls did. A value init cannot work out (a fractional constant in integer mode, a field of an object it does not follow, a call it cannot make) is each version's own, never the same in both; where init went past such a call or set, every variable and field it leaves is each version's own but a variable its function sets once (and no other sets) before the first, and where it stopped (a limit, a refusal, a branch on what it could not work out, a loop on a test it cannot decide) that too, and a variable it did not reach is undecided. Functions are paired by place: a top-level function with a top-level one of its name, a function's inner functions with those of the function it is paired with. Same-named siblings, such as a module's anonymous callbacks, are aligned in source order on those left unchanged, and one added or removed among them leaves its run, and what is inside it, unpaired (listed, not compared), unless a name only one function has in each version still names it. A variable of the module, or of a function a closure was made in, is compared in what each version leaves in it (one written by only one version is undecided, not a counterexample), and a closure's write to it is seen by its caller. A function made by another function, compared on its own, reads its maker's variables as inputs, as parameters are: each is the binding of its name that many scopes out, the same in both versions, so the pair is compared for every value they share (a closure setting one is undecided), and a field of an object one holds is read as a parameter's is (setting one, or reading one after the function set a field of another object, is undecided); a function its maker keeps in a variable set once, before the closure reading it is made (const f = () => ...), is that function. The maker, where such a closure leaves it (returned, stored, given to a call), is compared in what its variables, and the objects they hold, are left holding too. Values carry their type where the bits alone do not tell it, kept in a field, an element or a variable too: null is not undefined (but == it), true is not 1 under === nor left in a field in its place, and typeof and Array.isArray answer from what the value is: a parameter, or a field of one, is a number or an object (an object if used as one), the same one in both versions, so switch (typeof a) and typeof a === 'number' are decided alike and typeof a is not taken to be 'number'; one compared with a string (o.type === 'mesh') may be that string, one string at most and the same at every read, so t === 'a' || t === 'b' is not t === 'a' (undecided: its strings are not modelled further); == with a string (x == '5', true where x is 5) is an unknown test of the value and the string. A field whose key either version compares with undefined or null (opts.depth !== undefined) may be missing or null where it is read from an object the function was given (each anew after a call outside), and reading a field of it then throws. An argument keeps its type into a call (g(true) is not g(1)). A parameter either version tests for undefined or null (== null, === undefined, ??, typeof) is also tried as each, and the pair is proven only if it is proven so too: a difference the replay, given it, confirms is a counterexample (dt = undefined); otherwise (calls outside the module stand between, or the parameter may be used for its value where it is undefined: x + 1 is NaN in JS, any integer in the proof) the pair is undecided, "proven for numbers". Undefined and null are false (x || 5), typeof x compared with a string asks what x is, and what follows a throw on undefined does not see it. --changed compares only the functions whose IL, or that of a module function they call, differs, and marks those whose own IL is the same.

The CLI

bronze run   <entry.js>                   run JS directly in-memory via JIT
bronze eval  <code> (or -e <code>)        evaluate JS code directly in-memory via JIT
bronze build <entry.js> -o <exe>          compile to a native program (the
                                          executable, its module, its runtime)
bronze build <entry.js> -o <obj> \
             --emit-obj \
             --host-globals <manifest>    compile to an object for a host build
bronze build <entry.js> -o <lib> \
             --emit-shared                compile a module a host loads at run time
bronze check <entry.js>                   run static & bitvector hazard checks
bronze types <entry.js>                   show what inference proved
bronze il    <entry.js>                   dump the typed IL
bronze lex / bronze parse                 earlier pipeline stages
bronze version                            print the version

<entry.js> is the entry of a module graph: import/export with relative specifiers pull in the rest. AOT builds pair whole-program inference with inline-cache harvesting (--harvest-out followed by --feedback-in) for the fastest native code, and bronze run / bronze eval execute on the Auto multi-tier Brass JIT pipeline (Fast Tier-0 interpreter with background Tier-1, Tier-2, and OSR).

Getting bronze

Nightly builds are published for Windows x64, Linux x64, macOS x64 and macOS arm64, at fixed links (releases/latest/download/bronze-linux-x64.zip, -windows-x64, -macos-x64, -macos-arm64) — each zip is the compiler, the shared runtime and the program host it copies beside every program it builds, tested against the full suite before publishing. Unzip anywhere and run bronze build app.js -o app. Nothing else is needed: bronze writes its own executables and modules, so there is no linker to install.

Building bronze

Prereqs: CMake ≥ 3.24, Ninja, a C++20 compiler, and vcpkg. On Windows, dev.cmd wraps the MSVC environment: .\dev.cmd cmake ....

cmake --preset dev
cmake --build --preset dev
ctest --preset dev

The native code generation backend is Brass (src/codegen-brass), which compiles Bronze IL to native machine code (for both AOT builds and the multi-tier JIT). Brass is a dependency, not a submodule: a working tree at ../brass is used when there is one, otherwise configure fetches the head of brass's main (cmake/bro_deps.cmake; a release tag's cmake/bro_lock.cmake fixes the commit).

Embedding Bronze

Bronze can be embedded from C11, C++20, or any FFI-capable language (Rust, Zig, Go, Python) via <bronze/bronze_c_api.h> and bronze_runtime_shared.

cmake --preset dev
cmake --build --preset dev
cmake --install build/dev --prefix /usr/local
find_package(bronze CONFIG REQUIRED)
target_link_libraries(my_app PRIVATE bronze::runtime_shared)

Or with pkg-config:

cc main.c $(PKG_CONFIG_PATH=/usr/local/lib/pkgconfig pkg-config --cflags --libs bronze) -o app

See docs/embedding.md and examples/ (c_eval, c_host_functions, cpp_embed).

Testing

Correctness rests on a differential suite (tests/oracle/run.js, compiled to bronze-oracle by bronze build itself): JavaScript programs whose exact stdout is pinned in committed files derived from ECMA-262. Every case runs through run, aot, profile, switch, and adversarial modes (plus the three.js and pixi.js milestones), and all must produce identical bytes. Compiled objects carry an ABI fingerprint the runtime verifies at startup, so an object built against an older runtime fails at launch with both versions named.

License

MIT

About

Ahead-of-time & JIT JavaScript compiler and runtime on Brass, with profile-guided speculation, moving GC, compartments, and SMT verification.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages