Now that we have a Wasm file, we'll need some JavaScript code to compile and execute it. There's a few steps we'll have to follow to ensure the code can be successfully utilized in the browser. In this section, we will write some common JavaScript loading code that we can reuse for other examples, create an HTML file that demonstrates the use of the Wasm module, and test the results in the browser.
We will fetch and instantiate a .wasm
file in several of the examples, so it makes sense to move the JavaScript loading code to a common file. The actual fetch and instantiation code is only a few lines, but having to repeatedly redefine the importObj
object that Emscripten expects is a waste of time. We'll make this code available in a commonly accessible file to expedite the code-writing process. Create a new folder named /common
in the /book-examples
folder and add a file named load-wasm.js
with the following contents:
/** ...