From 115d384f88bdda0b21c513db6e25e7ce90058a60 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 23 Aug 2018 17:02:12 +0200 Subject: [PATCH] improve error proofness --- src/luaengine/luaengine/LuaEngine.cpp | 60 +++++++++++++++------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/src/luaengine/luaengine/LuaEngine.cpp b/src/luaengine/luaengine/LuaEngine.cpp index 27ad235..f005eef 100644 --- a/src/luaengine/luaengine/LuaEngine.cpp +++ b/src/luaengine/luaengine/LuaEngine.cpp @@ -293,7 +293,7 @@ int LuaEngine::luaDeletePointer_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 1) { void *pointer = getPointer(L_p, 1); - if (pointer != NULL && ((QObject*)pointer)->inherits("QObject")) { + if (pointer != NULL) { switch (getVariant(L_p, 2).toInt()) { case 1: @@ -310,19 +310,22 @@ int LuaEngine::luaDeletePointer_p(lua_State *L_p) int LuaEngine::luaTriggerConnect_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 3) { - QObject *object = (QObject*)getPointer(L_p, 1); - QString signalString = getVariant(L_p, 2).toString(); - int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); - if (signalIndex != -1) { - LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); - int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); - if (slotIndex != -1) { - QMetaMethod signal = object->metaObject()->method(signalIndex); - QMetaMethod slot = engine->metaObject()->method(slotIndex); - QString funcStorage; - QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); - pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); - QObject::connect(object, signal, engine, slot); + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + QObject *object = (QObject*)pointer; + QString signalString = getVariant(L_p, 2).toString(); + int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); + if (signalIndex != -1) { + LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); + int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); + if (slotIndex != -1) { + QMetaMethod signal = object->metaObject()->method(signalIndex); + QMetaMethod slot = engine->metaObject()->method(slotIndex); + QString funcStorage; + QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); + pushVariant(L_p, funcStorage.toUtf8().data(), getVariant(L_p, 3).toString()); + QObject::connect(object, signal, engine, slot); + } } } } @@ -332,19 +335,22 @@ int LuaEngine::luaTriggerConnect_p(lua_State *L_p) int LuaEngine::luaTriggerDisconnect_p(lua_State *L_p) { if (getArgumentCount(L_p) >= 2) { - QObject *object = (QObject*)getPointer(L_p, 1); - QString signalString = getVariant(L_p, 2).toString(); - int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); - if (signalIndex != -1) { - LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); - int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); - if (slotIndex != -1) { - QMetaMethod signal = object->metaObject()->method(signalIndex); - QMetaMethod slot = engine->metaObject()->method(slotIndex); - QString funcStorage; - QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); - pushVariant(L_p, funcStorage.toUtf8().data(), QVariant()); - QObject::disconnect(object, signal, engine, slot); + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + QObject *object = (QObject*)pointer; + QString signalString = getVariant(L_p, 2).toString(); + int signalIndex = object->metaObject()->indexOfSignal(signalString.toUtf8().data()); + if (signalIndex != -1) { + LuaEngine *engine = (LuaEngine*)getPointer(L_p, "__LuaEngine"); + int slotIndex = engine->metaObject()->indexOfSlot("luaTriggerSlot_p()"); + if (slotIndex != -1) { + QMetaMethod signal = object->metaObject()->method(signalIndex); + QMetaMethod slot = engine->metaObject()->method(slotIndex); + QString funcStorage; + QTextStream(&funcStorage) << "__ConnectFunc_" << object << "_" << signal.name(); + pushVariant(L_p, funcStorage.toUtf8().data(), QVariant()); + QObject::disconnect(object, signal, engine, slot); + } } } }