How to pass ByteArray to C code using FlasCC

2.2k views Asked by At

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.

3

There are 3 answers

0
mziwisky On BEST ANSWER

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.

0
bme On

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

1
Jack On
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);
}