1

Assume that I am new to AS3 and FlasCC and try to port some legacy C++ code to Flash. What I have is some number-crunching routine that takes an array of type double (the array is of fixed size). The C++ prototype is, say:

double doNumberCrunching(double input[512]);

Now I would like to build an SWC and later call doNumberCrunching() from AS3. SWIG generates the AS3 interface as:

doNumberCrunching(var _input:*):Number;

However, I have no idea how to feed it with Vector.<Number>(512) without working on raw bytes.

My question is: how to solve it neatly? Do you know a cheap way to access double* underlying the Vector.<Number>? Or something even better?

1 Answers1

0

If only your number crunching function was operating on ints! Then you would be able to use CModule.writeIntVector to write vector contents to DomainMemory and just pass over a pointer to the C++ code.

But since it's doubles, you will have to iterate through Vector and convert each element from AS3 Number to C++ double using CModule.writeDouble. Then you can manually expose your function to AS3 with an interface that accepts a DomainMemory pointer:

void doNumberCrunchingAS() __attribute__((used,
  annotate("as3sig:public function doNumberCrunching(inputPtr:int):Number"),
  annotate("as3package:mypackage"))
));

double doNumberCrunching( double* input )
{
  // Actual implementation here
  return 0;
}

void doNumberCrunchingAS()
{
  // Read the AS3 argument and convert it to a convenient C++ form.
  int memoryOffset = 0;
  AS3_GetScalarFromVar(memoryOffset, inputPtr);
  double* inputPtr = reinterpret_cast<double*>( memoryOffset );

  // Call an implementation routine.
  double result = doNumberCrunching( inputPtr );

  // Return result to AS3 code.
  AS3_Return( result );
}

AS3 code:

package
{
  using mypackage.doNumberCrunching;

  function testNumberCrunching():void
  {
    var ptr:int = CModule.malloc(blobSize);

    // a loop of writeDouble should be here

    doNumberCrunching(ptr);

    CModule.free(ptr);
  }
}
ilookha
  • 106
  • 4