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

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Tue Feb 25 08:36:18 UTC 2020


Hi Bert,


thanks for the helpful answer. So there is no chance to use a first-class object for the method dictionary - unless I set the instance variable methodDictionary to nil and force the VM to send #cannotInterpret: for every message? :-)

<http://www.hpi.de/>

Your SqueakJS sources are indeed very helpful for studying a VM implementation. Great work!

Best,
Christoph
________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Bert Freudenberg <bert at freudenbergs.de>
Gesendet: Montag, 24. Februar 2020 23:21:44
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] How does the VM deal with method dictionaries?

On Mon, Feb 24, 2020 at 5:14 AM Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de<mailto: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/20200225/e81efc0f/attachment.html>


More information about the Squeak-dev mailing list