In the beginning there was Object

After diving into the details of cross-compiling ActionScript language features to JavaScript we are now looking at runtime features. In my last post I suggested sorting all runtime features into groups that correspond to SWCs in the Flex SDK’s framework/libs folder. There are three SWCs that you would find in my “dreamed up” Flex SDK for JavaScript:

  • frameworks/libs/browser/browserglobal.swc
  • frameworks/libs/browser.swc
  • frameworks/libs/flash.swc

To keep things simple I will assume that you believe me that JavaScript can be embedded into SWCs using a custom COMPC.jar for JavaScript and later extracted with FalconJS’s MXMLC and baked into JavaScript applications. That should allow us to focus on the classes we could put into those three SWCs.

I’ll make the portions even smaller. Today I just want to look at what goes into browserglobal.swc .

 

Smashing Atoms

A reasonable goal for designing a Flex SDK for JavaScript starts in my opinion with the bare minimum set of classes and functions, the “atoms” of the runtime. Those atom classes should go into browserglobal.swc.

The best place to find our atom classes is the Tamarin source depot. In case you have never heard of Tamarin, it is Adobe’s open source version of the virtual machine that runs compiled ActionScript. Mozilla’s Tamarin project page looks a little bit like an abandoned space ship (“The Mozilla engineering team currently estimates that Tamarin will be incorporated into shipping versions of SpiderMonkey and Firefox in 2008″). But as far as I know Tamarin is still an active project. If you go to the Tamarin depot you’ll find under core a file called builtin.as, which includes 12 other ActionScript files:

include "Object.as"
include "Class.as"
include "Function.as"
include "Namespace.as"
include "Boolean.as"
include "Number.as"
include "Float.as"
include "String.as"
include "Array.as"
include "actionscript.lang.as"
include "Vector.as"
include "DescribeType.as"

I would like to prune that list above even further and only pick the classes and functions that are supported by JavaScript in every browser:

Note that I am allowed to add the functions and variables from actionscript.lang.as to that list if I remove isXMLName(), which is okay, because I said earlier that we don’t want to support E4X. Error is a bit tricky, because ActionScript’s Error constructor signature differs from JavaScript’s. But that can easily be adjusted to match JavaScript’s Error class. Number.as contains the definitions for Number, int, and uint. I’ll move out int and uint, because only Number is supported by JavaScript.

Here are my suggestions what we should do with the rest of the ActionScript classes in Tamarin’s core folder:

  • Boolean – should be emulated
  • Class – should be emulated
  • Number – int and uint need to be implemented as IntClass and UIntClass in browser.swc
  • Float – should be mapped to Number
  • Vector – should be mapped to Array
  • Namespace – should be mapped to package names
  • XML – should be mapped to browser.AS3XML in browser.swc
  • DescribeType – should go into avmplus of browser.swc
  • Proxy – should go into flash.utils of flash.swc
  • ByteArray – should go into flash.utils of flash.swc

Now that we have our list of classes that we picked as the bare minimum set for browserglobal.swc how shall we proceed?

 

browserglobal.swc

You compile them into a SWC using COMPC. But which COMPC? This might surprise you but I compile those atomic classes with the “standard” COMPC and not with FalconJS’s COMPC. Why? Because all of those classes are defined as native classes with native methods, which means that MXMLC (the application compiler) will assume that the host environment (in our case the browser) will provide the implementations. In other words when cross-compiling to ActionScript we will not need JavaScript implementations for those atomic classes and it doesn’t matter whether we use the “standard” COMPC or FalconJS’s COMPC.

Actually, it does seem to matter, because I have never been able to successfully build browserglobal.swc with FalconJS or in Flex. The only method that has worked for me is using a modified version of the Tamarin build scripts with the “standard” COMPC.