如何使用FlasCC将ByteArray传递给C代码

时间:2013-01-14 21:17:48

标签: actionscript flascc

我想将ActionScript中的ByteArray传递给C函数。

基本上我想做这样的事情:

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) 
}

不幸的是,我找不到flascc文档中的任何功能来帮助我。

3 个答案:

答案 0 :(得分:4)

示例:

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);
}

这里的关键是C中的堆在AS3端公开为CModule.ram,这是一个ByteArray对象。

C中的指针malloc'd在AS3中被视为CModule.ram的偏移量。

答案 1 :(得分:2)

您应该使用CModule.malloc和CModule.writeBytes方法以C风格的方式使用指针进行操作。看看$ FLASCC / samples / 06_SWIG / PassingData / PassData.as

答案 2 :(得分:-1)

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);
}