1

i have a variable declared in alchemy asm:

asm("var buffer:Vector.<Number> = new Vector.<Number>(100, true);");

i can populate it with data like so:

asm("buffer[%0] = %1;" : : "r"(index) : "r"(value));

what i can't figure out is how to get a reference of that asm "buffer" variable into actionscript.

(i did think of one way... what i did was to throw the "buffer" from alchemy asm, and then catch it in actionscript, but unfortunately it seems to leak a lot of memory).

is there a better alternative to doing this?

please note that performance is critical, and using default alchemy marshaling is way too slow.

paleozogt
  • 6,393
  • 11
  • 51
  • 94
kyunghoon
  • 41
  • 4

1 Answers1

2

asm is only for passing numbers back and forth, which means we'll have to use Alchemy's internal int-to-object mappings. Digging through the intermediate AS3 code (to see it, set the ACHACKS_TMPS environment variable to '1'), it seems that CTypemap.AS3ValType does the mapping. So you can return an asm-created object like this:

static AS3_Val alc_return_obj(void *self, AS3_Val args) {

    int len= 100;

    // create custom data in AS3
    asm("var as3Buffer:Vector.<Number> = new Vector.<Number>(%0, true);" : : "r"(len));

    // populate the vector with multiples of pi (just for fun)
    for (int idx= 0; idx < len; idx++) {
        double value= 3.14159265 * idx;
        asm("as3Buffer[%0] = %1;" : : "r"(idx) , "r"(value));
    }

    // get a C reference to the AS3 object
    AS3_Val alcBuffer;
    asm("%0 CTypemap.AS3ValType.createC(as3Buffer)[0];" : "=r"(alcBuffer));

    return alcBuffer;    
}

Note: While this is fun hackery, it might not be the best way to solve this problem. This is probably not the fastest way to get data out of Alchemy and into Flash. For that, I suggest using a ByteArray to copy data into and out of Alchemy's RAM. See this SO question for some techniques in that area.

Community
  • 1
  • 1
paleozogt
  • 6,393
  • 11
  • 51
  • 94