[Pharo-dev] [Spy] How to inject code *inside* a method

Tudor Girba tudor at tudorgirba.com
Fri Dec 6 17:10:47 EST 2013


Me, too! :)

Doru


On Fri, Dec 6, 2013 at 9:38 PM, Stéphane Ducasse
<stephane.ducasse at inria.fr>wrote:

> > “get a copy of the IR tree"
> > irMethod := (TestMy>>#test) ir copy.
> >
> > “get all instance variable access IR instructions”
> > allIvarAccesses :=  irMethod allInstructionsMatching: [ :ir |ir
> isInstVarAccess].
> >
> > “insert code for “1 halt” before all of them"
> > allIvarAccesses do: [ :each | each  addInstructionsBefore: {
>  IRInstruction pushLiteral: 1.  IRInstruction send:  #halt. IRInstruction
> popTop }].
> >
> > “generate a new compiledMethod"
> > cm := irMethod generate:  (TestMy>>#test) trailer.
> >
> > “install in the class"
> > TestMy addSelector: #test withMethod: cm.
> >
> > 17 <76> pushConstant: 1
> > 18 <D0> send: halt
> > 19 <87> pop
> > 20 <00> pushRcvr: 0
> > 21 <76> pushConstant: 1
> > 22 <B0> send: +
> > 23 <76> pushConstant: 1
> > 24 <D0> send: halt
> > 25 <87> pop
> > 26 <00> pushRcvr: 0
> > 27 <B0> send: +
> > 28 <7C> returnTop
> >
> >
> > —> TestMy new test
> >
> > What is now yet wiring is quick return methods —> need to clear the
> primitive in that case, should not be hard to do.
>
> I love this in 5 lines!!!!
>
>
>


-- 
www.tudorgirba.com

"Every thing has its own flow"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20131206/a018cbaa/attachment-0002.html>


More information about the Pharo-dev mailing list