Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Reflection and Metaclasses

thisContext and the Execution Stack

See how Smalltalk makes the call stack an ordinary object: thisContext, sender chains, stack traces, and how the debugger can restart or change running code. Runnable JavaScript stand-ins included.

In most languages the call stack is hidden machinery. In Smalltalk it is made of normal objects called contexts. Each running method or block has a context that stores its receiver, its arguments, its temporary variables and a link to the context that called it (its sender). The pseudo-variable thisContext hands you the current one. That is why Smalltalk debuggers can show every variable, let you edit a method while the program is paused, and then restart the frame as if nothing happened.

This lesson shows what a context contains, how to walk the stack, and why the same idea powers exceptions, the debugger and even coroutines. The Smalltalk examples are read-only; the JavaScript stand-ins simulate a stack you can walk and print.

What a context holds

SMALLTALK
Object subclass: #Demo package: 'Demo'.

Demo >> outer
    ^ self inner: 5

Demo >> inner: n
    ^ thisContext

Run Demo new outer and inspect the result. You would see roughly (read-only):

SMALLTALK
ctx := Demo new outer.
ctx receiver.        "a Demo"
ctx selector.        "#inner:"
ctx arguments.       "#(5)"
ctx sender.          "the context of Demo>>outer"
ctx sender sender.   "the context that ran the do-it"
ctx method.          "the CompiledMethod object"
ctx pc.              "position in the bytecodes"

A context is reified: the VM can turn its internal frame into an object on demand, so you can read it, store it in a variable or pass it to another method. Contexts that nothing refers to disappear when the method returns, so ordinary calls stay fast.

Walking the stack

Because each context points at its sender, printing a stack trace is a loop (read-only):

SMALLTALK
Demo >> printStack
    | ctx |
    ctx := thisContext.
    [ ctx notNil ] whileTrue: [
        Transcript show: ctx printString; cr.
        ctx := ctx sender ]

The debugger's stack list is the same walk, one row per context. Selecting a row shows that frame's variables; the inspector underneath is the normal object inspector.

Here is a runnable stand-in. We cannot read the real JavaScript frames as objects, so the example keeps its own explicit stack of context records and shows how a trace, a sender chain and a "who called me" query fall out of it.

JS Stand-in: an explicit stack of contexts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The trace printed at the base shows every pending call, innermost first, exactly like a Smalltalk walkback.

Why this matters

Exceptions. [ ... ] on: ZeroDivide do: [ ... ] works by marking a context. When an exception is signalled, the system walks the sender chain looking for a handler context. Because the stack is still intact at that moment, a handler can resume (continue after the signal), retry, return a value from the protected block or pass the error up, with no unwinding first.

The debugger. When an error reaches the top, the debugger receives the live context chain. It can:

  • show each frame's receiver and temporaries,
  • edit and recompile the method of a frame and restart that frame,
  • run to the next message send, step into a call, or step over it.

Coroutines and generators. Because a context can be saved and resumed, libraries implement cooperative processes and generators in plain Smalltalk, with no special VM feature.

flowchart TB
  C3["inner: 5<br/>thisContext"] -->|"sender"| C2["outer"]
  C2 -->|"sender"| C1["doIt"]
  C1 -->|"sender"| C0["nil"]
  H["on:do: handler"] -.->|"marked context<br/>found by walking"| C2

Each frame links to the one that called it. An exception handler is found by walking that chain.

This stand-in models handler lookup: each context may carry a handler, and signalling an error walks the chain from innermost outward.

JS Stand-in: finding an exception handler by walking senders

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real Smalltalk can go a step further and continue running after the handler returns control to the signal point. That is possible only because the stack was never thrown away. Compare it with reflection (objects describing themselves) and with the debugger workflow you will meet in the tools chapter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is thisContext?

    thisContext is a pseudo-variable that gives you the active context object, with sender, receiver and temporaries.
  2. 2. How does a context know who called it?

    Every context stores a reference to its sender, so the stack is a linked list of objects.
  3. 3. Why can a Smalltalk exception handler resume after signalling?

    The signal point's frames still exist while the handler runs, so execution can continue there.
  4. 4. What can the debugger do that most languages cannot?

    With reified contexts the debugger recompiles code and restarts a frame without stopping the program.

Summary

  • A context is an object for one running method or block: receiver, arguments, temporaries, sender, program counter.
  • thisContext returns the current one; following sender walks the stack.
  • Exceptions search the sender chain for a handler without unwinding first, which makes resume and retry possible.
  • The debugger edits code and restarts frames because the stack is data.
  • The JavaScript examples are stand-ins with an explicit stack; the Smalltalk code is read-only.

Next: The Image, the Changes File and the Sources File