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-->', (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)
++++++++++++++++++++++++++++++++++++++++
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?