[squeak-dev] About on:do:

stephane ducasse stephane.ducasse at free.fr
Fri Jul 25 09:26:47 UTC 2008


>
Thanks for all these explanation.
I like Smalltalk because I can learn a lot.
I'm writing some columns and chapters on exception-handling in  
Smalltalk for the next volume of Squeak
by Example (volume two) and I will make them public and call for  
feedback when they are more than a draft.


> I'm on a rather non existent internet connexion and I do not have the
> vm code and I would like to know where how the exception and  
> handlerAction block
> are stored.
>
> As the other posters have pointed out primitive 199 is simply a  
> primitive that aways fails.  So exception and handlerAction are  
> stored in the activation of on:do: in its first and second argument,  
> e.g. see
>        | exception handler |
>        [exception := thisContext sender at: 1.
>         handler := thisContext sender at: 2.
>         1 / 0]
>             on: Error
>             do: [:ex| ].
>         { exception. handler }

Excellent!

> The primitive doesn't set any bits in the context.  It merely fails,  
> ad the method is activated exactly as if on:do: did not have a  
> primitive.
>
> The VM finds activations of on:do: by examining the header of each  
> context's compiled method (no bits are set in the context).  Methods  
> with primitives have the primitive number stored in a field in the  
> method header.

Ok I see so when there is an error the VM look in the activation stack  
for 199Matching activation and then check if the raised error is a  
kind of "thisContext sender at: 1" and in such as case evaluate  
"thisContext sender at: 2"


> The VM's primitive is merely an optimization of the Smalltalk code  
> you see:
>
> findNextHandlerContextStarting
> 	"Return the next handler marked context, returning nil if there
>         is none.  Search starts with self and proceeds up to nil."
>
> 	| ctx |
> 	<primitive: 197>
> 	ctx := self.
> 		[ctx isHandlerContext ifTrue:[^ctx].
> 		(ctx := ctx sender) == nil ] whileFalse.
> 	^nil
>
> isHandlerContext
>        "is this context for  method that is marked?"
> 	^method primitive = 199
>
> and similarly for finding unwind contexts (marked with primitive 198).

this looks simple when reading the code.

> BTW, this is a really nice implementation by Andreas and Dan.  In VW  
> there are bits in the method header because primitives are not  
> stored in the method header but in the bytecode.  But this means we  
> need a little bit more machinery.  Using the primitive field is very  
> simple and just as effective

Thanks.!





More information about the Squeak-dev mailing list