From ef49d8a1f9811232178640d3b7cdb33ee0610594 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 08:50:15 +0100 Subject: [PATCH 1/8] Make BasicUI a regular value type [#400] Hold a pointer to the application instead of a reference so UI is assignable and usable in vectors with erase_if and in optionals. Compare by id(). --- docs/CHANGELOG.md | 5 +++++ include/webfront/WebFront.hpp | 31 ++++++++++++++++++------------- test/unit/CommandsTests.cpp | 14 ++++++++++++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9b4a8abf..f3d25f42 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,11 @@ ## Unreleased +- Make `BasicUI` (`UI`) a regular value type (#400): it holds a pointer to the application instead of a + reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in + `std::optional`. Two values compare equal when they have the same `id()`. The lifetime contract is + unchanged: a `UI` is valid while its application exists. + - Warn at the end of a consumer's configuration, with GCC 16, about each target linking `WebFront::WebFront` that CMake scans for C++ modules (`-fmodules-ts`), which makes GCC crash inside stdexec (#399). Add `webfront_disable_module_scan(target...)`; the global workaround is diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index cc363880..44d4677e 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -44,11 +44,11 @@ using ConnectionError = std::runtime_error; template class BasicUI { - WebFront& webFront; + WebFront* webFront; WebLinkId webLinkId; public: - BasicUI(WebFront& wf, WebLinkId id) : webFront(wf), webLinkId(id) {} + BasicUI(WebFront& wf, WebLinkId id) : webFront(&wf), webLinkId(id) {} /// Identifies this browser's connection among those connected. The 16-bit identifier of a released connection /// is only reused after 65,536 later connections. @@ -56,13 +56,18 @@ class BasicUI { return webLinkId; } + /// Two values are equal when they identify the same connection. + [[nodiscard]] friend bool operator==(const BasicUI& a, const BasicUI& b) noexcept { + return a.webLinkId == b.webLinkId; + } + /** * @brief Injects a Javascript script in the client * * @param script */ void addScript(std::string_view script) const { - const auto link = webFront.findLink(webLinkId); + const auto link = webFront->findLink(webLinkId); if (!link) throw ConnectionError("Connection with client lost"); link->sendCommand(msg::TextCommand(msg::TxtOpcode::injectScript, script)); @@ -70,7 +75,7 @@ class BasicUI { /// Queues one DOM command on the bridge context. Rejections arrive at wfDom.rejected(batchId, reason). void sendDomBatch(const msg::DomBatch& batch) const { - const auto link = webFront.findLink(webLinkId); + const auto link = webFront->findLink(webLinkId); if (!link) throw ConnectionError("Connection with client lost"); link->sendCommand(batch); } @@ -84,13 +89,13 @@ class BasicUI { template friend class dom::Document; [[nodiscard]] auto domState() const { - return webFront.domState(webLinkId); + return webFront->domState(webLinkId); } public: /// The frames queued for this browser and not yet written to the network; 0 once its connection is gone. [[nodiscard]] std::size_t pendingWrites() const { - const auto link = webFront.findLink(webLinkId); + const auto link = webFront->findLink(webLinkId); return link ? link->pendingWrites() : 0; } @@ -101,37 +106,37 @@ class BasicUI { * @return JsFunction */ [[nodiscard]] JsFunction jsFunction(std::string_view functionName) const { - return JsFunction{functionName, webFront, webLinkId}; + return JsFunction{functionName, *webFront, webLinkId}; } template [[nodiscard]] JsFunction jsFunction(std::string_view functionName) const { - return JsFunction{functionName, webFront, webLinkId}; + return JsFunction{functionName, *webFront, webLinkId}; } /** @brief Starts a coroutine owned by the application; the returned handle can cancel it. */ TaskHandle spawn(Task task) const { - return webFront.spawn(std::move(task)); + return webFront->spawn(std::move(task)); } /// A sender completing once at most `limit` frames await the network for this browser, or at once when it is gone; /// co_await propagates the task's stop token. It paces output to a browser that reads slower than it is written. [[nodiscard]] auto writesAtMost(std::size_t limit) const { - using Link = typename decltype(webFront.findLink(webLinkId))::element_type; - return detail::WritesAtMost{webFront.findLink(webLinkId), limit}; + using Link = typename decltype(webFront->findLink(webLinkId))::element_type; + return detail::WritesAtMost{webFront->findLink(webLinkId), limit}; } /// A sender completing on the server context after the duration; see BasicWF::resumeAfter. template [[nodiscard]] auto resumeAfter(std::chrono::duration duration) const { - return webFront.resumeAfter(duration); + return webFront->resumeAfter(duration); } /// Lazy typed call; co_await propagates the receiver's stop token. Cancellation /// stops waiting locally; a JavaScript function already dispatched keeps running. template [[nodiscard]] JsFunctionSender jsFunctionSender(std::string_view functionName) const { - return JsFunctionSender{functionName, webFront, webLinkId}; + return JsFunctionSender{functionName, *webFront, webLinkId}; } }; diff --git a/test/unit/CommandsTests.cpp b/test/unit/CommandsTests.cpp index c528b3a8..442c0e5a 100644 --- a/test/unit/CommandsTests.cpp +++ b/test/unit/CommandsTests.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -556,6 +557,19 @@ SCENARIO("BasicWF provides the application facilities of commands") { auto subscription = app.subscribeUIClosed([](RealApp::UI) { }); CHECK(subscription.active()); } + THEN("a UI is a regular value usable in containers") { + static_assert(std::is_copy_assignable_v); + static_assert(std::is_nothrow_move_assignable_v); + static_assert(std::equality_comparable); + std::vector clients{RealApp::UI(app, 1), RealApp::UI(app, 2), RealApp::UI(app, 3)}; + std::erase_if(clients, [](const RealApp::UI& ui) { return ui.id() == 2; }); + REQUIRE(clients.size() == 2); + CHECK(clients[0] == RealApp::UI(app, 1)); + CHECK(clients[1].id() == 3); + std::optional current; + current = clients[1]; + CHECK(current->id() == 3); + } THEN("a second Commands object is refused, and cppFunction tells a taken name") { Commands commands{app}; CHECK_THROWS_AS(Commands{app}, logic_error); From 825564987f0d18b6ef454d9e43bca389328de55d Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 09:47:12 +0100 Subject: [PATCH 2/8] Compare UI values by application and id [#400] --- include/webfront/WebFront.hpp | 4 ++-- test/unit/CommandsTests.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index 44d4677e..f7319c1d 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -56,9 +56,9 @@ class BasicUI { return webLinkId; } - /// Two values are equal when they identify the same connection. + /// Two values are equal when they identify the same connection of the same application. [[nodiscard]] friend bool operator==(const BasicUI& a, const BasicUI& b) noexcept { - return a.webLinkId == b.webLinkId; + return a.webFront == b.webFront && a.webLinkId == b.webLinkId; } /** diff --git a/test/unit/CommandsTests.cpp b/test/unit/CommandsTests.cpp index 442c0e5a..49f46f37 100644 --- a/test/unit/CommandsTests.cpp +++ b/test/unit/CommandsTests.cpp @@ -565,11 +565,19 @@ SCENARIO("BasicWF provides the application facilities of commands") { std::erase_if(clients, [](const RealApp::UI& ui) { return ui.id() == 2; }); REQUIRE(clients.size() == 2); CHECK(clients[0] == RealApp::UI(app, 1)); + CHECK(clients[0] != clients[1]); CHECK(clients[1].id() == 3); std::optional current; current = clients[1]; CHECK(current->id() == 3); } + THEN("a UI of another application is never equal, even with the same identifier") { + RealApp other(WebFrontConfig{.port = "0"}); + CHECK(RealApp::UI(app, 1) != RealApp::UI(other, 1)); + RealApp::UI ui(app, 1); + ui = RealApp::UI(other, 1); + CHECK(ui == RealApp::UI(other, 1)); + } THEN("a second Commands object is refused, and cppFunction tells a taken name") { Commands commands{app}; CHECK_THROWS_AS(Commands{app}, logic_error); From 2940448269c04d8cfbf0e8f4960901d81836ea08 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 09:47:49 +0100 Subject: [PATCH 3/8] Specify UI equality in the changelog [#400] --- docs/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f3d25f42..879e48f4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,7 +4,7 @@ - Make `BasicUI` (`UI`) a regular value type (#400): it holds a pointer to the application instead of a reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in - `std::optional`. Two values compare equal when they have the same `id()`. The lifetime contract is + `std::optional`. Two values compare equal when they belong to the same application and have the same `id()`. The lifetime contract is unchanged: a `UI` is valid while its application exists. - Warn at the end of a consumer's configuration, with GCC 16, about each target linking From bcf51e18c2aa45e858c156718b09cd02c27b10b6 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 09:47:56 +0100 Subject: [PATCH 4/8] Rewrap changelog entry [#400] --- docs/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 879e48f4..96388d66 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,8 +4,8 @@ - Make `BasicUI` (`UI`) a regular value type (#400): it holds a pointer to the application instead of a reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in - `std::optional`. Two values compare equal when they belong to the same application and have the same `id()`. The lifetime contract is - unchanged: a `UI` is valid while its application exists. + `std::optional`. Two values compare equal when they belong to the same application and have the same `id()`. + The lifetime contract is unchanged: a `UI` is valid while its application exists. - Warn at the end of a consumer's configuration, with GCC 16, about each target linking `WebFront::WebFront` that CMake scans for C++ modules (`-fmodules-ts`), which makes GCC crash inside From 15a8a6fe803edc6eed20381e85616a2e311208f8 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 10:15:57 +0100 Subject: [PATCH 5/8] Address review of BasicUI value type [#400] Document that the application pointer is never null and that a stale UI can equal one of a later application at the same address. Test equality across applications with stub types instead of a second server, and exercise a copy- and move-assigned UI. --- docs/CHANGELOG.md | 3 ++- include/webfront/WebFront.hpp | 6 ++++-- test/unit/CommandsTests.cpp | 27 ++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 96388d66..de9d19d5 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,7 +5,8 @@ - Make `BasicUI` (`UI`) a regular value type (#400): it holds a pointer to the application instead of a reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in `std::optional`. Two values compare equal when they belong to the same application and have the same `id()`. - The lifetime contract is unchanged: a `UI` is valid while its application exists. + The lifetime contract is unchanged: a `UI` is valid while its application exists, so a value kept after its + application is destroyed is not comparable with those of a later application. - Warn at the end of a consumer's configuration, with GCC 16, about each target linking `WebFront::WebFront` that CMake scans for C++ modules (`-fmodules-ts`), which makes GCC crash inside diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index f7319c1d..7523f537 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -44,7 +44,7 @@ using ConnectionError = std::runtime_error; template class BasicUI { - WebFront* webFront; + WebFront* webFront; ///< Never null: the only constructor takes a reference, and there is no default constructor. WebLinkId webLinkId; public: @@ -56,7 +56,9 @@ class BasicUI { return webLinkId; } - /// Two values are equal when they identify the same connection of the same application. + /// Two values are equal when they identify the same connection of the same application. A value only identifies + /// a connection while its application lives: a stale value can equal one of a later application built at the + /// same address. [[nodiscard]] friend bool operator==(const BasicUI& a, const BasicUI& b) noexcept { return a.webFront == b.webFront && a.webLinkId == b.webLinkId; } diff --git a/test/unit/CommandsTests.cpp b/test/unit/CommandsTests.cpp index 49f46f37..5bff9a35 100644 --- a/test/unit/CommandsTests.cpp +++ b/test/unit/CommandsTests.cpp @@ -572,11 +572,28 @@ SCENARIO("BasicWF provides the application facilities of commands") { CHECK(current->id() == 3); } THEN("a UI of another application is never equal, even with the same identifier") { - RealApp other(WebFrontConfig{.port = "0"}); - CHECK(RealApp::UI(app, 1) != RealApp::UI(other, 1)); - RealApp::UI ui(app, 1); - ui = RealApp::UI(other, 1); - CHECK(ui == RealApp::UI(other, 1)); + struct Application { } first, second; + using StubUI = BasicUI; + CHECK(StubUI(first, 1) != StubUI(second, 1)); + StubUI ui(first, 1); + ui = StubUI(second, 1); + CHECK(ui == StubUI(second, 1)); + } + THEN("a UI has no default state and a reassigned one acts on its new value") { + static_assert(!std::is_default_constructible_v); + RealApp::UI ui(app, 1); + const RealApp::UI gone(app, 42); + ui = gone; + CHECK(ui == gone); + CHECK_THROWS_AS(ui.addScript("1"), ConnectionError); + CHECK(ui.pendingWrites() == 0); + CHECK_NOTHROW(ui.jsFunction("console.log")("dropped")); + auto result = ui.jsFunction("Math.abs")(-1); + CHECK_THROWS_WITH(result.get(), "Browser connection closed"); + CHECK_NOTHROW(ui.writesAtMost(0)); + RealApp::UI moved(std::move(ui)); + CHECK(moved.id() == 42); + CHECK_THROWS_AS(moved.addScript("1"), ConnectionError); } THEN("a second Commands object is refused, and cppFunction tells a taken name") { Commands commands{app}; From d4f1bdd1d0c93a36de94868327dd1cd13e4a80ef Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:43:09 +0100 Subject: [PATCH 6/8] Pin the application address and document identifier reuse [#400] Delete the copy operations of BasicWF, which also removes its moves, so the pointer each UI holds cannot be invalidated by a move of the application. Document that a value kept after its connection closes can, once the identifier is reused, equal and reach a later browser, so applications remove kept values in subscribeUIClosed. --- docs/CHANGELOG.md | 5 ++++- include/webfront/WebFront.hpp | 12 +++++++++--- test/unit/CommandsTests.cpp | 2 ++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index cdc048ce..72ffaf78 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,7 +6,10 @@ reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in `std::optional`. Two values compare equal when they belong to the same application and have the same `id()`. The lifetime contract is unchanged: a `UI` is valid while its application exists, so a value kept after its - application is destroyed is not comparable with those of a later application. + application is destroyed is not comparable with those of a later application. A value only identifies its + connection until that connection closes: once the identifier is reused, a value kept after the close reaches the + later browser and equals its value, so applications remove kept values in `subscribeUIClosed`. `BasicWF` now + declares itself non-copyable and non-movable, which the pointer requires. - Add awaitable DOM events (#384): `co_await node.next(name, options)` completes with the value of the element's next event, for sequential dialogs and wizards. Starting the sender subscribes with diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index a3397ed8..32813c24 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -56,9 +56,11 @@ class BasicUI { return webLinkId; } - /// Two values are equal when they identify the same connection of the same application. A value only identifies - /// a connection while its application lives: a stale value can equal one of a later application built at the - /// same address. + /// Two values are equal when they belong to the same application and have the same id(). A value only identifies + /// its connection until that connection closes: once its identifier is reused, a value kept after the close equals + /// the later connection's and reaches that other browser, so remove kept values in subscribeUIClosed. A value kept + /// after its application is destroyed can equal one of a later application built at the same address. Assigning a + /// value is not atomic: a container of values shared between threads needs a lock. [[nodiscard]] friend bool operator==(const BasicUI& a, const BasicUI& b) noexcept { return a.webFront == b.webFront && a.webLinkId == b.webLinkId; } @@ -378,6 +380,10 @@ class BasicWF { explicit BasicWF(std::string_view port, std::filesystem::path docRoot = ".") : BasicWF(WebFrontConfig{.address = "0.0.0.0", .port = std::string(port), .documentRoot = std::move(docRoot)}) {} + // Each UI and the server callbacks hold the application's address: it never moves. + BasicWF(const BasicWF&) = delete; + BasicWF& operator=(const BasicWF&) = delete; + /** @brief The listening port, including the OS-assigned TCPStdnet port when configured with "0". */ std::string_view port() const noexcept { return httpPort; } diff --git a/test/unit/CommandsTests.cpp b/test/unit/CommandsTests.cpp index 5bff9a35..80152560 100644 --- a/test/unit/CommandsTests.cpp +++ b/test/unit/CommandsTests.cpp @@ -561,6 +561,8 @@ SCENARIO("BasicWF provides the application facilities of commands") { static_assert(std::is_copy_assignable_v); static_assert(std::is_nothrow_move_assignable_v); static_assert(std::equality_comparable); + static_assert(!std::is_copy_constructible_v && !std::is_move_constructible_v); + static_assert(!std::is_copy_assignable_v && !std::is_move_assignable_v); std::vector clients{RealApp::UI(app, 1), RealApp::UI(app, 2), RealApp::UI(app, 3)}; std::erase_if(clients, [](const RealApp::UI& ui) { return ui.id() == 2; }); REQUIRE(clients.size() == 2); From b56468e0a90e1c0dd583317c3fe50852b0819f24 Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:46:50 +0100 Subject: [PATCH 7/8] Refer to the stale UI follow-up in the changelog [#400] --- docs/CHANGELOG.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 72ffaf78..e3fe6eb4 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -8,8 +8,9 @@ The lifetime contract is unchanged: a `UI` is valid while its application exists, so a value kept after its application is destroyed is not comparable with those of a later application. A value only identifies its connection until that connection closes: once the identifier is reused, a value kept after the close reaches the - later browser and equals its value, so applications remove kept values in `subscribeUIClosed`. `BasicWF` now - declares itself non-copyable and non-movable, which the pointer requires. + later browser and equals its value, so applications remove kept values in `subscribeUIClosed` (#428 tracks + rejecting such stale values). `BasicWF` now declares itself non-copyable and non-movable, which the pointer + requires. - Add awaitable DOM events (#384): `co_await node.next(name, options)` completes with the value of the element's next event, for sequential dialogs and wizards. Starting the sender subscribes with From 1285f1e723a3d60ebbab025ac694c3da27dc6e4d Mon Sep 17 00:00:00 2001 From: Ambroise Leclerc <6860842+ambroise-leclerc@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:29:19 +0100 Subject: [PATCH 8/8] Identify browsers by UI equality and document the value type [#400] - The automation binding and the Jasmine session compare their browser with UI equality, which also checks the application, instead of ids; JasmineSession takes the caller type, a bare WebLinkId by default. - Assign kept UI values instead of re-emplacing an optional. - Say that a UI must not be used or compared after its application is destroyed, instead of describing that comparison as defined. - Move the UI value scenarios from CommandsTests to WebFrontTests. - Describe the value semantics and the stale identifier hazard (#428) in docs/ASYNC_EXECUTION.md. --- docs/ASYNC_EXECUTION.md | 9 +++++ docs/CHANGELOG.md | 11 +++--- include/webfront/WebFront.hpp | 6 +-- include/webfront/automation/Binding.hpp | 6 +-- include/webfront/testing/Jasmine.hpp | 39 ++++++++++--------- test/driver/WfDriver.hpp | 2 +- test/unit/AutomationTests.cpp | 3 ++ test/unit/CommandsTests.cpp | 41 -------------------- test/unit/JasmineConsumerTests.cpp | 4 +- test/unit/WebFrontTests.cpp | 50 +++++++++++++++++++++++++ test/unit/WfDriverTests.cpp | 8 ++-- test/web/JasmineTest.cpp | 2 +- 12 files changed, 104 insertions(+), 77 deletions(-) diff --git a/docs/ASYNC_EXECUTION.md b/docs/ASYNC_EXECUTION.md index fc718e32..6f626bc3 100644 --- a/docs/ASYNC_EXECUTION.md +++ b/docs/ASYNC_EXECUTION.md @@ -97,6 +97,15 @@ a `jsFunctionSender` completes with an error when started, and `addScript` throw `ConnectionError`. The 16-bit identifier of a released connection is reused only after 65,536 later connections. +A `UI` is an assignable value: it can be kept in a `std::vector` (removed with +`std::erase_if`) or a `std::optional`, and assigned. Two values are equal when they +belong to the same application and have the same `id()`. A value identifies its +connection only until that connection closes: once the identifier is reused, a value +kept after the close equals the later browser's and reaches that browser (#428 tracks +rejecting such stale values). Remove kept values in the `subscribeUIClosed` handler. +A value must not outlive its application, and assigning one is not atomic: a +container of values shared between threads needs a lock. + `app.spawn(task)` and `ui.spawn(task)` may be called from any thread while the application object remains alive, including concurrently with stop/shutdown. Coroutine bodies and their resumptions execute on the server context. Synchronize diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 56ac55b3..748f2faf 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -17,11 +17,12 @@ reference, so it is copy- and move-assignable and usable in `std::vector` with `std::erase_if` and in `std::optional`. It has no default constructor, so it does not satisfy `std::regular`. Two values compare equal when they belong to the same application and have the same `id()`. The lifetime contract is unchanged: a `UI` is - valid while its application exists, so a value kept after its application is destroyed is not comparable with - those of a later application. A value only identifies its connection until that connection closes: once the - identifier is reused, a value kept after the close reaches the later browser and equals its value, so applications - remove kept values in `subscribeUIClosed` (#428 tracks rejecting such stale values). `BasicWF` now declares itself - non-copyable and non-movable, which the pointer requires. + valid while its application exists and must not be used, compared included, after it is destroyed. A value only + identifies its connection until that connection closes: once the identifier is reused, a value kept after the close + reaches the later browser and equals its value, so applications remove kept values in `subscribeUIClosed` (#428 + tracks rejecting such stale values). `BasicWF` now declares itself non-copyable and non-movable, which the pointer + requires. The automation binding and the Jasmine session identify their browser by this equality, and assign it + instead of re-emplacing an optional. `docs/ASYNC_EXECUTION.md` describes the value semantics. - Add awaitable DOM events (#384): `co_await node.next(name, options)` completes with the value of the element's next event, for sequential dialogs and wizards. Starting the sender subscribes with diff --git a/include/webfront/WebFront.hpp b/include/webfront/WebFront.hpp index 32813c24..0159e12f 100644 --- a/include/webfront/WebFront.hpp +++ b/include/webfront/WebFront.hpp @@ -58,9 +58,9 @@ class BasicUI { /// Two values are equal when they belong to the same application and have the same id(). A value only identifies /// its connection until that connection closes: once its identifier is reused, a value kept after the close equals - /// the later connection's and reaches that other browser, so remove kept values in subscribeUIClosed. A value kept - /// after its application is destroyed can equal one of a later application built at the same address. Assigning a - /// value is not atomic: a container of values shared between threads needs a lock. + /// the later connection's and reaches that other browser (#428), so remove kept values in subscribeUIClosed. A value + /// must not outlive its application: comparing or using it afterwards reads a dangling application address, whose + /// result is meaningless. Assigning a value is not atomic: a container of values shared between threads needs a lock. [[nodiscard]] friend bool operator==(const BasicUI& a, const BasicUI& b) noexcept { return a.webFront == b.webFront && a.webLinkId == b.webLinkId; } diff --git a/include/webfront/automation/Binding.hpp b/include/webfront/automation/Binding.hpp index 8065769f..ec643735 100644 --- a/include/webfront/automation/Binding.hpp +++ b/include/webfront/automation/Binding.hpp @@ -90,7 +90,7 @@ class Binding { bool closing = false; void authorize(const AppUI& ui, std::string_view sequence, std::string_view mac, std::string_view method, std::initializer_list args) { - if (!browser || browser->id() != ui.id()) + if (!browser || *browser != ui) throw std::runtime_error("Automation callback belongs to another page"); pageAuthentication.authorize(sequence, mac, method, args); } @@ -112,7 +112,7 @@ class Binding { std::scoped_lock lock{owned->mutex}; if (owned->browser || !channel::sameToken(token, owned->launchToken)) throw std::runtime_error("Automation window claim rejected"); - owned->browser.emplace(ui); + owned->browser = ui; owned->launchToken.clear(); // one successful claim; later connections cannot replace the selected browser ui.addScript(owned->closing ? std::string{"window.close()"} : connectionScript(owned->pageAuthentication.secret())); return std::string{"null"}; @@ -157,7 +157,7 @@ class Binding { }); closed = app.subscribeUIClosed([owned](AppUI ui) { std::scoped_lock lock{owned->mutex}; - if (owned->browser && owned->browser->id() == ui.id()) + if (owned->browser == ui) owned->session->close("Developer window disconnected"); }); } diff --git a/include/webfront/testing/Jasmine.hpp b/include/webfront/testing/Jasmine.hpp index edbefb50..7cd86d52 100644 --- a/include/webfront/testing/Jasmine.hpp +++ b/include/webfront/testing/Jasmine.hpp @@ -5,6 +5,7 @@ #include "../WebFront.hpp" #include +#include #include #include #include @@ -163,7 +164,9 @@ inline std::string jasmineScript() { return "const CHUNK = " + std::to_string(jasmineChunkSize) + ";\n" + std::string(jasmineScriptBody); } -/// The state shared by the bridge callbacks of one test run. Independent of the application type so it can be tested alone. +/// The state shared by the bridge callbacks of one test run. The caller is the application's UI, compared by its +/// equality; any equality-comparable value (a bare WebLinkId by default) tests it without an application. +template class JasmineSession { public: static constexpr std::size_t chunkSize = jasmineChunkSize; @@ -172,23 +175,23 @@ class JasmineSession { : files(std::move(contents)), report(std::move(reportPath)) {} /// The newest page is the one under test (a reload replaces the previous connection). Returns false once finished. - bool attach(WebLinkId id) { + bool attach(const Caller& caller) { std::scoped_lock lock{mutex}; if (completed) return false; - if (browser && *browser != id) + if (browser && *browser != caller) std::cerr << "Jasmine: a new page replaced the instrumented one\n"; - browser = id; + browser = caller; return true; } - [[nodiscard]] double count(WebLinkId caller) { + [[nodiscard]] double count(const Caller& caller) { std::scoped_lock lock{mutex}; authorize(caller); return static_cast(files.size()); } - [[nodiscard]] std::vector read(WebLinkId caller, double index, double offset) { + [[nodiscard]] std::vector read(const Caller& caller, double index, double offset) { std::scoped_lock lock{mutex}; authorize(caller); if (!std::isfinite(index) || index < 0 || index >= static_cast(files.size()) || std::floor(index) != index) @@ -200,7 +203,7 @@ class JasmineSession { return std::vector(part.begin(), part.end()); } - void result(WebLinkId caller, const std::string& text) { + void result(const Caller& caller, const std::string& text) { std::scoped_lock lock{mutex}; authorize(caller); if (completed) @@ -234,14 +237,14 @@ class JasmineSession { } private: - void authorize(WebLinkId caller) const { + void authorize(const Caller& caller) const { if (!browser || *browser != caller) throw std::runtime_error("Jasmine callback belongs to another page"); } std::vector files; std::filesystem::path report; - std::optional browser; + std::optional browser; std::mutex mutex; bool completed = false; int status = 1; @@ -261,6 +264,9 @@ inline namespace jasmine_enabled { */ template class JasmineTest { + using AppUI = typename App::UI; + using Session = detail::JasmineSession; + public: explicit JasmineTest(App& app) { const auto path = detail::jasmineConfigurationPath(); @@ -270,14 +276,13 @@ class JasmineTest { std::vector contents; for (const auto& file : config.files) contents.push_back(detail::readJasmineFile(file)); - session = std::make_shared(std::move(contents), config.report); - using AppUI = typename App::UI; + session = std::make_shared(std::move(contents), config.report); const auto owned = session; - app.cppFunction("webfront.testing.count", [owned](AppUI ui) { return owned->count(ui.id()); }); - app.cppFunction("webfront.testing.read", [owned](AppUI ui, double index, double offset) { return owned->read(ui.id(), index, offset); }); - app.cppFunction("webfront.testing.result", [owned](AppUI ui, std::string result) { owned->result(ui.id(), result); }); + app.cppFunction("webfront.testing.count", [owned](AppUI ui) { return owned->count(ui); }); + app.cppFunction("webfront.testing.read", [owned](AppUI ui, double index, double offset) { return owned->read(ui, index, offset); }); + app.cppFunction("webfront.testing.result", [owned](AppUI ui, std::string result) { owned->result(ui, result); }); subscription = app.subscribeUIStarted([owned](AppUI ui) { - if (owned->attach(ui.id())) + if (owned->attach(ui)) ui.addScript(detail::jasmineScript()); }); } @@ -287,8 +292,8 @@ class JasmineTest { } private: - std::shared_ptr session; - UISubscription subscription; + std::shared_ptr session; + UISubscription subscription; }; } // namespace jasmine_enabled #else diff --git a/test/driver/WfDriver.hpp b/test/driver/WfDriver.hpp index 5951f772..a05209a9 100644 --- a/test/driver/WfDriver.hpp +++ b/test/driver/WfDriver.hpp @@ -407,7 +407,7 @@ class DriverBrowser { std::scoped_lock lock{mutex}; if (browser) return; // Commands and wf test create their own connected frames; inject only the native page. - browser.emplace(std::move(ui)); + browser = std::move(ui); browser->addScript(closing ? "window.close()" : driverScript); } void close() { diff --git a/test/unit/AutomationTests.cpp b/test/unit/AutomationTests.cpp index ea592387..53557ede 100644 --- a/test/unit/AutomationTests.cpp +++ b/test/unit/AutomationTests.cpp @@ -17,6 +17,9 @@ struct BindingApp { void addScript(std::string text) const { scripts->push_back(std::move(text)); } + friend bool operator==(const UI& a, const UI& b) { + return a.value == b.value; + } }; std::function claim; std::function started, closed; diff --git a/test/unit/CommandsTests.cpp b/test/unit/CommandsTests.cpp index 0a88a994..c528b3a8 100644 --- a/test/unit/CommandsTests.cpp +++ b/test/unit/CommandsTests.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -557,46 +556,6 @@ SCENARIO("BasicWF provides the application facilities of commands") { auto subscription = app.subscribeUIClosed([](RealApp::UI) { }); CHECK(subscription.active()); } - THEN("a UI is an assignable value usable in containers") { - static_assert(std::is_copy_assignable_v); - static_assert(std::is_nothrow_move_assignable_v); - static_assert(std::equality_comparable); - static_assert(!std::is_copy_constructible_v && !std::is_move_constructible_v); - static_assert(!std::is_copy_assignable_v && !std::is_move_assignable_v); - std::vector clients{RealApp::UI(app, 1), RealApp::UI(app, 2), RealApp::UI(app, 3)}; - std::erase_if(clients, [](const RealApp::UI& ui) { return ui.id() == 2; }); - REQUIRE(clients.size() == 2); - CHECK(clients[0] == RealApp::UI(app, 1)); - CHECK(clients[0] != clients[1]); - CHECK(clients[1].id() == 3); - std::optional current; - current = clients[1]; - CHECK(current->id() == 3); - } - THEN("a UI of another application is never equal, even with the same identifier") { - struct Application { } first, second; - using StubUI = BasicUI; - CHECK(StubUI(first, 1) != StubUI(second, 1)); - StubUI ui(first, 1); - ui = StubUI(second, 1); - CHECK(ui == StubUI(second, 1)); - } - THEN("a UI has no default state and a reassigned one acts on its new value") { - static_assert(!std::is_default_constructible_v); - RealApp::UI ui(app, 1); - const RealApp::UI gone(app, 42); - ui = gone; - CHECK(ui == gone); - CHECK_THROWS_AS(ui.addScript("1"), ConnectionError); - CHECK(ui.pendingWrites() == 0); - CHECK_NOTHROW(ui.jsFunction("console.log")("dropped")); - auto result = ui.jsFunction("Math.abs")(-1); - CHECK_THROWS_WITH(result.get(), "Browser connection closed"); - CHECK_NOTHROW(ui.writesAtMost(0)); - RealApp::UI moved(std::move(ui)); - CHECK(moved.id() == 42); - CHECK_THROWS_AS(moved.addScript("1"), ConnectionError); - } THEN("a second Commands object is refused, and cppFunction tells a taken name") { Commands commands{app}; CHECK_THROWS_AS(Commands{app}, logic_error); diff --git a/test/unit/JasmineConsumerTests.cpp b/test/unit/JasmineConsumerTests.cpp index 50333baa..e10578eb 100644 --- a/test/unit/JasmineConsumerTests.cpp +++ b/test/unit/JasmineConsumerTests.cpp @@ -44,7 +44,7 @@ struct ReportFile { } // namespace SCENARIO("A Jasmine session serves its files in bounded chunks to the instrumented page only") { - constexpr auto chunk = JasmineSession::chunkSize; + constexpr auto chunk = JasmineSession<>::chunkSize; ReportFile report{"chunks"}; GIVEN("a file that is an exact multiple of the chunk size, an empty file and a short one") { JasmineSession session{{std::string(chunk * 2, 'a'), "", "short"}, report.path}; @@ -120,7 +120,7 @@ SCENARIO("A Jasmine session validates and records exactly one result") { SCENARIO("The injected script reads in the same chunk size the session serves and reports load errors") { const auto script = webfront::testing::detail::jasmineScript(); - REQUIRE(script.starts_with("const CHUNK = " + std::to_string(JasmineSession::chunkSize) + ";")); + REQUIRE(script.starts_with("const CHUNK = " + std::to_string(JasmineSession<>::chunkSize) + ";")); REQUIRE(script.find("49152", script.find("49152") + 1) == std::string::npos); // the number appears only in the CHUNK definition REQUIRE(script.find("failed to load") != std::string_view::npos); } diff --git a/test/unit/WebFrontTests.cpp b/test/unit/WebFrontTests.cpp index 0756dfa1..b58f4c07 100644 --- a/test/unit/WebFrontTests.cpp +++ b/test/unit/WebFrontTests.cpp @@ -8,13 +8,16 @@ #include #include +#include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -969,6 +972,53 @@ SCENARIO("Sending a DOM batch through a lost UI link throws ConnectionError") { } } +SCENARIO("A UI is an assignable value identifying one browser of one application") { + using App = BasicWF; + GIVEN("an application over the networking mock") { + App app(WebFrontConfig{.port = "0"}); + THEN("a UI is an assignable value usable in containers") { + static_assert(std::is_copy_assignable_v); + static_assert(std::is_nothrow_move_assignable_v); + static_assert(std::equality_comparable); + static_assert(!std::is_copy_constructible_v && !std::is_move_constructible_v); + static_assert(!std::is_copy_assignable_v && !std::is_move_assignable_v); + std::vector clients{App::UI(app, 1), App::UI(app, 2), App::UI(app, 3)}; + std::erase_if(clients, [](const App::UI& ui) { return ui.id() == 2; }); + REQUIRE(clients.size() == 2); + CHECK(clients[0] == App::UI(app, 1)); + CHECK(clients[0] != clients[1]); + CHECK(clients[1].id() == 3); + std::optional current; + current = clients[1]; + CHECK(current->id() == 3); + } + THEN("a UI of another application is never equal, even with the same identifier") { + struct Application { } first, second; + using StubUI = BasicUI; + CHECK(StubUI(first, 1) != StubUI(second, 1)); + StubUI ui(first, 1); + ui = StubUI(second, 1); + CHECK(ui == StubUI(second, 1)); + } + THEN("a UI has no default state and a reassigned one acts on its new value") { + static_assert(!std::is_default_constructible_v); + App::UI ui(app, 1); + const App::UI gone(app, 42); + ui = gone; + CHECK(ui == gone); + CHECK_THROWS_AS(ui.addScript("1"), ConnectionError); + CHECK(ui.pendingWrites() == 0); + CHECK_NOTHROW(ui.jsFunction("console.log")("dropped")); + auto result = ui.jsFunction("Math.abs")(-1); + CHECK_THROWS_WITH(result.get(), "Browser connection closed"); + CHECK_NOTHROW(ui.writesAtMost(0)); + App::UI moved(std::move(ui)); + CHECK(moved.id() == 42); + CHECK_THROWS_AS(moved.addScript("1"), ConnectionError); + } + } +} + namespace { struct DomNoTimerNet : testing::InjectableNetworking { static inline bool accepted{}; diff --git a/test/unit/WfDriverTests.cpp b/test/unit/WfDriverTests.cpp index 605e25b3..db47506a 100644 --- a/test/unit/WfDriverTests.cpp +++ b/test/unit/WfDriverTests.cpp @@ -3,9 +3,9 @@ namespace { struct DriverUI { - std::vector& scripts; + std::vector* scripts; void addScript(std::string_view script) const { - scripts.emplace_back(script); + scripts->emplace_back(script); } }; webfront::testdriver::Options parseDriver(std::initializer_list extra) { @@ -22,7 +22,7 @@ SCENARIO("The driver deadline closes both connected and late browsers without lo std::vector scripts; webfront::testdriver::DriverBrowser browser; GIVEN("the browser connects before the deadline") { - browser.connected(DriverUI{scripts}); + browser.connected(DriverUI{&scripts}); REQUIRE(scripts == std::vector{webfront::testdriver::driverScript}); WHEN("the watchdog closes it twice") { browser.close(); @@ -37,7 +37,7 @@ SCENARIO("The driver deadline closes both connected and late browsers without lo browser.close(); REQUIRE(scripts.empty()); WHEN("the handshake arrives afterward") { - browser.connected(DriverUI{scripts}); + browser.connected(DriverUI{&scripts}); THEN("the page closes without starting the command runner") { REQUIRE(scripts == std::vector{"window.close()"}); } diff --git a/test/web/JasmineTest.cpp b/test/web/JasmineTest.cpp index b1c0557c..7d758032 100644 --- a/test/web/JasmineTest.cpp +++ b/test/web/JasmineTest.cpp @@ -531,7 +531,7 @@ class BrowserIntegrationTest { }); webFront->cppFunction("browserReady", [this](TestWF::UI ui) { // Raw origin probes create extra sessions; bind calls to the document reporting ready. - connectedUI.emplace(ui); + connectedUI = ui; browserReady(); }); webFront->cppFunction("recordFromJs", [this](const string& token) {