What programming languages will let me manipulate the sequence of instructions in a method?

328 views Asked by At

I have an upcoming project in which a core requirement will be to mutate the way a method works at runtime. Note that I'm not talking about a higher level OO concept like "shadow one method with another", although the practical effect would be similar.

The key properties I'm after are:

  • I must be able to modify the method in such a way that I can add new expressions, remove existing expressions, or modify any of the expressions that take place in it.

  • After modifying the method, subsequent calls to that method would invoke the new sequence of operations. (Or, if the language binds methods rather than evaluating every single time, provide me a way to unbind/rebind the new method.)

  • Ideally, I would like to manipulate the atomic units of the language (e.g., "invoke method foo on object bar") and not the assembly directly (e.g. "pop these three parameters onto the stack"). In other words, I'd like to be able to have high confidence that the operations I construct are semantically meaningful in the language. But I'll take what I can get.

If you're not sure if a candidate language meets these criteria, here's a simple litmus test:

Can you write another method called clean which:

  • accepts a method m as input

  • returns another method m2 that performs the same operations as m

  • such that m2 is identical to m, but doesn't contain any calls to the print-to-standard-out method in your language (puts, System.Console.WriteLn, println, etc.)?

I'd like to do some preliminary research now and figure out what the strongest candidates are. Having a large, active community is as important to me as the practicality of implementing what I want to do. I am aware that there may be some unforged territory here, since manipulating bytecode directly is not typically an operation that needs to be exposed.

What are the choices available to me? If possible, can you provide a toy example in one or more of the languages that you recommend, or point me to a recent example?


Update: The reason I'm after this is that I'd like to write a program which is capable of modifying itself at runtime in response to new information. This modification goes beyond mere parameters or configurable data, but full-fledged, evolved changes in behavior. (No, I'm not writing a virus. ;) )

5

There are 5 answers

4
tster On BEST ANSWER

Well, you could always use .NET and the Expression libraries to build up expressions. That I think is really your best bet as you can build up representations of commands in memory and there is good library support for manipulating, traversing, etc.

0
Alain Pannetier On

In java, many professional framework do so using the open source ASM framework.
Here is a list of all famous java apps and libs including ASM.

A few years ago BCEL was also very much used.

1
AudioBubble On

Well, those languages with really strong macro support (in particular Lisps) could qualify.

But are you sure you actually need to go this deeply? I don't know what you're trying to do, but I suppose you could emulate it without actually getting too deeply into metaprogramming. Say, instead of using a method and manipulating it, use a collection of functions (with some way of sharing state, e.g. an object holding state passed to each).

1
Ben Doerr On

I would say Groovy can do this.

For example

class Foo {
   void bar() {
      println "foobar"
   }
}

Foo.metaClass.bar = {->
    prinltn "barfoo"
}

Or a specific instance of foo without effecting other instances

fooInstance.metaClass.bar = {->
    println "instance barfoo"
}

Using this approach I can modify, remove or add expression from the method and Subsequent calls will use the new method. You can do quite a lot with the Groovy metaClass.

4
SK-logic On

There are languages/environments that allows a real runtime modification - for example, Common Lisp, Smalltalk, Forth. Use one of them if you really know what you're doing. Otherwise you can simply employ an interpreter pattern for an evolving part of your code, it is possible (and trivial) with any OO or functional language.