Tuesday, December 2, 2008

Lua ActionScript Alchemy

At Adobe MAX 2008 San Francisco, Alchemy was released. Alchemy was once known as FlaCC or Cassava and is a C/C++ to ActionScript compiler.

Alchemy works by translating C/C++ code to ActionScript through a LLVM compiler which instead of producing machine or bytecode produces ActionScript. The translated C/C++ code uses a big ActionScript ByteArray as the C/C++ heap and stack. The performance of alchemy is slow if you martial data from C/C++ code to ActionScript or back but faster (supposedly up to 10 times faster) than ActionScript code you write yourself for data intensive actions where everything operates in the C/C++ memory space - speed improvements over AS code are also due to LLVM doing code optimization which is missing from the normal AS compiler.

After downloading and setting up Alchemy, I started trying to port over a few C/C++ libraries. The following libraries have given me some trouble mainly with needing to adjust makefiles or the ./configure script: Angband, Nethack, guile, Python, Ruby, ARToolkitPlus, SDL, and Squeak. I haven't given up on them but I wasn't in the mood to do battle with Makefiles; I wanted to learn Alchemy.

My success came with compiling the scripting language Lua 5.1.4 and embedding it into a Flex application. The following proof of concept can run a Lua script which returns one or more values and are shown in the Lua Stack Panel. Lua can make the following calls into Flex:
  • getTextAreaProperty(property_name) - Returns the given property of the text area in the lower left hand corner.
  • setTextAreaProperty(property_name, value) - Sets the given property on the text area in the lower left hand corner (must accept string values).
  • callTextAreaFunction(function_name[, param1, ..., paramN]) - Calls a function on the text area in the lower left hand corner (must accept string values).
  • average(value1, ..., valueN) - Returns the average and sum of the given values.


Note that the demo above requires Flash Player 10 (required by Alchemy and my use of the local file system to allow opening/saving files). It is also hosted here.

Should I continue working on this library, I'd want to allow Lua to create and call functions on any arbitrary ActionScript class. Alchemy is still in beta and does have its own problems - not everything builds easily and while pure Alchemy code can be faster than ActionScript, marshaling from ActionScript to C/C++ code and back can slow things down - for example life.lua takes 4 seconds with the standard compile of Lua and 13 minutes! with the Alchemy build. Still it works quite well for my purposes of embedding a runtime scripting language into my Flex applications.

For more information check out the following blog posts:
Edit: Created a google code project to share efforts in using Alchemy to embed Lua in ActionScript. Uploaded the proof of concept:

9 comments:

  1. doesn't SDL have many OS dependent code?.
    You have a very interesting project bro, I'll be watching this.

    Bye!

    ReplyDelete
  2. Very interesting indeed! Congratulations!

    do you have anything that we can download and use/play?

    ReplyDelete
  3. Πετσαγγουράκης Γιώργος: I'll put together my projects and some instructions for download. Getting lua to compile was simple: 1) download and install Alchemy 2) Download and uncompress lua 3) "cd lua; alc-on; make generic". The wrapper was a bit more work. I'll put together a project for people to play with.

    Raul: SDL does have some OS dependent code. The trick would be to implement that dependent code in ActionScript and then compile the rest or see if it is implemented in the Alchemy libraries (someone got doom working after all); which is why I stepped back on that project for now.

    ReplyDelete
  4. Very nice work! I have been trying to get Ruby to compile also. I tried 1.8.7 and 1.9.0-4 with no luck. I'm not fantastic at make and configure so it might be some relatively easy problem.

    I ran into issues with memmove in both and some HAVE_* that appeared to be wrong.

    I have a port of Ruby 1.9 to Actionscript called Red Sun already, but was hoping to use Alchemy for better support and speed.

    I am a big fan of Lua as well, having used it in some game development several years back. Hit me up on twitter/JonathanBranam or jonathanbranam.net if you want.

    I don't believe we met at 360|Flex San Jose, so hello!

    ReplyDelete
  5. Since I've gotten some interest in collaboration I put together a lua-alchemy project on google code:
    http://code.google.com/p/lua-alchemy/

    I use git but most of the people I work with at Universal Mind don't so I used google code. I haven't used github, but a lua guy (Alexander Gladysh) will likely setup a mirror for the project.

    I'll post my proof of concept zip with some instructions to the project shortly - the POC isn't really what I want to start with for the project so I wasn't planning on adding it to the repository.

    ReplyDelete
  6. I am working on porting SDL to flash. I have the project up on github at http://github.com/bwr/flashsdl/tree/master

    It is in a very early state.

    ReplyDelete
  7. bwr: I'll watch the SDL port.

    I found at least one interested party in the port and he has made a github repository for lua-alchemy. We are discussing the merits of using google code and github - google code has forums and issue tracking while github uses git. My coworkers mainly use subversion (while I use git-svn).

    http://github.com/agladysh/lua-alchemy/tree/master

    ReplyDelete
  8. Great project! I can't wait to see where this goes. BTW -- many of the performance issues mentioned have been discussed on the Alchemy Forums . Marshaling is one that has come up a few times. I am working to get the answers into the Alchemy FAQ.

    ReplyDelete