Skip to content

Debugging

Volker Berlin edited this page Apr 10, 2022 · 4 revisions

Debugging in the Browser

If you have all compiled then you can run it in the browser. There is also a debugger support for WebAssembly code in the browser. If you build the WASM file with debugNames = true then a source map is created. If the source map file and the Java source files are accessible from the browser then you can debug through the Java code.

However, debugger support in the browser is very simple. You need first to build all, no hot code replacement and expression evaluation.

Debugging the WASM code

Debugging the WASM code can be helpful if you are looking for a JWebAssembly compiler error. For example, if the code does not work as in Java. Or you want to see the generated native WebAssembly code.

Nowadays, if you have a source map file and your Java source files are not available, the browser displays a blank page. To see the text form of the WASM code, you can remove the source map file. The flag [debugNames = true] (Build-with-Gradle#wasm-task) is still useful to show method and parameter names.

Debugging in the Emulator

Since version 0.3 of the JWebAssembly-API there is an emulator with browser support. If you want run your web application in your Java IDE then you need to add the follow command line parameter to your Java launcher:

-javaagent:<path>/jwebassembly-api.jar

Replace <path> with a real path. This can also be the path to the Gradle cache. Then create a launcher class in your test source folder with the follow code:

import de.inetsoftware.jwebassembly.emulator.JWebAssemblyEmulator;

public static void main( String[] args ) {
    JWebAssemblyEmulator.launchResource( "yourHtmlPage.html", () -> YourMainClass.yourMainMethod() );
}

This will run a browser via JavaFX. Your code will run as pure Java Code in your IDE with all debug features of your Java IDE. There is no WebAssembly invoked. With the emulator you can find logical errors in your code and the jwebassembly-api code. You can't find problems that are related to WebAssembly or the JWebAssembly self.

If you use a Java version higher as Java 8 then you need to add JavaFX libraries for your platform. Suggestion for an improvement are welcome.

Emulator without stub

If your program does not need a special start page and can run with an empty HTML page, then you can start it without a stub class with:

java -javaagent:<path>/jwebassembly-api.jar de.inetsoftware.jwebassembly.emulator.JWebAssemblyEmulator <classname> <methodname>