Skip to content
This repository was archived by the owner on Dec 29, 2024. It is now read-only.

Commit 7d1a811

Browse files
authored
Fix crashes with pushVariant (#138)
1 parent 04b607c commit 7d1a811

2 files changed

Lines changed: 20 additions & 36 deletions

File tree

src/luaState.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,10 @@ LuaError *LuaState::pushVariant(lua_State *state, Variant var) {
370370

371371
// If the type being pushed is a tuple, push its content instead.
372372
#ifndef LAPI_GDEXTENSION
373-
if (LuaTuple *tuple = Object::cast_to<LuaTuple>(var.operator Object *()); tuple != nullptr) {
373+
if (Ref<LuaTuple> tuple = Object::cast_to<LuaTuple>(var.operator Object *()); tuple.is_valid()) {
374374
#else
375375
// blame this on https://github.com/godotengine/godot-cpp/issues/995
376-
if (LuaTuple *tuple = dynamic_cast<LuaTuple *>(var.operator Object *()); tuple != nullptr) {
376+
if (Ref<LuaTuple> tuple = dynamic_cast<LuaTuple *>(var.operator Object *()); tuple.is_valid()) {
377377
#endif
378378
for (int i = 0; i < tuple->size(); i++) {
379379
Variant value = tuple->get(i);
@@ -384,53 +384,54 @@ LuaError *LuaState::pushVariant(lua_State *state, Variant var) {
384384

385385
// If the type being pushed is a thread, push a LUA_TTHREAD state.
386386
#ifndef LAPI_GDEXTENSION
387-
if (LuaCoroutine *thread = Object::cast_to<LuaCoroutine>(var.operator Object *()); thread != nullptr) {
387+
if (Ref<LuaCoroutine> thread = Object::cast_to<LuaCoroutine>(var.operator Object *()); thread.is_valid()) {
388388
#else
389389
// blame this on https://github.com/godotengine/godot-cpp/issues/995
390-
if (LuaCoroutine *thread = dynamic_cast<LuaCoroutine *>(var.operator Object *()); thread != nullptr) {
390+
if (Ref<LuaCoroutine> thread = dynamic_cast<LuaCoroutine *>(var.operator Object *()); thread.is_valid()) {
391391
#endif
392392
return LuaError::newError("pushing threads is currently not supported.", LuaError::ERR_TYPE);
393393
break;
394394
}
395395

396-
#ifdef LAPI_GDEXTENSION
397396
// If the type being pushed is a RefCounted, increase its refcount.
398-
if (RefCounted *ref = Object::cast_to<RefCounted>(var.operator Object *()); ref != nullptr) {
397+
if (Ref<RefCounted> ref = Object::cast_to<RefCounted>(var.operator Object *()); ref.is_valid()) {
399398
ref->reference();
400399
}
401-
#endif
402400

403401
// If the type being pushed is a LuaCallableExtra. use mt_CallableExtra instead
404402
#ifndef LAPI_GDEXTENSION
405-
if (LuaCallableExtra *func = Object::cast_to<LuaCallableExtra>(var.operator Object *()); func != nullptr) {
403+
if (Ref<LuaCallableExtra> func = Object::cast_to<LuaCallableExtra>(var.operator Object *()); func.is_valid()) {
406404
#else
407405
// blame this on https://github.com/godotengine/godot-cpp/issues/995
408-
if (LuaCallableExtra *func = dynamic_cast<LuaCallableExtra *>(var.operator Object *()); func != nullptr) {
406+
if (Ref<LuaCallableExtra> func = dynamic_cast<LuaCallableExtra *>(var.operator Object *()); func.is_valid()) {
409407
#endif
410408
Variant *userdata = (Variant *)lua_newuserdata(state, sizeof(Variant));
411-
#ifndef LAPI_GDEXTENSION
412-
*userdata = var;
413-
#else
414409
memmove(userdata, (void *)&var, sizeof(Variant));
415-
#endif
416410
luaL_setmetatable(state, "mt_CallableExtra");
417411
break;
418412
}
419413

420414
Variant *userdata = (Variant *)lua_newuserdata(state, sizeof(Variant));
421-
#ifndef LAPI_GDEXTENSION
422-
*userdata = var;
423-
#else
424415
memmove(userdata, (void *)&var, sizeof(Variant));
425-
#endif
426416
luaL_setmetatable(state, "mt_Object");
427417
break;
428418
}
429419
case Variant::Type::CALLABLE: {
430420
Callable callable = var.operator Callable();
421+
422+
if (!callable.is_valid() || callable.is_null()) {
423+
lua_pushnil(state);
424+
break;
425+
}
426+
431427
if (callable.is_custom()) {
432-
// If the type being pushed is a lua function ref, push the ref instead.
433-
if (LuaAPI *callObj = Object::cast_to<LuaAPI>(callable.get_object()); callObj != nullptr && (String)callable.get_method() == "call_function_ref") {
428+
// If the type being pushed is a lua function ref, push the ref instead.
429+
#ifndef LAPI_GDEXTENSION
430+
LuaAPI *callObj = Object::cast_to<LuaAPI>(callable.get_object());
431+
#else
432+
LuaAPI *callObj = dynamic_cast<LuaAPI *>(callable.get_object());
433+
#endif
434+
if (callObj != nullptr && (String)callable.get_method() == "call_function_ref") {
434435
Array argBinds = callable.get_bound_arguments();
435436
if (argBinds.size() == 1) {
436437
lua_rawgeti(state, LUA_REGISTRYINDEX, (int)argBinds[0]);
@@ -446,11 +447,7 @@ LuaError *LuaState::pushVariant(lua_State *state, Variant var) {
446447
}
447448

448449
Variant *userdata = (Variant *)lua_newuserdata(state, sizeof(Variant));
449-
#ifndef LAPI_GDEXTENSION
450-
*userdata = var;
451-
#else
452450
memmove(userdata, (void *)&var, sizeof(Variant));
453-
#endif
454451
luaL_setmetatable(state, "mt_Callable");
455452
break;
456453
}

src/metatables.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,6 @@ void LuaState::createObjectMetatable() {
520520
return 1;
521521
});
522522

523-
#ifndef LAPI_GDEXTENSION
524-
LUA_METAMETHOD_TEMPLATE(L, -1, "__gc", {
525-
if (!arg1.has_method("__gc")) {
526-
return 0;
527-
}
528-
529-
LuaState::pushVariant(inner_state, arg1.call("__gc", api));
530-
return 1;
531-
});
532-
#else
533523
LUA_METAMETHOD_TEMPLATE(L, -1, "__gc", {
534524
// If object is a RefCounted
535525
Ref<RefCounted> ref = Object::cast_to<RefCounted>(arg1);
@@ -544,7 +534,6 @@ void LuaState::createObjectMetatable() {
544534
LuaState::pushVariant(inner_state, arg1.call("__gc", api));
545535
return 1;
546536
});
547-
#endif
548537

549538
LUA_METAMETHOD_TEMPLATE(L, -1, "__tostring", {
550539
// If object overrides
@@ -774,7 +763,6 @@ void LuaState::createCallableMetatable() {
774763
void LuaState::createCallableExtraMetatable() {
775764
luaL_newmetatable(L, "mt_CallableExtra");
776765

777-
#ifdef LAPI_GDEXTENSION
778766
LUA_METAMETHOD_TEMPLATE(L, -1, "__gc", {
779767
Ref<RefCounted> ref = Object::cast_to<RefCounted>(arg1);
780768
if (ref != nullptr) {
@@ -783,7 +771,6 @@ void LuaState::createCallableExtraMetatable() {
783771

784772
return 0;
785773
});
786-
#endif
787774

788775
lua_pushstring(L, "__call");
789776
lua_pushcfunction(L, LuaCallableExtra::call);

0 commit comments

Comments
 (0)