docs/objects/lookup-iterator.md
This document explains the design and functionality of the LookupIterator in V8, which is the central mechanism for resolving property accesses on JavaScript objects.
The LookupIterator (src/objects/lookup.h) is a stateful iterator used to implement the ECMAScript specification's property lookup rules. It abstracts away the differences between:
Whenever V8 needs to get, set, or delete a property and cannot do it via a fast Inline Cache (IC) hit, it falls back to the LookupIterator.
The iterator operates as a state machine. Calling Next() advances the iterator to the next state or the next object in the prototype chain. States include:
NOT_FOUND: The property was not found. This is a terminal state.STRING_LOOKUP_START_OBJECT: Initial state for lookup starting from a primitive string. Avoids wrapper object creation.TYPED_ARRAY_INDEX_NOT_FOUND: Typed arrays return undefined immediately for invalid integer indices without walking the prototype chain.ACCESS_CHECK: The object requires an access check (e.g., cross-origin iframe access) before proceeding.INTERCEPTOR: API-level hooks for optionally handling a lookup in embedder code.JSPROXY: The current holder is a JSProxy. The lookup must be forwarded to the proxy's handler.ACCESSOR: The property is an accessor (getter/setter) defined by an AccessorPair or AccessorInfo.DATA: The property was found as a data property (field or constant) in the current holder.WASM_OBJECT: WasmGC objects are opaque in JS and appear to have no properties.MODULE_NAMESPACE: The property is accessed on a deferred module namespace.TRANSITION: Intermediate state during a data property write (not observed during lookup).The iterator can be configured via LookupIterator::Configuration, which consists of bit flags and convenience combinations:
kInterceptor: Consult embedder interceptors.kPrototypeChain: Walk the prototype chain.Convenience combinations:
OWN_SKIP_INTERCEPTOR: Only look at the receiver, skip interceptors.OWN: Only look at the receiver, consult interceptors. (Equivalent to kInterceptor).PROTOTYPE_CHAIN_SKIP_INTERCEPTOR: Full chain, skip interceptors. (Equivalent to kPrototypeChain).PROTOTYPE_CHAIN: Full chain, consult interceptors. (Equivalent to kPrototypeChain | kInterceptor).DEFAULT: Same as PROTOTYPE_CHAIN.The core logic of LookupIterator is implemented in src/objects/lookup.cc. Let's examine the key phases:
When the iterator is created, it calls Start().
JSReceiver, it reads its Map and calls LookupInHolder."length" property or valid index lookups within the string's length. If not found, it falls back to the prototype of the primitive (e.g., String.prototype).This method checks the current holder (the object currently being inspected in the chain) for the property.
DATA or ACCESSOR and fills in PropertyDetails.If the property is not found in the current holder, Next() is called to move up the prototype chain.
NextInternal calls NextHolder(map) to get the prototype of the current map.INTERCEPTOR. V8 may need to invoke embedder callbacks.
The LookupIterator is not just for reading; it also prepares for writes.
PrepareForDataProperty: Called before writing to an existing data property. It handles:
DOUBLE_ELEMENTS.PrepareTransitionToDataProperty: Called when adding a new property. It finds or creates a new Map with the added property and sets the iterator state to TRANSITION.The LookupIterator handles both named properties and indexed elements by templating key methods with a bool is_element parameter.
ElementsAccessor of the object to check the backing store.TYPED_ARRAY_INDEX_NOT_FOUND state) without walking the prototype chain.V8 also provides a ConcurrentLookupIterator (src/objects/lookup.h) for concurrent accesses from a background thread. Despite the name, it is currently an all-static class and not a stateful iterator. It implements safe lookups for specific cases like own data property lookup on fixed COW arrays, strings, and property cells in global objects, handling concurrency issues defensively.
src/objects/lookup.h: Main header file defining LookupIterator and its states.src/objects/lookup.cc: Implementation of the lookup logic, state transitions, and property accessors.