3

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

Seki
  • 11,135
  • 7
  • 46
  • 70
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • To be clear, you want to dynamically change the instructions of an *existing* method? (i.e. not throw together the implementation of a *new* method on the fly?) – Kirk Woll Feb 10 '11 at 15:30
  • 1
    @Kirk Woll: You raise a good point in that I don't particularly care about the implementation details of how the language might satisfy my goals. I've clarified my question a little to highlight the key properties I'm after. – John Feminella Feb 10 '11 at 15:37

5 Answers5

1

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.

tster
  • 17,883
  • 5
  • 53
  • 72
  • 2
    That will let me _build_ up expressions, yes. But can I _unbind an existing method at runtime_ and then subsequently rebind it with a new method that has my just-built expression? – John Feminella Feb 10 '11 at 15:37
  • 2
    @john-feminella: Drop the "method" idea and instead using delegates. This would allow changing the "method" (delegate) at runtime so that invocations of the "method" use the new/updated implementation instead of the existing implementation. Combine with .NET 4.0 System.Dynamic.DynamicObject, and you can have something that _looks_ like methods but are updatable, as requested. – jonp Feb 10 '11 at 15:38
  • @jonp: That sounds like a very attractive approach. I'll add this to my list of research targets. – John Feminella Feb 10 '11 at 15:45
  • Sorry, not DynamicObject, but System.Dynamic.ExpandoObject. – jonp Feb 10 '11 at 15:46
1

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

  • I agree Lisp is a good choice; it's definitely on my list to research. Not sure if your limited-metaprogramming proposal would be sufficient, because the method to be manipulated will have arbitrary contents and will need to be manipulated in arbitrary (but semantically-valid) ways. – John Feminella Feb 10 '11 at 17:04
1

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.

Ben Doerr
  • 1,655
  • 1
  • 13
  • 23
  • That's not quite the same thing as what I want. This demonstrates that one method can be _replaced_ with another of the same name. But what I want includes the ability to _manipulate_ `bar`. As a simple litmus test, it should be possible to write another method `capture_printlns` which accepts an existing method and produces a new method that has only the `println` statements from the original method. – John Feminella Feb 10 '11 at 16:04
0

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.

Alain Pannetier
  • 9,315
  • 3
  • 41
  • 46
0

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.

SK-logic
  • 9,605
  • 1
  • 23
  • 35
  • I am aware of the interpreter pattern. However, wouldn't it be massive overkill to use that here? I don't want to need to implement a good chunk of language internals all over again in miniature form, just to manipulate a sequence of operations. – John Feminella Feb 10 '11 at 16:21
  • If your only concern is a sequence, then you can implement a language with only a sequence primitive available. A mini-interpreter of this kind can be implemented in 10 lines of C# or Java. It is much simpler than feeding a compiler with code in runtime. – SK-logic Feb 10 '11 at 16:25
  • No, I will need the full range of the language's capabilities. That is why I need a language that exposes this, so that I don't have to implement a self-modifying compiler inside the language itself. (In some languages that may not even be possible.) – John Feminella Feb 10 '11 at 16:43
  • What is the "full range"? An embedded interpreter with sequences, bindings, branching, try/catch and loops is still very trivial. If your code evolves fast, it can even be the most efficient implementation, as otherwise you can spend more time compiling and JITing your volatile code than actually executing it. Another important thing is that non-dynamic methods are not garbage collected in .NET. – SK-logic Feb 10 '11 at 16:59