com.flagstone.transform
Class FSNewFunction2

java.lang.Object
  extended by com.flagstone.transform.FSTransformObject
      extended by com.flagstone.transform.FSActionObject
          extended by com.flagstone.transform.FSNewFunction2
All Implemented Interfaces:
java.lang.Cloneable

public class FSNewFunction2
extends FSActionObject

The FSNewFunction2 action is used to create a user-defined function.

It was added in Flash 7 to improve the performance of function calls by allowing variables to be pre-loaded to a set of up to 256 internal registers and controlling the loading of pre-defined variables such as _root, _parent, _global, super, this and the arguments array containing the arguments passed to the function.

Attributes
name A string defining the name of the function.
registerCount The number of registers, up to 256, to allocate for storing variables.
optimizations A code consisting of a number of flags that control the execution environment for the function.
arguments An array of FSRegisterVariable objects containing the names of the arguments and whether they will be assigned to internal registers or defined in memory as local variables. The order of the FSRegisterVariable objects in the argument array indicate the order in which the values will be popped off the stack when the function is executed. The fist argument is popped from the stack first.
actions An array containing the actions that are executed.

The optimisation attribute is a compound code, containing a number of flags that control how the environment of the function is initialised. Controlling the pre-defined variables that are created and pre-loading them into registers can significantly improve performance.

Environment Description
CreateSuper Create and initialise the super variable with the parent class of the function.
CreateArguments Create the arguments variable which contains the arguments passed to the function.
CreateThis Create the and initialise the this variable with the object.
LoadThis Pre-load the this variable into register number 1.
LoadArguments Pre-load the parent variable into register number 2.
LoadSuper Pre-load the super variable into register number 3.
LoadRoot Pre-load the _root variable into register number 4.
LoadParent Pre-load the _parent variable into register number 5.
LoadGlobal Pre-load the _global variable into register number 5.

The register numbers that the predefined variables, this, parent, super, _root, _parent and _global are assigned to are fixed. When specifying which of the functions arguments are also assigned to registers it is important avoid these locations otherwise the variables will be overwritten.

User-defined functions are also used to create methods for user-defined objects. The name of the function is omitted and the function definition is assigned to a variable which allows it to be referenced at a alter time. See the example below.

The arguments supplied to the function can be referenced by the name supplied in the arguments array.

All the action objects added are owned by the function. They will be deleted when the function definition is deleted.

Examples

1. Define a function
Define a function that increments the value passed to it:

 // List the names of the arguments
 
 ArrayList arguments = new ArrayList();
 
 arguments.add("value");
 
 // Now define the actions performed by the function. Values passed to the function
 // can be referenced by the name defined in the array of arguments.
 
 ArrayList actions = new ArrayList();
 
 actions.add(new FSPush("value"));
 actions.add(FSAction.GetVariable());
 actions.add(new FSPush(1));
 actions.add(FSAction.Add());
 actions.add(FSAction.Return());
 actions.add(new FSNewFunction2("increment", arguments, actions));
 

The function can then be referenced using it's name:

 actions.add(new FSPush(1));
 actions.add(new FSPush("increment"));
 actions.add(FSAction.ExecuteFunction());
 

2. Defining a method.
When creating a user-defined object the name of the function can be omitted. Simply assign the function definition to a variable:

  actions.add(new FSPush(methodVariable));
 
  FSVector<FSString> arguments;
 
  arguments.push_back("value");
 
  ArrayList actions = new ArrayList();
 
  actions.add(new FSPush("value"));
  actions.add(FSAction.GetVariable());
  actions.add(new FSPush(1));
  actions.add(FSAction.Add());
  actions.add(FSAction.Return());
  actions.add(new FSNewFunction2(arguments, actions));
  actions.add(FSAction.SetVariable()));
 

The function can then be executed by pushing the arguments onto the stack then calling the function assigned to the variable:

 // Push argument(s) onto stack
 
 actions.add(new FSPush(1));
 
 // Get the variable that contains the function
 
 actions.add(new FSPush(methodVariable));
 actions.add(FSAction.GetVariable());
 
 // Execute the function
 actions.add(FSAction.ExecuteMethod());
 

3. Controlling the execution environment.
The code that defines the optimizations that improve execution performance is created by bitwise-ORing the individual flags together.

 int optimizations = FSNewFunction2.CreateSuper | FSNewFunction2.PreloadSuper;
 

History

The FSNewFunction class represents the ActionDefineFunction2 action of the Macromedia Flash (SWF) File Format Specification. It was introduced in Flash 7.


Field Summary
static int CreateArguments
          Create and initialized the predefined variable, arguments.
