[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.
Thanks again for your help.
---- On Wed, 01 Jan 2014 14:15:01 -0800 Ryan Macnak<rmacnak at gmail.com> wrote ----
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 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?
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/0fc05957/attachment-0001.htm
More information about the Vm-dev
mailing list