Personal tools

Views

Alchemy:Documentation:Developing with Alchemy:AS3 API

From Adobe Labs

Table of contents

Manipulating the Alchemy state machine

CLibInit

This is the class that exposes functionality from your C code. It has a few useful functions.

init():*: This is the function you call to initialize the C code. It has no parameters. It returns an Object which should contain your exported methods.

setSprite(sprite:Sprite):void: This method takes a sprite to use as the "command line" for Alchemy. It will create a new TextField on this Sprite and channel output from STDOUT, STDERR to it. It will also accept input and route to STDIN. This is not required to be set, but it will be by default.

putEnv(key:String, value:String: This method allows you to set an environment variable that can be retrieved by your C code using getenv().

supplyFile(path:String, data:ByteArray): This method allows you to provide a file at a specified path for the C code. This is useful if your C code expects a configuration file at a specific location that is not accessible to the runtime due to security restrictions.

The Alchemy state machine is stored in a global variable gstate

GLEByteArrayProvider

This class allows you to get the ByteArray used as RAM by your C library directly.

get: Returns the current domainMemory or allocates a new one as "little endian" if not present.

MemUser

This class allows you to manipulate the RAM used by your C library directly. In all the functions below addr is the offset into memory (which corresponds to a pointer passed back from your C code).

_mr32(addr:int):int: Get a 32 bit value at the location addr and return as an int.

_mru16(addr:int):int: Get a 16 bit unsigned value at the location addr and return as an int.

_mrs16(addr:int):int: Get a 16 bit signed value at the location addr and return as an int.

_mru8(addr:int):int: Get a 8 bit value at the location addr and return as an int.

_mrs8(addr:int):int: Get a 8 bit value at the location addr and return as an int.

_mrf(addr:int):Number: Get a float value at the location addr and return as an Number.

_mrd(addr:int):Number: Get a double value at the location addr and return as an Number.

_mw32(addr:int, val:int):void: Write an int as a 32 bit value at the location addr.

_mw16(addr:int, val:int):void: Write an int as a 16 bit value at the location addr.

_mw8(addr:int, val:int):void: Write an int as a 8 bit value at the location addr.

_mwf(addr:int, val:Number):void: Write a Number as a float at the location addr.

_mwd(addr:int, val:Number):void: Write a Number as a double at the location addr.

Calling Functions Exported from C

To use functions exported from C, you must do the following:

  1. Make sure the SWC containing your library is linked to your FlexBuilder project
  2. Add an import line referring to your C libraries namespace and its exported CLibInit class. The namespace will be cmodule.<your_lib_name>
  3. Create an instance of CLibInit
  4. Call init() on the newly created CLibInit and store the returned reference as an Object
  5. Now call your exported functions using the returned reference!

Here is an example of an ActionScript class calling a C exported function:

import cmodule.example.CLibInit;
const libInitializer:CLibInit = new CLibInit();
const lib:Object = libInitializer.init();
lib.myExportedFunc("param1", 1234);

You may see additional exported symbols in your SWC. You should ignore them and use the CLibInit to initialize your library. Calling methods any way other than what is mentioned above is unsupported.

Retrieved from "http://labs.adobe.com/wiki/index.php/Alchemy:Documentation:Developing_with_Alchemy:AS3_API"