[Cuis-dev] Transparent guard contexts in simulation

Jaromir Matas mail at jaromir.net
Wed Nov 18 13:47:12 PST 2026


Hi Juan,

thanks so much for your positive feedback!

Of course I'm going to write tests to prevent accidental breaking some
hard to spot dependencies between different components of the solution
:) This is just a draft; some polishing and lots of commenting still
need to be done before eventual integration.

Yes, I'm developing the solution in Squeak. Tons of Process and Debugger
tests help to converge to an acceptable solution :)

Compared to Cincom VisualWorks, Instantiations VAST and Dolphin
Smalltalk, this solution allows us to debug so many more exotic examples
involving non-local returns and exceptions, even in unwind blocks.

I'll get back later, any ideas you might have in the meantime are
welcome. Thanks again,

Best regards,
Jaromir


On Nov 18 2026, at 7:15 pm, Juan Vuletich via Cuis-dev
<cuis-dev at lists.cuis.st> wrote:

> Hi Jaromir,
>  
> I haven't reviewed this in full detail, but didn't want to delay an answer.
>  
> This looks great. It is a nice step forward!
>  
> Would you write some tests of the new, correct behavior? I guess this  
> may be a bit tedious, needing to simulate the execution of a debugger  
> controlled by a test. But it seems doable and valuable. Otherwise  
> there's real risk that in the future someone else who doesn't get this  
> as well as you do modifies the code and breaks it.
>  
> I hope you're also suggesting this for Squeak, and it is possible to  
> keep both systems in sync in this area.
>  
> Finally, to load your code in a more recent Cuis image, you may need  
> something like what I'm pasting below. It is not a big deal and I can  
> check these details when we integrate it.
>  
> Thanks!
>  
> !ClassDescription methodsFor: 'fileIn/Out' stamp: 'jmv 18/Nov/2026,  
> 4:48:40 pm (UTC)'!
> methodsFor: categoryName library: popo stamp: changeStamp
>     "May appear in method preambles in chunk files. Therefore may be  
> called during #fileIn.
>     See #methods, #methodsFor:*, scanCategory:*"
> "turn library into method property"
>     ^ self methodsFor: categoryName stamp: changeStamp prior: 0! !
>  
>  
> On 2026-11-16 1:41 PM, Jaromir Matas via Cuis-dev wrote:
>> Hi Juan,
>>  
>> I'm experimenting with an idea to make #runUntilErrorOrReturnFrom:
>> ensure and exception handler contexts (let's call them "guard contexts")
>> transparent to the code being executed in simulation. This would allow
>> debugging scenarios that are currently failing or provide misleading
>> results. I'd like to know your opinion about this idea.
>>  
>> In the enclosed changeset I'm trying to address the following four scenarios:
>>  
>> 1) Inserting guard contexts into the context chain changes the structure
>> of the code being simulated; for instance the semantics of #sender
>> changes fatally for contexts directly above the guard contexts. This is
>> a source of quite a few bugs, especially when doing "step over" such
>> contexts - executing #sender answers the inserted #on:do: guard context
>> instead of true sender (see examples 1 and 2).
>>  
>> The solution I propose is based on marking the guard contexts for the
>> Debugger to be able to differentiate between normal and guard ensure and
>> handler contexts. This marking would allow us to define an additional
>> #genuineSender accessor that would ignore guard contexts and answer the
>> sender that would normally be answered by #sender during execution.
>>  
>> 2) When you select an arbitrary context in the Debugger and do step over
>> it, the selected context may happen to be located in the part of the
>> execution stack that is in the process of unwinding; context chains that
>> are subject to an unwind are supposed to be scanned for unwind contexts
>> (i.e. normal #ensure: and #ifCurtailed: contexts) and only their unwind
>> blocks are supposed to be executed, not the intermediate contexts (see
>> examples 3 and 4).
>>  
>> The solution I propose is that the Process>>#complete: method would
>> check for such a scenario and adjust the non-local return target
>> accordingly (using the new #adjustReturnFor: method).
>>  
>> 3) When a non-local return tries to jump over the guard contexts, the
>> #ensure: guard catches such non-local return, stops it in the middle,
>> and incorrectly lets the execution continue right below the guard
>> context where the non-simulated execution would never continue.
>>  
>> The solution I propose is to use the new #genuineSender accessor in the
>> return methods (return:, returnEvaluating:, #aboutToReturn:through:) and
>> modify #runUntilErrorOrReturnFrom: to allow non-local returns finish
>> properly: the idea is when a guard context catches a non-local return,
>> instead of simply jumping back to the Debugger, the guard reinserts new
>> guard contexts at the intended destination of the caught non-local
>> return, and lets the unwind continue until the destination of the
>> non-local return is reached.
>>  
>> An interesting example is mentioned in #stepToHome: comment: "[Catching
>> UnhandledErrors while stepping] will cause weird effects if using
>> through to step through UnhandledError code, but as the doctor ordered,
>> don't do that; use over or into instead."
>>  
>> These weird effects are, again, caused by the insertion of guard
>> contexts in the sender chain and a non-local return trying to jump over
>> them. In this convoluted case two different executions of
>> #runUntilErrorOrReturnFrom: will interleave and cause the confusion/bug
>> (see example 5).
>>  
>> 4) Simulating ^-return is already covered in the current image.
>>  
>> Summary of all covered scenarios:
>>  
>> 1. sender referring to a guard context -> apply #genuineSender
>> 2. non-local return over a guard context -> reinsert guards in #runUntilErrorOrReturnFrom:
>> 3. step over inside unwind -> search for unwind sections in
>> #adjustReturnFor: and adjust accordingly
>> 4. ^-return -> catch #aboutToReturn:through: in Process>>#complete: and
>> extract the return context
>>  
>>  
>> If you find some time, I'll very much appreciate your feedback, as
>> usual :)
>>  
>> best,
>>  
>> Jaromir
>>  
>>  
>> Examples (failing before the enclosed patch):
>>  
>> 1) debug and step over the block (i.e. over #value) (expect the explorer
>> open on the Doit context, get the explorer on the #on:do: guard
>> context instead)
>> [thisContext sender explore] value.
>> compare:
>> [thisContext genuineSender explore] value.
>>  
>> 2) debug, step into return: 2 and then step over
>> #unwindAndResume:evaluating: (expect to to resume the #ensure: context
>> but get BCR error instead)
>> [thisContext return: 2] ensure: [].
>>  
>> 3) debug, step through to return: 42, select #ensure: context and step
>> over (expect return from DoIt, get BCR error instead)
>> [^2] ensure: [thisContext return: 42]
>>  
>> 4) debug and step through to #stop, then select the second #ensure:
>> context from the top and step over it (expect return from DoIt, get
>> BCR error)
>> [^2] ensure: [[thisContext sender return: 42] ensure: [#stop]].
>>  
>> 5) when you debug the execution of stepping through (i.e. debugging the
>> Debugger) in the following really convoluted example you can watch where
>> will the execution fail to follow the expected logic. The idea is: place
>> `self halt` into #completeStepUpTo:, start debugging the example and
>> step through it which gets you to #completeStepUpTo:; here you watch the
>> suspended context and keep stepping until the suspended context changes
>> from #signalForException: to #runUntilErrorOrReturnFrom:, then step into
>> #completeStep:, in there step into complete:, in there step into
>> #runUntilErrorOrReturnFrom:, in there step into #jump and keep stepping
>> through until the jump transfers you to the debugged process's stack:
>> there you can see the two guard contexts right below #resumeUnchecked:
>> and this is going to be the culprit: #resumeUnchecked: is supposed to
>> resume the signalContext but the guard contexts will catch the non-local
>> return and let the execution continue instead of letting the non-local
>> return complete the jump and resuming at the intended destination. You
>> can keep stepping deeper all the way into #unwindAndResume:evaluating:
>> to watch the exact mechanism of the "weird effects" mentioned in the
>> #stepToHome: comment.
>> self error: #error
>>  
> --  
> Juan Vuletich
> www.cuis.st
> github.com/jvuletich
> researchgate.net/profile/Juan-Vuletich
> independent.academia.edu/JuanVuletich
> patents.justia.com/inventor/juan-manuel-vuletich
>  
> --  
> Cuis-dev mailing list
> Cuis-dev at lists.cuis.st
> https://lists.cuis.st/mailman/listinfo/cuis-dev
> 


More information about the Cuis-dev mailing list