ActionScript Inheritance

From Gnash Project Wiki

Jump to: navigation, search

Members __proto__ and prototype

Each ActionScript Object instance has a __proto__ member being a reference to its Class interface.

A Class interface consists of a set of members and a reference to the class constructor.

Whenever a Function is defined, that's automatically a Class, and its interface is stored in the 'prototype' member.

Thus, for example:

  Array;           <-- this is a built-in class
  Array.prototype  <-- this is Array class interface
  Array.prototype.constructor == Array

The last reference is used to do RunTime checking on instances, see below:

  var a = new Array;
  a.__proto__ == Array.prototype;
  a.__proto__.constructor == Array (see above)

In Gnash, object.__proto__ is as_object::m_prototype, and function.prototype is function_as_object::m_properties. Confusing eh ?

Now, the problem is providing m_properties (function.prototype) for built-in classes such as Array, String, Date and the like. We cannot set Global.Array to be a C_FUNCTION, but we have to make it a function_as_object to allow for m_properties definition.

m_properties is where the interface is specified, so for Array it contains all methods and properties of that ActionScript class. Don't forget to add a "constructor" member to m_properties as well, pointing to the array constructor.

I've implemented this approach for _global.Function (see Function.{cpp,h}).
The new model must be applied to all built-in classes --strk 05:52, 13 February 2006 (MST)

The root of the ActionScript class hierarchy is the Object class. We need a way to model this. A simpler approach could be to have the as_object::get_member return a static Object interface when a __proto__ member is requested and an m_prototype is NULL. Alternatively the m_prototype itself might point to this Object interface when not set to some other value.

Function implements

Starting at SWF7 each Function has an associated list of function names being the classes implemented by the Function. The association is triggered by the ACTION_IMPLEMENTS (0x2C) block.


The association is used by the ACTION_INSTANCEOF (0x54) block handler to find out whether a given Function implements the given class (function name).


The ACTION_INSTANCEOF will likely be recursively scan each of the parent classes until a match is found, or fail.


See ActionScriptSuper