From f0820b778f2344903cfd380f9acfe06d8dfe6c3d Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Thu, 3 Dec 2026 17:43:14 +0300 Subject: [PATCH] Build: qualify wasm test suite with portable libc++ fallbacks (refs #38) --- cmake/Targets.cmake | 5 +- docs/targets.md | 36 ++++++--- include/Void/Core/Publication.hpp | 48 ++++++++++++ include/Void/TestingMock/Expectation.hpp | 5 ++ include/Void/TestingMock/MoveOnlyFunction.hpp | 73 +++++++++++++++++++ tests/standalone/Containers/Debug.cpp | 4 + tests/standalone/Testing/StructRepr.cpp | 4 + 7 files changed, 163 insertions(+), 12 deletions(-) create mode 100644 include/Void/TestingMock/MoveOnlyFunction.hpp diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index 94078da..65cfed8 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -20,8 +20,8 @@ set_property(CACHE VOID_TARGET PROPERTY STRINGS auto ${VOID_KNOWN_TARGETS}) # C++26 reflection. Keep this list fail-closed: a module is added only after # it has been configured and compiled for wasm32-unknown-emscripten. # Qualified with Emscripten 6.0.9 (Clang 22, libc++) for configure, build, -# install, consume and run under Node; the wasm test suite is not qualified -# yet (see docs/targets.md). +# install, consume and run under Node, including the test suite with +# -pthread (see docs/targets.md). set(VOID_WASM_MODULES Algorithms Binary @@ -53,6 +53,7 @@ set(VOID_WINDOWS_MODULES Discovery Fsm Functional + IO Log LogState Math diff --git a/docs/targets.md b/docs/targets.md index a8af84d..2b77771 100644 --- a/docs/targets.md +++ b/docs/targets.md @@ -147,7 +147,7 @@ cmake -S . -B build-mingw -G Ninja \ A regular MinGW-w64 installation with winpthreads does not need those extra flags. -### Browser / Wasm subset (library qualified, test suite unqualified) +### Browser / Wasm subset (qualified) | Item | Value | |------|-------| @@ -157,7 +157,7 @@ flags. | CMake / generator | CMake 4.5 via `emcmake`, Ninja, Node 24.22.3 as the test emulator | | Language | C++26 without reflection (`VOID_HAS_CXX26_REFLECTION=no`) | | Modules | `Algorithms Binary Containers Core Data Functional LogState Math Memory Testing TestingMock Text Time Unicode` | -| Observed | configure, compile, `cmake --install`, consume (`find_package(Void)`) and run the consumer under Node (`node_exit=0`) | +| Observed | library: configure, compile, install, consume and run under Node; tests: **901 tests, 895 passed, 6 skipped, 0 failed** under Node | The allowlist is fail-closed: native networking (`Net`, `RPC`), processes and filesystem (`System`, `SystemAsync`), IPC, SQLite, plugins and anything that @@ -190,15 +190,31 @@ scanner calling Emscripten's `emscan-deps`, which is not shipped by every Emscripten package. `CMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH` is needed because the Emscripten toolchain restricts package lookup to its sysroot. -The **test suite** does not build yet on wasm (7 translation units after the -reflection gating below). The remaining blockers are libc++ gaps, not -reflection: +The **test suite** is qualified with pthreads, exception handling and a +larger stack: -- `Core/Publication` uses `std::atomic>`, whose - specialization is not implemented by this libc++ (`__cpp_lib_atomic_shared_ptr` - absent). A portable fallback is needed. -- Several `TestingMock` tests need `std::move_only_function`, also absent - from this libc++. +```bash +emcmake cmake -S . -B build-wasm-tests -G Ninja -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_CXX_SCAN_FOR_MODULES=OFF \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_CXX_FLAGS="-fexceptions -pthread" \ + -DCMAKE_EXE_LINKER_FLAGS="-fexceptions -pthread -sSTACK_SIZE=16777216 \ + -sINITIAL_MEMORY=134217728 -sALLOW_MEMORY_GROWTH=1 -sMAXIMUM_MEMORY=1073741824" \ + -DVOID_ENABLE_TESTING=ON \ + -DVOID_ENABLED_MODULES="Algorithms;Binary;Containers;Core;Data;Functional;LogState;Math;Memory;Testing;TestingMock;Text;Time;Unicode" +cmake --build build-wasm-tests +node build-wasm-tests/tests/VoidTests.js --no-color +``` + +Static linking (`BUILD_SHARED_LIBS=OFF`) is required for exception handling +across the test binary; `-fexceptions` enables catching, `-pthread` enables +the threading tests, and the stack/memory flags accommodate the deepest +recursive tests. + +Portable fallbacks added for libc++ without the C++23/26 pieces: +`Core/Publication` uses a mutex-protected `shared_ptr` when +`__cpp_lib_atomic_shared_ptr` is absent, and `TestingMock` uses an internal +move-only function wrapper when `__cpp_lib_move_only_function` is absent. Reflection-dependent test files are gated with `// @requires-reflection` (Core/Meta, Core/FlagsReflect, Data/CsvReflect, Data/Reflect*, diff --git a/include/Void/Core/Publication.hpp b/include/Void/Core/Publication.hpp index 7524030..a3ad157 100644 --- a/include/Void/Core/Publication.hpp +++ b/include/Void/Core/Publication.hpp @@ -7,6 +7,10 @@ #include #include +#if !defined(__cpp_lib_atomic_shared_ptr) +#include +#endif + namespace Void::Core { @@ -226,7 +230,51 @@ template class Publication private: +#if defined(__cpp_lib_atomic_shared_ptr) std::atomic> current_; +#else + // Older libc++ (e.g. Emscripten) does not implement the + // atomic specialization. Keep the same load / + // compare-exchange surface with a mutex-protected pointer; the + // publication is still coherent for single-writer/multi-reader use. + class AtomicStatePtr + { + public: + + explicit AtomicStatePtr(std::shared_ptr initial) + : current_(std::move(initial)) + { + } + + [[nodiscard]] std::shared_ptr + load(std::memory_order) const + { + std::lock_guard lock{mutex_}; + return current_; + } + + bool compare_exchange_weak(std::shared_ptr& expected, + std::shared_ptr desired, + std::memory_order, + std::memory_order) + { + std::lock_guard lock{mutex_}; + if (current_ != expected) { + expected = current_; + return false; + } + current_ = std::move(desired); + return true; + } + + private: + + mutable std::mutex mutex_; + std::shared_ptr current_; + }; + + AtomicStatePtr current_; +#endif }; } // namespace Void::Core diff --git a/include/Void/TestingMock/Expectation.hpp b/include/Void/TestingMock/Expectation.hpp index 2c6fb66..9276318 100644 --- a/include/Void/TestingMock/Expectation.hpp +++ b/include/Void/TestingMock/Expectation.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -30,7 +31,11 @@ namespace Void::TestingMock::Detail std::tuple>...>; /// Move-only function — supports unique_ptr-capturing lambdas, /// move-only arguments, and move-only return values. +#if defined(__cpp_lib_move_only_function) using Action = std::move_only_function; +#else + using Action = MoveOnlyFunction; +#endif Expectation(MatcherTuple matchers, std::source_location loc) : m_matchers(std::move(matchers)), m_location(loc) diff --git a/include/Void/TestingMock/MoveOnlyFunction.hpp b/include/Void/TestingMock/MoveOnlyFunction.hpp new file mode 100644 index 0000000..a054bf4 --- /dev/null +++ b/include/Void/TestingMock/MoveOnlyFunction.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Void::TestingMock::Detail +{ + /// Minimal move-only type-erased callable with the subset of + /// std::move_only_function used by TestingMock. Only needed on standard + /// libraries that do not implement std::move_only_function (older libc++, + /// e.g. Emscripten). + template class MoveOnlyFunction; + + template + class MoveOnlyFunction + { + private: + + struct Base { + virtual ~Base() = default; + virtual R invoke(Args&&... args) = 0; + }; + + template struct Model final : Base { + explicit Model(F callable) : fn(std::move(callable)) {} + + R invoke(Args&&... args) override + { + return std::invoke(fn, std::forward(args)...); + } + + F fn; + }; + + public: + + MoveOnlyFunction() noexcept = default; + MoveOnlyFunction(std::nullptr_t) noexcept {} + + template + requires std::invocable && + (!std::same_as, + MoveOnlyFunction>) + MoveOnlyFunction(F&& callable) + : impl_(std::make_unique>>( + std::forward(callable))) + { + } + + MoveOnlyFunction(MoveOnlyFunction&&) noexcept = default; + MoveOnlyFunction& operator=(MoveOnlyFunction&&) noexcept = default; + MoveOnlyFunction(const MoveOnlyFunction&) = delete; + MoveOnlyFunction& operator=(const MoveOnlyFunction&) = delete; + + [[nodiscard]] explicit operator bool() const noexcept + { + return impl_ != nullptr; + } + + R operator()(Args... args) + { + return impl_->invoke(std::forward(args)...); + } + + private: + + std::unique_ptr impl_; + }; +} // namespace Void::TestingMock::Detail diff --git a/tests/standalone/Containers/Debug.cpp b/tests/standalone/Containers/Debug.cpp index 77aeba8..fad6205 100644 --- a/tests/standalone/Containers/Debug.cpp +++ b/tests/standalone/Containers/Debug.cpp @@ -16,9 +16,13 @@ namespace // @tag reflection Void::Testing::Test debug_dump_reflects_struct_elements() { +#if !VOID_HAS_CXX26_REFLECTION + Void::Testing::skip("C++26 reflection is not available"); +#else std::vector

v{{1, 2}, {3, 4}}; Void::Testing::expect_eq(Void::Containers::debug_dump(v), std::string{"[P{x: 1, y: 2}, P{x: 3, y: 4}]"}); +#endif } // @tag reflection diff --git a/tests/standalone/Testing/StructRepr.cpp b/tests/standalone/Testing/StructRepr.cpp index 2941b7f..9036d3a 100644 --- a/tests/standalone/Testing/StructRepr.cpp +++ b/tests/standalone/Testing/StructRepr.cpp @@ -27,10 +27,14 @@ Void::Testing::Test struct_eq_passes_when_equal() // includes the type name and field values. Void::Testing::Test repr_renders_struct_via_reflection() { +#if !VOID_HAS_CXX26_REFLECTION + Void::Testing::skip("C++26 reflection is not available"); +#else const auto out = Void::Testing::Detail::repr(Point{3, 4}); Void::Testing::expect(out.find("Point") != std::string::npos); Void::Testing::expect(out.find("x: 3") != std::string::npos); Void::Testing::expect(out.find("y: 4") != std::string::npos); +#endif } // repr for primitives still works.