Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Private methods (non-static as well as static ones) and private static fields are declared in a new scope, shared by all class members. Thus, they are visible for every method, but not from outside. Non-static private method calls have to be rewritten to run with the correct this.
  • Private non-static fields and internal members are renamed: they are post-fixed by $ plus the inheritance level of the declaring class, so that they cannot name-clash with fields of the same name, declared in a subclass or superclass. The inheritance level is a number starting at 0 for Object, continuing with 1 for each direct subclass of Object, then 2 for classes inheriting from these classes, and so on. So for example a private field foo in a class extending Object would be renamed to foo$1. Internal members are also renamed, because they could name-clash with members of the same name, declared in a subclass or superclass residing in another package.

Static code execution / lazy class creation

In ActionScript, a class may not only contain static members, but also static code. Static fields may also have initializers which are quite similar to static code.
Static code and static initializers are executed exactly once , let's call this "when the class is initialized"created. A class is initialized created right before code that references it is executed. Note that this has nothing to do with import directives, nor does a class trigger immediate initialization of all classes to which it has static dependencies.

No matter how class initialization creation is triggered, it is always executed as a whole:

  1. first, its superclass is initializedcreated (which creates its superclass transitively)
  2. then, all static initializers are evaluated, in the order in which they appear in the source code
  3. last, all static code statements are executed, in the order in which they appear in the source code

This AS3 feature is quite tricky to simulate efficiently without relying on ES5 get/set properties (see below).

Any reference to another class that could not yet be initialized would have to be wrapped by a function that triggers initialization if necessary:

Code Block

OtherClass.something

would become

Code Block

ensureInitialized(OtherClass).something

where ensureInitialized would be a runtime function that checks whether the given class is initialized, if not, perform the initialization, and in any case return the initialized class.
The problem is that this adds a function call to almost all expressions referring to another class, since it is often not possible to determine at compile time that another class must already have been initialized. Only superclasses are guaranteed to have been initialized before code in the current class may run.

Relying on ES5 get/set properties, self-initializing classes can be implemented much more efficiently. A potentially uninitialized class would be accessed as a property of a wrapping object, which could be the package object, e.g. acme.Foo. The trick is that property Foo is first defined by a get function that performs the initialization, then replaces itself by a simple read-only property with a direct reference to the (now initialized) class.

Another approach is to implement the most important cases more efficiently by wrapping the constructor and all static methods in initializing functions that first initialize the class, then execute the original code. This is the approach currently used by Frank's runtime prototype.

Interfaces plus "as" and "is" operator

...

For our implementation, we have to take into account that in a browser, code loading cannot be done synchronously "on demand". Thus we use AMD to resolve all static dependencies and load the code of all classes that are potentially needed at runtime, but create those classes only right before they are actually used.
This changes what our AMD modules return: Instead of the class / constructor function, the return value is a class definition that still has to be initialized to create the actual class.

A class definition is a factory object for a class. A trick for an efficient implementation of the factory method is to use a get property that, upon its first invocation, can be replaced by a simple field with a direct reference to the (now initialized) class. For the sake of brevity and readability (we want the reader to mainly ignore this helper property), let's call that property _ (underscore).

Using this approach, a reference to another class like

Code Block

import com.acme.OtherClass;

public class ThisClass {
  public function ThisClass() {
    OtherClass.doSomething();
  }
  ...
}

would in generated JavaScript become

Code Block

define(["com/acme/OtherClass"], function(OtherClass_) {
  return Object.defineProperty({}, "_", {
    configurable: true,
    get: function() {
      function ThisClass() {
        OtherClass_._.doSomething();
      }
      Object.defineProperty(this, "_", { value: ThisClass });
      ...
      return ThisClass;
    }
  });
});

Note how the definition of ThisClass, instead of directly returning its constructor function, returns an object with a get property _, and how its implementation replaces that property by a read-only reference to the constructor (that's why it has to be configurable).
Also note that we use OtherClass_ (postfixed by an underscore!) to be able to use the original name OtherClass for a local variable as a shortcut to the class when we know it has already been initialized, like so:

Code Block

define(["com/acme/OtherClass"], function(OtherClass_) {
  return Object.defineProperty({}, "_", {
    configurable: true,
    get: function() {
      var OtherClass;
      function ThisClass() {
        (OtherClass = OtherClass_._).doSomething();
        OtherClass.doSomethingElse();
        var x = OtherClass.CONSTANT;
        ...
      }
      Object.defineProperty(this, "_", { value: ThisClass });
      ...
      return ThisClass;
    }
  });
});

Since for the time being, we still target Internet Explorer 8 (or even 7), we need a solution that also works without get properties.
In order not to always need to perform a function call (which results in a runtime penalty and is distracting during debugging), we try to read the field first and only call the factory function in case the property is not yet defined. Following the general pattern of naming explicit get functions (see below), the factory function for IE8 would be called get$_, resulting in the following expression to access a potentially not-yet-created class: (OtherClass_._ || OtherClass_.get$_())

Granted, simulating lazy class creation makes the solution a bit more complex, but it is an important AS3 language feature, and using the property access trick makes the implementation quite efficient, at least in modern browsers. In many cases, creating classes only when they are actually needed even increases performance, at least application start-up time, but also when many static dependencies are not actually needed at runtime at all.

Interfaces plus "is" and "as" operator

In ActionScript, interfaces are mainly used by the compiler for type checking. However, type checks can also be performed at runtime, using the AS3 operators instanceof, is, and as. instanceof only works for classes and automatically has the correct semantics when simulating classes via the prototype chain, so it can be used as-is in JavaScript.
x as T is simply defined as x is T ? x : null and would be implemented as a runtime helper function.
So the only operator we really have to simulate is is. To do so, we need some information about interfaces:

  • for a class, the set of interfaces it implements
  • for an interface, the set of interfaces it extends

For easy on-demand loading of the second information, we should implement interfaces as AMD modules, too. The AMD value of an interface could simply be set set of extended interfaces, but I chose to use a function that receives an object and sets the names of all extended interfaces as keys in that object (the values do not matter, they are simply true). This implementation makes it easy and efficient to unite interfaces: simply apply all interface functions on an empty object.
A class definition requires all AMD modules of interfaces it implements and can thus easily create the set of all interfaces it (transitively) implements. This set is stored as a static meta property, let's call it $implements.
It is convenient to have a check whether a given object implements an interface as a method isInstance(object) each interface provides. This method has to check whether the given object's constructor (its class) has a $implements property, and if so, whether the interface's name is contained in $implements:

Code Block

function isInstance(object) {
  return object !== null && typeof object === "object" &&
         !!object.constructor.$implements &&
         fullyQualifiedName in object.constructor.$implements;
}

where fullyQualifiedName is the fully qualified name of the interface.

The is function now has to tackle some special cases, then check instanceof, and finally check whether T is an interface, and if so, call its isInstance() method:

Code Block

function is(object, type) {
  return !!type && object !== undefined && object !== null &&
         (object instanceof type ||
          typeof type.isInstance === "function" && type.isInstance(object));
}

Packages

In ActionScript, packages do not exist at run-time, instead they lead to class and interface declarations having a fully qualified name to avoid name clashes.
To make a JavaScript expression referencing a fully qualified class resemble the ActionScript syntax, packages are often simulated by nested JavaScript objects, for example a class org.apache.flex.test.Foo would be assigned to a package object and then be used like so:

...