[Newbies] How to introspect method instance variables
bert at freudenbergs.de
Wed Aug 26 08:58:27 UTC 2009
On 26.08.2009, at 00:36, rdmerrio wrote:
> The reason for the question is strictly pedagogical. I am learning
> Smalltalk and i am playing with implementation of a Cells type
> package ala LISP or pyCells and Cellulose from the Python world.
So you basically want something that looks like an instance variable
but in fact is a computed value.
Squeak's instance variables are not reified. The compiler produces
bytecodes accessing them directly (and very efficiently) but it's not
possible to intercept inst var accesses at the byte code level. You
would need to change the compiler to not generate the direct access
bytecodes, but a message send instead, that can be intercepted.
Tweak's compiler does this. See class CCompiler in Croquet.
It has an extended notion of instance variables called "fields". A
field can be an actual instance variable (like in Smalltalk), a
property (a dictionary entry, like in Python), or a "virtual
field" (like in Cells). Accesses to virtual fields are actually
compiled as message sends. So if "foo" is a virtual field, then
foo := foo + 1
is compiled to
self foo: self foo + 1
behind the scenes, and you are free to implement #foo and #foo:
however you like.
So ... all you need to do is hack the Compiler :)
Alternatively, look at Marcus Denker's IRBuilder and ByteSurgeon which
lets you experiment with byte codes.
- Bert -
More information about the Beginners