[squeak-dev] About on:do:

Eliot Miranda eliot.miranda at gmail.com
Mon Jul 21 14:13:44 UTC 2008


On Sun, Jul 20, 2008 at 11:59 AM, stephane ducasse <stephane.ducasse at free.fr>
wrote:

> Hello
>
> I would like to know how the VM defines on:do:
>
> on: exception do: handlerAction
>        "Evaluate the receiver in the scope of an exception handler."
>
>        | handlerActive |
>        <primitive: 199>  "just a marker, fail and execute the following"
>        handlerActive _ true.
>        ^ self value
>
>
> 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 }

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.  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).

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


>
> I was imagining that may be the vm stores them in the previous context.
>
> Stef
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20080721/d0de27bc/attachment.htm


More information about the Squeak-dev mailing list