static int CreateSuper
          Create and initialized the predefined variable, super.
static int CreateThis
          Create and initialized the predefined variable, this.
static int LoadArguments
          Load the predefine variable, arguments, into register 2.
static int LoadGlobal
          Load the predefine variable, _global, into register 6.
static int LoadParent
          Load the predefine variable, _parent, into register 5.
static int LoadRoot
          Load the predefine variable, _root, into register 4.
static int LoadSuper
          Load the predefine variable, super, into register 3.
static int LoadThis
          Load the predefine variable, this, into register 1.
 
Fields inherited from class com.flagstone.transform.FSActionObject
Call, ExceptionHandler, GetUrl, GetUrl2, GotoFrame, GotoFrame2, GotoLabel, If, Jump, length, NewFunction, NewFunction2, Push, RegisterCopy, SetTarget, Table, type, WaitForFrame, WaitForFrame2, With
 
Constructor Summary
FSNewFunction2(java.util.ArrayList arguments, java.util.ArrayList actions)
          Constructs an anonymous FSNewFunction with the specified argument names and actions to be executed.
FSNewFunction2(FSCoder coder)
          Construct an FSNewFunction2 object, initialising it with values decoded from an encoded object.
FSNewFunction2(FSNewFunction2 obj)
          Constructs an FSNewFunction2 object by copying values from an existing object.
FSNewFunction2(java.lang.String name, java.util.ArrayList arguments, java.util.ArrayList actions)
          Constructs an FSNewFunction with the specified name, argument names and actions to be executed.
 
Method Summary
 void add(FSActionObject anAction)
          Adds the action object to the array of actions.
 void add(FSRegisterVariable anArgument)
          Adds the name of an argument to the array of argument names.
 void appendDescription(java.lang.StringBuffer buffer, int depth)
          AppendDescription is used to present a string description of the object including all nested objects up to a specified depth.
 java.lang.Object clone()
          Creates a deep copy of the entire object.
 void decode(FSCoder coder)
           
 void encode(FSCoder coder)
           
 boolean equals(java.lang.Object anObject)
          Returns true if anObject is equal to this one.
 java.util.ArrayList getActions()
          Gets the actions executed by the function.
 java.util.ArrayList getArguments()
          Gets the array of FSRegisterVariables that define the function arguments and whether they are assigned to internal registers or to local variables in memory.
 java.lang.String getName()
          Gets the name of the function.
 int getOptimizations()
          Get the code containing the compound flags that control the execution environment of the function or method.
 int getRegisterCount()
          Gets the number of registers to allocate for function variables.
 int length(FSCoder coder)
           
 void setActions(java.util.ArrayList anArray)
          Sets the actions.
 void setArguments(java.util.ArrayList anArray)
          Sets the array of FSRegisterVariables that define the function arguments and whether they are assigned to internal registers or to local variables in memory.
 void setName(java.lang.String aString)
          Sets the name of the function.
 void setOptimizations(int code)
          Set the code containing the compound flags that control the execution environment of the function or method.
 void setRegisterCount(int count)
          Sets the number of registers to allocate for function variables.
 
Methods inherited from class com.flagstone.transform.FSActionObject
getType, length
 
Methods inherited from class com.flagstone.transform.FSTransformObject
name, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CreateSuper

public static final int CreateSuper
Create and initialized the predefined variable, super.

See Also:
Constant Field Values

CreateArguments

public static final int CreateArguments
Create and initialized the predefined variable, arguments.

See Also:
Constant Field Values

CreateThis

public static final int CreateThis
Create and initialized the predefined variable, this.

See Also:
Constant Field Values

LoadThis

public static final int LoadThis
Load the predefine variable, this, into register 1.

See Also:
Constant Field Values

LoadArguments

public static final int LoadArguments
Load the predefine variable, arguments, into register 2.

See Also:
Constant Field Values

LoadSuper

public static final int LoadSuper
Load the predefine variable, super, into register 3.

See Also:
Constant Field Values

LoadRoot

public static final int LoadRoot
Load the predefine variable, _root, into register 4.

See Also:
Constant Field Values

LoadParent

public static final int LoadParent
Load the predefine variable, _parent, into register 5.

See Also:
Constant Field Values

LoadGlobal

public static final int LoadGlobal
Load the predefine variable, _global, into register 6.

See Also:
Constant Field Values
Constructor Detail

FSNewFunction2

public FSNewFunction2(FSCoder coder)
Construct an FSNewFunction2 object, initialising it with values decoded from an encoded object.

Parameters:
coder - an FSCoder containing the binary data.

FSNewFunction2

