Showing posts with label ActionScript. Show all posts
Showing posts with label ActionScript. Show all posts

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:

Sunday, October 5, 2008

FlaCC (C/C++ to Flash Compiler)

More news on the C/C++ to Flash compiler, now called FlaCC. With this tool developed by Scott Petersen at Adobe would allow almost any arbitrary C/C++ code to be compiled down to ActionScript bytecode to run on the Flash player. I first heard of this at MAX 2007 and there will be a session at MAX 2008 on this (plus hints it will be in keynotes).

While we wait here are a few links to some more recent news:
The MAX 2008 Session is "Using C++ Libraries in Flash Player and Adobe AIR" Monday, November 17, 2:00 pm - 3:00 pm. I'm looking forward to that session.

Sunday, May 11, 2008

ActionScript Cross Compiler Theories

This post is a bit late coming, but I'm trying to be good and post once a week.

Last year after Adobe MAX 2007, I posted a bit on the preview of a C++ to ActionScript compiler. This year there has been a little more buzz buzz on the subject. I've been curious how this might be done and have a few theories:
  • Create a C++ parser/compiler to either produce Flash bytecodes or ActionScript. C++ is notoriously hard to parse so this would be quite a pain without something to get you started. There is a partial ANTLR grammar for C++ here.
  • Create a gcc backend to produce Flash bytecodes. All gcc languages compile to intermediate languages called GIMPLE and GENERIC. Target platforms "merely" translate this intermediate form to the target platform. See this link for more information. This path would be the most generic solution and allow any gcc language to be compiled to flash.
  • Leverage PyPy (a python VM generator and more) to create an ActionScript or Flash bytecode target. Of course this would only really help you use python unless you created a translator for C++, but flex-pypy is cool enough to mention.
Overall I think the gcc solution is the most likely and most powerful. I'm looking forward to the cross compiler and hope it will be included in Flex 4 as part of the broadening horizons theme.

Friday, October 5, 2007

C++ to ActionScript

One of the sneak peeks at Adobe MAX 2007 Chicago was a C++ to ActionScript translator by Scott Peterson. This was the most interesting of the sneak peeks for me in part because I was a C++ developer for 12 years (until 3 weeks ago) and because of the implications of the translator.

First check out the video here:
http://youtube.com/watch?v=0hX-Uh3oTcE

During the demo he showed:
  • A C++ XSLT library turned into ActionScript
  • A fractal program from C++ to ActionScript simulating green threads using ActionScripts asyncronous calls.
  • Doom 2 running in Flash.
How does it work:
  1. C/C++ code is translated to ActionScript 3
  2. AS3 code is compiled into a SWF
  3. Run the code on Flash/AIR

What does it mean:
  • Easy porting useful open source C++ libraries/programs to ActionScript to run on the browser.
  • Porting interpreters for languges like Ruby, PHP, Lua, Python, Smalltalk. ... to run on Flash.
  • Writing your own DSL using ANTLR and porting the C code to ActionScript without having to write an ANTLR target language. I recently wrote a shunting yard algorithm Lexer and RPN parser in ActionScript for an Excel like language where this would have saved me some time (but not the fun of writing my own Lexer/Parser).
Scott Peterson, if you ever read this and want an alpha/beta tester I have two projects in mind: