Thursday 23 February 2012

HOWTO: Transfer Array and Vector from AS3 to C with AIR Native

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;   

No comments:

Post a Comment