Skip to content

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

Open
kripken wants to merge 2 commits intoWebAssembly:mainfrom
kripken:bjs.fatal.handle
Open

[JS API] Throw useful exceptions on parse errors#8264
kripken wants to merge 2 commits intoWebAssembly:mainfrom
kripken:bjs.fatal.handle

Conversation

@kripken
Copy link
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
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
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
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.

@@ -3273,7 +3294,7 @@ Module['readBinary'] = function(data) {
Module['parseText'] = function(text) {
Copy link
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
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?

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