Skip to content

Commit 3cb753b

Browse files
authored
Merge pull request #103 from gilzoide/bugfix/reload-gdextension
Use Ref<LuaFunction> instead of sol::protected_function in Lua methods
2 parents 1146922 + 76ab44b commit 3cb753b

13 files changed

+42
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
### Fixed
2121
- Use `xcframework` instead of `dylib` in iOS exports
2222
- Crash when Lua errors, but the error object is not a string
23+
- Crash when reloading the GDExtension
2324

2425

2526
## [0.3.0](https://github.com/gilzoide/lua-gdextension/releases/tag/0.3.0)

src/LuaCoroutine.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ sol::protected_function_result LuaCoroutine::_resume(lua_State *L, const Variant
8080
return function_result;
8181
}
8282

83+
Variant LuaCoroutine::invoke_lua(Ref<LuaFunction> f, const VariantArguments& args, bool return_lua_error) {
84+
return invoke_lua(f->get_function(), args, return_lua_error);
85+
}
86+
8387
Variant LuaCoroutine::invoke_lua(const sol::protected_function& f, const VariantArguments& args, bool return_lua_error) {
8488
LuaCoroutinePool pool(f.lua_state());
8589
sol::thread coroutine = pool.acquire(f);

src/LuaCoroutine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class LuaCoroutine : public LuaThread {
4646
Variant resumev(const Array& args);
4747
Variant resume(const Variant **argv, GDExtensionInt argc, GDExtensionCallError& error);
4848

49+
static Variant invoke_lua(Ref<LuaFunction> f, const VariantArguments& args, bool return_lua_error);
4950
static Variant invoke_lua(const sol::protected_function& f, const VariantArguments& args, bool return_lua_error);
5051

5152
protected:

src/LuaFunction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Variant LuaFunction::invoke(const Variant **args, GDExtensionInt arg_count, GDEx
4949
return invoke_lua(lua_object, VariantArguments(args, arg_count), true);
5050
}
5151

52+
Variant LuaFunction::invoke_lua(Ref<LuaFunction> f, const VariantArguments& args, bool return_lua_error) {
53+
return invoke_lua(f->get_function(), args, return_lua_error);
54+
}
55+
5256
Variant LuaFunction::invoke_lua(const sol::protected_function& f, const VariantArguments& args, bool return_lua_error) {
5357
sol::protected_function_result result = f.call(args);
5458
return to_variant(result, return_lua_error);

src/LuaFunction.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class LuaFunction : public LuaObjectSubclass<sol::protected_function> {
4242
Variant invokev(const Array& args);
4343
Variant invoke(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error);
4444

45+
static Variant invoke_lua(Ref<LuaFunction> f, const VariantArguments& args, bool return_lua_error);
4546
static Variant invoke_lua(const sol::protected_function& f, const VariantArguments& args, bool return_lua_error);
4647

4748
Callable to_callable() const;

src/LuaObject.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LuaObject : public RefCounted {
4141
uint64_t get_pointer_value() const;
4242

4343
template<typename Subclass, typename ref_t>
44-
static Subclass *wrap_object(const sol::basic_object<ref_t>& lua_obj) {
44+
static Ref<Subclass> wrap_object(const sol::basic_object<ref_t>& lua_obj) {
4545
if (LuaObject **known_obj = known_objects.getptr(lua_obj.pointer())) {
4646
return (Subclass *) *known_obj;
4747
}

src/LuaState.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ Variant LuaState::do_file(const String& filename, LoadMode mode, LuaTable *env)
189189
return ::luagdextension::do_file(lua_state, filename, (sol::load_mode) mode, env);
190190
}
191191

192-
LuaTable *LuaState::get_globals() const {
192+
Ref<LuaTable> LuaState::get_globals() const {
193193
return LuaObject::wrap_object<LuaTable>(lua_state.globals());
194194
}
195195

196-
LuaTable *LuaState::get_registry() const {
196+
Ref<LuaTable> LuaState::get_registry() const {
197197
return LuaObject::wrap_object<LuaTable>(lua_state.registry());
198198
}
199199

200-
LuaThread *LuaState::get_main_thread() const {
200+
Ref<LuaThread> LuaState::get_main_thread() const {
201201
return LuaObject::wrap_object<LuaThread>(sol::thread(lua_state, lua_state));
202202
}
203203

src/LuaState.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ class LuaState : public RefCounted {
114114
Variant do_string(const String& chunk, const String& chunkname = "", LuaTable *env = nullptr);
115115
Variant do_file(const String& filename, LoadMode mode = LOAD_MODE_ANY, LuaTable *env = nullptr);
116116

117-
LuaTable *get_globals() const;
118-
LuaTable *get_registry() const;
119-
LuaThread *get_main_thread() const;
117+
Ref<LuaTable> get_globals() const;
118+
Ref<LuaTable> get_registry() const;
119+
Ref<LuaThread> get_main_thread() const;
120120

121121
String get_package_path() const;
122122
String get_package_cpath() const;

src/LuaThread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void LuaThread::set_hook(Variant hook, BitField<HookMask> mask, int count) {
9292
lua_push(L, hook); // value (hook)
9393
lua_rawset(L, -3); // hooktable[L] = hook
9494
lua_pop(L, 1);
95-
if (hook) {
95+
if (hook && mask) {
9696
lua_sethook(L, hookf, mask, count);
9797
}
9898
else {

src/script-language/LuaScriptMethod.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,32 @@
2121
*/
2222
#include "LuaScriptMethod.hpp"
2323

24+
#include "../LuaDebug.hpp"
2425
#include "../utils/stack_top_checker.hpp"
2526

2627
namespace luagdextension {
2728

2829
LuaScriptMethod::LuaScriptMethod(const StringName& name, sol::protected_function method)
2930
: name(name)
30-
, method(method)
31+
, method(LuaObject::wrap_object<LuaFunction>(method))
3132
{
3233
}
3334

3435
bool LuaScriptMethod::is_valid() const {
35-
return method.valid();
36+
return method.is_valid();
3637
}
3738

3839
int LuaScriptMethod::get_line_defined() const {
3940
#ifdef DEBUG_ENABLED
40-
lua_Debug lua_info;
41-
method.push();
42-
lua_getinfo(method.lua_state(), ">S", &lua_info);
43-
return lua_info.linedefined;
41+
return method->get_debug_info()->get_line_defined();
4442
#else
4543
return -1;
4644
#endif
4745
}
4846

4947
Variant LuaScriptMethod::get_argument_count() const {
5048
#ifdef DEBUG_ENABLED
51-
lua_Debug lua_info;
52-
method.push();
53-
lua_getinfo(method.lua_state(), ">u", &lua_info);
54-
return lua_info.nparams;
49+
return method->get_debug_info()->get_nparams();
5550
#else
5651
return {};
5752
#endif
@@ -62,22 +57,19 @@ MethodInfo LuaScriptMethod::to_method_info() const {
6257
mi.name = name;
6358

6459
#ifdef DEBUG_ENABLED
65-
sol::state_view state = method.lua_state();
60+
sol::state_view state = method->get_function().lua_state();
6661
StackTopChecker topcheck(state);
6762

68-
lua_Debug lua_info;
69-
method.push(state);
70-
lua_getinfo(state, ">u", &lua_info);
71-
72-
auto methodpop = sol::stack::push_pop(state, method);
73-
for (int i = 0; i < lua_info.nparams; i++) {
63+
auto debug_info = method->get_debug_info();
64+
auto methodpop = sol::stack::push_pop(state, method->get_function());
65+
for (int i = 0; i < debug_info->get_nparams(); i++) {
7466
String arg_name = lua_getlocal(state, nullptr, i + 1);
7567
if (i == 0 && arg_name == "self") {
7668
continue;
7769
}
7870
mi.arguments.push_back(PropertyInfo(Variant::Type::NIL, arg_name));
7971
}
80-
if (lua_info.isvararg) {
72+
if (debug_info->is_vararg()) {
8173
mi.flags |= GDEXTENSION_METHOD_FLAG_VARARG;
8274
}
8375
#endif

0 commit comments

Comments
 (0)