Implementing Private Fields for JavaScript
SpiderMonkey implements JavaScript private fields as hidden properties keyed by special PrivateName symbols, diverging from the specification's per-object [[PrivateFieldValues]] list so it can reuse the engine's already-optimized property machinery. The WeakMap mental model explains surprising semantics such as the constructor-override trick that stamps private fields onto arbitrary or sealed objects, and private fields living on a Proxy rather than its target. To preserve spec semantics, a new CheckPrivateField bytecode throws TypeErrors on missing or duplicate fields and is inline-cached via CacheIR using object Shapes. Private fields are also hidden from enumeration, and Proxies get a lazily allocated expando object to hold them. The feature ships in Firefox 90.
Related posts
- Getting lively with Firefox 90
Mozilla Hacks mediumAdeptFirefox 90 enables JavaScript private class fields and methods by default, using a # prefix so properties can only be read or written inside the class body, and adds the relative-indexing at() method to Array, String and TypedArray, which accepts negative indexes to count from the end. The 2D Canvas API gains createConicGradient() for gradients swept around a center point, and Fetch metadata request headers (Sec-Fetch-Site, -Mode, -User, -Dest) are now sent by default so servers can decide whether to allow requests based on their origin context. Windows also gets Background Update, letting Firefox update while not running.
- How Bun supports V8 APIs without using V8 (part 2)
Bun hardMaster
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.
- The File System API with Origin Private File System
WebKit mediumAdeptWebKit now supports the File System Standard's origin private file system, a per-origin sandboxed storage area where web apps can create, read, write, move, enumerate and delete files and directories. It is available from Safari on macOS 12.2 and iOS 15.2, has the same lifetime as IndexedDB and localStorage, and is disabled in Private Browsing. Worked examples cover navigator.storage.getDirectory(), getFileHandle/getDirectoryHandle, move(), resolve(), async iteration, removeEntry() and getFile(). Writing in WebKit currently requires FileSystemSyncAccessHandle, which works only in Workers, takes exclusive access to a file, and offers synchronous read, write, truncate and flush for the best performance.
- 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.
- Turbocharging V8 with mutable heap numbers
V8 hardMasterV8 eliminated a performance cliff in JetStream2's async-fs benchmark caused by a custom Math.random whose `seed` lives in a ScriptContext slot. Every update to the seed allocated a new immutable HeapNumber, and the integer bitwise math was compiled as floating-point. V8 now tracks slot types (constant, SMI, HeapNumber, Int32, other) and lets a context slot own a mutable HeapNumber that is updated in place, storing raw Int32 values when they stay in range. Optimized code depends on the slot type staying stable and deoptimizes if it changes, and the change gives about a 2.5x speedup on async-fs and about 1.6% on the overall JetStream2 score.