Search code examples
smalltalksqueak

getting an instance of a CompiledMethod class


As far as I understand CompiledMethod is a class holding the compiled form of a method. An instance of this class is created each time a method is compiled. This instance is saved in the class to which the method belongs.

My question is if I have a the name of the method, how can I get that instance that holds the compile form of a method in order to run that method with valueWithReceiver: ?

is it by using compiledMethodAt: selector ?


Solution

  • I think that we need more context here.

    Because using the reflection mechanisms you can even do something like:

    CompiledMethod allInstances select: [ :m | m selector = #asString ]
    

    And this will give you all methods with a selector asString. But this action is very strange.

    You can also use #detect: instead of #select: to find a single method.

    If you need to evaluate all found methods, you can use:

    CompiledMethod allInstances
        select: [ :m | m selector = #asString ]
        thenDo: [ :m | m valueWithReceiver: aReceiver ]
    

    Also if you are interested in a methods for one hierarchy, you can do

    YourClass withAllSubclassesDo: [ :class |
        class
            compiledMethodAt: #selector
            ifPresent: [ :method | method valueWithReceiver: aReceiver ]
            ifAbsent:  [ "do nothing" ]