How Bun supports V8 APIs without using V8 (part 2)
Bun emulates V8's memory layout on top of JavaScriptCore so that precompiled native addons, which inline V8 functions assuming specific pointer layouts, keep working. The approach uses tagged pointers (Smis or heap pointers), a small set of static fake Map objects with instance types at the expected offset, and 24-byte Handles in a handle scope that store the tagged pointer, map pointer and JSCell pointer side by side, avoiding separate allocations outside the GC. Walkthroughs of Local, HandleScope, Number::New/Value and objects with internal fields show how JSC values are presented to V8-compiled code.
Related posts
- How Bun supports V8 APIs without using V8 (part 1)
Bun hardMaster
Bun, which runs on JavaScriptCore, implements a growing subset of V8's C++ embedding API so Node.js native modules like cpu-features load without V8. The post contrasts JSC and V8 value representation, calling conventions, isolates/contexts and handle-based lifetimes. It then shows why an initial approach of reinterpreting JSC's 8-byte JSValue as V8's Local<T> pointer failed. V8's inline header functions such as Object::GetInternalField dereference Local pointers and read map and instance-type fields at fixed offsets, so Bun's in-memory layouts must match what V8's compiled-in inline code expects.
- Bun v0.1.9
Bun mediumAdept
Bun v0.1.9 starts using JavaScriptCore's DOMJIT API to JIT-optimize native functions, making `ptr()` in `bun:ffi` 6-10x faster and `TextEncoder.encodeInto` 1.5-5x faster. To get there, Bun extended DOMJIT to accept Typed Array arguments and to declare more kinds of side effects. Bun now code-generates C++ binding classes and ABI-checked Zig bindings from JavaScript class definitions, an approach inspired by WebIDL, replacing most use of the JSC C API. Other changes include a more reliable, lower-CPU event loop on Linux, about 20% faster `Bun.serve` through better `Headers` handling, native `buffer` and `process` modules, and numerous crash fixes.
- V8 release v6.9 · V8
V8 easyNoviceV8 v6.9 embeds built-in functions directly into the binary on x64, so all Isolates share one copy instead of each holding up to 700 KB on its own heap. Across the top 10k sites this cut median V8 heap size by 9%, and 10% of sites saved 3.7 MB or more. Liftoff, a new baseline WebAssembly compiler, speeds startup by over 10×. DataView methods were reimplemented in Torque and inlined by TurboFan to match TypedArray speed, and concurrent, incremental and parallel WeakMap processing roughly halves Mark-Compact GC pauses, with a linear-time fallback replacing worst cases that could take seconds. The release also adds Array.prototype.flat and flatMap.
- Bun v1.2.17
Bun mediumAdept
Bun v1.2.17 adds ahead-of-time bundling for HTML imports in server code, so `bun build --target=bun` or `--compile` can produce a full-stack build or a single self-contained executable with no runtime bundling. Bun Shell no longer uses the native call stack, which prevents stack overflows in deeply nested scripts. Removing a pointer indirection in Strong references cuts per-timer memory for `setTimeout` and `setImmediate` by 8-15%. Other changes include `columnTypes`/`declaredTypes` in `bun:sqlite`, zstd in `node:zlib`, `--unhandled-rejections` modes, `child_process.fork` `execArgv`, and a JavaScriptCore upgrade bringing JIT string and array optimizations, a zero-copy JSON stringifier and WebAssembly interpreter hardening.
- The V8 Sandbox
V8 hardMasterThe V8 Sandbox is a lightweight, in-process software sandbox that isolates the V8 heap so memory corruption from engine bugs cannot spread to the rest of the renderer process. It is now included in Chrome's VRP as of Chrome 123. V8 exploits usually stem from 2nd-order logic bugs, often in JIT compilers, which neither Rust nor memory tagging can prevent. The sandbox therefore replaces raw pointers and 64-bit sizes on the heap with 40-bit offsets from a 1TB reserved region, plus a pointer-table indirection for external objects. Overhead is about 1% on Speedometer and JetStream, and the design is testable through a memory-corruption API and a sandbox-violation detection mode used for fuzzing.