New Block Closure

Anthony Hannan ajh18 at cornell.edu
Fri May 18 19:45:45 UTC 2001


I decided to go for it and implement block closures "correctly".  I've been
working on it this week and hopefully I'll have something to submit in a
week or two.  Below is a overview of my design.

In addition to a context for temporary variables, a block has a closure for
holding references to local variables outside its scope.  Like Associations
are used for sharing global vars, these variables are placed in value
holders which both the outer context and inner closure refer to.  Thus if
one changes a holder value the other will see it.  (Note, if the var is
read-only then the holder is not needed and the value can just be copied).

Here is an example:

sum
	| x |
	x := 0.
	self do: [:y | x := x + y].
	^ x

When this method is executed the MethodContext tempAt: 1 will equal an Array
with: num (single-element arrays are used as value holders).  New bytecodes
for indirect push, store, and storePop are used to access x's value.  When
the block is created a BlockClosure is created instead of a BlockContext.
The BlockClosure closureAt: 1 will contain the same Array with: num for x as
above.  When the block is executed the BlockContext will have a reference to
the BlockClosure and accesses to x will use new bytecodes for indirectly
pushing, and storing closure vars.  The BlockContext will hold its own temps
(y in this case), and there will be no reference to the home context.  Also,
the BlockClosure will contain its own CompiledMethod for just its code in
the block.

Obviously, I'm making many changes to the Compiler and Interpreter.  But I'm
going to keep backwards compatibility so both old and new blocks work, and
you'll be able to set a preference on how you want to compile.  Thank God
(or should I say, thanks to the Smalltalk/Squeak originators) that this is
all changeable in Smalltalk.

Cheers,
Anthony





More information about the Squeak-dev mailing list