Tuesday, October 6, 2009

AS3 Getter/Setter support in AbcExplorationLib

Recently I added initial support for reading and writing AS3/Avm2 getters and setters to AbcExplorationLib.

Given the following ActionScript class:


class Complex {
public var radius:Number;
public var angle:Number;
public function Complex(ar:Number,aa:Number):void {
radius = ar;
angle = aa;
}

public function set imaginary(newImaginary:Number):void
{
var oldReal = this.real;
angle = Math.atan(newImaginary/oldReal);
radius = Math.sqrt(oldReal*oldReal + newImaginary*newImaginary);
}
public function get imaginary():Number
{
return radius*Math.sin(angle);
}

public function set real(newReal:Number):void
{
var oldImaginary = this.real;
angle = Math.atan(oldImaginary/newReal);
radius = Math.sqrt(newReal*newReal + oldImaginary*oldImaginary);
}
public function get real():Number
{
return radius*Math.cos(angle);
}
}


We compile it using the Flex SDK:
java -jar c:\flexsdk\lib\asc.jar complexclasstest.as


Then we can load it using the library:


Microsoft F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.6.16, compiling for .NET Framework Version v2.0.50727

Please send bug reports to fsbugs@microsoft.com
For help type #help;;

> #r "abcexplorationlib.dll";;

--> Referenced 'abcexplorationlib.dll'

> open System.IO;;
> open Langexplr.Abc;;
> let f = using (new FileStream("complexclasstest.abc",FileMode.Open)) (fun s -> AvmAbcFile.Create(s));;

val f : AvmAbcFile

> let complexClass = List.hd f.Classes;;

val complexClass : AvmClass


> complexClass.Properties |> List.map (fun p -> p.Name);;
val it : QualifiedName list =
[CQualifiedName (Ns ("",PackageNamespace),"real");
CQualifiedName (Ns ("",PackageNamespace),"imaginary")]
> let realGetter = complexClass.Properties |> List.map (fun p -> p.Getter.Value) |> List.hd;;

val realGetter : AvmMemberMethod:
> realGetter.Method.Body.Value.Instructions |> Array.map (fun x -> x.Name);;
val it : string array =
[|"getlocal_0"; "pushscope"; "getlocal_0"; "getproperty";
"findpropertystrict"; "getproperty"; "getlocal_0"; "getproperty";
"callprop"; "multiply"; "returnvalue"|]





The library can be found here.