Mark As Completed Discussion

In the previous sections, we've learned to compile and call WASM functions from JavaScript. Inevitably, there will come a time during your WebAssembly journey when you'll encounter bugs and issues within your WASM modules. Think of it as that moment during an intense basketball game when the coach needs to track metrics and analyze the player's performance to adjust the game plan.

To facilitate this, modern browsers have implemented features within their developer tools to allow debugging of WebAssembly code much in the same way you'd debug JavaScript. For instance, you can see functions, variables, and even step though the WASM code in the debugger.

However, to get a readable version of WASM code, it's important to have a source map. This is much like having a match playbook in a basketball game. Without it, WASM code is displayed in its low-level format, which can be harder to navigate than the original C++ code.

Remember, debugging requires two key elements: the debug information and source map; both of which should have been created during the build process of your project. For existing projects, you might need to adjust the build settings to produce these.

Here's an example of how this works:

By switching to the Sources panel in the Chrome developer tools and navigating to our loaded WASM module, we can then set breakpoints and step through our code. The debugger will halt on our breakpoint and we can inspect variables and the call stack. Note that how modules appear can depend on your project structure.

Debugging WebAssembly becomes like any other debugging session in JavaScript, though there are some limitations due to the difference between JavaScript and WASM, such as differences in handling of types and memory.

In conclusion, debugging WASM is a pivotal skill any WASM developer needs. Now, with the tools modern browsers provide, this task is straightforward and integrated into your current development workflow.

CPP
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment