b-ccall-cwrap-and-direct-function-calls

B ccall, cwrap, and direct function calls

 

This appendix covers

  • Calling a module’s function from JavaScript using the Emscripten helper functions ccall and cwrap
  • Calling a module’s function directly from JavaScript without using the Emscripten helper functions
  • Passing arrays to a function

When working with Emscripten’s generated JavaScript plumbing code you have a few options for calling into a module. The most common approach is by using the ccall or cwrap functions, which help with memory management when passing and returning strings for example. You can also call a module function directly.

B.1   ccall

The ccall function allows you to call a function in the WebAssembly module and receive the results. The ccall function accepts four parameters:

B.1.1   Building a simple WebAssembly module

B.1.2   Building the webpage that will talk to the WebAssembly module

B.2   cwrap

B.2.1   Adjusting the JavaScript code to use cwrap

B.3   Direct function calls

B.4   Passing an array to a module