Chapter 4. Reusing your existing C++ codebase

 

This chapter covers

  • Adjusting a C++ codebase so that it can also be compiled by Emscripten
  • Exporting WebAssembly functions so that they can be called by JavaScript
  • Calling a WebAssembly function using Emscripten helper functions
  • Passing strings and arrays to the WebAssembly module via the module’s memory

Typically, when people talk about the advantages of WebAssembly, it’s from the standpoint of performance. But WebAssembly brings another advantage to the table—code reuse. Rather than writing the same logic multiple times for each target environment (desktop, website, and others), WebAssembly lets you reuse the same code in multiple locations.

Imagine a scenario in which a company already has a desktop point-of-sale application written in C++ but wants to add an online solution. The company decides that the first part of the website it should build is the Edit Product web page shown in figure 4.1. The new website will also use Node.js for the server-side logic, but I’ll leave the discussion of working with Node.js for a later chapter.

Figure 4.1. The Edit Product page that you’ll be building

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

4.2. Using C or C++ to create a module without Emscripten

Real-world use cases

Exercises

Summary

sitemap