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

gettimothy gettimothy at zoho.com
Wed Jan 1 22:38:03 UTC 2014

```Thank you.

I had the mis-conception of 1 MethodContext representing the code in that Doit.

>From the BlueBook

A MethodContext ...  Represents the execution of a CompiledMethod in response to a message.

Funny how you can read a thing a dozen times and until you try to do something with it, you don't really understand it.

---- On Wed, 01 Jan 2014 14:15:01 -0800 Ryan Macnak&lt;rmacnak at gmail.com&gt; wrote ----

On Wed, Jan 1, 2014 at 2:55 PM, gettimothy &lt;gettimothy at zoho.com&gt; wrote:

In Eliot's first example on fixing the reentrancy problem 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--&gt;', (thisContext sender) class name, '(',   (thisContext sender) identityHash asString,')'.
Transcript show: '--home--&gt;', (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--&gt;', (thisContext sender) class name, '(',   (thisContext sender) identityHash asString,')'.
Transcript show: '--home--&gt;', (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--&gt;', (factorial sender) class name, '(',   (factorial sender) identityHash asString,')'.
Transcript show: '--home--&gt;', (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--&gt;', (fc sender) class name, '(',   (fc sender) identityHash asString,')'.
Transcript show: '--home--&gt;', (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--&gt;UndefinedObject(3840)--home--&gt;MethodContext(2677)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
fc = BlockContext(3934)--sender--&gt;UndefinedObject(3840)--home--&gt;MethodContext(2677)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
n=1 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)
++++++++++++++++++++++++++++++++++++++++
-----------------------------------------------
n=2 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)
n=1 BlockContext(1198)--sender--&gt;BlockContext(3934)--home--&gt;MethodContext(2677)
++++++++++++++++++++++++++++++++++++++++
-----------------------------------------------
n=3 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)
-----------------------------------------------
n=2 BlockContext(1684)--sender--&gt;BlockContext(3934)--home--&gt;MethodContext(2677)
n=1 BlockContext(2780)--sender--&gt;BlockContext(1684)--home--&gt;MethodContext(2677)
++++++++++++++++++++++++++++++++++++++++

Look at the 3'rd line, where n=1.
Why is the sender--&gt;MethodContext(2367) instead of sender--&gt;MethodContext(2677)
What created that new MethodContext?

That would be the activation of Interval&gt;&gt;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/0fc05957/attachment-0001.htm
```