public FSNewFunction2(java.lang.String name,
                      java.util.ArrayList arguments,
                      java.util.ArrayList actions)
Constructs an FSNewFunction with the specified name, argument names and actions to be executed. The order of the Strings in the argument array indicate the order in which the values will be popped off the stack when the function is executed. The first argument is popped from the stack first.

Parameters:
name - the name of the function.
arguments - an array of Strings listing the names of the arguments.
actions - the array of actions that define the operation performed by the function.

FSNewFunction2

public FSNewFunction2(java.util.ArrayList arguments,
                      java.util.ArrayList actions)
Constructs an anonymous FSNewFunction with the specified argument names and actions to be executed. Use this constructor when defining functions that will be assigned to object variables and used as methods.

Parameters:
arguments - an array of Strings listing the names of the arguments.
actions - the array of actions that define the operation performed by the function.

FSNewFunction2

public FSNewFunction2(FSNewFunction2 obj)
Constructs an FSNewFunction2 object by copying values from an existing object.

Parameters:
obj - an FSNewFunction2 object.
Method Detail

add

public void add(FSRegisterVariable anArgument)
Adds the name of an argument to the array of argument names.

Parameters:
anArgument - the name of an argument passed to the FSNewFunction object.

add

public void add(FSActionObject anAction)
Adds the action object to the array of actions.

Parameters:
anAction - an object belonging to a class derived from FSActionObject.

getName

public java.lang.String getName()
Gets the name of the function.

Returns:
the name of the function.

setName

public void setName(java.lang.String aString)
Sets the name of the function. The name may be null when defining methods.

Parameters:
aString - the name of the function or null for a method.

getRegisterCount

public int getRegisterCount()
Gets the number of registers to allocate for function variables.

Returns:
the number of registers to allocate.

setRegisterCount

public void setRegisterCount(int count)
Sets the number of registers to allocate for function variables. Up to 256 registers may be allocated for each function.

Parameters:
count - the number of registers to allocate.

getOptimizations

public int getOptimizations()
Get the code containing the compound flags that control the execution environment of the function or method.

Returns:
the code for the optimizations.

setOptimizations

public void setOptimizations(int code)
Set the code containing the compound flags that control the execution environment of the function or method.

Parameters:
code - the compound code can be created by bitwise-ORing the constants that identify the optimizations performed.

getArguments

public java.util.ArrayList getArguments()
Gets the array of FSRegisterVariables that define the function arguments and whether they are assigned to internal registers or to local variables in memory.

Returns:
the array of FSRegisterVariables that define the functions arguments.

setArguments

public void setArguments(java.util.ArrayList anArray)
Sets the array of FSRegisterVariables that define the function arguments and whether they are assigned to internal registers or to local variables in memory.

Parameters:
anArray - an array of Strings listing the names of the arguments.

getActions

public java.util.ArrayList getActions()
Gets the actions executed by the function.

Returns:
the array of actions executed.

setActions

public void setActions(java.util.ArrayList anArray)
Sets the actions.

Parameters:
anArray - the array of actions that define the operation performed by the function.

clone

public java.lang.Object clone()
Description copied from class: FSTransformObject
Creates a deep copy of the entire object.

Overrides:
clone in class FSTransformObject
Returns:
a copy of the object.

equals

public boolean equals(java.lang.Object anObject)
Description copied from class: FSActionObject
Returns true if anObject is equal to this one. Objects are considered equal if they would generate identical binary data when they are encoded to a Flash file.

Overrides:
equals in class FSActionObject
Returns:
true if this object would be identical to anObject when encoded.

appendDescription

public void appendDescription(java.lang.StringBuffer buffer,
                              int depth)
Description copied from class: FSTransformObject
AppendDescription is used to present a string description of the object including all nested objects up to a specified depth. This method provide a more controlled way of creating a string representation of an object since large objects such as font or shape definitions can contain dozens of nested objects. The representation of the object is appended to the StringBuffer, showing the name of the class and values of the attributes it contains. If the object contains any attributes that are objects then the object graph will be traversed up to the specified depth. If objects are nested at a level less than specified depth then the full string representation of the object is displayed. For objects at the specified depth only the name of the class is displayed. Any objects below this depth are not displayed.

Specified by:
appendDescription in class FSTransformObject
Parameters:
buffer - a StringBuffer to which the description of each object is appended.
depth - the maximum level of nesting up to which objects will be displayed.

length

public int length(FSCoder coder)
Overrides:
length in class FSActionObject

encode

public void encode(FSCoder coder)
Overrides:
encode in class FSActionObject

decode

public void decode(FSCoder coder)
Overrides:
decode in class FSActionObject