HOWTO: Transfer Array and Vector from AS3 to C with AIR Native Extensions for iOS
February 8th, 2012
Last time I wrote about casting basic types from AS3 to C like Number, String, Boolean, int and uint. Now I’d like to cover two more complex types: Array and Vector.<Type>.
Array cast (AS3->C->AS3) reverse function
In this little sample, we will implement reverse function, that basically gets array like [1,2,3] and returns [3,2,1].
AS3 part:
public function reverseArray(array:Array):Array{ var ret:Array= context.call("reverseArray",array) as Array; return ret; }
C part:
This function consist of few steps, first get an array from argument, find length, create new AS3 Array instance in C, loop through original array + fill new array and return it.
FREObject reverseArray(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ FREObject arr = argv[0]; // array uint32_t arr_len; // array length FREGetArrayLength(arr, &arr_len); FREObject populatedArray = NULL; // Create a new AS3 Array, pass 0 arguments to the constructor (and no arguments values = NULL) FRENewObject((const uint8_t*)"Array", 0, NULL, &populatedArray, nil); FRESetArrayLength(populatedArray, arr_len); NSLog(@"Going through the array: %d",arr_len); int32_t j = 0; for(int32_t i=arr_len-1; i>=0;i--){ // get an element at index FREObject element; FREGetArrayElementAt(arr, i, &element); // OPTIONAL: get an int value out of the element int32_t value; FREGetObjectAsInt32(element, &value); // log index and value NSLog(@"Get item %d: %d",i,value); NSLog(@"Set item %d: %d",j,value); FRESetArrayElementAt(populatedArray, j, element); j++; } return populatedArray; }
Vector.<Type> (AS3->C->AS3) reverse function
Same thing applies for Vector.<Type>, the only difference is that the creation of the Vector.<Type> will look like this:
FRENewObject((const uint8_t*)"Vector.<int>", 0, NULL, &populatedArray, nil);
The implementation will look like this:
public function reverseVector(vector:Vector.<int>):Vector.<int>{
var ret:Vector.<int>= context.call("reverseVector",vector) as Vector.<int>;
return ret;
}FREObject reverseVector(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]){ FREObject arr = argv[0]; // array uint32_t arr_len; // array length FREGetArrayLength(arr, &arr_len); FREObject populatedVector = NULL; // Create a new AS3 Array, pass 0 arguments to the constructor (and no arguments values = NULL) FRENewObject((const uint8_t*)"Vector.<int>", 0, NULL, &populatedVector, nil); FRESetArrayLength(populatedVector, arr_len); NSLog(@"Going through the vector: %d",arr_len); int32_t j = 0; for(int32_t i=arr_len-1; i>=0;i--){ // get an element at index FREObject element; FREGetArrayElementAt(arr, i, &element); // OPTIONAL: get an int value out of the element int32_t value; FREGetObjectAsInt32(element, &value); // log index and value NSLog(@"Get item %d: %d",i,value); NSLog(@"Set item %d: %d",j,value); FRESetArrayElementAt(populatedVector, j, element); j++; } return populatedVector; }
Complete C implementation: IOSExtension.m
Complete AS3 implementation: IOSExtension.as
Facebook comments:
4 Comments »
RSS feed for comments on this post. / TrackBack URL

Great post! Any chance you can cover converting UIImage to bitmapdata. I am trying to create a native extension for selecting multiple images from the cameraroll, but I have troubles with getting the image data back to as3. Do I need to hand over the (empty) bitmapdata to C and then release it back again?
Comment by Till — February 10, 2012 @ 5:07 pm
Hi Till, yup, I will prepare that one.
Comment by tom — February 20, 2012 @ 10:56 am
Hi, good tutorial for a newbie like me to this world.
I will appreciate your efforts on BitmapData.
I’ll have to convert from H264 or a proprietary format to RGB. For that I was considering native extensions.
Comment by Marco — April 5, 2012 @ 12:37 pm
Thank you for the examples. I have one question: suppose the C part of the ANE allocates some memory (say, for a new ByteArray): who is then responsible for releasing it? Does it become the ownership of the AS, and its garbage collector, or should the C part take care of it later?
Comment by Keith — May 9, 2012 @ 5:22 pm