Temp variables inside a context

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

Temp variables inside a context

MrGwen
Hi Paolo,

Is it possible to have access to the value of temp variables inside a context
?

Thanks,
Gwenael


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Temp variables inside a context

Paolo Bonzini-2
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


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk