Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Frame
- The logical state of one invocation: parameter bindings, local work and where its caller continues. It is not a physical memory-layout promise.
- Call stack
- The unfinished nested calls in the synchronous chain being examined: the innermost call runs while its callers wait.
- Stack trace
- A runtime or tool report of a captured call chain. Its ordering and available frames belong to that report’s profile.
- Shared state
- State reached by several calls, owned by a caller or object that remains alive across them. Separate counters may need separate owners.
A frame is a model of an unfinished call
A conceptual call frame records one invocation’s parameters, ordinary locals and place to resume its caller. A nested call adds another unfinished invocation; returning resumes the waiting one. The call stack describes this nesting without promising a particular physical allocation for every variable.
Data lifetime depends on ownership and the language’s rules, rather than on the continued existence of the local name that first referred to it. Likewise, a failure report exposes only the caller evidence its runtime supplies. Use the source together with that evidence to distinguish local work, retained state and the responsible call site.
01Each call’s own variables
Which count belongs to this arrival call?
A service-counter replay tool checks arrival numbers and purchase/refund receipts. Opening a second counter means its count must survive helper returns without leaking into the other queue. A receipt replay also stops on an unknown product; the failure report must lead back to the caller that supplied it.
Before wiring the arrival helper, use encore as a reduced lifetime probe. It increments one invocation-local count and one count whose owner spans both calls. Compare the second observation with the first: which work is initialized again, and which value can carry between arrivals?
local is declared inside encore; shared is a module-level let.
let shared = 0;
function encore(): void {
let local = 0;
local += 1;
shared += 1;
console.log(`local ${local} shared ${shared}`);
}
encore();
encore();
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment.
02After the return
Can the created counter survive the construction call?
Creating two service counters raises a different lifetime question: can each created count remain usable after its construction helper returns? The selected program tries two constructions and separate updates. Distinguish a returned value or valid owner from a reference to a local whose lifetime ends.
newCounter creates and returns a fresh object value.
function newCounter(): { count: number } {
const counter = { count: 0 };
return counter;
}
const a = newCounter();
const b = newCounter();
a.count += 2;
b.count += 1;
console.log(a.count, b.count);
03When a call fails
What caller evidence does the receipt failure provide?
The receipt path now stops during a lookup. This reduction sends kiwi from main through lineTotal to a priceOf that fails. Predict the evidence produced after start, then use the source and that evidence to find which expression supplied the product code.
priceOf creates and throws a new Error that nothing catches; use Node’s default stack formatter.
function priceOf(code: string): number {
throw new Error(`unknown product ${code}`);
}
function lineTotal(code: string, quantity: number): number {
return priceOf(code) * quantity;
}
function main(): void {
console.log("start");
console.log(lineTotal("kiwi", 2));
}
main();
04Repair the service-counter helpers
Queue numbers, receipt diagnostics, then counter-local sequences
The service-counter model has an arrival helper and receipt helpers. First keep the next queue number across arrivals. Then use the receipt call chain to locate a wrong product argument: the catalog prices apple at 2, pear at 4 and plum at 3; purchases multiply by quantity and refunds return one unit’s price. Finally replay interleaved arrivals at independent counters, each starting at 1.
The input is an in-memory replay of arrivals and receipt requests. The output is the counter’s queue/receipt record. Counters run in input order here; independent sequences are required even without threads. The receipt example concerns call frames and arguments, rather than a separate pricing algorithm.
Repair the service-counter helpers
State that must outlive a call, a crash traced back to its caller, then state that one shared variable cannot hold.
In the frontend Keep arrival state with the counter instance Optional
A ticket button also needs a count that survives handler calls. Keep it with the mounted component instance, so separate counters own separate values. The framework’s retained state is distinct from a handler invocation’s ordinary local variables.
'use client';
import { useState } from 'react';
export default function ServiceCounter() {
const [issued, setIssued] = useState(0);
function arrive() {
setIssued((current) => current + 1);
}
return <button onClick={arrive}>Ticket {issued}</button>;
}
React calls the component function again during rendering. useState retains the count for that mounted instance; an ordinary initializer inside arrive would run on each click, and a render-local initializer would run on each render.
Sources: React: state as component memory,Svelte: component scripts,Svelte: $state.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?