[Vm-dev] BlockContext question from one of Eliot's examples.

Ryan Macnak rmacnak at gmail.com
Wed Jan 1 22:15:01 UTC 2014


On Wed, Jan 1, 2014 at 2:55 PM, gettimothy <gettimothy at zoho.com> wrote:

>
> In Eliot's first example on fixing the reentrancy problem<http://www.mirandabanda.org/cogblog/category/cog/page/14/>that uses "factorial copy" for the recursion.
>
> I modifed his code:
>
>
>    | factorial |
>         factorial := [:n| n = 1 ifTrue: [1] ifFalse: [(factorial copy
> value: n - 1) * n]].
>         (1 to: 10) collect: factorial copy
>
>
>
> into something I could trace out a bit easier.
>
>
> | factorial fc |
> Transcript clear.
> factorial := [:n  |
>     n = 1
>     ifTrue:[
>         Transcript show: ' n=',  (n asString),' ', (thisContext class
> name), '(',  (thisContext identityHash asString),')' .
>         Transcript show: '--sender-->', (thisContext sender) class name,
> '(',   (thisContext sender) identityHash asString,')'.
>         Transcript show: '--home-->', (thisContext home) class name,  '(',
> (thisContext home) identityHash asString,')'; cr.
>
>         Transcript show: '++++++++++++++++++++++++++++++++++++++++';cr.
>
>          "thisContext explore.    "
>         1
>     ]
>     ifFalse:[    Transcript show:
> '-----------------------------------------------';cr.
>         Transcript show: ' n=',  (n asString),' ', (thisContext class
> name), '(',  (thisContext identityHash asString),')' .
>         Transcript show: '--sender-->', (thisContext sender) class name,
> '(',   (thisContext sender) identityHash asString,')'.
>         Transcript show: '--home-->', (thisContext home) class name,  '(',
> (thisContext home) identityHash asString,')'; cr.
>
>         (factorial copy value: n-1) * n
>         ]].
> Transcript show:' factorial = ' , (factorial class name),'(',factorial
> identityHash asString,')'.
> Transcript show: '--sender-->', (factorial sender) class name, '(',
> (factorial sender) identityHash asString,')'.
> Transcript show: '--home-->', (factorial home) class name,  '(',
> (factorial home) identityHash asString,')'; cr.
> Transcript show: '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^';cr.
>
> fc := factorial copy .
>
> Transcript show:' fc = ' , (fc class name),'(',fc identityHash
> asString,')'.
> Transcript show: '--sender-->', (fc sender) class name, '(',   (fc sender)
> identityHash asString,')'.
> Transcript show: '--home-->', (fc home) class name,  '(', (fc home)
> identityHash asString,')'; cr.
> Transcript show: '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^';cr.
>
>
> thisContext inspect.
> factorial inspect.
> fc inspect.
> (1 to: 3) collect: fc
>
>
>
>
> The Transcript output is as follows:
>
>
>  factorial =
> BlockContext(694)--sender-->UndefinedObject(3840)--home-->MethodContext(2677)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>  fc =
> BlockContext(3934)--sender-->UndefinedObject(3840)--home-->MethodContext(2677)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>  n=1
> BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)
> ++++++++++++++++++++++++++++++++++++++++
> -----------------------------------------------
>  n=2
> BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)
>  n=1
> BlockContext(1198)--sender-->BlockContext(3934)--home-->MethodContext(2677)
> ++++++++++++++++++++++++++++++++++++++++
> -----------------------------------------------
>  n=3
> BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)
> -----------------------------------------------
>  n=2
> BlockContext(1684)--sender-->BlockContext(3934)--home-->MethodContext(2677)
>  n=1
> BlockContext(2780)--sender-->BlockContext(1684)--home-->MethodContext(2677)
> ++++++++++++++++++++++++++++++++++++++++
>
>
>
> <http://www.mirandabanda.org/cogblog/category/cog/page/14/>
> Look at the 3'rd line, where n=1.
> Why is the sender-->MethodContext(2367) instead of
> sender-->MethodContext(2677)
> What created that new MethodContext?
>

That would be the activation of Interval>>collect:, which performs the
first call of the copied factorial block for each 1 through 3.

collect: aBlock
| nextValue result |
result := self species new: self size.
nextValue := start.
1 to: result size do:
[:i |
result at: i put: (*aBlock value: nextValue*).
nextValue := nextValue + step].
^ result
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20140101/62e4e463/attachment.htm


More information about the Vm-dev mailing list