Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(aot): a resolver for rows the scripting language keeps moving

yhh66d79f029678
diff --git a/src/esengine/aot/AotHost.hpp b/src/esengine/aot/AotHost.hpp
index bf3633b90..df11db9dc 100644
--- a/src/esengine/aot/AotHost.hpp
+++ b/src/esengine/aot/AotHost.hpp
@@ -141,14 +141,30 @@ struct RowSpan {
std::uint32_t indexMask = 0;
};
-/** `span` as a resolver. Absent is zero in the table, as it is in a sparse set. */
+/** One row out of a span. Absent is zero in the table, as it is in a sparse set. */
+inline void* rowAt(const RowSpan& span, std::uint32_t entity) {
+ const std::uint32_t at = entity & span.indexMask;
+ if (at >= span.sparseCount) return nullptr;
+ const std::uint32_t slot = span.sparse[at];
+ if (slot == 0u) return nullptr;
+ return span.rows + static_cast<std::size_t>(slot - 1u) * span.stride;
+}
+
+/** `span` as a resolver, for rows that will not move while it is held. */
inline ComponentAt fromRows(const RowSpan& span) {
- return [span](std::uint32_t entity) -> void* {
- const std::uint32_t at = entity & span.indexMask;
- if (at >= span.sparseCount) return nullptr;
- const std::uint32_t slot = span.sparse[at];
- if (slot == 0u) return nullptr;
- return span.rows + static_cast<std::size_t>(slot - 1u) * span.stride;
+ return [span](std::uint32_t entity) -> void* { return rowAt(span, entity); };
+}
+
+/**
+ * The same, for rows that MOVE.
+ *
+ * A pool reallocates as it grows, and a resolver holding the span by value then
+ * reads memory nobody owns; this reads it through a slot the owner overwrites.
+ * The slot must outlive the resolver; a null one is simply absent.
+ */
+inline ComponentAt fromMovingRows(const RowSpan* slot) {
+ return [slot](std::uint32_t entity) -> void* {
+ return slot == nullptr ? nullptr : rowAt(*slot, entity);
};
}
diff --git a/tests/aot/test_aot_host.cpp b/tests/aot/test_aot_host.cpp
index 1a5945f1b..188b25e0a 100644
--- a/tests/aot/test_aot_host.cpp
+++ b/tests/aot/test_aot_host.cpp
@@ -478,3 +478,62 @@ TEST_CASE("the digest a host carries is the one a native module bakes") {
? ES_ENGINE_ABI_DIGEST_64
: ES_ENGINE_ABI_DIGEST_32));
}
+
+#ifdef ES_AOT_MODULE_PATH
+TEST_CASE("a pool that moved its rows takes the twin with it") {
+ World w = makeWorld();
+ World linked = makeWorld();
+ aot::Dispatcher dispatcher;
+ std::string why;
+
+ // The slot the OWNER overwrites. A resolver holding the span by value reads
+ // the rows' first home for as long as it lives.
+ aot::RowSpan slot = w.moverSpan();
+ const aot::ComponentLookup moving = [&w, &slot](const char* name) -> aot::ComponentAt {
+ if (std::strcmp(name, "Transform") == 0) {
+ return [&w](std::uint32_t e) -> void* {
+ return w.registry.tryGet<ecs::Transform>(Entity::fromRaw(e));
+ };
+ }
+ if (std::strcmp(name, "Mover") == 0) return aot::fromMovingRows(&slot);
+ return nullptr;
+ };
+
+ REQUIRE_MESSAGE(dispatcher.install(ES_AOT_MODULE_PATH, aot::abiHash(ES_EXPECTED_CONTRACT_HASH),
+ moving, resourcesOf(w), &why), why);
+ const std::size_t at = dispatcher.indexOf("MoveSystem");
+ REQUIRE(at != aot::Dispatcher::npos);
+
+ aot::CallArena arena;
+ const auto runLinked = [&] {
+ aot::runBound(aot::bind(*declOf("MoveSystem"), componentsOf(linked), resourcesOf(linked)),
+ linked.entities, resourcesOf(linked), arena);
+ };
+ dispatcher.run(at, w.entities, resourcesOf(w));
+ runLinked();
+
+ // The new home's CONTENT differs, or a stale resolver reads the same bytes
+ // and passes. A live second vector, not a freed buffer: what a stale read
+ // returns has to be defined or this proves nothing.
+ std::vector<MoverRow> relocated = w.movers;
+ for (MoverRow& row : relocated) row.speed *= 3.0;
+ for (MoverRow& row : linked.movers) row.speed *= 3.0;
+ slot = aot::RowSpan{
+ w.moverSparse.data(), static_cast<std::uint32_t>(w.moverSparse.size()),
+ reinterpret_cast<unsigned char*>(relocated.data()),
+ static_cast<std::uint32_t>(sizeof(MoverRow)), Entity::Layout::INDEX_MASK,
+ };
+
+ dispatcher.run(at, w.entities, resourcesOf(w));
+ runLinked();
+
+ for (std::size_t i = 0; i < w.entities.size(); ++i) {
+ const auto* want = linked.registry.tryGet<ecs::Transform>(Entity::fromRaw(linked.entities[i]));
+ const auto* got = w.registry.tryGet<ecs::Transform>(Entity::fromRaw(w.entities[i]));
+ REQUIRE((want != nullptr) == (got != nullptr));
+ if (want == nullptr) continue;
+ CHECK(want->position.x == got->position.x);
+ CHECK(want->position.y == got->position.y);
+ }
+}
+#endif