grabbing value of a variabe by its name?

Jecel Assumpcao Jr jecel at merlintec.com
Thu Jul 25 16:43:08 UTC 2002


On Thursday 25 July 2002 13:07, Randal L. Schwartz wrote:
> And it's evil magick.  The kind of which I will rip out if I'm ever
> forced to maintain your code.  If I don't just get frustrated and
> erase your code and start over when forced to maintain it.

Who am I to disagree with Merlyn? :-)

I was pointing out that a solution does exist but it should never 
(NEVER!) be used in application programs. It is for writing debuggers 
and mobile agent platforms. Different tools for different jobs (even 
though C is the language I have programmed in, I still laugh every time 
somebody shows me an *application* written in C).

But I would rather people know what is possible and why it shouldn't be 
done than that they don't do something out of ignorance. So here is a 
little more information about it:

Smalltalk has three pseudo variables - self, super and thisContext. The 
first two will let you access the receiver of the message (with super 
changing the way any messages sent to that are looked up) and 
thisContext will let you access the special object (of class 
MethodContext or BlockContext) which represents the current state of 
execution (with temporary variables and arguments, the receiver, the 
sender, the method being executed, the current instruction pointer, the 
current stack pointer and the stack itself, etc).

Starting from thisContext there is little that you can't do. In 
particular, you can read and change the values of temporary variables 
via their index. And you can get the list of temporary variable names 
in order (and so find the index of the one you want). But remember: 
"With great powers comes great responsibility!!"

-- Jecel



More information about the Squeak-dev mailing list