So you have started writing an AIR Native Extension (ANE). You’ve got a pretty good idea of how AIR loads and unloads it from memory and know that you need an Extension Context to tell AIR about what functionality your native code exposes. The infographic in this article will show you the mechanism of function calls from ActionScript to native code works.
How ActionScript calls native functions
1. The app calls one of the AIR library’s APIs, let us name it CallNativeFunctionA and say that it takes two arguments:
nativeExtInstance.CallNativeFunctionA( argument1, argument2 );
where nativeExtInstance is an instance of YourNativeExtension – a class that serves as the interface to your ANE.
2. Inside CallNativeFunctionA() the AIR library makes a call to
myContext.call( “as_nativeFunctionA”, argument1, argument2 );
where as_nativeFunctionA is the name of the function which the extension context has mapped to the function pointer of NativeFunctionA(). If you are a die-hard C or C++ coder like me and like utilizing the compiler as much as possible, you are likely to cringe at this way of making calls. Ce’st la vie.
3. On the native side this call is translated to:
NativeFunctionA( myContext, functionData, argumentCount, arrayOfArguments );
in the case of C/Objective-C, where:
• myContext is FREContext, a pointer to the extension context you created in your AIR library. Really, FREContext is just void*.
• functionData is a pointer to data, which you set (or did not set) in your context initializer to be passed to the function every time it is called;
• argumentCount and arrayOfArguments are how argument1 and argument2 get passed to native code: pointers to them are put in an array and the array and its size are passed to NativeFunctionA. For the pedantically inclined of you the array actually contains FREObject instances, which are again nothing more than void*.
In Java the same thing looks like this:
NativeFunctionA.call( extensionContext, args )
- NativeFunctionA is an instance of FREFunction;
- extensionContext is an instance of FREContext, your extension context;
- args is a FREObject array of arguments that your function may need to handle.
Both the C and the Java calls return a FREObject as a result, which is one of the ways for native code to talk to ActionScript. We’ll see how different data types can be wrapped as FREObject in a future post.
We’ll see how different data types are passed between ActionScript and C or Java.
I have also prepared a recipe for you: Recipe for packaging an ANE.
And, if you find this too much work (I do!), I’ll show you how to automate it: Automatic ANE packaging.
Over to you
At what stage is your ANE at the moment? What’s stopping you from finishing it? Let us know in the comments below.