Skip to content

fix(wasm): enable C++ exception catching in the Emscripten build - #136

Closed
charlieh0tel wants to merge 2 commits into
tmolteno:masterfrom
charlieh0tel:fix-wasm-exceptions
Closed

charlieh0tel wants to merge 2 commits into
tmolteno:masterfrom
charlieh0tel:fix-wasm-exceptions

Conversation

@charlieh0tel

Copy link
Copy Markdown
Contributor

Summary

Emscripten disables C++ exception catching by default: a throw aborts the module and catch blocks are dropped. The NECPP_BUILD_WASM target didn't pass -fexceptions, so this adds it to both the compile and link options of nec2pp_wasm. It has to be a compile flag too; the link flag alone can't restore a catch that was already dropped from the objects.

Two places depend on it:

  • nec_context::simulate uses throw 1 / catch (int) to end a finished run and return to card input (src/nec_context.cpp).
  • nec_process_input in src/nec_wasm.cpp catches nec_exception so it can return -2 with the error message.

nec_process_input is still a stub that parses geometry from stdin and never calls simulate(), so today only the error path is reachable from the WASM API.

Also adds tests/wasm_smoke.js, which runs in the CI wasm job. Until now that job only checked that nec2pp.js and nec2pp.wasm were produced, and the build succeeds with or without exception support. The test loads the module under Node, gives nec_process_input empty stdin so the geometry parser throws, and requires rc -2 with the error message in the output.

Test plan

  • node tests/wasm_smoke.js nec2pp.js passes against a build of this branch (emscripten/emsdk:4.0.7, via scripts/build_wasm_docker.sh)
  • The same test fails against a build without the flag: the exception escapes into JS as a raw pointer
  • CI wasm job passes

🤖 Generated with Claude Code

https://claude.ai/code/session_01Uri4eguGitqE1abazuTstd

charlieh0tel and others added 2 commits October 1, 2026 19:57
Emscripten disables exception catching by default, so throws abort and
catch blocks are elided. nec_context uses `throw 1` to unwind a finished
solve, and nec_process_input relies on catching nec_exception to report
errors. Pass -fexceptions at both compile and link time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The CI wasm job only checked that nec2pp.js/nec2pp.wasm were produced.
Load the module under Node, feed nec_process_input a deck that makes the
geometry parser throw, and require rc -2 with the error in the output.
Without exception catching the throw escapes into JS and the test fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
charlieh0tel added a commit to charlieh0tel/nec2-js that referenced this pull request Oct 2, 2026
tmolteno/necpp#129 was closed as superseded by d0f3259 upstream. The
-fexceptions fix for NECPP_BUILD_WASM went up as tmolteno/necpp#136 and
was withdrawn: that target is a stub, and build.sh already passes the flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Uri4eguGitqE1abazuTstd
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.

1 participant