LuaFunction.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../IO/Log.h"
  5. #include "../IO/VectorBuffer.h"
  6. #include "../LuaScript/LuaFunction.h"
  7. #include "../LuaScript/LuaScript.h"
  8. #include "../LuaScript/LuaScriptInstance.h"
  9. #include <toluapp/tolua++.h>
  10. #include "../LuaScript/ToluaUtils.h"
  11. #include "../DebugNew.h"
  12. namespace Urho3D
  13. {
  14. LuaFunction::LuaFunction(lua_State* L, int index) :
  15. luaState_(L),
  16. numArguments_(-1)
  17. {
  18. assert(L);
  19. lua_pushvalue(L, index);
  20. functionRef_ = luaL_ref(L, LUA_REGISTRYINDEX);
  21. }
  22. LuaFunction::LuaFunction(lua_State* L, lua_CFunction func) :
  23. luaState_(L),
  24. numArguments_(-1)
  25. {
  26. assert(L);
  27. lua_pushcfunction(L, func);
  28. functionRef_ = luaL_ref(L, LUA_REGISTRYINDEX);
  29. }
  30. LuaFunction::~LuaFunction()
  31. {
  32. luaL_unref(luaState_, LUA_REGISTRYINDEX, functionRef_);
  33. functionRef_ = LUA_NOREF;
  34. }
  35. bool LuaFunction::IsValid() const
  36. {
  37. return functionRef_ != LUA_REFNIL && functionRef_ != LUA_NOREF;
  38. }
  39. bool LuaFunction::BeginCall(const LuaScriptInstance* instance)
  40. {
  41. if (!IsValid())
  42. return false;
  43. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef_);
  44. if (instance)
  45. {
  46. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, instance->GetScriptObjectRef()); // Will get a nil when reference is invalid
  47. numArguments_ = 1;
  48. }
  49. else
  50. numArguments_ = 0;
  51. return true;
  52. }
  53. bool LuaFunction::EndCall(int numReturns)
  54. {
  55. assert(numArguments_ >= 0);
  56. int numArguments = numArguments_;
  57. numArguments_ = -1;
  58. if (lua_pcall(luaState_, numArguments, numReturns, 0) != 0)
  59. {
  60. const char* message = lua_tostring(luaState_, -1);
  61. URHO3D_LOGERRORF("Execute Lua function failed: %s", message);
  62. lua_pop(luaState_, 1);
  63. return false;
  64. }
  65. return true;
  66. }
  67. void LuaFunction::PushInt(int value)
  68. {
  69. assert(numArguments_ >= 0);
  70. ++numArguments_;
  71. lua_pushinteger(luaState_, value);
  72. }
  73. void LuaFunction::PushBool(bool value)
  74. {
  75. assert(numArguments_ >= 0);
  76. ++numArguments_;
  77. lua_pushboolean(luaState_, value);
  78. }
  79. void LuaFunction::PushFloat(float value)
  80. {
  81. assert(numArguments_ >= 0);
  82. ++numArguments_;
  83. lua_pushnumber(luaState_, value);
  84. }
  85. void LuaFunction::PushDouble(double value)
  86. {
  87. assert(numArguments_ >= 0);
  88. ++numArguments_;
  89. lua_pushnumber(luaState_, value);
  90. }
  91. void LuaFunction::PushString(const String& string)
  92. {
  93. assert(numArguments_ >= 0);
  94. ++numArguments_;
  95. tolua_pushurho3dstring(luaState_, string);
  96. }
  97. void LuaFunction::PushUserType(void* userType, const char* typeName)
  98. {
  99. assert(numArguments_ >= 0);
  100. ++numArguments_;
  101. tolua_pushusertype(luaState_, userType, typeName);
  102. }
  103. void LuaFunction::PushVariant(const Variant& variant, const char* asType)
  104. {
  105. assert(numArguments_ >= 0);
  106. ++numArguments_;
  107. ToluaPushVariant(luaState_, &variant, asType);
  108. }
  109. void LuaFunction::PushLuaTable(const char* tableName)
  110. {
  111. assert(numArguments_ >= 0);
  112. ++numArguments_;
  113. // Performance impact wasn't confirmed here. However, I'd like to avoid unneeded allocations.
  114. if (!strchr(tableName, '.'))
  115. {
  116. lua_getglobal(luaState_, tableName);
  117. }
  118. else
  119. {
  120. Vector<String> splitNames = String::Split(tableName, '.');
  121. String currentName = splitNames.Front();
  122. lua_getglobal(luaState_, currentName.CString());
  123. if (splitNames.Size() > 1)
  124. {
  125. for (unsigned i = 0; i < splitNames.Size() - 1; ++i)
  126. {
  127. if (i)
  128. {
  129. currentName = currentName + "." + splitNames[i];
  130. lua_getfield(luaState_, -1, splitNames[i].CString());
  131. lua_replace(luaState_, -2);
  132. }
  133. if (!lua_istable(luaState_, -1))
  134. {
  135. lua_pop(luaState_, 1);
  136. lua_pushnil(luaState_);
  137. URHO3D_LOGERRORF("Could not find lua table '%s'", currentName.CString());
  138. return;
  139. }
  140. }
  141. currentName = currentName + "." + splitNames.Back();
  142. lua_getfield(luaState_, -1, splitNames.Back().CString());
  143. lua_replace(luaState_, -2);
  144. }
  145. }
  146. if (!lua_istable(luaState_, -1))
  147. URHO3D_LOGERRORF("Could not find lua table '%s'", tableName); // nil is pushed instead
  148. }
  149. }