Hi all,
Im looking for a method that extracts the source code of a block. For instance If I have the following blockContext: [^ self] I need a method that returns the following string: ^self. Is there any method in Squeak that does this job or I have to write a new one?
Thanks,
Houssam
____
"Le paradis touche les pieds des mères..."
BlockContext >> #decompile
Seems pretty fragile though as it has to check the sources files if you need to fetch temp names.
Romain
On Aug 8, 2005, at 8:41 PM, Houssam Fakih wrote:
Hi all,
I’m looking for a method that extracts the source code of a block. For instance If I have the following blockContext: [^ self] I need a method that returns the following string: ‘^self’. Is there any method in Squeak that does this job or I have to write a new one?
Thanks,
Houssam
"Le paradis touche les pieds des mères..."
Am 08.08.2005 um 20:41 schrieb Houssam Fakih:
Hi all,
I’m looking for a method that extracts the source code of a block. For instance If I have the following blockContext: [^ self] I need a method that returns the following string: ‘^self’. Is there any method in Squeak that does this job or I have to write a new one?
You could use the decompiler, so something like this:
BlockContext>>printSource ^String streamContents: [:str | self decompile printStatementsOn: aStream indent: 0]
But this would loose all comments and reformate the code... as it's decompiled.
Another thing you could do is to just use the decompiler to build up the mapping from bytecode --> source. (aMethod methodNode sourceMap), and then copy the string directly from the text using the offsets the encoder gives you.
Overall: These parts of Squeak are really strange, it's very old stuff that realy should be redone cleanly. If we would have a good meta-model, stuff like that would be trivial to do.
Marcus
Hi,
I have a related question, that I have been recently struggling with. Short of removing the ifTrue:ifFalse: bytecodes from the VM and the compiler, I am trying to write a #mustBeBoolean method and find the boolean protocol sent (#ifTrue:) and the block. Then I am going to resend it with #perform:with:. I reached through the methodNode and got the selector. I can't find the block argument, however. Is it lost because the #mustBeBoolean overwrote the frame for #ifTrue:? Can I re-instatiate it in the context of ctx? Of course, there is no guarantee that that context is where the block was created.
Here's my method, so far:
mustBeBoolean | ctx msgNode sel | ctx := thisContext sender. msgNode := ctx methodNode block statements at: ctx stackPtr. sel := msgNode selector key. ^ context redirectMessage: (MessageSend receiver: nil selector: sel arguments: (Array with: [])).
thank you, Rob
On Aug 8, 2005, at 1:10 PM, Marcus Denker wrote:
Am 08.08.2005 um 20:41 schrieb Houssam Fakih:
Hi all,
I’m looking for a method that extracts the source code of a block. For instance If I have the following blockContext: [^ self] I need a method that returns the following string: ‘^self’. Is there any method in Squeak that does this job or I have to write a new one?
You could use the decompiler, so something like this:
BlockContext>>printSource ^String streamContents: [:str | self decompile printStatementsOn: aStream indent: 0]
But this would loose all comments and reformate the code... as it's decompiled.
Another thing you could do is to just use the decompiler to build up the mapping from bytecode --> source. (aMethod methodNode sourceMap), and then copy the string directly from the text using the offsets the encoder gives you.
Overall: These parts of Squeak are really strange, it's very old stuff that realy should be redone cleanly. If we would have a good meta-model, stuff like that would be trivial to do.
Marcus
Am 11.08.2005 um 04:50 schrieb Rob Withers:
Hi,
I have a related question, that I have been recently struggling with. Short of removing the ifTrue:ifFalse: bytecodes from the VM and the compiler, I am trying to write a #mustBeBoolean method and find the boolean protocol sent (#ifTrue:) and the block. Then I am going to resend it with #perform:with:. I reached through the methodNode and got the selector. I can't find the block argument, however. Is it lost because the #mustBeBoolean overwrote the frame for #ifTrue:?
there is no frame for ifTrue:, we inline ifTrue to jumps because we then don't need a frame.
Can I re-instatiate it in the context of ctx? Of course, there is no guarantee that that context is where the block was created.
Here's my method, so far:
mustBeBoolean | ctx msgNode sel | ctx := thisContext sender. msgNode := ctx methodNode block statements at: ctx stackPtr.
Is this mapping valid? stackPtr --> statement in the method that is executed. Why should it?
sel := msgNode selector key. ^ context redirectMessage: (MessageSend receiver: nil selector: sel arguments: (Array with: [])).
Marcus
On Aug 11, 2005, at 1:23 AM, Marcus Denker wrote:
Am 11.08.2005 um 04:50 schrieb Rob Withers:
Hi,
I have a related question, that I have been recently struggling with. Short of removing the ifTrue:ifFalse: bytecodes from the VM and the compiler, I am trying to write a #mustBeBoolean method and find the boolean protocol sent (#ifTrue:) and the block. Then I am going to resend it with #perform:with:. I reached through the methodNode and got the selector. I can't find the block argument, however. Is it lost because the #mustBeBoolean overwrote the frame for #ifTrue:?
there is no frame for ifTrue:, we inline ifTrue to jumps because we then don't need a frame.
That is most unfortunate. The blocks are lost off the stack and I will have to change the vm and/or the compiler to stop the inlining.
mustBeBoolean | ctx msgNode sel | ctx := thisContext sender. msgNode := ctx methodNode block statements at: ctx stackPtr.
Is this mapping valid? stackPtr --> statement in the method that is executed. Why should it?
No, probably not. It just happened to work when I was throwing it together. I guess I need to use the pc with the sourceMap to find the current selector, since the Compiler and VM overwrote the original selector. I guess I really don't like the VM changing reality like that.
thanks, Rob
squeak-dev@lists.squeakfoundation.org