diff options
Diffstat (limited to 'src/state.cpp')
-rw-r--r-- | src/state.cpp | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/state.cpp b/src/state.cpp index 7ce8db0..73c94f0 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -238,24 +238,28 @@ void InitializeOnStateCreate(Universe* U_, lua_State* L_) | |||
238 | 238 | ||
239 | lua_State* create_state([[maybe_unused]] Universe* U_, lua_State* from_) | 239 | lua_State* create_state([[maybe_unused]] Universe* U_, lua_State* from_) |
240 | { | 240 | { |
241 | lua_State* _L; | 241 | lua_State* const _L { |
242 | #if LUAJIT_FLAVOR() == 64 | 242 | std::invoke( |
243 | // for some reason, LuaJIT 64 bits does not support creating a state with lua_newstate... | 243 | [U = U_, from = from_]() { |
244 | _L = luaL_newstate(); | 244 | if constexpr (LUAJIT_FLAVOR() == 64) { |
245 | #else // LUAJIT_FLAVOR() == 64 | 245 | // for some reason, LuaJIT 64 bits does not support creating a state with lua_newstate... |
246 | if (U_->provideAllocator != nullptr) { // we have a function we can call to obtain an allocator | 246 | return luaL_newstate(); |
247 | lua_pushcclosure(from_, U_->provideAllocator, 0); | 247 | } else { |
248 | lua_call(from_, 0, 1); | 248 | if (U->provideAllocator != nullptr) { // we have a function we can call to obtain an allocator |
249 | { | 249 | lua_pushcclosure(from, U->provideAllocator, 0); |
250 | AllocatorDefinition* const def{ lua_tofulluserdata<AllocatorDefinition>(from_, -1) }; | 250 | lua_call(from, 0, 1); |
251 | _L = lua_newstate(def->allocF, def->allocUD); | 251 | AllocatorDefinition* const _def{ lua_tofulluserdata<AllocatorDefinition>(from, -1) }; |
252 | } | 252 | lua_State* const _L{ lua_newstate(_def->allocF, _def->allocUD) }; |
253 | lua_pop(from_, 1); | 253 | lua_pop(from, 1); |
254 | } else { | 254 | return _L; |
255 | // reuse the allocator provided when the master state was created | 255 | } else { |
256 | _L = lua_newstate(U_->protectedAllocator.allocF, U_->protectedAllocator.allocUD); | 256 | // reuse the allocator provided when the master state was created |
257 | } | 257 | return lua_newstate(U->protectedAllocator.allocF, U->protectedAllocator.allocUD); |
258 | #endif // LUAJIT_FLAVOR() == 64 | 258 | } |
259 | } | ||
260 | } | ||
261 | ) | ||
262 | }; | ||
259 | 263 | ||
260 | if (_L == nullptr) { | 264 | if (_L == nullptr) { |
261 | raise_luaL_error(from_, "luaG_newstate() failed while creating state; out of memory"); | 265 | raise_luaL_error(from_, "luaG_newstate() failed while creating state; out of memory"); |
@@ -355,20 +359,20 @@ lua_State* luaG_newstate(Universe* U_, SourceState from_, char const* libs_) | |||
355 | open1lib(_L, kLanesCoreLibName); | 359 | open1lib(_L, kLanesCoreLibName); |
356 | libs_ = nullptr; // done with libs | 360 | libs_ = nullptr; // done with libs |
357 | } else { | 361 | } else { |
358 | #if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, always open jit | 362 | if constexpr (LUAJIT_FLAVOR() != 0) { // building against LuaJIT headers, always open jit |
359 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'jit' library" << std::endl); | 363 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'jit' library" << std::endl); |
360 | open1lib(_L, LUA_JITLIBNAME); | 364 | open1lib(_L, LUA_JITLIBNAME); |
361 | #endif // LUAJIT_FLAVOR() | 365 | } |
362 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'base' library" << std::endl); | 366 | DEBUGSPEW_CODE(DebugSpew(U_) << "opening 'base' library" << std::endl); |
363 | #if LUA_VERSION_NUM >= 502 | 367 | if constexpr (LUA_VERSION_NUM >= 502) { |
364 | // open base library the same way as in luaL_openlibs() | 368 | // open base library the same way as in luaL_openlibs() |
365 | luaL_requiref(_L, LUA_GNAME, luaopen_base, 1); | 369 | luaL_requiref(_L, LUA_GNAME, luaopen_base, 1); |
366 | lua_pop(_L, 1); | 370 | lua_pop(_L, 1); |
367 | #else // LUA_VERSION_NUM | 371 | } else { |
368 | lua_pushcfunction(_L, luaopen_base); | 372 | lua_pushcfunction(_L, luaopen_base); |
369 | lua_pushstring(_L, ""); | 373 | lua_pushstring(_L, ""); |
370 | lua_call(_L, 1, 0); | 374 | lua_call(_L, 1, 0); |
371 | #endif // LUA_VERSION_NUM | 375 | } |
372 | } | 376 | } |
373 | } | 377 | } |
374 | STACK_CHECK(_L, 0); | 378 | STACK_CHECK(_L, 0); |