Eval() and ActionScript.

Over a year ago, Adobe open sourced Tamarin, and there was much rejoicing.
As part of the source drop, Adobe included an actionscript compiler written in actionscript.
A few folks noticed that it sounded a whole lot like an “eval()” method, and thought that once the good folks at Mozilla and Adobe hammered at it for a while, we’d get a fully working eval() function, that would be able to compile JavaScript, and AS3. And probably ES4 too.
Fast-forward to now. Tamarin has evolved quite a bit, and the self-hosting compiler is a lot closer to being done. However, much like a mogwai fed after midnight, it has mutated from its cute AS3 codebase to a somewhat more alien ES4 state.
On the plus side, it still targets a vanilla AVM2, so there aren’t new weird opcodes to worry about. Yet, anyway.

Long story short, I’ve ported that stuff into an as3 library. It’s not done, and won’t be done until the es4 self-compiler itself is done.
It works though, whenever it doesn’t crash. You can try it here.
It has a lot of potential too. Let me count the ways:

  • Dynamically compiled code within the flash player. eval() is possibly the most misused command in JS, but you can do some really neat stuff with it. Plus, the flash VM lets you segregate code rather well, so you can eval() code without chancing a clobber of your existing code. With a bit more investigation, it might even be possible to run untrusted code safely. Maybe.
  • Letting web apps accelerate themselves by eval()-ing themselves in the flash VM. It’d require proxying the browser DOM through ExternalInterface and the Proxy class, which might negate some of the speed benefits. Still, it could probably beat up every other implementation on a SunSpider benchmark, and take their lunch money too.
  • A poor man’s ScreamingMonkey. If esc ever gets to support the full set of ES4 proposals, then every non-ES4 browser out there that already has Flash could suddenly run ES4. That’s a nifty thought right there.

Anyway, that’s a way off. For now, I’ll try and backport esc updates on a semi-regular basis.

Also, crypto people, I haven’t forgotten you. I’ll put a better ASN-1 parser and a bunch of contributed bug fixes out soon.

Oh, and happy new year!

Explore posts in the same categories: flash, actionscript, eval

15 Comments on “Eval() and ActionScript.”

  1. LEE Says:

    eval() huh? Parsing string as code? Scary neat!!

    Happy New Year to you too Metal!

  2. Joan Garnet Says:

    Simply amazing…
    Good start for 2008 :)
    Cheers!

  3. Debabrata Acharjee Says:

    This is next big thing on flex! Thanks buddy for sharing …:)

  4. Metal Hurlant Says:

    A reasonable approach for this project is just to wait.
    The Mozilla and Adobe folks are actively hacking at Tamarin and Esc.
    I probably won’t mess with the current as3 port much, except to try some weird features here and there. Updates will come in the form of drop-ins of compiled .abc files of the Esc project.
    Now once Esc is actually done, except another port to AS3, and a more permanent codebase to go with it.

  5. joe Says:

    awesome! flash now has a REPL!

    I think the link to the binary download is broken. this gives me a 404:
    https://eval.hurlant.com/demo/EvalES4.swc

    btw, what did you use to compile this? when I tried compc, I got a bunch of random syntax errors:

    [compc] /eval/com/hurlant/eval/ast/ForStmt.as(5): col: 36 Error: Syntax error: semicolon is unexpected.
    [compc]
    [compc] public var init : IAstExpr?;

    [compc] /eval/com/hurlant/eval/parse/Parser.as(277): col: 9 Error: Syntax error: expecting colon before leftbrace.
    [compc]
    [compc] {

    and the occasional non-syntax error which makes me wonder if I wasn’t passing the right options to the compiler. I’m pretty new to AS.

    [compc] /eval/com/hurlant/eval/ast/Aster.as(54): col: 1 Error: The public attribute can only be used inside a package.
    [compc]
    [compc] public namespace Ast
    [compc] ^

  6. Jeroen Says:

    I dont understand this (or I dont know how to use it).

    If I pass a string to the Util.print, it doesn’t execute the code.
    For example: Util.print(”5+5″); traces 5+5 and not 10?

    So this isn’t an eval function like in PHP?

  7. Metal Hurlant Says:

    joe: Woops. I did forget to upload that SWC. I’ll fix that soon.
    The demo was compiled directly within Flex Builder. My memory is a bit fuzzy, but I’ll take a closer look at the compiler options to see if I’m doing anything sneaky.

    Jeroen: Try Util.print(5+5); without the double quotes.

  8. Tyler Larson Says:

    This is really great,
    I’d love for it to get to a place that you felt more comfortable with the release. I’ve tested the demo out with a few things and never had problems with crashing or things that didn’t work.

    Do you have a bug list or any areas in mind that need to be cleaned up or to stay away from using?

    I think I’m going to try to implement script tags in my project using this. https://code.google.com/p/htmlwrapper/

    Thanks a bunch for doing this

  9. Pleh Says:

    I just downloaded the source and got it compiling and it all looks good so far, I tried updating the abc files from the latest from tamarin central but it just crashes now.

    That aside, do you know how to manage imports? for example if I try to compile and run a small hello world example that extends sprite and uses a textField then it needs 2 imports to run, so it gives missing reference exceptions at runtime. Including the Import lines at the top of the script doesnt stop it compiling, it just seems to ignore them.

    Also what about compiling multiple classes, is it just a case of compiling each one seperatly, then just wrapping the combined bytearray in a swf?

    and last but not least, packages :) it clearly does not like them at the moment, hopefully the guys at adobe have already implemented them in the latest tamarin :)

    Other than that, congrats on a great job so far :D

  10. Engkee Kwang Says:

    Very nice.

    Would I be able to access custom AS3 classes with this?… Assuming that I incorporate this into my Flex/AS3 application.

  11. Raoul Duke Says:

    w00ty. imports (especially via urls to totally external SWFs) would be keen.

  12. Noemata Says:

    I was trying to build the swc lib, but got a namespace undefined error related to the ast namespace. Any hints on how I should proceed given that you don’t have a build setup for the lib yet?

  13. Noemata Says:

    I’ve concluded that you must have access to documentation that the rest of the world doesn’t have access to. “Aster.as” won’t compile for me, nor can I find documentation that describes its use of namespace in the form of “public namespce Ast” followed by a series of type definitions in what should be a package block but is a “namespace” block that shouldn’t be legal code. The compiler certainly doesn’t think it’s legal.

    About those type definitions … I wasn’t able to find any documentation on that either. So where does all this magic come from? Who do you get your information from?

    Interesting, interesting …

  14. Noemata Says:

    Curious … there are parts of Aster.as that appear to be required in the rest of the project. Those parts wouldn’t be able to compile without it. Do you have a buildable version of the project available? It would appear that the project sources you have published do not represent something that can build.

    I would like to build the project, despite the fact that you have chosen to abandon the effort. Any chance of getting a copy of buildable sources privately?

  15. Anonymous Says:

Comment: