Skip to content

Commit 7074022

Browse files
committed
Add LuaState.are_libraries_opened
1 parent 1b75d88 commit 7074022

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
When running in the Godot editor, it returns the globalized version of `res://` path.
1313
Otherwise, it returns the base directory of the executable.
1414
- Advanced project settings for setting the `LuaScriptLanguage` state's `package_path` and `package_cpath` properties
15+
- `LuaState.are_libraries_opened` method for checking if a subset of libraries were already opened
1516

1617
### Changed
1718
- The GDExtension is now marked as reloadable

src/LuaState.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ void LuaState::open_libraries(BitField<Library> libraries) {
131131
lua_state.require(module_names::local_paths, &luaopen_godot_local_paths, false);
132132
}
133133
}
134+
135+
lua_state.registry().set("_GDEXTENSION_OPEN_LIBS", lua_state.registry().get_or("_GDEXTENSION_OPEN_LIBS", 0) | libraries);
136+
}
137+
138+
bool LuaState::are_libraries_opened(BitField<Library> libraries) const {
139+
int64_t currently_opened_libs = lua_state.registry().get_or("_GDEXTENSION_OPEN_LIBS", 0L);
140+
return (currently_opened_libs & libraries) == libraries;
134141
}
135142

136143
Ref<LuaTable> LuaState::create_table(const Dictionary& initial_values) {
@@ -247,15 +254,13 @@ void LuaState::_bind_methods() {
247254
BIND_BITFIELD_FLAG(LUA_JIT);
248255
BIND_BITFIELD_FLAG(LUA_UTF8);
249256
BIND_BITFIELD_FLAG(LUA_ALL_LIBS);
250-
251257
BIND_BITFIELD_FLAG(GODOT_VARIANT);
252258
BIND_BITFIELD_FLAG(GODOT_UTILITY_FUNCTIONS);
253259
BIND_BITFIELD_FLAG(GODOT_SINGLETONS);
254260
BIND_BITFIELD_FLAG(GODOT_CLASSES);
255261
BIND_BITFIELD_FLAG(GODOT_ENUMS);
256262
BIND_BITFIELD_FLAG(GODOT_LOCAL_PATHS);
257263
BIND_BITFIELD_FLAG(GODOT_ALL_LIBS);
258-
259264
BIND_BITFIELD_FLAG(ALL_LIBS);
260265

261266
// LoadMode enum
@@ -265,6 +270,7 @@ void LuaState::_bind_methods() {
265270

266271
// Methods
267272
ClassDB::bind_method(D_METHOD("open_libraries", "libraries"), &LuaState::open_libraries, DEFVAL(BitField<Library>(ALL_LIBS)));
273+
ClassDB::bind_method(D_METHOD("are_libraries_opened", "libraries"), &LuaState::are_libraries_opened);
268274
ClassDB::bind_method(D_METHOD("create_table", "initial_values"), &LuaState::create_table, DEFVAL(Dictionary()));
269275
ClassDB::bind_method(D_METHOD("load_buffer", "chunk", "chunkname", "mode", "env"), &LuaState::load_buffer, DEFVAL(""), DEFVAL(LOAD_MODE_ANY), DEFVAL(nullptr));
270276
ClassDB::bind_method(D_METHOD("load_string", "chunk", "chunkname", "env"), &LuaState::load_string, DEFVAL(""), DEFVAL(nullptr));

src/LuaState.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class LuaState : public RefCounted {
101101
sol::state_view get_lua_state() const;
102102

103103
void open_libraries(BitField<Library> libraries = ALL_LIBS);
104+
bool are_libraries_opened(BitField<Library> libraries) const;
104105

105106
Ref<LuaTable> create_table(const Dictionary& initial_values = {});
106107
Variant load_buffer(const PackedByteArray& chunk, const String& chunkname = "", LoadMode mode = LOAD_MODE_ANY, LuaTable *env = nullptr);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extends RefCounted
2+
3+
4+
func test_libraries_opened() -> bool:
5+
var lua_state = LuaState.new()
6+
assert(not lua_state.are_libraries_opened(LuaState.LUA_BASE))
7+
lua_state.open_libraries(LuaState.LUA_BASE | LuaState.LUA_PACKAGE)
8+
assert(lua_state.are_libraries_opened(LuaState.LUA_BASE))
9+
assert(lua_state.are_libraries_opened(LuaState.LUA_PACKAGE))
10+
assert(lua_state.are_libraries_opened(LuaState.LUA_BASE | LuaState.LUA_PACKAGE))
11+
assert(not lua_state.are_libraries_opened(LuaState.LUA_ALL_LIBS))
12+
return true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://u714sanh5wtq

0 commit comments

Comments
 (0)