...and 1 minute later I found the problem by mulling over the ccall source 
in preamble.js :D

The problem is that I'm passing a raw ArrayBuffer object, when ccall 
expects an Uint8Array. ArrayBuffer has no .length property, which is 
expected by ccall.

So the JS side needs to look something like this:

var content = new Uint8Array(loadEvent.target.result);
Module.ccall('emsc_pass_data',
    null,
    ['string', 'array', 'number'],
    [file.name, content, content.length]);

...some more examples in the docs and probably a test for ccall with 
'array' would be nice though ;)

Cheers,
-Floh.

Am Mittwoch, 14. Dezember 2016 17:01:45 UTC+1 schrieb Floh:
>
> Hi,
>
> I can't seem to find example code of how to pass an ArrayBuffer from JS to 
> C via ccall() (for instance, there doesn't seem to be a test for this in 
> the SDK?), and I have problems doing this (the passed data is broken). I'm 
> sure it's something simple...
>
> Background: I want to pass file content data from a JS drag-n-drop event 
> handler to the C side.
>
> What I have on the JS side:
>  
>
...
> // this is the array buffer as result of a FileReader, this contains the
> // expected data (I checked through logging the first couple of bytes to 
> the console)
> var content = loadEvent.target.result;      
>
> // now the ccall to a C function "emsc_pass_data" with 3 params, a string 
> name, the content, and the length
> Module.ccall('emsc_pass_data',
>     null,
>     ['string', 'array', 'number'],
>     [file.name, content, content.byteLength]);
> ...
>
> ...and on the C side I have:
>
> void emsc_pass_data(const char* name, const uint8_t* data, int size) {
>
> }
>
> The function is called, name has a valid string, size is also right, data 
> is some pointer, but the pointed-to data behind the pointers doesn't match 
> the ArrayBuffer content (it looks fairly random with lots of zeros).
>
> What am I doing wrong? I would most appreciate some code example to look 
> at :) 
>
> PS: the emscripten documentation on ccall is a bit hand-wavy on the array 
> type, a small code example there would be very helpful (both the JS and C 
> side), see here: 
> https://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#calling-compiled-c-functions-from-javascript
>
> Cheers,
> -Floh.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to