Search code examples
flashactionscript-3alchemy

Populating a C array with from an AS3_Val


When receiving an array from flash, in C, how do I populate that array when its size isn't constant but the values types are?

In Alchemy's documentation, the example given is:

S3_ArrayValue(arr, "IntType, StrType, DoubleType", &arg0, &arg1, &arg2);

But this means that if I my array is of size 100, I need to create a string describing each of the cells types.

Isn't there a way to populate it by saying something like "fill the following 'int * array', with the first 100 value from this AS3_Val int array?"


Solution

  • The nth element of an AS3 Array can be accessed via a property who's name is equal to n.

    For example, this will trace 9001:

    var arr:Array = new Array();
    arr[47] = 9001;
    trace( arr["47"] );
    

    You can exploit this behaviour to gain random access to an AS3 Array from Alchemy:

    AS3_Val arr = AS3_Array("");
    AS3_SetS(arr, "47", AS3_Int(9001));
    AS3_Trace( AS3_GetS(arr, "47") );
    

    For random access, you'll need to convert the index to a string:

    AS3_Val AS3_ArrayGet(AS3_Val arr, int index) {
      if (index >= 0) {
        // static_assert(sizeof(int) <= 4);
        char buffer[11];
        return AS3_GetS(arr, itoa(index, buffer, 10));
      }
      return AS3_Undefined();
    }
    
    void AS3_ArraySet(AS3_Val arr, int index, AS3_Val val) {
      if (index >= 0) {
        // static_assert(sizeof(int) <= 4);
        char buffer[11];
        AS3_SetS(arr, itoa(index, buffer, 10), val);
      }
    }
    

    Something like that.