[squeak-dev] How does the VM deal with method dictionaries?

Bert Freudenberg bert at freudenbergs.de
Mon Feb 24 22:21:44 UTC 2020


On Mon, Feb 24, 2020 at 5:14 AM Thiede, Christoph <
Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:

> Hi all,
>
>
> one curious question:
>
>
> c := Object newSubclass.
> c compile: 'foo ^#foo'.
> c methodDict: (ObjectTracer on: c methodDict).
> c flushCache.
> o := c new.
> o foo.
>
>
> I would have expected to get a notification from the ObjectTracer
> when executing the last line, because somehow the execution machinery must
> resolve the method to be executed. But actually, what I get is an MNU
> (Object1 >> #foo).
>
> For comparison: If I skip the ObjectTracer line, the example works and
> #foo is returned.
>
The VM assumes the object in the methodDict slot of a class is a
MethodDictionary. It will look for a method with the #foo selector, and if
it does not find that, it will invoke #doesNotUnderstand instead.

There is only one special value the VM recognizes instead, and that is nil.
If a nil methodDict is encountered, the VM will send #cannotInterpret:.

This lets the image intercept the VM's method lookup. The default handler
for cannotInterpret: invokes the class's #lookupSelector: method and
retries the message send if found.

This was used for example to swap out classes to disk when memory was still
expensive. The class is replaced (via become:) by a stub that has a nil in
the slot that normally is the method dict. So when a message is sent to an
instance of a swapped-out class, the cannotInterpret: mechanism loads the
actual class from disc, replaces the stub, and then continues execution as
if nothing ever happened.

I find looking at the SqueakJS sources quite instructive, since they are
not obscured by all the optimizations in the OpenSmalltalk VM. Here's the
relevant part, findSelectorInClass() which is invoked from send()

https://github.com/bertfreudenberg/SqueakJS/blob/13581caeb489875c4a39a78aa9d95866cc8892a5/vm.interpreter.js#L619

Have fun!
- Bert -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200224/fd565d2c/attachment-0001.html>


More information about the Squeak-dev mailing list