aboutsummaryrefslogtreecommitdiff
path: root/src/universe.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/universe.cpp36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/universe.cpp b/src/universe.cpp
index c98e2c8..d24a784 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -176,9 +176,23 @@ Universe::Universe()
176// Do I need to disable this when compiling for LuaJIT to prevent issues? 176// Do I need to disable this when compiling for LuaJIT to prevent issues?
177void Universe::initializeAllocatorFunction(lua_State* const L_) 177void Universe::initializeAllocatorFunction(lua_State* const L_)
178{ 178{
179 // start by just grabbing whatever allocator was provided to lua_newstate
180 protectedAllocator.initFrom(L_);
179 STACK_CHECK_START_REL(L_, 1); // L_: settings 181 STACK_CHECK_START_REL(L_, 1); // L_: settings
180 if (luaG_getfield(L_, -1, "allocator") != LuaType::NIL) { // L_: settings allocator|nil|"protected" 182 switch (luaG_getfield(L_, -1, "allocator")) { // L_: settings allocator|nil|"protected"
181 // store C function pointer in an internal variable 183 case LuaType::NIL:
184 // nothing else to do
185 break;
186
187 case LuaType::STRING:
188 LUA_ASSERT(L_, luaG_tostring(L_, -1) == "protected");
189 // set the original allocator to call from inside protection by the mutex
190 protectedAllocator.installIn(L_);
191 // before a state is created, this function will be called to obtain the allocator
192 provideAllocator = luaG_provide_protected_allocator;
193 break;
194
195 case LuaType::FUNCTION:
182 provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator 196 provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator
183 if (provideAllocator != nullptr) { 197 if (provideAllocator != nullptr) {
184 // make sure the function doesn't have upvalues 198 // make sure the function doesn't have upvalues
@@ -190,17 +204,13 @@ void Universe::initializeAllocatorFunction(lua_State* const L_)
190 // when we transfer the config table in newly created Lua states 204 // when we transfer the config table in newly created Lua states
191 lua_pushnil(L_); // L_: settings allocator nil 205 lua_pushnil(L_); // L_: settings allocator nil
192 lua_setfield(L_, -3, "allocator"); // L_: settings allocator 206 lua_setfield(L_, -3, "allocator"); // L_: settings allocator
193 } else if (luaG_type(L_, -1) == LuaType::STRING) { // should be "protected" 207 } else {
194 LUA_ASSERT(L_, strcmp(lua_tostring(L_, -1), "protected") == 0); 208 raise_luaL_error(L_, "Bad config.allocator, must be a C function");
195 // set the original allocator to call from inside protection by the mutex
196 protectedAllocator.initFrom(L_);
197 protectedAllocator.installIn(L_);
198 // before a state is created, this function will be called to obtain the allocator
199 provideAllocator = luaG_provide_protected_allocator;
200 } 209 }
201 } else { 210 break;
202 // just grab whatever allocator was provided to lua_newstate 211
203 protectedAllocator.initFrom(L_); 212 default: // should be filtered out in lanes.lua
213 raise_luaL_error(L_, "Bad config.allocator type %s", luaG_typename(L_, -1).data());
204 } 214 }
205 lua_pop(L_, 1); // L_: settings 215 lua_pop(L_, 1); // L_: settings
206 STACK_CHECK(L_, 1); 216 STACK_CHECK(L_, 1);
@@ -208,7 +218,7 @@ void Universe::initializeAllocatorFunction(lua_State* const L_)
208 std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" 218 std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator"
209 std::string_view const _allocator{ luaG_tostring(L_, -1) }; 219 std::string_view const _allocator{ luaG_tostring(L_, -1) };
210 if (_allocator == "libc") { 220 if (_allocator == "libc") {
211 internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; 221 internalAllocator = AllocatorDefinition{ AllocatorDefinition::kAllocatorVersion, libc_lua_Alloc, nullptr };
212 } else if (provideAllocator == luaG_provide_protected_allocator) { 222 } else if (provideAllocator == luaG_provide_protected_allocator) {
213 // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. 223 // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case.
214 internalAllocator = protectedAllocator.makeDefinition(); 224 internalAllocator = protectedAllocator.makeDefinition();