[Cuis-dev] Some experimental libraries based on method wrappers

Mariano Montone marianomontone at gmail.com
Fri Jun 17 12:41:57 PDT 2022


Hello,

I've been looking at CodeCoverage package by Nicolas Papagna (it is 
excellently written btw), to try to understand how it worked, in 
particular the method wrapping.

Once I learned how they are implemented, I got the idea of trying some 
experiments with them.

So I'm implementing three packages: MethodAdvisers, BreakpointsManager 
and MessageTracer.

----------------------

MethodAdvisers implements before/after/around "advice" methods.

This is available for example in Emacs: 
https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html

The idea is to modify the semantics of some method "from the outside", 
evaluating things before, after or around the method.

This is an example of how it looks atm:

MyClass after: #someMethod do: [:receiver | Transcript show: 'After some 
method'].

I'm also experimenting with using them for extending Erudite grammar via 
extensions:

UMLExtension>>installGrammarExtension
     EruditeMarkupGrammar around: #markup
         do: [:nextMethod | nextMethod value / self umlParser]

This is on the "dangerous" side and should not be abused, but I think 
can be useful for particular cases where nothing else will do.

https://raw.githubusercontent.com/Cuis-Smalltalk/Erudite/master/experimental/MethodAdvisers.pck.st

----------------------

BreakpointsManager uses method wrappers for installing breakpoints.

List, enable, disable, toggle breakpoints.

Looks like this:

SomeClass setBreakOnEntry: #myMethod.
BreakpointsManager disableAllBreakpoints.
BreakpointsManager enableAllBreakpoints.
BreakpointsManager toggleAllBreakpoints.
BreakpointsManager uninstallAll.

Big downside is that the debugger opens on the method wrapper source, 
not the real method. I wish that could be solved.

https://bitbucket.org/mmontone/mold/raw/master/BreakpointsManager.pck.st

----------------------

MessageTracer uses method wrappers for tracing calls to methods.

Looks like this:

MessageTracerTestSubject trace: #fact:.

And the call trace (with arguments and return values) are printed to the 
Transcript:

Calling a MessageTracerTestSubject>>#fact:
           arguments: #(5)
Calling a MessageTracerTestSubject>>#fact:
           arguments: #(4)
Calling a MessageTracerTestSubject>>#fact:
           arguments: #(3)
Calling a MessageTracerTestSubject>>#fact:
           arguments: #(2)
Calling a MessageTracerTestSubject>>#fact:
           arguments: #(1)
Returning from a MessageTracerTestSubject>>fact:
         returned: 1
Returning from a MessageTracerTestSubject>>fact:
         returned: 2
Returning from a MessageTracerTestSubject>>fact:
         returned: 6
Returning from a MessageTracerTestSubject>>fact:
         returned: 24
Returning from a MessageTracerTestSubject>>fact:
         returned: 120

https://bitbucket.org/mmontone/mold/raw/master/MessageTracer.pck.st


These are all very much in the making, with missing things, tests, and 
tools that would be cool to add; but I think they have potential to be a 
good set of utilities.


Cheers,

      Mariano



More information about the Cuis-dev mailing list