Apr 29, 2007

avmplus 101

Now that we have an avmplus executable, let's see how we can use it.

First let's define what is the avmplus,
the name stand for ActionScript Virtual Machine +,
so basically it's a virtual machine that can execute actionscript bytecode.

That's it, you can not feed the avmplus source code directly, but only bytecode, and as a normal human sorry I do not write bytecode by hand, at most I can barely write readable source code.

So even if we got the avmplus we're missing one piece of the puzzle, the way to generate bytecode (*.abc file) from source code (*.as file).

Download the Flex SDK, and install it.

It's not open source yet, but it will be in few months, just to say that we gonna use it (or at least a part of it) in an unconventionnal way but not that much to bring us the thunder of Adobe ;).

What interest us in that Flex SDK is a library named asc.jar, yep it is the shortname for actionscript compiler, and this little gem can generate *.abc files from *.as sources.

In your Flex SDK installation dir you should find asc.jar, copy and paste it in a working directory, I named mine tamarin/sandbox :p.

Let's run it using Java.
c:\>java -jar asc.jar
And you will obtain the default options for the actionscript compiler
--------------------------------------------------------------
ActionScript 3.0 for AVM+
version 1.0 build d628
Copyright (c) 2003-2004 Macromedia, Inc.
Copyright (c) 1998-2003 Mountain View Compiler Company
All rights reserved

Usage:
  asc {-AS3|-ES|-d|-f|-h|-i|-import |-in |-m|-p}* filespec
  -AS3 = use the AS3 class based object model for greater performance and better error reporting
  -ES = use the ECMAScript edition 3 prototype based object model to allow dynamic overriding of prototype properties
  -d = emit debug info into the bytecode
  -f = print the flow graph to standard out
  -h = print this message
  -i = write intermediate code to the .il file
  -import  = make the packages in the
       specified file available for import
  -in  = include the specified filename
       (multiple -in arguments allowed)
  -m = write the avm+ assembly code to the .il file
  -p = write parse tree to the .p file
  -md = emit metadata information into the bytecode
  -warnings = warn on common actionscript mistakes
  -strict = treat undeclared variable and method access as errors
  -sanity = system-independent error/warning output -- appropriate for sanity testing
  -log = redirect all error output to a logfile
  -exe  = emit an EXE file (projector)
  -swf classname,width,height[,fps] = emit a SWF file
  -language = set the language for output strings {EN|FR|DE|IT|ES|JP|KR|CN|TW}
  -optimize = produced an optimized abc file
--------------------------------------------------------------
Now let's try to run our first example "hello world" using avmplus.

test.as

package
    {
    
    print( "hello world" );
    
    }


Let's see first what happen if we try to run an *.as file directly in avmplus
c:\>avmplus_sd test.as
VerifyError: Error #1042: Not an ABC file.  major_version=24944 minor_version=2573.
You see, avmplus need an *.abc file.

but as we can run asc to compile an *.as file to *.abc, we should be OK.

so first we compile an abc file
c:\>java -jar asc.jar test.as
test.abc, 87 bytes written
then we run it using avmplus
c:\>avmplus test.abc
hello world
Voila we run our "hello world" example with avmplus :).

But wait, there is more to it, if you look closely at the asc options you will see that little gem
-exe  = emit an EXE file (projector)
Yes, you can also use asc to compile an abc file and combine it with the avmplus to compile your own executable file.
c:\>java -jar asc.jar -exe avmplus.exe test.as
test.abc, 87 bytes written
test.exe, 467039 bytes written
and off course you can run that exe
c:\>test.exe
hello world
And if a 400KB executable bother you for a simple hello world, you can still use UPX (the Ultimate Packer for eXecutable) and that will size down that exe almost by half.
And if you're not convinced by a simple hello world, look here for an abc decompiler, that simply reuse abcdump.as.

You can not do much with avmplus for now, look at toplevel.as, you will see that there is only basic file IO and basic command line interaction (readline, writeline,etc.), but this will evolve.

Be the first to comment