help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Temp variables inside a context


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] Temp variables inside a context
Date: Wed, 25 Mar 2009 14:59:52 +0100
User-agent: Thunderbird 2.0.0.21 (Macintosh/20090302)

Gwenael Casaccio wrote:
> Hi Paolo,
> 
> Is it possible to have access to the value of temp variables inside a context 
> ?

The temporary variables are just after the arguments in the initial
slots of a context.

The problem is that block arguments and temporaries are not in the same
context, but in the block's own context.  This makes it a bit more
complicated, but there is already code that will help you.  In
particular, the packages/browser/DebugSupport.st file contains code to
find the temporary variable names for a context.  It adds a
#variableNames method to contexts.

To find all the variable active in a context you have to get the
variable names for each context following the static chain (i.e. going
from most to least nested block).  See this code in
packages/browser/Debugger.st:

    computeFieldList: anObject [
        <category: 'private'>
        vars := OrderedCollection new.
        fields add: 'thisContext'.
        self setFieldsIn: anObject
    ]

    setFieldsIn: context [
        <category: 'private'>
        | prefix numVars prefixSize |
        numVars := context numArgs + context numTemps.
        (context home == context or: [context outerContext == nil])
            ifTrue: [prefixSize := -2]
            ifFalse: [prefixSize := self setFieldsIn: context outerContext].
        numVars > 0 ifTrue: [prefixSize := prefixSize + 2].
        prefix := String new: (prefixSize max: 0) withAll: $-.
        (1 to: numVars) with: context variableNames
            do:
                [:i :varName |
                fields add: prefix , varName.
                vars add: context -> i].
        ^prefixSize
    ]

fields contains the variable names, while vars contains where they live.
Here is how the contents of vars is accessed:

    currentFieldValue: obj [
        <category: 'private'>
        | variable |
        currentField < 2 ifTrue: [^self].
        variable := vars at: currentField - 1.
        ^variable key at: variable value put: obj
    ]

    currentFieldValue [
        "Return value at currently selected key"

        <category: 'private'>
        | variable |
        currentField == 0 ifTrue: [^nil].
        currentField == 1 ifTrue: [^self value].
        variable := vars at: currentField - 1.
        ^variable key at: variable value
    ]

Paolo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]