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): the type half of the contract, from the same reflection

yhh57db2350d803
diff --git a/src/esengine/aot/AotComponents.generated.cpp b/src/esengine/aot/AotComponents.generated.cpp
new file mode 100644
index 000000000..48e6ef538
--- /dev/null
+++ b/src/esengine/aot/AotComponents.generated.cpp
@@ -0,0 +1,284 @@
+// Auto-generated by EHT (aot_components) — DO NOT EDIT.
+// The type half of the AOT contract: a manifest names a component, and a
+// compiled system needs the bytes. Regenerate with `pnpm eht`.
+#include "esengine/aot/AotComponents.generated.hpp"
+
+#include <cstdint>
+#include <cstring>
+
+#include "esengine/ecs/Entity.hpp"
+#include "esengine/ecs/Registry.hpp"
+#include "esengine/ecs/components/BitmapText.hpp"
+#include "esengine/ecs/components/Collider2D.hpp"
+#include "esengine/ecs/components/RigidBody3D.hpp"
+#include "esengine/ecs/components/Camera.hpp"
+#include "esengine/ecs/components/Canvas.hpp"
+#include "esengine/ecs/components/Hierarchy.hpp"
+#include "esengine/ecs/components/Draggable.hpp"
+#include "esengine/ecs/components/DragonBonesAnimation.hpp"
+#include "esengine/ecs/components/FlexContainer.hpp"
+#include "esengine/ecs/components/Interactable.hpp"
+#include "esengine/ecs/components/Light.hpp"
+#include "esengine/ecs/components/MeshRenderer.hpp"
+#include "esengine/ecs/components/ParticleEmitter.hpp"
+#include "esengine/ecs/components/ParticleForceField.hpp"
+#include "esengine/ecs/components/RigidBody2D.hpp"
+#include "esengine/ecs/components/ShadowCaster2D.hpp"
+#include "esengine/ecs/components/ShapeRenderer.hpp"
+#include "esengine/ecs/components/SpineAnimation.hpp"
+#include "esengine/ecs/components/Sprite.hpp"
+#include "esengine/ecs/components/TilemapLayer.hpp"
+#include "esengine/ecs/components/TrailRenderer.hpp"
+#include "esengine/ecs/components/Transform.hpp"
+#include "esengine/ecs/components/UIInteraction.hpp"
+#include "esengine/ecs/components/UIMask.hpp"
+#include "esengine/ecs/components/UINode.hpp"
+#include "esengine/ecs/components/UIScroll.hpp"
+#include "esengine/ecs/components/UIVisual.hpp"
+#include "esengine/ecs/components/Velocity.hpp"
+
+namespace esengine::aot {
+
+ComponentAt engineComponentAt(ecs::Registry& registry, const char* name) {
+ if (std::strcmp(name, "BitmapText") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::BitmapText>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "BoxCollider2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::BoxCollider2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "BoxCollider3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::BoxCollider3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Camera") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Camera>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Canvas") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Canvas>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "CapsuleCollider2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::CapsuleCollider2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "CapsuleCollider3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::CapsuleCollider3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "CharacterController3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::CharacterController3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Children") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Children>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "CircleCollider2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::CircleCollider2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "ConvexCollider3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::ConvexCollider3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Draggable") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Draggable>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "DragonBonesAnimation") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::DragonBonesAnimation>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "FlexContainer") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::FlexContainer>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Interactable") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Interactable>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Light") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Light>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "MeshCollider3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::MeshCollider3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "MeshRenderer") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::MeshRenderer>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "MeshSkin") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::MeshSkin>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Parent") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Parent>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "ParticleEmitter") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::ParticleEmitter>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "ParticleForceField") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::ParticleForceField>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "RigidBody2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::RigidBody2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "RigidBody3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::RigidBody3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "SegmentCollider2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::SegmentCollider2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "ShadowCaster2D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::ShadowCaster2D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "ShapeRenderer") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::ShapeRenderer>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "SphereCollider3D") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::SphereCollider3D>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "SpineAnimation") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::SpineAnimation>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Sprite") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Sprite>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "TilemapLayer") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::TilemapLayer>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "TrailRenderer") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::TrailRenderer>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Transform") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Transform>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "UIInteraction") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::UIInteraction>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "UIMask") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::UIMask>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "UINode") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::UINode>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "UIScroll") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::UIScroll>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "UIVisual") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::UIVisual>(Entity::fromRaw(raw));
+ };
+ }
+ if (std::strcmp(name, "Velocity") == 0) {
+ return [&registry](std::uint32_t raw) -> void* {
+ return registry.tryGet<esengine::ecs::Velocity>(Entity::fromRaw(raw));
+ };
+ }
+ return {};
+}
+
+bool isEngineComponent(const char* name) {
+ if (std::strcmp(name, "BitmapText") == 0) return true;
+ if (std::strcmp(name, "BoxCollider2D") == 0) return true;
+ if (std::strcmp(name, "BoxCollider3D") == 0) return true;
+ if (std::strcmp(name, "Camera") == 0) return true;
+ if (std::strcmp(name, "Canvas") == 0) return true;
+ if (std::strcmp(name, "CapsuleCollider2D") == 0) return true;
+ if (std::strcmp(name, "CapsuleCollider3D") == 0) return true;
+ if (std::strcmp(name, "CharacterController3D") == 0) return true;
+ if (std::strcmp(name, "Children") == 0) return true;
+ if (std::strcmp(name, "CircleCollider2D") == 0) return true;
+ if (std::strcmp(name, "ConvexCollider3D") == 0) return true;
+ if (std::strcmp(name, "Draggable") == 0) return true;
+ if (std::strcmp(name, "DragonBonesAnimation") == 0) return true;
+ if (std::strcmp(name, "FlexContainer") == 0) return true;
+ if (std::strcmp(name, "Interactable") == 0) return true;
+ if (std::strcmp(name, "Light") == 0) return true;
+ if (std::strcmp(name, "MeshCollider3D") == 0) return true;
+ if (std::strcmp(name, "MeshRenderer") == 0) return true;
+ if (std::strcmp(name, "MeshSkin") == 0) return true;
+ if (std::strcmp(name, "Parent") == 0) return true;
+ if (std::strcmp(name, "ParticleEmitter") == 0) return true;
+ if (std::strcmp(name, "ParticleForceField") == 0) return true;
+ if (std::strcmp(name, "RigidBody2D") == 0) return true;
+ if (std::strcmp(name, "RigidBody3D") == 0) return true;
+ if (std::strcmp(name, "SegmentCollider2D") == 0) return true;
+ if (std::strcmp(name, "ShadowCaster2D") == 0) return true;
+ if (std::strcmp(name, "ShapeRenderer") == 0) return true;
+ if (std::strcmp(name, "SphereCollider3D") == 0) return true;
+ if (std::strcmp(name, "SpineAnimation") == 0) return true;
+ if (std::strcmp(name, "Sprite") == 0) return true;
+ if (std::strcmp(name, "TilemapLayer") == 0) return true;
+ if (std::strcmp(name, "TrailRenderer") == 0) return true;
+ if (std::strcmp(name, "Transform") == 0) return true;
+ if (std::strcmp(name, "UIInteraction") == 0) return true;
+ if (std::strcmp(name, "UIMask") == 0) return true;
+ if (std::strcmp(name, "UINode") == 0) return true;
+ if (std::strcmp(name, "UIScroll") == 0) return true;
+ if (std::strcmp(name, "UIVisual") == 0) return true;
+ if (std::strcmp(name, "Velocity") == 0) return true;
+ return false;
+}
+
+} // namespace esengine::aot
diff --git a/src/esengine/aot/AotComponents.generated.hpp b/src/esengine/aot/AotComponents.generated.hpp
new file mode 100644
index 000000000..962c95677
--- /dev/null
+++ b/src/esengine/aot/AotComponents.generated.hpp
@@ -0,0 +1,33 @@
+// Auto-generated by EHT (aot_components) — DO NOT EDIT.
+// The type half of the AOT contract: a manifest names a component, and a
+// compiled system needs the bytes. Regenerate with `pnpm eht`.
+#pragma once
+
+#include "esengine/aot/AotHost.hpp"
+
+namespace esengine::ecs { class Registry; }
+
+namespace esengine::aot {
+
+/**
+ * Where a named ENGINE component lives, for this registry.
+ *
+ * `tryGet`, never `getOrEmplace`: a resolver that created the component
+ * would give one to every entity the query walked, and a row is kept only
+ * when every component resolves — the absence IS the filter.
+ *
+ * No lazily-derived field is refreshed, because the interpreted side does
+ * not refresh one either and it is the oracle. A component whose fields are
+ * stale until a method runs cannot be read by a compiled system at all, and
+ * that is a subset question rather than a resolver one.
+ *
+ * An unknown name yields an EMPTY resolver: a project component lives in the
+ * script pool and is not this table's to answer for. Call it and it throws,
+ * so a caller must ask `isEngineComponent` first.
+ */
+ComponentAt engineComponentAt(ecs::Registry& registry, const char* name);
+
+/** Whether this table answers for that name, without building a resolver. */
+bool isEngineComponent(const char* name);
+
+} // namespace esengine::aot
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b4b7f75e8..e49a6adc1 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -44,6 +44,7 @@ add_executable(test_aot_host
aot/test_aot_host.cpp
aot/generated/move_system.c
aot/generated/move_system_decl.c
+ ${CMAKE_SOURCE_DIR}/src/esengine/aot/AotComponents.generated.cpp
${CMAKE_SOURCE_DIR}/src/esengine/core/Log.cpp
)
target_include_directories(test_aot_host PRIVATE
diff --git a/tests/aot/test_aot_host.cpp b/tests/aot/test_aot_host.cpp
index 2c0bba32e..e04831c73 100644
--- a/tests/aot/test_aot_host.cpp
+++ b/tests/aot/test_aot_host.cpp
@@ -31,6 +31,7 @@
#include <vector>
#include "esengine/aot/AotHost.hpp"
+#include "esengine/aot/AotComponents.generated.hpp"
#include "esengine/ecs/Registry.hpp"
#include "esengine/ecs/components/Transform.hpp"
@@ -290,3 +291,54 @@ TEST_CASE("a row the query cannot complete is not a row") {
}
CHECK(moved > 0);
}
+
+TEST_CASE("the generated table answers for an engine component, and only for one") {
+ World w = makeWorld();
+
+ // The same question this file answers by hand above, asked of the table a
+ // native host will use. Two answers to "where is Transform" is the drift
+ // the generator exists to prevent.
+ const aot::ComponentAt fromTable = aot::engineComponentAt(w.registry, "Transform");
+ REQUIRE(static_cast<bool>(fromTable));
+ for (std::uint32_t e : w.entities) {
+ CHECK(fromTable(e) == w.registry.tryGet<ecs::Transform>(Entity::fromRaw(e)));
+ }
+
+ // Absence is the row filter, so it has to be nullptr and not a fresh
+ // component: resolving with getOrEmplace would give every entity walked one.
+ w.registry.remove<ecs::Transform>(Entity::fromRaw(w.entities[0]));
+ CHECK(fromTable(w.entities[0]) == nullptr);
+ CHECK(w.registry.has<ecs::Transform>(Entity::fromRaw(w.entities[0])) == false);
+
+ // A project component lives in the script pool; this table must say so
+ // rather than hand back something that reads as an engine miss.
+ CHECK(aot::isEngineComponent("Transform"));
+ CHECK(aot::isEngineComponent("Mover") == false);
+ CHECK(static_cast<bool>(aot::engineComponentAt(w.registry, "Mover")) == false);
+}
+
+TEST_CASE("a compiled system runs on the generated resolvers, exactly as on hand-written ones") {
+ World byHand = makeWorld();
+ World byTable = makeWorld();
+ aot::CallArena arena;
+
+ const auto tableLookup = [&byTable](const char* name) -> aot::ComponentAt {
+ if (aot::isEngineComponent(name)) return aot::engineComponentAt(byTable.registry, name);
+ if (std::strcmp(name, "Mover") == 0) return aot::fromRows(byTable.moverSpan());
+ return nullptr;
+ };
+
+ aot::runBound(aot::bind(*declOf("MoveSystem"), componentsOf(byHand), resourcesOf(byHand)),
+ byHand.entities, resourcesOf(byHand), arena);
+ aot::runBound(aot::bind(*declOf("MoveSystem"), tableLookup, resourcesOf(byTable)),
+ byTable.entities, resourcesOf(byTable), arena);
+
+ for (std::size_t i = 0; i < byHand.entities.size(); ++i) {
+ const auto* want = byHand.registry.tryGet<ecs::Transform>(Entity::fromRaw(byHand.entities[i]));
+ const auto* got = byTable.registry.tryGet<ecs::Transform>(Entity::fromRaw(byTable.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);
+ }
+}
diff --git a/tools/eht/__main__.py b/tools/eht/__main__.py
index f6d076323..92d855d95 100644
--- a/tools/eht/__main__.py
+++ b/tools/eht/__main__.py
@@ -8,7 +8,7 @@ from .parser import CppParser
from .abi import compute_abi_hash
from .generators import (
EmbindGenerator, TypeScriptGenerator, MetadataGenerator,
- PtrLayoutGenerator, EditorAPIGenerator, NativeBindingsGenerator,
+ PtrLayoutGenerator, EditorAPIGenerator, NativeBindingsGenerator, AotComponentsGenerator,
NativeFunctionsGenerator,
)
@@ -185,6 +185,17 @@ def main() -> int:
)
embind_path.write_text(embind_gen.generate(), encoding='utf-8')
+ # ── Component resolvers for compiled systems ──
+ # Committed like the two above: a host that dispatches a compiled system
+ # needs the type half of the contract, and it must not be a second table.
+ aot_dir = Path('src/esengine/aot')
+ aot_dir.mkdir(parents=True, exist_ok=True)
+ aot_gen = AotComponentsGenerator(cpp_parser.components, cpp_parser.enums)
+ for name, text in (('AotComponents.generated.hpp', aot_gen.generate_header()),
+ ('AotComponents.generated.cpp', aot_gen.generate_source())):
+ print(f"Generating: {aot_dir / name}")
+ (aot_dir / name).write_text(text, encoding='utf-8')
+
# ── Native QuickJS Bindings (opt-in) ──
if args.native_output is not None:
_emit_native(cpp_parser.components, cpp_parser.enums, args)
diff --git a/tools/eht/generators/__init__.py b/tools/eht/generators/__init__.py
index d4eafabed..4aa8ac518 100644
--- a/tools/eht/generators/__init__.py
+++ b/tools/eht/generators/__init__.py
@@ -7,9 +7,10 @@ from .ptr_layout import PtrLayoutGenerator
from .editor_api import EditorAPIGenerator
from .native_bindings import NativeBindingsGenerator
from .native_functions import NativeFunctionsGenerator
+from .aot_components import AotComponentsGenerator
__all__ = [
'EmbindGenerator', 'TypeScriptGenerator', 'MetadataGenerator',
'PtrLayoutGenerator', 'EditorAPIGenerator', 'NativeBindingsGenerator',
- 'NativeFunctionsGenerator',
+ 'NativeFunctionsGenerator', 'AotComponentsGenerator',
]
diff --git a/tools/eht/generators/aot_components.py b/tools/eht/generators/aot_components.py
new file mode 100644
index 000000000..f6d2ecc0e
--- /dev/null
+++ b/tools/eht/generators/aot_components.py
@@ -0,0 +1,135 @@
+"""Component resolvers for compiled systems (AotComponents.generated.hpp/.cpp).
+
+A compiled system calls the engine zero times, so everything it reads has to be
+in memory before the call. `AotHost.hpp` asks the caller for one `ComponentAt`
+per component — "supplied by the layer that knows its TYPE" — and this is that
+layer: the same reflection the bindings come from, turned into a table from the
+NAME a manifest uses to the pointer a Registry has.
+
+Two artifacts on purpose. The definitions need every component's header, and a
+consumer that included them would pay for the whole engine to ask where one
+component is; the declaration needs only `AotHost.hpp` and a forward-declared
+Registry.
+
+Resolved once per query and called per entity, not looked up per entity: a
+strcmp chain inside the row loop would be paid on exactly the path AOT exists
+to make cheap.
+"""
+
+from typing import List
+
+from ..data import Component, Enum
+
+_BANNER = [
+ '// Auto-generated by EHT (aot_components) — DO NOT EDIT.',
+ '// The type half of the AOT contract: a manifest names a component, and a',
+ '// compiled system needs the bytes. Regenerate with `pnpm eht`.',
+]
+
+
+class AotComponentsGenerator:
+ """Generates the name -> ComponentAt table a native host dispatches through."""
+
+ def __init__(self, components: List[Component], enums: List[Enum]):
+ self.components = components
+ self.enums = enums
+
+ def _sorted(self) -> List[Component]:
+ return sorted(self.components, key=lambda c: c.name)
+
+ def _full(self, comp: Component) -> str:
+ return f'{comp.namespace}::{comp.name}' if comp.namespace else comp.name
+
+ @staticmethod
+ def _include_path(comp: Component) -> str:
+ hp = comp.header_path.replace('\\', '/')
+ # The parser may hand back an absolute path, and the include has to be
+ # the one a compiler's -I resolves — which is rooted at src/.
+ if hp.startswith('src/'):
+ return hp[len('src/'):]
+ if '/src/' in hp:
+ return hp[hp.rindex('/src/') + len('/src/'):]
+ return hp
+
+ def generate_header(self) -> str:
+ return '\n'.join(_BANNER + [
+ '#pragma once',
+ '',
+ '#include "esengine/aot/AotHost.hpp"',
+ '',
+ 'namespace esengine::ecs { class Registry; }',
+ '',
+ 'namespace esengine::aot {',
+ '',
+ '/**',
+ ' * Where a named ENGINE component lives, for this registry.',
+ ' *',
+ ' * `tryGet`, never `getOrEmplace`: a resolver that created the component',
+ ' * would give one to every entity the query walked, and a row is kept only',
+ ' * when every component resolves — the absence IS the filter.',
+ ' *',
+ ' * No lazily-derived field is refreshed, because the interpreted side does',
+ ' * not refresh one either and it is the oracle. A component whose fields are',
+ ' * stale until a method runs cannot be read by a compiled system at all, and',
+ ' * that is a subset question rather than a resolver one.',
+ ' *',
+ ' * An unknown name yields an EMPTY resolver: a project component lives in the',
+ ' * script pool and is not this table\'s to answer for. Call it and it throws,',
+ ' * so a caller must ask `isEngineComponent` first.',
+ ' */',
+ 'ComponentAt engineComponentAt(ecs::Registry& registry, const char* name);',
+ '',
+ '/** Whether this table answers for that name, without building a resolver. */',
+ 'bool isEngineComponent(const char* name);',
+ '',
+ '} // namespace esengine::aot',
+ '',
+ ])
+
+ def generate_source(self) -> str:
+ components = self._sorted()
+ lines = list(_BANNER)
+ lines += [
+ '#include "esengine/aot/AotComponents.generated.hpp"',
+ '',
+ '#include <cstdint>',
+ '#include <cstring>',
+ '',
+ '#include "esengine/ecs/Entity.hpp"',
+ '#include "esengine/ecs/Registry.hpp"',
+ ]
+ seen = set()
+ for comp in components:
+ hp = self._include_path(comp)
+ if hp and hp not in seen:
+ seen.add(hp)
+ lines.append(f'#include "{hp}"')
+ lines += [
+ '',
+ 'namespace esengine::aot {',
+ '',
+ 'ComponentAt engineComponentAt(ecs::Registry& registry, const char* name) {',
+ ]
+ for comp in components:
+ full = self._full(comp)
+ lines.append(f' if (std::strcmp(name, "{comp.name}") == 0) {{')
+ lines.append(' return [&registry](std::uint32_t raw) -> void* {')
+ lines.append(f' return registry.tryGet<{full}>(Entity::fromRaw(raw));')
+ lines.append(' };')
+ lines.append(' }')
+ lines += [
+ ' return {};',
+ '}',
+ '',
+ 'bool isEngineComponent(const char* name) {',
+ ]
+ for comp in components:
+ lines.append(f' if (std::strcmp(name, "{comp.name}") == 0) return true;')
+ lines += [
+ ' return false;',
+ '}',
+ '',
+ '} // namespace esengine::aot',
+ '',
+ ]
+ return '\n'.join(lines)