Skip to content

[JS API] Throw useful exceptions on parse errors#8264

Merged
kripken merged 2 commits into
WebAssembly:mainfrom
kripken:bjs.fatal.handle
Feb 9, 2026
Merged

[JS API] Throw useful exceptions on parse errors#8264
kripken merged 2 commits into
WebAssembly:mainfrom
kripken:bjs.fatal.handle

Conversation

@kripken
Copy link
Copy Markdown
Member

@kripken kripken commented Feb 3, 2026

wasm-opt will just fatally error on invalid module inputs, but binaryen.js
is a library and users want to get something they can handle, and see
the actual error. This PR throws a C++ exception instead, and converts
it on the JS side.

Fixes #8256

Copy link
Copy Markdown
Contributor

@spotandjake spotandjake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me.

@@ -3264,7 +3285,7 @@ Object.defineProperty(Module, 'readBinary', { writable: true });
Module['readBinary'] = function(data) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nit but with #8122 I started trying to add some documentation with jsdoc how would we feel about continuing that pattern when possible?

Suggested change
Module['readBinary'] = function(data) {
/**
* Creates a module from binary data.
*
* @param {Uint8Array} data - A Uint8Array containing a valid WebAssembly binary.
*
* @return {Module} The constructed wasm module.
*
* @throws {Error} If the input is invalid or parsing fails.
*/
Module['readBinary'] = function(data) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to autogenerate some doc? The comment here seems kind of obvious.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume @spotandjake you are thinking of autogenerating a docs page from the jsdocs comments?

That might be useful, though I'm not sure how much.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave the topic of adding jsdocs for separate discussion. I'll land this for now.

@@ -3273,7 +3294,7 @@ Module['readBinary'] = function(data) {
Module['parseText'] = function(text) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above:

Suggested change
Module['parseText'] = function(text) {
/**
* Creates a module from Binaryen's s-expression text format (not official stack-style text format).
*
* @param {string} text - A string containing a WebAssembly text module (Binaryen-style WAT).
*
* @return {Module} The constructed wasm module.
*
* @throws {Error} If the input is invalid or parsing fails.
*/
Module['parseText'] = function(text) {

@aheejin
Copy link
Copy Markdown
Member

aheejin commented Feb 6, 2026

Emscripten supports printing exception messages when run with -sEXCEPTION_STACK_TRACES. Can we use that without half-reimplementing this? Or do we not want the stack traces but only want the message?

@kripken
Copy link
Copy Markdown
Member Author

kripken commented Feb 6, 2026

Or do we not want the stack traces but only want the message?

Yes, this is not focused on the stack trace in the exception. This is about being able to catch an exception in JS, and to get the message. Before this PR, a parse error just shut down the entire application, with no useful message for the code calling the binaryen.js library.

@kripken kripken merged commit 2f3cba0 into WebAssembly:main Feb 9, 2026
17 checks passed
@kripken kripken deleted the bjs.fatal.handle branch February 9, 2026 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parseText writes uncatchable usefull log to STDOUT or browser console.

3 participants