John Feminella
John Feminella

Reputation: 311536

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

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:

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. ;) )

Upvotes: 3

Views: 334

Answers (5)

SK-logic
SK-logic

Reputation: 9715

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.

Upvotes: 0

Ben Doerr
Ben Doerr

Reputation: 1655

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.

Upvotes: 1

user395760
user395760

Reputation:

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).

Upvotes: 1

Alain Pannetier
Alain Pannetier

Reputation: 9514

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.

Upvotes: 0

tster
tster

Reputation: 18237

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.

Upvotes: 1

Related Questions