valueWithReceiver:Arguments: Question

Mike Klein mike at twinsun.com
Fri Feb 6 21:54:39 UTC 1998


I like to call this the "magic" method.
It is the only way to break encapsulation (without resorting to C, etc.)
Basicly, it "invokes" a method without first doing dispatch.
Normally, an object can protect itself by making sure that only allowed
messages can be sent to it.  The primitive in this method can bypass this.

This can be used for very powerfull introspection.
For instance, lets say I want to know the class of some object, foo.
(Specifcally, I want to know the object that is in it's hidden 'isA' slot,
the Behavior that is first looked at by the VM for methods after a message is
sent)

I could say:

	foo class
	
but the Behavior of foo may have over-ridden the default implemenation, and
return whatever it feels.  Alternatively, (in VW)

	((Object compiledMethodAt: #class) copy mclass: nil)
		valueWithReceiver: foo arguments: #()

bypasses the message dispatch and invokes my method directly.
(Incidentally, the "mclass: nil" part is to disable the check that this
primitive does to make sure that the compilation class of the method (mclass)
is on the superclass chain of the reciever's (foo's) behavior (true behavior, not
necesarily foo class)

I have found this technique usefull when debugging proxies.  I.E. How to look
at an object without sending it messages (which may cause it to change state)
I actually have a class called Investigator.  It's kinda like an Inspector,
but never sends messages to its target.  Inspector sends things like class,
and instVarAt: which proxies over-ride to do whatever they are proxying for)


I dont know for sure, but I think that this primitive was introduced to allow
unbound methods to be invoked, primarily for multiple doits... but I'm guessing.
The mechanism of adding a method DOIT, sending DOIT, then removing DOIT is kinda
clunky....

Hack away....
	Mike Klein


> Hi,
> 
> I was wondering why there is no valueWithReceiver:arguments: 
> method defined in CompiledMethod class like in VisualWorks or in IBM Smalltalk
> implementation. 
> 
> Are there some really drawbacks?
> Does somebody know exactly why such a method was not introduce in first 
> ST versions and why the later implementations introduce it?
> 
> Thanks to elaborate ;)
> 
> Stephane 
> 
> http://iamwww.unibe.ch/~ducasse/
> "if you knew today was your last day on earth, what would you do 
> different? ... especially if, by doing something different, today 
> might not be your last day on earth" Calvin&Hobbes





More information about the Squeak-dev mailing list