We are trying to pass a raw data from C++ to Haxe, but we had a lot of trebles.
We tried this solution Passing bytes from c++ to haxe but was not working for us.
C++
void Clazz::func(unsigned char *data, int length)
{
XXX_ENTER_HAXE();
val_call1(m_callback->get(), alloc_array(length));
XXX_EXIT_HAXE();
}
Haxe
function func(result:ByteArray) : Void {
//Do the callback
}
Thanks in advance for any help;
Here’s a couple other approaches
If you know the size in advance, allocate it on the Haxe side, first:
var bytes = Bytes.alloc (length);
make_cffi_call (bytes);
then get the pointer to the data on the C++ side
unsigned char* ptr = (unsigned char*)buffer_data (b);
if (!ptr) ptr = (unsigned char*)val_string (b);
// use ptr to set data
Another approach is to use a Haxe callback to create the array
function allocateBytes (length:Int):Bytes {
return Bytes.alloc (length);
}
value bytes = val_call1 (m_callback->get(), alloc_int (length));
unsigned char* ptr = (unsigned char*)buffer_data (bytes);
if (!ptr) ptr = (unsigned char*)val_string (bytes);
// use ptr to set data
Hi. Thanks for the quick response.
I’m working on this with hocine also.
Your samples look like they might work for us, but I was wondering, in your example, I don’t see where we plug our bytes into the result.
void CPPClass::sendBytesToHaxe(unsigned char *myBytes, int length)
{
value haxeBytes = val_call1 (m_callback->get(), alloc_int (length));
unsigned char* ptr = (unsigned char*)buffer_data (haxeBytes);
if (!ptr) ptr = (unsigned char*)val_string (haxeBytes);
//Here we want to return myBytes to haxe but
//how do we transfer myBytes into haxeBytes?
val_call1(m_myCallback->get(), haxeBytes);
}
If you have Haxe bytes that are allocated with at least the correct size, and get a pointer to where it’s data is stored, you should be able to do something like:
memcpy (ptr, myBytes, length);
…you may also be able to use the Haxe bytes pointer and write to it directly, rather than performing a copy