diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-26 11:01:01 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-26 11:01:01 +0200 |
commit | 57176e9b135b037fd33b8291d12416b9485b759b (patch) | |
tree | d716ce09d572f8ebf7272117928c7b6072dd7aff /src | |
parent | 2c6898964400e6fa77b0e368f477b739fc155f90 (diff) | |
download | lanes-57176e9b135b037fd33b8291d12416b9485b759b.tar.gz lanes-57176e9b135b037fd33b8291d12416b9485b759b.tar.bz2 lanes-57176e9b135b037fd33b8291d12416b9485b759b.zip |
Make lanes.gen stricter on base libraries
Diffstat (limited to 'src')
-rw-r--r-- | src/lanes.cpp | 1 | ||||
-rw-r--r-- | src/lanes.lua | 47 | ||||
-rw-r--r-- | src/state.cpp | 36 | ||||
-rw-r--r-- | src/state.h | 1 | ||||
-rw-r--r-- | src/universe.cpp | 2 |
5 files changed, 60 insertions, 27 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index cab5944..39723ca 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -636,6 +636,7 @@ namespace { | |||
636 | { "set_thread_priority", LG_set_thread_priority }, | 636 | { "set_thread_priority", LG_set_thread_priority }, |
637 | { "set_thread_affinity", LG_set_thread_affinity }, | 637 | { "set_thread_affinity", LG_set_thread_affinity }, |
638 | { "sleep", LG_sleep }, | 638 | { "sleep", LG_sleep }, |
639 | { "supported_libs", state::LG_supported_libs }, | ||
639 | { "wakeup_conv", LG_wakeup_conv }, | 640 | { "wakeup_conv", LG_wakeup_conv }, |
640 | { nullptr, nullptr } | 641 | { nullptr, nullptr } |
641 | }; | 642 | }; |
diff --git a/src/lanes.lua b/src/lanes.lua index 90f9cd2..6a4f149 100644 --- a/src/lanes.lua +++ b/src/lanes.lua | |||
@@ -222,23 +222,25 @@ end | |||
222 | 222 | ||
223 | local valid_libs = | 223 | local valid_libs = |
224 | { | 224 | { |
225 | ["package"] = true, | 225 | ["base"] = true, |
226 | ["table"] = true, | 226 | ["bit"] = true, |
227 | ["bit32"] = true, | ||
228 | ["coroutine"] = true, | ||
229 | ["debug"] = true, | ||
230 | ["ffi"] = true, | ||
227 | ["io"] = true, | 231 | ["io"] = true, |
232 | ["jit"] = true, | ||
233 | ["math"] = true, | ||
228 | ["os"] = true, | 234 | ["os"] = true, |
235 | ["package"] = true, | ||
229 | ["string"] = true, | 236 | ["string"] = true, |
230 | ["math"] = true, | 237 | ["table"] = true, |
231 | ["debug"] = true, | 238 | ["utf8"] = true, |
232 | ["bit32"] = true, -- Lua 5.2 only, ignored silently under 5.1 | ||
233 | ["utf8"] = true, -- Lua 5.3 only, ignored silently under 5.1 and 5.2 | ||
234 | ["bit"] = true, -- LuaJIT only, ignored silently under PUC-Lua | ||
235 | ["jit"] = true, -- LuaJIT only, ignored silently under PUC-Lua | ||
236 | ["ffi"] = true, -- LuaJIT only, ignored silently under PUC-Lua | ||
237 | -- | 239 | -- |
238 | ["base"] = true, | ||
239 | ["coroutine"] = true, -- part of "base" in Lua 5.1 | ||
240 | ["lanes.core"] = true | 240 | ["lanes.core"] = true |
241 | } | 241 | } |
242 | -- same structure, but contains only the libraries that the current Lua flavor actually supports | ||
243 | local supported_libs | ||
242 | 244 | ||
243 | -- ################################################################################################# | 245 | -- ################################################################################################# |
244 | 246 | ||
@@ -375,14 +377,17 @@ local gen = function(...) | |||
375 | error "Libs specification '*' must be used alone" | 377 | error "Libs specification '*' must be used alone" |
376 | end | 378 | end |
377 | local found = {} | 379 | local found = {} |
378 | for s in string_gmatch(libs, "[%a%d.]+") do | 380 | -- accept lib identifiers followed by an optional question mark |
381 | for s, question in string_gmatch(libs, "([%a%d.]+)(%??)") do | ||
379 | if not valid_libs[s] then | 382 | if not valid_libs[s] then |
380 | error("Bad library name: " .. s, 2) | 383 | error("Bad library name: " .. string_format("%q", tostring(s)), 2) |
381 | else | 384 | end |
382 | found[s] = (found[s] or 0) + 1 | 385 | if question == '' and not supported_libs[s] then |
383 | if found[s] > 1 then | 386 | error("Unsupported library: " .. string_format("%q", tostring(s)), 2) |
384 | error("Libs specification contains '" .. s .. "' more than once", 2) | 387 | end |
385 | end | 388 | found[s] = (found[s] or 0) + 1 |
389 | if found[s] > 1 then | ||
390 | error("Libs specification contains " .. string_format("%q", tostring(s)) .. " more than once", 2) | ||
386 | end | 391 | end |
387 | end | 392 | end |
388 | end | 393 | end |
@@ -810,6 +815,11 @@ local configure = function(settings_) | |||
810 | lanes.configure = function() return lanes end -- no need to configure anything again | 815 | lanes.configure = function() return lanes end -- no need to configure anything again |
811 | 816 | ||
812 | -- now we can configure Lanes core | 817 | -- now we can configure Lanes core |
818 | |||
819 | |||
820 | |||
821 | |||
822 | |||
813 | local settings = core.configure and core.configure(params_checker(settings_)) or core.settings | 823 | local settings = core.configure and core.configure(params_checker(settings_)) or core.settings |
814 | 824 | ||
815 | -- | 825 | -- |
@@ -825,6 +835,7 @@ local configure = function(settings_) | |||
825 | -- avoid pulling the whole core module as upvalue when cancel_error is enough | 835 | -- avoid pulling the whole core module as upvalue when cancel_error is enough |
826 | -- these are locals declared above, that we need to set prior to calling configure_timers() | 836 | -- these are locals declared above, that we need to set prior to calling configure_timers() |
827 | cancel_error = assert(core.cancel_error) | 837 | cancel_error = assert(core.cancel_error) |
838 | supported_libs = assert(core.supported_libs()) | ||
828 | timerLinda = assert(core.timerLinda) | 839 | timerLinda = assert(core.timerLinda) |
829 | 840 | ||
830 | if settings.with_timers then | 841 | if settings.with_timers then |
diff --git a/src/state.cpp b/src/state.cpp index 9cd7343..b50d135 100644 --- a/src/state.cpp +++ b/src/state.cpp | |||
@@ -57,32 +57,37 @@ namespace { | |||
57 | 57 | ||
58 | namespace local { | 58 | namespace local { |
59 | static luaL_Reg const sLibs[] = { | 59 | static luaL_Reg const sLibs[] = { |
60 | { "base", nullptr }, // ignore "base" (already acquired it) | 60 | { "base", nullptr }, // ignore "base" is always valid, but opened separately |
61 | |||
62 | #if LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503 | ||
63 | { LUA_BITLIBNAME, luaopen_bit32 }, // active in Lua 5.2, replaced with an error-throwing loader in Lua 5.3, gone after. | ||
64 | #endif // LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503 | ||
65 | |||
61 | #if LUA_VERSION_NUM >= 502 | 66 | #if LUA_VERSION_NUM >= 502 |
62 | #ifdef luaopen_bit32 | ||
63 | { LUA_BITLIBNAME, luaopen_bit32 }, | ||
64 | #endif | ||
65 | { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base! | 67 | { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base! |
66 | #else // LUA_VERSION_NUM | ||
67 | { LUA_COLIBNAME, nullptr }, // Lua 5.1: part of base package | ||
68 | #endif // LUA_VERSION_NUM | 68 | #endif // LUA_VERSION_NUM |
69 | |||
69 | { LUA_DBLIBNAME, luaopen_debug }, | 70 | { LUA_DBLIBNAME, luaopen_debug }, |
71 | |||
70 | #ifndef PLATFORM_XBOX // no os/io libs on xbox | 72 | #ifndef PLATFORM_XBOX // no os/io libs on xbox |
71 | { LUA_IOLIBNAME, luaopen_io }, | 73 | { LUA_IOLIBNAME, luaopen_io }, |
72 | { LUA_OSLIBNAME, luaopen_os }, | 74 | { LUA_OSLIBNAME, luaopen_os }, |
73 | #endif // PLATFORM_XBOX | 75 | #endif // PLATFORM_XBOX |
76 | |||
74 | { LUA_LOADLIBNAME, luaopen_package }, | 77 | { LUA_LOADLIBNAME, luaopen_package }, |
75 | { LUA_MATHLIBNAME, luaopen_math }, | 78 | { LUA_MATHLIBNAME, luaopen_math }, |
76 | { LUA_STRLIBNAME, luaopen_string }, | 79 | { LUA_STRLIBNAME, luaopen_string }, |
77 | { LUA_TABLIBNAME, luaopen_table }, | 80 | { LUA_TABLIBNAME, luaopen_table }, |
81 | |||
78 | #if LUA_VERSION_NUM >= 503 | 82 | #if LUA_VERSION_NUM >= 503 |
79 | { LUA_UTF8LIBNAME, luaopen_utf8 }, | 83 | { LUA_UTF8LIBNAME, luaopen_utf8 }, |
80 | #endif | 84 | #endif // LUA_VERSION_NUM >= 503 |
85 | |||
81 | #if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs | 86 | #if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs |
82 | { LUA_BITLIBNAME, luaopen_bit }, | 87 | { LUA_BITLIBNAME, luaopen_bit }, |
83 | { LUA_FFILIBNAME, luaopen_ffi }, | 88 | { LUA_FFILIBNAME, luaopen_ffi }, |
84 | { LUA_JITLIBNAME, luaopen_jit }, | 89 | { LUA_JITLIBNAME, luaopen_jit }, |
85 | #endif // LUAJIT_FLAVOR() | 90 | #endif // LUAJIT_FLAVOR() != 0 |
86 | 91 | ||
87 | { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function) | 92 | { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function) |
88 | }; | 93 | }; |
@@ -312,6 +317,21 @@ namespace state { | |||
312 | } | 317 | } |
313 | 318 | ||
314 | // ############################################################################################# | 319 | // ############################################################################################# |
320 | |||
321 | // for internal use only: tell lanes.lua which base libraries are actually supported internally | ||
322 | LUAG_FUNC(supported_libs) | ||
323 | { | ||
324 | STACK_CHECK_START_REL(L_, 0); | ||
325 | lua_newtable(L_); // L_: out | ||
326 | for (luaL_Reg const& _entry : local::sLibs) { | ||
327 | lua_pushboolean(L_, 1); // L_: out true | ||
328 | luaG_setfield(L_, -2, std::string_view{ _entry.name }); // out[name] = true // L_: out | ||
329 | } | ||
330 | STACK_CHECK(L_, 1); | ||
331 | return 1; | ||
332 | } | ||
333 | |||
334 | // ############################################################################################# | ||
315 | // ############################################################################################# | 335 | // ############################################################################################# |
316 | } // namespace state | 336 | } // namespace state |
317 | // ################################################################################################# | 337 | // ################################################################################################# |
diff --git a/src/state.h b/src/state.h index f809d50..a6bb43b 100644 --- a/src/state.h +++ b/src/state.h | |||
@@ -10,4 +10,5 @@ class Universe; | |||
10 | namespace state { | 10 | namespace state { |
11 | [[nodiscard]] lua_State* CreateState(Universe* U_, lua_State* from_, std::string_view const& hint_); | 11 | [[nodiscard]] lua_State* CreateState(Universe* U_, lua_State* from_, std::string_view const& hint_); |
12 | [[nodiscard]] lua_State* NewLaneState(Universe* U_, SourceState from_, std::optional<std::string_view> const& libs_); | 12 | [[nodiscard]] lua_State* NewLaneState(Universe* U_, SourceState from_, std::optional<std::string_view> const& libs_); |
13 | LUAG_FUNC(supported_libs); | ||
13 | } // namespace state | 14 | } // namespace state |
diff --git a/src/universe.cpp b/src/universe.cpp index 0dc5646..1cb4fd0 100644 --- a/src/universe.cpp +++ b/src/universe.cpp | |||
@@ -111,7 +111,7 @@ void Universe::callOnStateCreate(lua_State* const L_, lua_State* const from_, Lo | |||
111 | std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; | 111 | std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; |
112 | luaG_pushstring(L_, _stateType); // L_: on_state_create() "<type>" | 112 | luaG_pushstring(L_, _stateType); // L_: on_state_create() "<type>" |
113 | if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { | 113 | if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { |
114 | raise_luaL_error(from_, "%s failed: \"%s\"", kOnStateCreate.data(), lua_isstring(L_, -1) ? luaG_tostring(L_, -1).data() : luaG_typename(L_, -1).data()); | 114 | raise_luaL_error(from_, "%s failed in %s: \"%s\"", kOnStateCreate.data(), _stateType.data(), lua_isstring(L_, -1) ? luaG_tostring(L_, -1).data() : luaG_typename(L_, -1).data()); |
115 | } | 115 | } |
116 | STACK_CHECK(L_, 0); | 116 | STACK_CHECK(L_, 0); |
117 | } | 117 | } |