Arguments and return11 / 19

Stack frame: the state of a function call

Each call has its own arguments, local values, and return location.

In the previous lesson, we looked at the call stack. Each entry must let the engine resume a particular call by remembering its data and where to return control.

Consider two calls to the same function:

function double(value) {
  const result = value * 2;
  return result;
}
const first = double(3);
const second = double(8);
  1. For double(3), the engine needs the values value = 3 and result = 6. After return, the number 6 is assigned to first.
  2. The first call finishes and its frame is popped off the stack.
  3. The call to double(8) gets its own frame: now value = 8, result = 16, and the result must be returned to second.
First call · before return
Frame for double(3)
Argument
value = 3
Local value
result = 6
Where the result goes
To the assignment to first
return 6After returning: first = 6
Next call · before return
Frame for double(8)
Argument
value = 8
Local value
result = 16
Where the result goes
To the assignment to second
return 16After returning: second = 16
Two moments during execution. The first call has already finished when the second begins. The diagram shows call data, not V8's exact memory layout.

During nested calls, the caller’s frame stays on the stack while the called function runs. This lets the engine resume the previous function with its data after return. It also works for recursion, where a function calls itself.

The specification describes execution state through execution contexts. Stack frames are one way of organizing that state inside the engine.

Key takeaway

A stack frame stores the state of a particular function call: the data needed to execute it and where execution resumes when the function returns.

Check the primary source ↗

ECMA-262 · Execution Contexts↗How the specification describes the state of individual calls and the execution-context stack.V8 · Stack frames↗The V8 team on function-call state, local values, and returning control.