Question

I want to pass ByteArray from ActionScript to C function.

basically I want do something like this:

void init() __attribute__((used,annotate("as3sig:public function init(byteData: ByteArray):int"),
        annotate("as3package:example")));

void init()
{
   //here I want to pass byteArray data to C variable.
   //similar to AS3_GetScalarFromVar(cVar, asVar) 
}

Unforunately I cannot find any function in flascc docs to help me with this.

Was it helpful?

Solution

Example:

void _init_c(void) __attribute((used,
    annotate("as3sig:public function init(byteData:ByteArray) : void"),
    annotate("as3import:flash.utils.ByteArray")));

void _init_c()
{
    char *byteArray_c;
    unsigned int len;

    inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
    byteArray_c = (char *)malloc(len);

    inline_as3("CModule.ram.position = %0;" : : "r"(byteArray_c));
    inline_as3("byteData.readBytes(CModule.ram);");

    // Now byteArray_c points to a copy of the data from byteData.
    // Note that byteData.position has changed to the end of the stream.

    // ... do stuff ...

    free(byteArray_c);
}

The key here is that the heap in C is exposed on the AS3 side as CModule.ram, which is a ByteArray object.

A pointer malloc'd in C is seen in AS3 as an offset into CModule.ram.

OTHER TIPS

You should use CModule.malloc and CModule.writeBytes methods to manipulate with pointers in C-style manner. Take a look on $FLASCC/samples/06_SWIG/PassingData/PassData.as

void _init_c(void) __attribute((used,
    annotate("as3sig:public function init(byteData:ByteArray) : void"),
    annotate("as3import:flash.utils.ByteArray")));

void _init_c()
{
    char *byteArray_c;
    unsigned int len;

    inline_as3("%0 = byteData.bytesAvailable;" : "=r"(len));
    byteArray_c = (char *) malloc(len);

    inline_as3("byteData.readBytes(CModule.ram, %0, %1);" : : "r"(byteArray_c), "r"(len));

    // Now byteArray_c points to a copy of the data from byteData.
    // Note that byteData.position has changed to the end of the stream.

    // ... do stuff ...

    free(byteArray_c);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top