How to introspect method instance variables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to introspect method instance variables

rdmerrio
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.

I do not really expect complete this, as I said its just a learning
exercise.

The question around the instance variables stems from the fact that they
appear to be tantalizingly close to the surface given the fact that the
Browser recognizes undeclared identifiers and asks if they are temps or
instance and then plugs in the code for me.

I am just too new to be able to figure out how to intercept the messages
being sent.

Thanks for the help and suggestions.

[hidden email] wrote:

> Send Beginners mailing list submissions to
> [hidden email]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
> or, via email, send a message with subject or body 'help' to
> [hidden email]
>
> You can reach the person managing the list at
> [hidden email]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Beginners digest..."
>
>
> Today's Topics:
>
>    1. How to introspect method instance variables (rdmerrio)
>    2. Re: How to introspect method instance variables
>       (Michael van der Gulik)
>    3. Re: How to introspect method instance variables (Yoshiki Ohshima)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 24 Aug 2009 21:16:16 -0500
> From: rdmerrio <[hidden email]>
> Subject: [Newbies] How to introspect method instance variables
> To: [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I have defined a method, i.e.,
>
> someMethod
>     anInstVariable := anotherInstVariable1 + anotherInstVariable2.
>
> I would like to intercept the acceptance of this method by the browser
> and programatically determine what instance variables this method is
> using so that I can grab these names for other processing tasks.
>
> Additionally, I would really like to be able to determine what instance
> variables are being assigned to, for instance, anInstVariable in this
> case and which ones are the "independent" instance variables,
> anotherInstVariable1 and anotherInstVariable2 in this case.
>
> How can I do this?
>
> Thanks
>
>
> ------------------------------
>
> Message: 2
> Date: Tue, 25 Aug 2009 14:34:18 +1200
> From: Michael van der Gulik <[hidden email]>
> Subject: Re: [Newbies] How to introspect method instance variables
> To: "A friendly place to get answers to even the most basic questions
> about Squeak." <[hidden email]>
> Message-ID:
> <[hidden email]>
> Content-Type: text/plain; charset="iso-8859-1"
>
> On Tue, Aug 25, 2009 at 2:16 PM, rdmerrio <[hidden email]> wrote:
>
>  
>> I have defined a method, i.e.,
>>
>> someMethod
>>   anInstVariable := anotherInstVariable1 + anotherInstVariable2.
>>
>> I would like to intercept the acceptance of this method by the browser and
>> programatically determine what instance variables this method is using so
>> that I can grab these names for other processing tasks.
>>
>> Additionally, I would really like to be able to determine what instance
>> variables are being assigned to, for instance, anInstVariable in this case
>> and which ones are the "independent" instance variables,
>> anotherInstVariable1 and anotherInstVariable2 in this case.
>>
>> How can I do this?
>>
>>    
>
> Why? What are you trying to achieve? You're talking about some pretty
> intrusive techniques. Unless you're developing a code analyser of some sort,
> you probably should be looking at a better way of doing what you're doing.
>
> To capture the acceptance of a method (assuming you mean the action that
> happens when you press alt+s), you insert a bit of code into
> PluggableTextMorph>>accept.
>
> To determine which instance variables are being assigned to, you'll need to
> somehow look at the bytecodes. They're not too hard to analyse, but it can
> be a bit of work. Alternatively, maybe the refactory browser can help, or
> maybe you can look at the intermediate code that the compiler generates.
>
> The bytecodes are described here:
> http://burks.bton.ac.uk/burks/language/smaltalk/goldberg/blueb003.htm.
> They're in the "Blue book chapter 28" if you need to Google it. You'll want
> the "store" bytecodes.
>
> To see real bytecodes, either inspect "Morph>>#basicInitialize" to see a
> CompiledMethod, or use the "byteCodes" view in a Browser (hidden behind the
> "source" button).
>
> Gulik.
>
>  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

Randal L. Schwartz
>>>>> "rdmerrio" == rdmerrio  <[hidden email]> writes:

rdmerrio> The question around the instance variables stems from the fact that
rdmerrio> they appear to be tantalizingly close to the surface given the fact
rdmerrio> that the Browser recognizes undeclared identifiers and asks if they
rdmerrio> are temps or instance and then plugs in the code for me.

Not the browser, the compiler.  And the compiler knows all the identifiers in
scope, or it couldn't compile the code.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

Bert Freudenberg
In reply to this post by rdmerrio
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.


You mean

http://common-lisp.net/project/cells/

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 -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

Yoshiki Ohshima-2
> So ... all you need to do is hack the Compiler :)

  And it takes two new methods and four very minor overrides in some
subclasses.

-- Yoshiki

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to introspect method instance variables

rdmerrio
In reply to this post by rdmerrio
The fact that I would potentially need to hack the compiler to write my
little app raised a huge red flag for me, which indicated that I was
probably not approaching the problem correctly. I was not reaching
Smalltalk ZEN. As such, I approached the issue from a different angle
and am happy to say that i accomplished my goal.

I went from ground zero with Smalltalk to accomplishing my goal in just
a few days and am actually quite pleased with the outcome. I have not
had the same early success in other languages I have tried ... So i am
very pleased with Smalltalk and its development environment.

>  
>> So ... all you need to do is hack the Compiler :)
>>    
>
>   And it takes two new methods and four very minor overrides in some
> subclasses.
>
> -- Yoshiki
>
>  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Re: How to introspect method instance variables

David T. Lewis
I think that many of us have had experiences similar to this. It is
ironic but true, the more you "know" about computers and languages, the
harder it is to grok Smalltalk the first time. But it's certainly worth
the effort :)

Dave

On Thu, Aug 27, 2009 at 06:16:25PM -0500, rdmerrio wrote:

> The fact that I would potentially need to hack the compiler to write my
> little app raised a huge red flag for me, which indicated that I was
> probably not approaching the problem correctly. I was not reaching
> Smalltalk ZEN. As such, I approached the issue from a different angle
> and am happy to say that i accomplished my goal.
>
> I went from ground zero with Smalltalk to accomplishing my goal in just
> a few days and am actually quite pleased with the outcome. I have not
> had the same early success in other languages I have tried ... So i am
> very pleased with Smalltalk and its development environment.
> >  
> >>So ... all you need to do is hack the Compiler :)
> >>    
> >
> >  And it takes two new methods and four very minor overrides in some
> >subclasses.
> >
> >-- Yoshiki
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners