Using D.eval for Actionscript Evaluation in Flash CS3
Not too long ago I posted about executing Actionscript 3.0 at runtime using Hurlant's eval library. I had used it successfully in a Flex project and was quite happy with the results. However, it turns out the awesomeness doesn't translate to Flash.
It was Tor Damian who pointed out that the library generates compiler errors in Flash. After spending a bit of time trying to resolve them I gave up.
Eventually, I poked around a bit more and found D.eval, an alternative which I somehow missed in the first round.
This library is distributed as freeware and does a great job of processing the bit of Actionscript I've had a chance to test with it. It also has a built-in method for passing data around: the eval() method receives a context object and a this object as parameters.
Unfortunately, it doesn't have try ... catch ... finally support (v1.1 at time of writing). I think this is pretty important for handling runtime errors that could appear in the custom code but it might be possible to catch the error outside the dynamic code and give error feedback there.
Another great feature is the ability to store a program and execute it later. This is especially useful if you need to execute a program several times using different inputs. You only have to parse the expression once and then you can run it over and over again using different this objects.
It looks like D.eval is still well-maintained—another bonus. I'll definitely be using this library more in future projects. Thanks to the folks at RiaOne!
Delicious
Digg
Reddit
Facebook
Google
Yahoo
Technorati

Comments
I can't get it to compile in Flash CS4, it gives me the following error:
5000: The class 'r1.deval.D' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
Do you have any pointers about what I could do?
Hi Anonymous,
Are you linking the D class to a symbol in the library? That's the only way I can think to cause that error and you don't need to do that. If you're trying to make sure the library gets compiled, you just need to import it and use the class in your Actionscript code somewhere.
I drag 'D' from the components window to the library, and then on frame 1, y type "import r1.deval.D". That worked in CS3, am I doing something wrong?
If I don't drag it to the library, I get the error "1172: Definition r1.deval:D could not be found." both in CS3 and CS4.
Hi Anonymous,
Not sure why it's behaving that way but I'd check with the D.eval folks directly.
dynamically generated functions used by D.eval doesnt seem to be real functions that addEventListener can use as feedbacks.
Post new comment