Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ModuleLoader.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SCRIPTMODULELOADER
19#define INCLUDED_SCRIPTMODULELOADER
20
22#include "lib/path.h"
23
24#include <cstddef>
25#include <exception>
26#include <functional>
27#include <iterator>
28#include <js/RootingAPI.h>
29#include <js/TypeDecls.h>
30#include <tuple>
31#include <unordered_map>
32#include <utility>
33#include <variant>
34#include <vector>
35
36class JSObject;
37namespace JS { class Value; }
38namespace Script { class Context; }
39namespace Script { class Interface; }
40namespace Script { class Request; }
41struct JSContext;
42
43namespace Script
44{
46{
47public:
48 friend Script::Context;
49
50 class Future;
51 class Result;
52
53 using AllowModuleFunc = std::function<bool(const VfsPath&)>;
54
56 {
57 public:
58 CompiledModule(const Script::Request& rq, const AllowModuleFunc& allowModule, const VfsPath& filePath);
59
60 std::tuple<const std::vector<VfsPath>&,
61 const std::vector<std::reference_wrapper<Result>>&> GetRequesters() const;
62
63 void AddRequester(VfsPath importer);
64 void AddRequester(Result& callback);
65 void RemoveRequester(Result* toErase);
66
67 JS::PersistentRootedObject m_ModuleObject;
68 private:
69 std::vector<VfsPath> m_Importer;
70 std::vector<std::reference_wrapper<Result>> m_Callbacks;
71 };
72
73 using RegistryType = std::unordered_map<VfsPath, CompiledModule>;
74
75 ModuleLoader(AllowModuleFunc allowModule);
76 ModuleLoader(const ModuleLoader&) = delete;
81
82 /**
83 * Load the specified module and all module it imports recursively.
84 *
85 * @param rq @c globalThis is taken from this @c Script::Request.
86 * @param modulePath The path to the file which should be loaded as a
87 * module.
88 * @return A range of futures. The compilation of the first future is
89 * already started. The evaluation of the subsequent futures start once
90 * the module file is edited.
91 */
92 [[nodiscard]] Result LoadModule(const Script::Request& rq, const VfsPath& modulePath);
93
94private:
95 // Functions used by the `Script::Context`.
96 [[nodiscard]] static bool MetadataHook(JSContext* cx, JS::HandleValue privateValue,
97 JS::HandleObject metaObject) noexcept;
98 [[nodiscard]] static JSObject* ResolveHook(JSContext* cx, JS::HandleValue referencingPrivate,
99 JS::HandleObject moduleRequest) noexcept;
100 [[nodiscard]] static bool DynamicImportHook(JSContext* cx, JS::HandleValue referencingPrivate,
101 JS::HandleObject moduleRequest, JS::HandleObject promise) noexcept;
102
105};
106
107/**
108 * The future is fulfilled once the evaluation of the module
109 * completes.
110 * Note: The evaluation might not be started yet.
111 */
113{
114 friend Result;
115public:
117 {
118 JS::PersistentRootedObject moduleNamespace;
119 JS::PersistentRootedObject fulfill;
120 JS::PersistentRootedObject reject;
121 };
123 {
124 JS::PersistentRootedObject moduleNamespace;
125 };
126 struct Rejected
127 {
128 std::exception_ptr error;
129 };
131 struct Invalid {};
132 using Status = std::variant<Evaluating, Fulfilled, Rejected, WaitingForFileChange, Invalid>;
133
134 explicit Future(const Script::Request& rq, ModuleLoader& reqistry, Result& result, VfsPath modulePath);
135 Future() = default;
136 Future(const Future&) = delete;
137 Future& operator=(const Future&) = delete;
138 Future(Future&& other) noexcept;
139 Future& operator=(Future&& other) noexcept;
140 ~Future();
141
142 [[nodiscard]] bool IsDone() const noexcept;
143
144 /**
145 * Throws if the evaluation of the module failed.
146 * @return The module namespace. All exported values are a property
147 * of this object. @c default is a property with name "default".
148 */
149 [[nodiscard]] JSObject* Get();
150
151private:
152 [[nodiscard]] bool IsWaiting() const noexcept;
153 void SetWaiting() noexcept;
154
155 // It's save to not require a `JS::HandleValue` here.
156 void SetReservedSlot(JS::Value privateValue) noexcept;
157
159};
160
162{
163public:
164 class iterator;
165
166 explicit Result(const Script::Request& rq, const VfsPath& modulePath);
167 Result(const Result&) = delete;
168 Result& operator=(const Result&) = delete;
169 Result(Result&&) = delete;
170 Result& operator=(Result&&) = delete;
171 ~Result();
172
173 [[nodiscard]] iterator begin() noexcept;
174 [[nodiscard]] iterator end() const noexcept;
175
176 void Resume();
177
178private:
182};
183
185{
186public:
187 using difference_type = std::ptrdiff_t;
191 using iterator_category = std::input_iterator_tag;
192
193 explicit iterator() = default;
194 explicit iterator(Result& backReference);
195
196 [[nodiscard]] reference operator*() const;
197 [[nodiscard]] pointer operator->() const;
198
199 iterator& operator++();
200 iterator& operator++(int);
201
202 [[nodiscard]] bool operator==(const iterator&) const;
203 [[nodiscard]] bool operator!=(const iterator&) const;
204
205private:
206 Result* backRef{nullptr};
207};
208} // namespace Script
209
210#endif // INCLUDED_SCRIPTMODULELOADER
Future(auto &taskManager, Callback &&callback, Args &&... args) -> Future< CallbackResult< Callback > >
bool operator==(const FCDJointWeightPair &a, const FCDJointWeightPair &b)
Definition: GeomReindex.cpp:62
Definition: path.h:84
Definition: Context.h:51
Definition: Interface.h:93
Definition: ModuleLoader.h:56
std::vector< VfsPath > m_Importer
Definition: ModuleLoader.h:69
void RemoveRequester(Result *toErase)
Definition: ModuleLoader.cpp:320
std::tuple< const std::vector< VfsPath > &, const std::vector< std::reference_wrapper< Result > > & > GetRequesters() const
Definition: ModuleLoader.cpp:305
CompiledModule(const Script::Request &rq, const AllowModuleFunc &allowModule, const VfsPath &filePath)
Definition: ModuleLoader.cpp:269
void AddRequester(VfsPath importer)
Definition: ModuleLoader.cpp:310
std::vector< std::reference_wrapper< Result > > m_Callbacks
Definition: ModuleLoader.h:70
JS::PersistentRootedObject m_ModuleObject
Definition: ModuleLoader.h:67
The future is fulfilled once the evaluation of the module completes.
Definition: ModuleLoader.h:113
void SetReservedSlot(JS::Value privateValue) noexcept
Definition: ModuleLoader.cpp:406
std::variant< Evaluating, Fulfilled, Rejected, WaitingForFileChange, Invalid > Status
Definition: ModuleLoader.h:132
JSObject * Get()
Throws if the evaluation of the module failed.
Definition: ModuleLoader.cpp:387
Status m_Status
Definition: ModuleLoader.h:158
bool IsDone() const noexcept
Definition: ModuleLoader.cpp:382
bool IsWaiting() const noexcept
Definition: ModuleLoader.cpp:396
Future & operator=(const Future &)=delete
Future(const Future &)=delete
~Future()
Definition: ModuleLoader.cpp:377
void SetWaiting() noexcept
Definition: ModuleLoader.cpp:401
friend Result
Definition: ModuleLoader.h:114
Definition: ModuleLoader.h:185
std::ptrdiff_t difference_type
Definition: ModuleLoader.h:187
std::input_iterator_tag iterator_category
Definition: ModuleLoader.h:191
Definition: ModuleLoader.h:162
Result & operator=(const Result &)=delete
Result(const Script::Request &rq, const VfsPath &modulePath)
Definition: ModuleLoader.cpp:454
Result & operator=(Result &&)=delete
void Resume()
Definition: ModuleLoader.cpp:481
Result(Result &&)=delete
const Script::Interface & m_Script
Definition: ModuleLoader.h:179
Result(const Result &)=delete
~Result()
Definition: ModuleLoader.cpp:461
iterator end() const noexcept
Definition: ModuleLoader.cpp:476
Future m_Storage
Definition: ModuleLoader.h:181
VfsPath m_ModulePath
Definition: ModuleLoader.h:180
iterator begin() noexcept
Definition: ModuleLoader.cpp:471
Definition: ModuleLoader.h:46
RegistryType m_Registry
Definition: ModuleLoader.h:104
std::function< bool(const VfsPath &)> AllowModuleFunc
Definition: ModuleLoader.h:53
AllowModuleFunc m_AllowModule
Definition: ModuleLoader.h:103
static JSObject * ResolveHook(JSContext *cx, JS::HandleValue referencingPrivate, JS::HandleObject moduleRequest) noexcept
Definition: ModuleLoader.cpp:524
static bool DynamicImportHook(JSContext *cx, JS::HandleValue referencingPrivate, JS::HandleObject moduleRequest, JS::HandleObject promise) noexcept
Definition: ModuleLoader.cpp:545
static bool MetadataHook(JSContext *cx, JS::HandleValue privateValue, JS::HandleObject metaObject) noexcept
This is only executed once per module.
Definition: ModuleLoader.cpp:508
Result LoadModule(const Script::Request &rq, const VfsPath &modulePath)
Load the specified module and all module it imports recursively.
Definition: ModuleLoader.cpp:498
~ModuleLoader()
Definition: ModuleLoader.cpp:493
std::unordered_map< VfsPath, CompiledModule > RegistryType
Definition: ModuleLoader.h:73
ModuleLoader(ModuleLoader &&)=delete
ModuleLoader(const ModuleLoader &)=delete
ModuleLoader & operator=(ModuleLoader &&)=delete
ModuleLoader & operator=(const ModuleLoader &)=delete
ModuleLoader(AllowModuleFunc allowModule)
Definition: ModuleLoader.cpp:487
Definition: Request.h:51
Definition: CGUI.h:63
bool operator!=(const SStencilOpState &lhs, const SStencilOpState &rhs)
Definition: DeviceCommandContext.cpp:65
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
Definition: ModuleLoader.h:117
JS::PersistentRootedObject reject
Definition: ModuleLoader.h:120
JS::PersistentRootedObject fulfill
Definition: ModuleLoader.h:119
JS::PersistentRootedObject moduleNamespace
Definition: ModuleLoader.h:118
Definition: ModuleLoader.h:123
JS::PersistentRootedObject moduleNamespace
Definition: ModuleLoader.h:124
Definition: ModuleLoader.h:131
Definition: ModuleLoader.h:127
std::exception_ptr error
Definition: ModuleLoader.h:128