Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
AotDispatcher.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
27#pragma once
28
29#include <cstdint>
30#include <optional>
31#include <span>
32#include <string>
33#include <utility>
34#include <vector>
35
38
39namespace esengine::aot {
40
42public:
50 bool install(const char* path, std::uint64_t expected,
51 const ComponentLookup& components, const ResourceLookup& resources,
52 std::string* why = nullptr, const CandidateLookup& candidates = {},
53 std::optional<std::uint64_t> expectedContract = std::nullopt) {
54 // Prepare a complete replacement before touching the live module. The
55 // staged owner closes its library on refusal or a lookup/allocation throw.
56 Dispatcher next;
57 if (!next.module_.open(path, expected, why)) return false;
58 if (expectedContract && next.module_.contract() != 0 && next.module_.contract() != *expectedContract) {
59 if (why) *why = "module and manifest are from different builds";
60 return false;
61 }
62 next.components_ = components;
63 next.resources_ = resources;
64 next.candidates_ = candidates;
65 for (const EsSystemDecl& decl : next.module_.systems()) {
66 next.names_.push_back(decl.name != nullptr ? decl.name : "");
67 next.bound_.push_back(::esengine::aot::bind(decl, next.components_, next.resources_, next.candidates_));
68 }
69 next.arenas_ = std::vector<CallArena>(next.bound_.size());
70 // These swaps cannot throw; next now owns and retires the old runtime.
71 std::swap(module_, next.module_);
72 names_.swap(next.names_);
73 bound_.swap(next.bound_);
74 arenas_.swap(next.arenas_);
75 components_.swap(next.components_);
76 resources_.swap(next.resources_);
77 candidates_.swap(next.candidates_);
78 return true;
79 }
80
82 std::size_t count() const { return bound_.size(); }
83
86 std::uint64_t contract() const { return module_.contract(); }
87
89 const char* nameAt(std::size_t i) const { return i < names_.size() ? names_[i] : ""; }
90
92 bool boundAt(std::size_t i) const { return i < bound_.size() && bound_[i].fn != nullptr; }
93
101 bool narrowsAt(std::size_t i) const {
102 if (i >= bound_.size() || bound_[i].fn == nullptr) return false;
103 return ::esengine::aot::narrows(bound_[i].queries);
104 }
105
107 std::size_t indexOf(const char* name) const {
108 for (std::size_t i = 0; i < names_.size(); ++i) {
109 if (names_[i] != nullptr && name != nullptr && std::strcmp(names_[i], name) == 0) return i;
110 }
111 return npos;
112 }
113
122 std::span<const EsCmd> run(std::size_t i, std::span<const std::uint32_t> candidates,
123 const ResourceLookup& resources, WorldStamp world = {}) {
124 if (i >= bound_.size()) return {};
125 // Asked again where install could not name everything: a pool the
126 // scripting language owns does not exist until an entity has that
127 // component, so the answer is about the frame, not the module.
128 if (bound_[i].fn == nullptr) {
129 // Qualified: `std::bind` is visible through <functional> and takes anything.
130 bound_[i] = ::esengine::aot::bind(module_.systems()[i], components_, resources_, candidates_);
131 }
132 if (bound_[i].fn == nullptr) return {};
133 bool runnable = false;
134 const auto commands = runBound(bound_[i], candidates, resources, arenas_[i], world, &runnable);
135 if (!runnable) bound_[i].fn = nullptr;
136 return commands;
137 }
138
140 std::uint32_t candidatesWalked(std::size_t i) const {
141 return i < arenas_.size() ? arenas_[i].candidatesWalked() : 0u;
142 }
143
146 std::uint32_t rowsPacked(std::size_t i) const {
147 return i < arenas_.size() ? arenas_[i].rowsPacked() : 0u;
148 }
149
151 void reset() {
152 bound_.clear();
153 names_.clear();
154 arenas_.clear();
155 components_ = {};
156 resources_ = {};
157 candidates_ = {};
158 module_.close();
159 }
160
161 static constexpr std::size_t npos = static_cast<std::size_t>(-1);
162
163private:
164 Module module_;
165 std::vector<const char*> names_;
166 std::vector<BoundSystem> bound_;
169 ComponentLookup components_;
170 ResourceLookup resources_;
173 CandidateLookup candidates_;
175 std::vector<CallArena> arenas_;
176};
177
178} // namespace esengine::aot
The host half of the Estella ABI.
A built module of compiled systems, opened by a host that links.
std::span< const EsCmd > run(std::size_t i, std::span< const std::uint32_t > candidates, const ResourceLookup &resources, WorldStamp world={})
bool install(const char *path, std::uint64_t expected, const ComponentLookup &components, const ResourceLookup &resources, std::string *why=nullptr, const CandidateLookup &candidates={}, std::optional< std::uint64_t > expectedContract=std::nullopt)
bool boundAt(std::size_t i) const
const char * nameAt(std::size_t i) const
std::uint32_t rowsPacked(std::size_t i) const
std::uint32_t candidatesWalked(std::size_t i) const
std::size_t count() const
static constexpr std::size_t npos
std::uint64_t contract() const
std::size_t indexOf(const char *name) const
bool narrowsAt(std::size_t i) const
std::uint64_t contract() const
Definition AotModule.hpp:89
std::span< const EsSystemDecl > systems() const
Definition AotModule.hpp:94
bool open(const char *path, std::uint64_t expected, std::string *why=nullptr)
Definition AotModule.hpp:59
std::function< CandidatesOf(const char *)> CandidateLookup
Definition AotHost.hpp:358
std::function< void *(const char *)> ResourceLookup
Definition AotHost.hpp:120
std::span< const EsCmd > runBound(const BoundSystem &bound, std::span< const std::uint32_t > fallback, const ResourceLookup &resources, CallArena &arena, WorldStamp world={}, bool *runnable=nullptr)
Definition AotHost.hpp:404
std::function< ComponentAt(const char *)> ComponentLookup
Definition AotHost.hpp:350
BoundSystem bind(const EsSystemDecl &decl, const ComponentLookup &components, const ResourceLookup &resources, const CandidateLookup &candidates={})
Definition AotHost.hpp:376