Module.ccall()

Module.ccall() calls a compiled C function from JavaScript and returns the result of that function. The function signature for Module.ccall() is as follows:

ccall(ident, returnType, argTypes, args, opts)

You must specify a type name for the returnType and argTypes parameters. The possible types are "number", "string", "array", and "boolean", which correspond to the appropriate JavaScript types. You cannot specify "array" for the returnType parameter because there is no way to know the length of the array. If the function doesn't return anything, you can specify null for the returnType (note the absence of quotation marks).

The opts parameter is an optional options object that can contain a Boolean property named async. Specifying a value of true for this property implies that the call will perform an async operation. We won't use this parameter for any of our examples, but if you wish to learn more about it, the documentation is available at http://kripken.github.io/emscripten-site/docs/api_reference/preamble.js.html#calling-compiled-c-functions-from-javascript.

Let's look at an example of ccall(). The following code, taken from the Emscripten site, demonstrates how to call a function named c_add() from the compiled output of a C file:

// Call C from JavaScript
var result = Module.ccall(
'c_add', // name of C function
'number', // return type
['number', 'number'], // argument types
[10, 20] // arguments
);

// result is 30
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset