diff options
Diffstat (limited to 'src/universe.cpp')
-rw-r--r-- | src/universe.cpp | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/universe.cpp b/src/universe.cpp index 77f799b..55c3e69 100644 --- a/src/universe.cpp +++ b/src/universe.cpp | |||
@@ -40,6 +40,11 @@ THE SOFTWARE. | |||
40 | 40 | ||
41 | // ################################################################################################# | 41 | // ################################################################################################# |
42 | 42 | ||
43 | // xxh64 of string "kUniverseFullRegKey" generated at https://www.pelock.com/products/hash-calculator | ||
44 | static constexpr RegistryUniqueKey kUniverseFullRegKey{ 0x1C2D76870DD9DD9Full }; | ||
45 | |||
46 | // ################################################################################################# | ||
47 | |||
43 | Universe::Universe() | 48 | Universe::Universe() |
44 | { | 49 | { |
45 | //--- | 50 | //--- |
@@ -69,12 +74,12 @@ Universe::Universe() | |||
69 | // ################################################################################################# | 74 | // ################################################################################################# |
70 | 75 | ||
71 | // only called from the master state | 76 | // only called from the master state |
72 | [[nodiscard]] Universe* universe_create(lua_State* L_) | 77 | [[nodiscard]] Universe* Universe::Create(lua_State* const L_) |
73 | { | 78 | { |
74 | LUA_ASSERT(L_, universe_get(L_) == nullptr); | 79 | LUA_ASSERT(L_, Universe::Get(L_) == nullptr); |
75 | Universe* const _U{ lua_newuserdatauv<Universe>(L_, 0) }; // universe | 80 | STACK_CHECK_START_REL(L_, 0); |
76 | _U->Universe::Universe(); | 81 | Universe* const _U{ new (L_) Universe{} }; // L_: universe |
77 | STACK_CHECK_START_REL(L_, 1); | 82 | STACK_CHECK(L_, 1); |
78 | kUniverseFullRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); | 83 | kUniverseFullRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); |
79 | kUniverseLightRegKey.setValue(L_, [U = _U](lua_State* L_) { lua_pushlightuserdata(L_, U); }); | 84 | kUniverseLightRegKey.setValue(L_, [U = _U](lua_State* L_) { lua_pushlightuserdata(L_, U); }); |
80 | STACK_CHECK(L_, 1); | 85 | STACK_CHECK(L_, 1); |
@@ -86,7 +91,7 @@ Universe::Universe() | |||
86 | // ################################################################################################# | 91 | // ################################################################################################# |
87 | 92 | ||
88 | // same as PUC-Lua l_alloc | 93 | // same as PUC-Lua l_alloc |
89 | extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud_, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) | 94 | [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud_, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) |
90 | { | 95 | { |
91 | if (nsize_ == 0) { | 96 | if (nsize_ == 0) { |
92 | free(ptr_); | 97 | free(ptr_); |
@@ -98,11 +103,11 @@ extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud_, | |||
98 | 103 | ||
99 | // ################################################################################################# | 104 | // ################################################################################################# |
100 | 105 | ||
101 | [[nodiscard]] static int luaG_provide_protected_allocator(lua_State* L_) | 106 | [[nodiscard]] static int luaG_provide_protected_allocator(lua_State* const L_) |
102 | { | 107 | { |
103 | Universe* const _U{ universe_get(L_) }; | 108 | Universe* const _U{ Universe::Get(L_) }; |
104 | // push a new full userdata on the stack, giving access to the universe's protected allocator | 109 | // push a new full userdata on the stack, giving access to the universe's protected allocator |
105 | [[maybe_unused]] AllocatorDefinition* const def{ new (L_) AllocatorDefinition{ _U->protectedAllocator.makeDefinition() } }; | 110 | [[maybe_unused]] AllocatorDefinition* const _def{ new (L_) AllocatorDefinition{ _U->protectedAllocator.makeDefinition() } }; |
106 | return 1; | 111 | return 1; |
107 | } | 112 | } |
108 | 113 | ||
@@ -149,7 +154,7 @@ void Universe::closeKeepers() | |||
149 | 154 | ||
150 | // called once at the creation of the universe (therefore L_ is the master Lua state everything originates from) | 155 | // called once at the creation of the universe (therefore L_ is the master Lua state everything originates from) |
151 | // Do I need to disable this when compiling for LuaJIT to prevent issues? | 156 | // Do I need to disable this when compiling for LuaJIT to prevent issues? |
152 | void Universe::initializeAllocatorFunction(lua_State* L_) | 157 | void Universe::initializeAllocatorFunction(lua_State* const L_) |
153 | { | 158 | { |
154 | STACK_CHECK_START_REL(L_, 1); // L_: settings | 159 | STACK_CHECK_START_REL(L_, 1); // L_: settings |
155 | if (luaG_getfield(L_, -1, "allocator") != LuaType::NIL) { // L_: settings allocator|nil|"protected" | 160 | if (luaG_getfield(L_, -1, "allocator") != LuaType::NIL) { // L_: settings allocator|nil|"protected" |
@@ -198,7 +203,7 @@ void Universe::initializeAllocatorFunction(lua_State* L_) | |||
198 | // ################################################################################################# | 203 | // ################################################################################################# |
199 | 204 | ||
200 | // should be called ONLY from the state that created the universe | 205 | // should be called ONLY from the state that created the universe |
201 | int Universe::InitializeFinalizer(lua_State* L_) | 206 | int Universe::InitializeFinalizer(lua_State* const L_) |
202 | { | 207 | { |
203 | luaL_argcheck(L_, lua_gettop(L_) <= 1, 1, "too many arguments"); // L_: f? | 208 | luaL_argcheck(L_, lua_gettop(L_) <= 1, 1, "too many arguments"); // L_: f? |
204 | lua_settop(L_, 1); // L_: f|nil | 209 | lua_settop(L_, 1); // L_: f|nil |
@@ -230,7 +235,7 @@ int Universe::InitializeFinalizer(lua_State* L_) | |||
230 | * function never fails. | 235 | * function never fails. |
231 | * settings table is expected at position 1 on the stack | 236 | * settings table is expected at position 1 on the stack |
232 | */ | 237 | */ |
233 | void Universe::initializeKeepers(lua_State* L_) | 238 | void Universe::initializeKeepers(lua_State* const L_) |
234 | { | 239 | { |
235 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | 240 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); |
236 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 241 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
@@ -258,8 +263,9 @@ void Universe::initializeKeepers(lua_State* L_) | |||
258 | keepers->gc_threshold = keepers_gc_threshold; | 263 | keepers->gc_threshold = keepers_gc_threshold; |
259 | keepers->nb_keepers = _nb_keepers; | 264 | keepers->nb_keepers = _nb_keepers; |
260 | 265 | ||
261 | for (int const _i : std::ranges::iota_view{ 0, _nb_keepers }) { | 266 | // we have to manually call the Keeper constructor on the additional array slots |
262 | keepers->keeper_array[_i].Keeper::Keeper(); | 267 | for (int const _i : std::ranges::iota_view{ 1, _nb_keepers }) { |
268 | new (&keepers->keeper_array[_i]) Keeper{}; // placement new | ||
263 | } | 269 | } |
264 | } | 270 | } |
265 | 271 | ||
@@ -279,7 +285,7 @@ void Universe::initializeKeepers(lua_State* L_) | |||
279 | STACK_CHECK_START_ABS(_K, 0); | 285 | STACK_CHECK_START_ABS(_K, 0); |
280 | 286 | ||
281 | // copy the universe pointer in the keeper itself | 287 | // copy the universe pointer in the keeper itself |
282 | universe_store(_K, this); | 288 | Universe::Store(_K, this); |
283 | STACK_CHECK(_K, 0); | 289 | STACK_CHECK(_K, 0); |
284 | 290 | ||
285 | // make sure 'package' is initialized in keeper states, so that we have require() | 291 | // make sure 'package' is initialized in keeper states, so that we have require() |
@@ -325,7 +331,7 @@ void Universe::initializeKeepers(lua_State* L_) | |||
325 | 331 | ||
326 | // ################################################################################################# | 332 | // ################################################################################################# |
327 | 333 | ||
328 | void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTimeout_, CancelOp op_) | 334 | void Universe::terminateFreeRunningLanes(lua_State* const L_, lua_Duration const shutdownTimeout_, CancelOp const op_) |
329 | { | 335 | { |
330 | if (selfdestructFirst != SELFDESTRUCT_END) { | 336 | if (selfdestructFirst != SELFDESTRUCT_END) { |
331 | // Signal _all_ still running threads to exit (including the timer thread) | 337 | // Signal _all_ still running threads to exit (including the timer thread) |
@@ -391,7 +397,7 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim | |||
391 | // ################################################################################################# | 397 | // ################################################################################################# |
392 | 398 | ||
393 | // process end: cancel any still free-running threads | 399 | // process end: cancel any still free-running threads |
394 | int universe_gc(lua_State* L_) | 400 | LUAG_FUNC(universe_gc) |
395 | { | 401 | { |
396 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; | 402 | lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; |
397 | std::string_view const _op_string{ lua_tostringview(L_, lua_upvalueindex(2)) }; | 403 | std::string_view const _op_string{ lua_tostringview(L_, lua_upvalueindex(2)) }; |