actionscriptflascc

How to pass ByteArray to C code using FlasCC


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.


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.