Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
AotModule.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
22#pragma once
23
24#include <cstdint>
25#include <cstring>
26#include <span>
27#include <string>
28#include <utility>
29
30#include "esengine/aot/AotHost.hpp" // abiHash — the width half of the handshake
32
33namespace esengine::aot {
34
35class Module {
36public:
37 Module() = default;
38 ~Module() { close(); }
39 Module(const Module&) = delete;
40 Module& operator=(const Module&) = delete;
41 Module(Module&& other) noexcept { *this = std::move(other); }
42 Module& operator=(Module&& other) noexcept {
43 if (this != &other) {
44 close();
45 handle_ = std::exchange(other.handle_, nullptr);
46 systems_ = std::exchange(other.systems_, {});
47 contract_ = std::exchange(other.contract_, 0);
48 }
49 return *this;
50 }
51
59 bool open(const char* path, std::uint64_t expected, std::string* why = nullptr) {
60 const auto fail = [why](const char* msg) { if (why != nullptr) *why = msg; return false; };
61 Module next;
62 next.handle_ = core::openLibrary(path);
63 if (next.handle_ == nullptr) return fail("the platform could not load it");
64
65 const auto* hash = static_cast<const std::uint64_t*>(core::librarySymbol(next.handle_, "es_abi_hash"));
66 const auto* count = static_cast<const std::uint32_t*>(core::librarySymbol(next.handle_, "es_system_count"));
67 const auto* decls = static_cast<const EsSystemDecl*>(core::librarySymbol(next.handle_, "es_systems"));
68 if (hash == nullptr || count == nullptr || decls == nullptr) {
69 // A wasm module is built WITHOUT the declaration table on purpose;
70 // handing one to a linking host lands exactly here.
71 return fail("it carries no declaration table — built for a host that shares memory?");
72 }
73 if (*hash != expected) {
74 return fail("it was built against a different engine or address width");
75 }
76 next.systems_ = std::span<const EsSystemDecl>(decls, *count);
77 next.contract_ = next.readContract_();
78 // The candidate owns its handle until every check passed. Failed loads
79 // leave this owner's declarations and their library alive.
80 *this = std::move(next);
81 return true;
82 }
83
89 std::uint64_t contract() const { return contract_; }
90
91 bool isOpen() const { return handle_ != nullptr; }
92
94 std::span<const EsSystemDecl> systems() const { return systems_; }
95
97 const EsSystemDecl* find(const char* name) const {
98 for (const auto& decl : systems_) {
99 if (decl.name != nullptr && std::strcmp(decl.name, name) == 0) return &decl;
100 }
101 return nullptr;
102 }
103
104 void close() {
105 // The declarations live IN the library, so they stop existing with it.
106 systems_ = {};
107 contract_ = 0;
108 core::closeLibrary(handle_);
109 handle_ = nullptr;
110 }
111
112private:
118 std::uint64_t readContract_() const {
119 using Half = std::uint32_t (*)();
120 auto* lo = reinterpret_cast<Half>(core::librarySymbol(handle_, "es_module_contract_lo"));
121 auto* hi = reinterpret_cast<Half>(core::librarySymbol(handle_, "es_module_contract_hi"));
122 if (lo == nullptr || hi == nullptr) return 0;
123 return (static_cast<std::uint64_t>(hi()) << 32) | lo();
124 }
125
126 void* handle_ = nullptr;
127 std::span<const EsSystemDecl> systems_{};
128 std::uint64_t contract_ = 0;
129};
130
131} // namespace esengine::aot
The host half of the Estella ABI.
Open a shared library and take a symbol out of it.
std::uint64_t contract() const
Definition AotModule.hpp:89
Module & operator=(Module &&other) noexcept
Definition AotModule.hpp:42
Module(Module &&other) noexcept
Definition AotModule.hpp:41
Module(const Module &)=delete
Module & operator=(const Module &)=delete
const EsSystemDecl * find(const char *name) const
Definition AotModule.hpp:97
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
bool isOpen() const
Definition AotModule.hpp:91
void closeLibrary(void *handle)
void * openLibrary(const char *path)
void * librarySymbol(void *handle, const char *name)