Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap or call a C function with void as return in Emscripten?

Tags:

emscripten

In the Emscripten wiki (Interacting with code), there are only two types listed as possible return types to be passed to ccall or cwrap ("number" and "string"). How can I wrap / call a function which doesn't return anything (void return type)?

like image 956
jplatte Avatar asked Mar 19 '23 05:03

jplatte


2 Answers

"null" works as a return type for void functions instead of a string.

For example:

    my_fun = Module.cwrap('my_fun', null, ['number', 'number']);
like image 105
Charles Ofria Avatar answered May 12 '23 10:05

Charles Ofria


Examples on the site also use number, as it will just be ignored. This is what the example in the wiki uses.

I use this in all of my emscripted code, and it works great. In general, looking in the examples folder of the emscripten repo is your best bet for syntax. Also, this is the documented use, so it may be safer to hedge against future changes.


For example, a C function

void test(char* buffer, int buffersize) {
  // ...
}

could be wrapped as

var test = Module.cwrap('test', 'number', ['number', 'number']);
like image 29
zzmp Avatar answered May 12 '23 12:05

zzmp