Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog
## [Unreleased](https://github.com/gilzoide/lua-gdextension/compare/0.7.0...HEAD)
### Changed
- Opening `GODOT_CLASSES` now registers all classes at once instead of setting up a lazy getter in `_G`'s metatable
- Opening `GODOT_SINGLETONS` now registers all singletons at once instead of setting up a lazy getter in `_G`'s metatable


## [0.7.0](https://github.com/gilzoide/lua-gdextension/releases/tag/0.7.0)
Expand Down
2 changes: 1 addition & 1 deletion doc_classes/LuaState.xml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
[gdscript]
var lua_state = LuaState.new()
lua_state.open_libraries(LuaState.Library.LUA_BASE | LuaState.Library.GODOT_CLASSES)
var vec3 = lua_state.do_string("""
var node = lua_state.do_string("""
-- Create a Node in Lua
local node = Node:new()
return node
Expand Down
8 changes: 3 additions & 5 deletions src/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "LuaTable.hpp"
#include "LuaThread.hpp"
#include "luaopen/godot.hpp"
#include "utils/_G_metatable.hpp"
#include "utils/convert_godot_lua.hpp"
#include "utils/module_names.hpp"

Expand Down Expand Up @@ -75,7 +74,6 @@ LuaState::LuaState()
: lua_state(lua_panic_handler, lua_alloc)
#endif
{
setup_G_metatable(lua_state);
#ifdef HAVE_LUA_WARN
lua_setwarnf(lua_state, lua_warn_handler, this);
#endif
Expand Down Expand Up @@ -153,12 +151,12 @@ void LuaState::open_libraries(BitField<Library> libraries) {
if (libraries.has_flag(GODOT_UTILITY_FUNCTIONS)) {
lua_state.require(module_names::utility_functions, &luaopen_godot_utility_functions, false);
}
if (libraries.has_flag(GODOT_SINGLETONS)) {
lua_state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false);
}
if (libraries.has_flag(GODOT_CLASSES)) {
lua_state.require(module_names::classes, &luaopen_godot_classes, false);
}
if (libraries.has_flag(GODOT_SINGLETONS)) {
lua_state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false);
}
if (libraries.has_flag(GODOT_ENUMS)) {
lua_state.require(module_names::enums, &luaopen_godot_enums, false);
}
Expand Down
19 changes: 7 additions & 12 deletions src/luaopen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,23 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include "../script-language/LuaScriptLanguage.hpp"
#include "../utils/Class.hpp"
#include "../utils/module_names.hpp"
#include "../utils/convert_godot_std.hpp"

#include <godot_cpp/classes/project_settings.hpp>
#include <sol/sol.hpp>
#include <godot_cpp/classes/class_db_singleton.hpp>

using namespace luagdextension;

extern "C" int luaopen_godot_classes(lua_State *L) {
sol::state_view state = L;

state.registry()[module_names::classes] = true;
sol::table global_class_paths = state.registry().create_named("_GDEXTENSION_GLOBAL_CLASS_PATHS");
Class::register_usertype(state);

TypedArray<Dictionary> global_class_list = ProjectSettings::get_singleton()->get_global_class_list();
for (int64_t i = 0; i < global_class_list.size(); ++i) {
Dictionary type_info = global_class_list[i];
global_class_paths[to_std_string(type_info["class"])] = to_std_string(type_info["path"]);
ClassDBSingleton *classdb = ClassDBSingleton::get_singleton();
for (auto&& class_name : classdb->get_class_list()) {
state.set(class_name.ascii().get_data(), Class(class_name));
}

LuaScriptLanguage::get_singleton()->register_global_classes(L);

return 0;
}
2 changes: 1 addition & 1 deletion src/luaopen/godot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ extern "C" int luaopen_godot(lua_State *L) {

state.require(module_names::variant, &luaopen_godot_variant, false);
state.require(module_names::utility_functions, &luaopen_godot_utility_functions, false);
state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false);
state.require(module_names::classes, &luaopen_godot_classes, false);
state.require(module_names::singleton_access, &luaopen_godot_singleton_access, false);
state.require(module_names::enums, &luaopen_godot_enums, false);
state.require(module_names::local_paths, &luaopen_godot_local_paths, false);

Expand Down
14 changes: 9 additions & 5 deletions src/luaopen/singleton_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "../script-language/LuaScriptLanguage.hpp"

#include "../utils/module_names.hpp"

#include <sol/sol.hpp>
#include <godot_cpp/classes/engine.hpp>

using namespace godot;
using namespace luagdextension;

extern "C" int luaopen_godot_singleton_access(lua_State *L) {
sol::state_view state = L;
sol::state_view state(L);
Engine *engine = Engine::get_singleton();
for (auto&& singleton_name : engine->get_singleton_list()) {
state.set(singleton_name.ascii().get_data(), engine->get_singleton(singleton_name));
}

state.registry()[module_names::singleton_access] = true;
LuaScriptLanguage::get_singleton()->register_named_globals(L);

return 0;
}
31 changes: 29 additions & 2 deletions src/script-language/LuaScriptLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "../LuaTable.hpp"
#include "../LuaState.hpp"
#include "../generated/lua_script_globals.h"
#include "../utils/convert_godot_lua.hpp"
#include "../utils/project_settings.hpp"
#include "../utils/stack_top_resetter.hpp"

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/project_settings.hpp>
Expand Down Expand Up @@ -67,6 +69,10 @@ void LuaScriptLanguage::_init() {

// Additional globals defined in Lua code
lua_state->do_string(lua_script_globals);

// Now, after everything is setup, we can load global classes (which might be LuaScripts)
global_class_list = project_settings->get_global_class_list();
register_global_classes(lua_state->get_lua_state());
}

String LuaScriptLanguage::_get_type() const {
Expand Down Expand Up @@ -421,8 +427,29 @@ PackedStringArray LuaScriptLanguage::get_lua_member_keywords() const {
);
}

const Dictionary& LuaScriptLanguage::get_named_globals() const {
return named_globals;
void LuaScriptLanguage::register_named_globals(lua_State *L) const {
StackTopResetter resettop(L);
lua_pushglobaltable(L);
Array keys = named_globals.keys();
for (int64_t i = 0, count = keys.size(); i < count; ++i) {
Variant key = keys[i];
lua_push(L, key);
lua_push(L, named_globals[key]);
lua_rawset(L, -3);
}
}

void LuaScriptLanguage::register_global_classes(lua_State *L) const {
StackTopResetter resettop(L);
ResourceLoader *resource_loader = ResourceLoader::get_singleton();
lua_pushglobaltable(L);
for (int64_t i = 0, count = global_class_list.size(); i < count; ++i) {
Dictionary type_info = global_class_list[i];
Ref<Resource> script = resource_loader->load(type_info["path"]);
lua_push(L, type_info["class"]);
lua_push(L, script);
lua_rawset(L, -3);
}
}

LuaState *LuaScriptLanguage::get_lua_state() {
Expand Down
5 changes: 4 additions & 1 deletion src/script-language/LuaScriptLanguage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class LuaScriptLanguage : public ScriptLanguageExtension {

PackedStringArray get_lua_keywords() const;
PackedStringArray get_lua_member_keywords() const;
const Dictionary& get_named_globals() const;

void register_named_globals(lua_State *L) const;
void register_global_classes(lua_State *L) const;

LuaState *get_lua_state();
LuaParser *get_lua_parser() const;
Expand All @@ -113,6 +115,7 @@ class LuaScriptLanguage : public ScriptLanguageExtension {
Ref<LuaState> lua_state;
Ref<LuaParser> lua_parser;
Dictionary named_globals;
TypedArray<Dictionary> global_class_list;

private:
static LuaScriptLanguage *instance;
Expand Down
15 changes: 8 additions & 7 deletions src/script-language/LuaScriptMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,32 @@
#include "LuaScriptMethod.hpp"

#include "../LuaDebug.hpp"
#include "../LuaFunction.hpp"
#include "../utils/stack_top_checker.hpp"

namespace luagdextension {

LuaScriptMethod::LuaScriptMethod(const StringName& name, sol::protected_function method)
: name(name)
, method(LuaObject::wrap_object<LuaFunction>(method))
, method(method)
{
}

bool LuaScriptMethod::is_valid() const {
return method.is_valid();
return method.valid();
}

int LuaScriptMethod::get_line_defined() const {
#ifdef DEBUG_ENABLED
return method->get_debug_info()->get_line_defined();
return LuaObject::wrap_object<LuaFunction>(method)->get_debug_info()->get_line_defined();
#else
return -1;
#endif
}

Variant LuaScriptMethod::get_argument_count() const {
#if defined(DEBUG_ENABLED) && LUA_VERSION_NUM >= 502
return method->get_debug_info()->get_nparams();
return LuaObject::wrap_object<LuaFunction>(method)->get_debug_info()->get_nparams();
#else
return {};
#endif
Expand All @@ -57,11 +58,11 @@ MethodInfo LuaScriptMethod::to_method_info() const {
mi.name = name;

#if defined(DEBUG_ENABLED) && LUA_VERSION_NUM >= 502
sol::state_view state = method->get_function().lua_state();
sol::state_view state = LuaObject::wrap_object<LuaFunction>(method)->get_function().lua_state();
StackTopChecker topcheck(state);

auto debug_info = method->get_debug_info();
auto methodpop = sol::stack::push_pop(state, method->get_function());
auto debug_info = LuaObject::wrap_object<LuaFunction>(method)->get_debug_info();
auto methodpop = sol::stack::push_pop(state, method);
for (int i = 0; i < debug_info->get_nparams(); i++) {
String arg_name = lua_getlocal(state, nullptr, i + 1);
if (i == 0 && arg_name == "self") {
Expand Down
4 changes: 1 addition & 3 deletions src/script-language/LuaScriptMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <sol/sol.hpp>
#include <godot_cpp/core/object.hpp>

#include "../LuaFunction.hpp"

typedef struct lua_State lua_State;

using namespace godot;
Expand All @@ -35,7 +33,7 @@ namespace luagdextension {

struct LuaScriptMethod {
StringName name;
Ref<LuaFunction> method;
sol::protected_function method;

LuaScriptMethod() = default;
LuaScriptMethod(const StringName& name, sol::protected_function method);
Expand Down
77 changes: 0 additions & 77 deletions src/utils/_G_metatable.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions src/utils/_G_metatable.hpp

This file was deleted.

Loading
Loading