From 71a3a700d26680bf1c1d3e36a66abc43969e0d0d Mon Sep 17 00:00:00 2001
From: Jaswant Panchumarti <jaswant.panchumarti@kitware.com>
Date: Mon, 9 Dec 2024 11:35:20 -0500
Subject: [PATCH] wasm: enable exception support for exprtk

- `TestGlyph3DMapperOrientationArray` uses vtkArrayCalculator
which internally uses exprtk. This test threw an uncaught exception
from exprtk because in wasm, exceptions are not enabled by default.
- This commit enables exception support when compiling the exprtk
library with emscripten. The exception support is not enabled
for all targets yet because the emscripten docs say this flag
has a large overhead in performance.
---
 Common/Misc/CMakeLists.txt | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/Common/Misc/CMakeLists.txt b/Common/Misc/CMakeLists.txt
index 33b0ef1f217..87e6c89047e 100644
--- a/Common/Misc/CMakeLists.txt
+++ b/Common/Misc/CMakeLists.txt
@@ -14,6 +14,21 @@ if (MINGW AND NOT ANDROID) # MINGW is set when cross-compiling for ANDROID, see
       COMPILE_FLAGS "-Wa,-mbig-obj")
 endif ()
 
+# ExprTk internally relies on exceptions. This compile flag enables exception support in emscripten compiler.
+if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
+  set_property(SOURCE vtkExprTkFunctionParser.cxx APPEND
+    PROPERTY
+      COMPILE_FLAGS "-fexceptions")
+endif ()
+
 vtk_module_add_module(VTK::CommonMisc
   CLASSES ${classes})
+
+# The final executable linking to CommonMisc must use -sDISABLE_EXCEPTION_CATCHING=0 and -fexceptions as well.
+if (CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
+  vtk_module_link_options(VTK::CommonMisc
+    INTERFACE
+      "-sDISABLE_EXCEPTION_CATCHING=0"
+      "-fexceptions")
+endif ()
 vtk_add_test_mangling(VTK::CommonMisc)
-- 
GitLab