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);
- For
double(3), the engine needs the valuesvalue = 3andresult = 6. Afterreturn, the number6is assigned tofirst. - The first call finishes and its frame is popped off the stack.
- The call to
double(8)gets its own frame: nowvalue = 8,result = 16, and the result must be returned tosecond.
- Argument
value = 3- Local value
result = 6- Where the result goes
- To the assignment to
first
return 6After returning: first = 6- Argument
value = 8- Local value
result = 16- Where the result goes
- To the assignment to
second
return 16After returning: second = 16During 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.