Monday, December 15, 2008

LuaAlchemy Project v.1a Preview

Since my last post my experiment to make Lua a runtime scripting language in Flash using Alchemy has become a full open source project located here:
http://code.google.com/p/lua-alchemy/

I even have a partner named Alexander Gladysh whom I met on the Lua boards.

We are approaching a V.1a release of the project. Using LuaAlchemy you can now create any ActionScript class available at runtime, get/set properties, call functions, and set event listeners (actually you can return a Lua function to use as an ActionScript function). The Lua to ActionScript interface is a little raw right now but we plan on adding some Lua sugar to make accessing ActionScript objects, classes, and namespaces in Lua look and feel like using Lua objects.

Below is a preview of the release:

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 where it can take up a full browser screen and is much easier to read.

Along the way to this release we have encountered a few problems which I will discuss. The first problem was speed. Although the Alchemy V.4a compiled code was fast enough for our purposes, it was about 30 times slower running a life simulator written in Lua than natively compiled Lua. The release of Alchemy V.5a has made life run 4 times faster so progress is being made (v.4a had some debug code compiled in that slowed things down).

The second problem has been gluegen. Gluegen is a tool to help you create wrappers around C code that takes a mix of C and ActionScript code to generate C and ActionScript wrappers. Unfortunately this seems to interfear with CLibInit.setSprite() which we wanted to use to provide a sprite that Lua could use for stdin, stdout, and stderr. Glugen created code creates and inits CLibInit as static members and you seemingly need to call setSprite() before init(). We worked around that problem by providing an output TextArea in the demo below, but we plan on writing the wrapper ourselves and not using gluegen at all.

The third problem is related to garbage collection in Flash and Lua. To make Lua functions usable as an ActionScript function we call AS3_Function() to create an ActionScript function that calls a C callback function passing in some contextual data. This data includes some Lua references in a malloced structure and we would like to know when it isn't needed anymore so it can be released. We haven't figured out any way for Alchemy to tell us when the object is no longer needed. A future version will do some cleanup when the lua_state is closed.

Overall our experience has been a positive one and Alchemy is still in Alpha.

No comments:

Post a Comment