Calling native functions from ActionScript

Opt In Image
Early bird offer on the ANE eBooks

 

Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.

 

 

  • step-by-step guide to making your iOS extension in under an hour
  • library for data conversion between ActionScript and native code
  • tutorials
  • infographics
  • code included

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

Calling 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:

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 

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:

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:

where:

  • 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.

What else?

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.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/easyna6/public_html/easynativeextensions_wp/wp-includes/class-wp-comment-query.php on line 399

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">