Module.cwrap()

Module.cwrap() is similar to ccall() in that it calls a compiled C function. However, rather than returning a value, it returns a JavaScript function that can be reused as many times as needed. The function signature for Module.cwrap() is as follows:

cwrap(ident, returnType, argTypes)

Just as with ccall(), you can specify string values that represent types for the returnType and argTypes parameters. You cannot use the "array" type in argTypes because there is no way to know the length of the array when the function is called. For a function that doesn't return a value, use null (with no quotation marks) for the returnType parameter.

The following code, taken from the Emscripten site, demonstrates the use of cwrap() to create a reusable function:

// Call C from JavaScript
var c_javascript_add = Module.cwrap(
'c_add', // name of C function
'number', // return type
['number', 'number'] // argument types
);

// Call c_javascript_add normally
console.log(c_javascript_add(10, 20)); // 30
console.log(c_javascript_add(20, 30)); // 50
..................Content has been hidden....................

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