JSFunction, SharedFunctionInfo, Context, FeedbackVector)Consider what happens when a JavaScript function is defined inside a loop or method:
function makeAdder(x) { return function add(y) { return x + y; }; } const add5 = makeAdder(5); const add10 = makeAdder(10);
Here, add5 and add10 are two distinct JavaScript function closures (add5 !== add10) because add5 captures x = 5 while add10 captures x = 10. However, both closures execute the exact same source code, have the same AST and Ignition bytecode, and share the same scope layout.
If V8 duplicated the source metadata and bytecode inside every closure instance, memory usage would explode. Conversely, if V8 attached optimization and Inline Cache (IC) feedback strictly to the shared source definition without per-closure cells, closures with different lifecycles could not manage their feedback allocations cleanly.
V8 solves this by splitting a JavaScript function into four cooperating objects:
SharedFunctionInfo (SFI): The static, closure-independent representation of a function definition in source code (one per syntactic function literal).JSFunction: The lightweight, per-closure heap object instantiated each time a function expression or declaration is evaluated at runtime.Context: The heap-allocated lexical environment holding variables (x) captured from enclosing scopes.FeedbackCell & FeedbackVector: The runtime profiling and IC state that drives V8's tiering pipeline (JSDispatchTable + TieringManager).Let us trace a complete example with an outer function and an inner closure:
function outer(a) { let captured = a * 2; return function inner(b) { return captured + b; }; } const fn = outer(21); fn(1);
When const fn = outer(21) is called, V8 creates an activation FunctionContext (Outer_Ctx) to store captured = 42, and instantiates the inner JSFunction (Inner_JSF) pointing its context() slot at Outer_Ctx:
digraph FunctionArchitecture { rankdir=LR; node [shape=record, fontname="monospace", fontsize=10]; subgraph cluster_closures { label="Per-Closure Instances (JS Heap)"; Outer_JSF [label="<h> JSFunction (outer) | <sfi> shared | <ctx> context | <fc> raw_feedback_cell | <dh> dispatch_handle"]; Inner_JSF [label="<h> JSFunction (inner / fn) | <sfi> shared | <ctx> context | <fc> raw_feedback_cell | <dh> dispatch_handle"]; } subgraph cluster_contexts { label="Lexical Context Chain"; TL_Ctx [label="<h> NativeContext / ScriptContext"]; Outer_Ctx [label="<h> FunctionContext (outer activation) | prev: NativeContext | slot[captured]: 42"]; Outer_Ctx -> TL_Ctx [label="previous()"]; } subgraph cluster_shared { label="Shared Metadata & Trusted Space"; Outer_SFI [label="<h> SharedFunctionInfo (outer) | <tfd> trusted_function_data | <ufd> untrusted_function_data | <si> name_or_scope_info"]; Inner_SFI [label="<h> SharedFunctionInfo (inner) | <tfd> trusted_function_data | <ufd> untrusted_function_data | <si> name_or_scope_info"]; Inner_BC [label="BytecodeArray (inner)\n(TrustedSpace)"]; } subgraph cluster_feedback { label="Feedback & Dispatch"; Inner_FC [label="<h> FeedbackCell | <val> value | interrupt_budget"]; Inner_FV [label="<h> FeedbackVector | shared_function_info | closure_feedback_cell_array | invocation_count | IC Slots..."]; JSDT [label="JSDispatchTable Entry\n[ object: BytecodeArray/Code, entrypoint: Builtin/JIT ]"]; } Outer_JSF:ctx -> TL_Ctx:h [label="context()"]; Outer_JSF:sfi -> Outer_SFI:h; // CRITICAL INVARIANT: Inner_JSF's context() points to Outer_Ctx (where it was created)! Inner_JSF:ctx -> Outer_Ctx:h [label="context()"]; Inner_JSF:sfi -> Inner_SFI:h; Inner_JSF:fc -> Inner_FC:h; Inner_JSF:dh -> JSDT; Inner_FC:val -> Inner_FV:h; Inner_SFI:tfd -> Inner_BC; }
[!IMPORTANT]
JSFunction::context()Invariant: Notice thatInner_JSF -> Outer_Ctx [label="context()"]. AJSFunction‘scontext_field always points to the **enclosing scope’sContext** active when theJSFunctionclosure was created (CreateClosure). AnInner_Ctx(ifinneritself had variables captured by a deeper nested function) is only allocated wheninner()is called, never stored asInner_JSF->context().
SharedFunctionInfo (SFI) and Dual Function Data Slots (src/objects/shared-function-info.h)A SharedFunctionInfo represents the static identity of a function across all closures created from that source location. It holds source positions (StartPosition, EndPosition), parameter count (internal_formal_parameter_count), syntax flags (FunctionKind, LanguageMode), ScopeInfo (name_or_scope_info_), and the function's compilation state.
trusted_function_data_ vs untrusted_function_data_)Under the V8 Sandbox (V8_ENABLE_SANDBOX), executable artefacts like BytecodeArray, InterpreterData, and Code live in Trusted Space outside the attacker-mutable main heap, while uncompiled parser metadata (UncompiledData) and Smi builtin IDs live on the untrusted heap.
Therefore, SharedFunctionInfo splits its function data into two paired fields (src/objects/shared-function-info.h):
trusted_function_data_ (TrustedPointerMember<ExposedTrustedObject, kUnknownIndirectPointerTag>):TrustedPointerTable (or kNullTrustedPointerHandle when unused).BytecodeArray, InterpreterData (when Sparkplug or debug bytecode wrappers are attached), Code (for trusted builtins/wasm), WasmExportedFunctionData, WasmJSFunctionData, WasmCapiFunctionData.untrusted_function_data_ (TaggedMember<Object>):Smi::FromInt(Builtin::kNoBuiltinId) when trusted_function_data_ is active).Smi (encoding a Builtin ID, such as Builtin::kCompileLazy), UncompiledDataWithoutPreparseData, UncompiledDataWithPreparseData, UncompiledDataWithoutPreparseDataWithJob, UncompiledDataWithPreparseDataAndJob, FunctionTemplateInfo (for C++ API callbacks), or AsmWasmData.SharedFunctionInfo Function DatastateDiagram-v2 [*] --> Uncompiled: Lazy PreParse (Syntax-only) Uncompiled: untrusted = UncompiledData (Without/With PreparseData)<br/>trusted = null Uncompiled --> UncompiledWithJob: Background Compile Job Started UncompiledWithJob: untrusted = UncompiledData...WithJob<br/>trusted = null Uncompiled --> Bytecode: First Call (CompileLazy -> Ignition) UncompiledWithJob --> Bytecode: Finalize Background Compile Bytecode: trusted = BytecodeArray<br/>untrusted = kNoBuiltinId Bytecode --> Baseline: Sparkplug Compile Baseline: trusted = InterpreterData (BytecodeArray + Sparkplug Code)<br/>untrusted = kNoBuiltinId Bytecode --> Flushed: GC Bytecode Flushing (cold function) Flushed: untrusted = UncompiledDataWithoutPreparseData<br/>trusted = null
JSFunction and the JSDispatchTable (src/objects/js-function.h)A JSFunction is a JSObject (so user code can attach custom properties like fn.customProp = 123) with four core execution fields:
shared_ (TaggedMember<SharedFunctionInfo>): Points to the SharedFunctionInfo.context_ (TaggedMember<Context>): Points to the enclosing lexical Context (or NativeContext if no outer variables were captured).raw_feedback_cell_ (TaggedMember<FeedbackCell>): Points to the closure's FeedbackCell.dispatch_handle_ (JSDispatchHandle): A 32-bit handle indexing into the JSDispatchTable (src/sandbox/js-dispatch-table.h).JSDispatchTable (V8_ENABLE_LEAPTIERING)In modern V8 (Leaptiering), JSFunction does not store a direct Code pointer in the untrusted heap. Instead, calling a JSFunction indexes into the trusted JSDispatchTable using function->dispatch_handle():
JSDispatchTable Entry (128 bits in Trusted Memory) +-----------------------------------+-----------------------------------+ | ObjectPointer (BytecodeArray/Code)| Entrypoint (Raw Machine PC) | | + ParameterCount (uint16_t) | + Tiering Request State | +-----------------------------------+-----------------------------------+
JSDispatchTable entry stores the function‘s expected parameter_count. Call sites compare the actual argument count against the dispatch table entry’s parameter_count to prevent stack corruption if a JSFunction is swapped inside the sandbox.entrypoint points to Builtin::kCompileLazy.ObjectPointer holds the BytecodeArray and entrypoint points to InterpreterEntryTrampoline.ObjectPointer holds the Code object and entrypoint points directly to code->instruction_start().TieringManager requests optimization (JSDispatchTable::SetTieringRequest), it temporarily patches entrypoint to a TieringBuiltin (e.g., Builtin::kStartMaglevOptimizeJob) while leaving ObjectPointer pointing to the existing Code / BytecodeArray!FeedbackCell and FeedbackVector Lifecycle (src/objects/feedback-vector.h)Allocating a full FeedbackVector (which contains an array of IC slots for every load, store, call, and binary operation in a function) for every single function in a web page immediately upon bytecode compilation would waste huge amounts of memory, because many functions are only called once (such as IIFEs, module initializers, or setup functions).
V8 avoids allocating FeedbackVectors for cold functions through a three-stage FeedbackCell::value() lifecycle:
ClosureFeedbackCellArray Exists Before FeedbackVectorSuppose function outer() contains an inner closure const f = () => ... (compiled to the CreateClosure bytecode).
outer()'s very first execution (before outer() has a FeedbackVector), executing CreateClosure requires a FeedbackCell to pass to the newly allocated JSFunction f!outer()'s bytecode is compiled (Compiler::Compile), V8 allocates a lightweight ClosureFeedbackCellArray (containing one FeedbackCell per CreateClosure site in outer) and stores it in outer's FeedbackCell::value().outer() runs long enough to exhaust its initial FeedbackCell::interrupt_budget_ does TieringManager::OnInterruptTick call JSFunction::CreateAndAttachFeedbackVector:FeedbackVector.ClosureFeedbackCellArray from FeedbackCell::value() into FeedbackVector::closure_feedback_cell_array().FeedbackCell::value() to point to the new FeedbackVector!src/objects/js-function.h & src/objects/js-function-inl.h: JSFunction, dispatch_handle_, and CreateAndAttachFeedbackVector.src/objects/shared-function-info.h & -inl.h: SharedFunctionInfo, trusted_function_data_, and untrusted_function_data_.src/objects/feedback-cell.h & src/objects/feedback-vector.h: FeedbackCell, ClosureFeedbackCellArray, and FeedbackVector.src/sandbox/js-dispatch-table.h: JSDispatchTable layout and tiering request mechanics.