[squeak-dev] Global variable lookup

Igor Stasenko siguctua at gmail.com
Fri Jan 11 19:05:57 UTC 2013


FYI, this is a code from Pharo, which we are did _right_ (of course
from Pharo perspective ;)

Class>>bindingOf: varName
	"Answer the binding of some variable resolved in the scope of the
receiver, or nil
	if variable with such name is not defined"

	"The lookup recurses up to superclasses looking inside their class
and shared pools,
	but not the environment, since two classes, even if they have
ancestry relationship,
	could use different environments.
	That's why we doing an environment lookup only as a last step of symbol lookup
	and taking only the environment of receiver only, not any of it's
superclass(es) "
	
	| aSymbol binding |
	aSymbol := varName asSymbol.

	^ (self innerBindingOf: aSymbol) ifNil: [
		 self environment bindingOf: aSymbol
	]


Class>>innerBindingOf: aSymbol
	"Answer the binding of some variable resolved in the scope of the
receiver, or one of its superclass
	but do not look up binding in receiver's environment.
	Use #bindingOf: for looking up the variable binding in a full scope,
including receiver's environment"
	
	| binding |

	"First look in classVar dictionary."
	binding := self classPool bindingOf: aSymbol.
	binding ifNotNil: [^binding].

	"Next look in shared pools."
	self sharedPools do: [:pool |
		| aBinding |
		aBinding := pool bindingOf: aSymbol.
		aBinding ifNotNil: [^aBinding ].
	].

	superclass ifNotNil: [
		^ superclass innerBindingOf: aSymbol.
	].	
	^ nil


On 11 January 2013 19:28, Bert Freudenberg <bert at freudenbergs.de> wrote:
> bindingOf:environment:



-- 
Best regards,
Igor Stasenko.


More information about the Squeak-dev mailing list