Chapter 6. Creating a WebAssembly module that talks to JavaScript using function pointers
This chapter covers
- Adjusting C or C++ code to work with function pointers
- Using Emscripten’s helper functions to pass JavaScript functions to the WebAssembly module
- Calling function pointers in the WebAssembly module when not using Emscripten’s plumbing code
In chapter 5, you modified your module so that it was no longer passing a validation error message back to the JavaScript through a parameter. Instead, you modified the module so that it called a JavaScript function directly, as figure 6.1 illustrates.
Imagine being able to pass a JavaScript function to the module based on your JavaScript code’s needs at the time. When the module finishes processing, it can then call the function that was specified, as figure 6.2 shows.
In this section, you’re going to build the C++ code for the validation logic. You’ll include the standard C library and Emscripten helper functions, which is the recommended way to build a module for use in production. Later in this chapter, you’ll learn the other approach to building a WebAssembly module, which doesn’t include the standard C library or Emscripten helper functions.