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.

Figure 6.1. The module calling a function in the JavaScript code

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.

Figure 6.2. The module calling a JavaScript function pointer

6.1. Using C or C++ to create a module with Emscripten plumbing

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.

6.1.1. Using a function pointer given to the module by JavaScript

6.2. Using C or C++ to create a module without Emscripten plumbing

Real-world use cases

Exercises

Summary

sitemap