LuaFunction.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../IO/Log.h"
  24. #include "../IO/VectorBuffer.h"
  25. #include "../LuaScript/LuaFunction.h"
  26. #include "../LuaScript/LuaScript.h"
  27. #include "../LuaScript/LuaScriptInstance.h"
  28. #include <toluapp/tolua++.h>
  29. #include "../LuaScript/ToluaUtils.h"
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. LuaFunction::LuaFunction(lua_State* L, int index) :
  34. luaState_(L),
  35. numArguments_(-1)
  36. {
  37. assert(L);
  38. lua_pushvalue(L, index);
  39. functionRef_ = luaL_ref(L, LUA_REGISTRYINDEX);
  40. }
  41. LuaFunction::LuaFunction(lua_State* L, lua_CFunction func) :
  42. luaState_(L),
  43. numArguments_(-1)
  44. {
  45. assert(L);
  46. lua_pushcfunction(L, func);
  47. functionRef_ = luaL_ref(L, LUA_REGISTRYINDEX);
  48. }
  49. LuaFunction::~LuaFunction()
  50. {
  51. luaL_unref(luaState_, LUA_REGISTRYINDEX, functionRef_);
  52. functionRef_ = LUA_NOREF;
  53. }
  54. bool LuaFunction::IsValid() const
  55. {
  56. return functionRef_ != LUA_REFNIL && functionRef_ != LUA_NOREF;
  57. }
  58. bool LuaFunction::BeginCall(const LuaScriptInstance* instance)
  59. {
  60. if (!IsValid())
  61. return false;
  62. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef_);
  63. if (instance)
  64. {
  65. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, instance->GetScriptObjectRef()); // Will get a nil when reference is invalid
  66. numArguments_ = 1;
  67. }
  68. else
  69. numArguments_ = 0;
  70. return true;
  71. }
  72. bool LuaFunction::EndCall(int numReturns)
  73. {
  74. assert(numArguments_ >= 0);
  75. int numArguments = numArguments_;
  76. numArguments_ = -1;
  77. if (lua_pcall(luaState_, numArguments, numReturns, 0) != 0)
  78. {
  79. const char* message = lua_tostring(luaState_, -1);
  80. URHO3D_LOGERRORF("Execute Lua function failed: %s", message);
  81. lua_pop(luaState_, 1);
  82. return false;
  83. }
  84. return true;
  85. }
  86. void LuaFunction::PushInt(int value)
  87. {
  88. assert(numArguments_ >= 0);
  89. ++numArguments_;
  90. lua_pushinteger(luaState_, value);
  91. }
  92. void LuaFunction::PushBool(bool value)
  93. {
  94. assert(numArguments_ >= 0);
  95. ++numArguments_;
  96. lua_pushboolean(luaState_, value);
  97. }
  98. void LuaFunction::PushFloat(float value)
  99. {
  100. assert(numArguments_ >= 0);
  101. ++numArguments_;
  102. lua_pushnumber(luaState_, value);
  103. }
  104. void LuaFunction::PushDouble(double value)
  105. {
  106. assert(numArguments_ >= 0);
  107. ++numArguments_;
  108. lua_pushnumber(luaState_, value);
  109. }
  110. void LuaFunction::PushString(const String& string)
  111. {
  112. assert(numArguments_ >= 0);
  113. ++numArguments_;
  114. tolua_pushurho3dstring(luaState_, string);
  115. }
  116. void LuaFunction::PushUserType(void* userType, const char* typeName)
  117. {
  118. assert(numArguments_ >= 0);
  119. ++numArguments_;
  120. tolua_pushusertype(luaState_, userType, typeName);
  121. }
  122. void LuaFunction::PushVariant(const Variant& variant, const char* asType)
  123. {
  124. assert(numArguments_ >= 0);
  125. ++numArguments_;
  126. ToluaPushVariant(luaState_, &variant, asType);
  127. }
  128. void LuaFunction::PushLuaTable(const char* tableName)
  129. {
  130. assert(numArguments_ >= 0);
  131. ++numArguments_;
  132. lua_getglobal(luaState_, tableName);
  133. if (!lua_istable(luaState_, -1))
  134. URHO3D_LOGERRORF("Could not find lua table %s", tableName); // nil is pushed instead
  135. }
  136. }