From fedf7bfcb3eefb1939ece41d498e3f76b628c00c Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Tue, 1 Apr 2025 12:40:16 +0530 Subject: [PATCH 1/2] Add test for verifying exception handling for emscripten builds --- lib/Interpreter/exports.ld | 1 + unittests/CppInterOp/CMakeLists.txt | 1 + unittests/CppInterOp/InterpreterTest.cpp | 28 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/lib/Interpreter/exports.ld b/lib/Interpreter/exports.ld index d834500e0..1f7301dd4 100644 --- a/lib/Interpreter/exports.ld +++ b/lib/Interpreter/exports.ld @@ -2,6 +2,7 @@ -Wl,--export=_ZN4llvm11raw_ostream16SetBufferAndModeEPcmNS0_10BufferKindE -Wl,--export=_ZN4llvm11raw_ostream5writeEPKcm -Wl,--export=_ZN4llvm11raw_ostreamD2Ev +-Wl,--export=_ZN4llvm11raw_ostreamlsEm -Wl,--export=_ZN4llvm15SmallVectorBaseIjE8grow_podEPvmm -Wl,--export=_ZN4llvm15allocate_bufferEmm -Wl,--export=_ZN4llvm21logAllUnhandledErrorsENS_5ErrorERNS_11raw_ostreamENS_5TwineE diff --git a/unittests/CppInterOp/CMakeLists.txt b/unittests/CppInterOp/CMakeLists.txt index 734714238..1665110cb 100644 --- a/unittests/CppInterOp/CMakeLists.txt +++ b/unittests/CppInterOp/CMakeLists.txt @@ -43,6 +43,7 @@ if(EMSCRIPTEN) # --preload-file ${SYSROOT_PATH}/include@/include: # Preloads the system include directory into the Emscripten virtual filesystem to make headers accessible at runtime. target_link_options(CppInterOpTests + PUBLIC "SHELL: -fexceptions" PUBLIC "SHELL: -s MAIN_MODULE=1" PUBLIC "SHELL: -s WASM_BIGINT" PUBLIC "SHELL: -s ALLOW_MEMORY_GROWTH=1" diff --git a/unittests/CppInterOp/InterpreterTest.cpp b/unittests/CppInterOp/InterpreterTest.cpp index 459b71bc3..7e3efecda 100644 --- a/unittests/CppInterOp/InterpreterTest.cpp +++ b/unittests/CppInterOp/InterpreterTest.cpp @@ -104,6 +104,34 @@ TEST(InterpreterTest, Process) { clang_Interpreter_dispose(CXI); } +TEST(InterpreterTest, EmscriptenExceptionHandling) { +#ifndef EMSCRIPTEN + GTEST_SKIP() << "This test is intended to check exception handling for Emscripten builds."; +#endif + + std::vector Args = { + "-std=c++20", + "-v", + "-fexceptions", + "-fcxx-exceptions", + "-mllvm", "-enable-emscripten-cxx-exceptions", + "-mllvm", "-enable-emscripten-sjlj" + }; + + auto* I = Cpp::CreateInterpreter(Args); + + // Should compile and execute successfully + const char* tryCatchCode = R"( + try { + throw 1; + } catch (...) { + 0; + } + )"; + + EXPECT_TRUE(Cpp::Process(tryCatchCode) == 0); +} + TEST(InterpreterTest, CreateInterpreter) { auto* I = Cpp::CreateInterpreter(); EXPECT_TRUE(I); From cdf42b91b50525851e83d26f9ebaffea5bf15c1d Mon Sep 17 00:00:00 2001 From: Anutosh Bhat Date: Tue, 1 Apr 2025 14:53:54 +0530 Subject: [PATCH 2/2] Update InterpreterTest.cpp --- unittests/CppInterOp/InterpreterTest.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/unittests/CppInterOp/InterpreterTest.cpp b/unittests/CppInterOp/InterpreterTest.cpp index 7e3efecda..55cd3f742 100644 --- a/unittests/CppInterOp/InterpreterTest.cpp +++ b/unittests/CppInterOp/InterpreterTest.cpp @@ -118,9 +118,8 @@ TEST(InterpreterTest, EmscriptenExceptionHandling) { "-mllvm", "-enable-emscripten-sjlj" }; - auto* I = Cpp::CreateInterpreter(Args); + Cpp::CreateInterpreter(Args); - // Should compile and execute successfully const char* tryCatchCode = R"( try { throw 1;