LuaFunction.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright (c) 2008-2015 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* luaState, int functionRef, bool needUnref) :
  34. luaState_(luaState),
  35. functionRef_(functionRef),
  36. needUnref_(needUnref)
  37. {
  38. }
  39. LuaFunction::~LuaFunction()
  40. {
  41. if (needUnref_ && functionRef_ != LUA_REFNIL)
  42. luaL_unref(luaState_, LUA_REGISTRYINDEX, functionRef_);
  43. }
  44. bool LuaFunction::IsValid() const
  45. {
  46. return functionRef_ != LUA_REFNIL;
  47. }
  48. bool LuaFunction::BeginCall()
  49. {
  50. if (!IsValid())
  51. return false;
  52. stackTop_ = lua_gettop(luaState_);
  53. numArguments_ = 0;
  54. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef_);
  55. return true;
  56. }
  57. bool LuaFunction::BeginCall(const LuaScriptInstance* instance)
  58. {
  59. if (!IsValid())
  60. return false;
  61. stackTop_ = lua_gettop(luaState_);
  62. numArguments_ = 1;
  63. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef_);
  64. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  65. return true;
  66. }
  67. bool LuaFunction::EndCall(int numReturns)
  68. {
  69. if (lua_pcall(luaState_, numArguments_, numReturns, 0) != 0)
  70. {
  71. const char* message = lua_tostring(luaState_, -1);
  72. LOGERROR("Execute Lua function failed: " + String(message));
  73. lua_settop(luaState_, stackTop_);
  74. return false;
  75. }
  76. return true;
  77. }
  78. void LuaFunction::PushInt(int value)
  79. {
  80. ++numArguments_;
  81. lua_pushinteger(luaState_, value);
  82. }
  83. void LuaFunction::PushBool(bool value)
  84. {
  85. ++numArguments_;
  86. lua_pushboolean(luaState_, value);
  87. }
  88. void LuaFunction::PushFloat(float value)
  89. {
  90. ++numArguments_;
  91. lua_pushnumber(luaState_, value);
  92. }
  93. void LuaFunction::PushString(const String& string)
  94. {
  95. ++numArguments_;
  96. tolua_pushurho3dstring(luaState_, string);
  97. }
  98. void LuaFunction::PushUserType(void* userType, const char* typeName)
  99. {
  100. ++numArguments_;
  101. tolua_pushusertype(luaState_, userType, typeName);
  102. }
  103. bool LuaFunction::PushVariant(const Variant& variant)
  104. {
  105. switch (variant.GetType())
  106. {
  107. case VAR_INT:
  108. PushInt(variant.GetInt());
  109. return true;
  110. case VAR_BOOL:
  111. PushBool(variant.GetBool());
  112. return true;
  113. case VAR_FLOAT:
  114. PushFloat(variant.GetFloat());
  115. return true;
  116. case VAR_VECTOR2:
  117. PushUserType(variant.GetVector2(), "Vector2");
  118. return true;
  119. case VAR_VECTOR3:
  120. PushUserType(variant.GetVector3(), "Vector3");
  121. return true;
  122. case VAR_VECTOR4:
  123. PushUserType(variant.GetVector4(), "Vector4");
  124. return true;
  125. case VAR_QUATERNION:
  126. PushUserType(variant.GetQuaternion(), "Quaternion");
  127. return true;
  128. case VAR_COLOR:
  129. PushUserType(variant.GetColor(), "Color");
  130. return true;
  131. case VAR_STRING:
  132. PushString(variant.GetString());
  133. return true;
  134. case VAR_BUFFER:
  135. {
  136. VectorBuffer buffer(variant.GetBuffer());
  137. PushUserType(buffer, "VectorBuffer");
  138. }
  139. return true;
  140. case VAR_RESOURCEREF:
  141. PushUserType(variant.GetResourceRef(), "ResourceRef");
  142. return true;
  143. case VAR_INTRECT:
  144. PushUserType(variant.GetIntRect(), "IntRect");
  145. return true;
  146. case VAR_INTVECTOR2:
  147. PushUserType(variant.GetIntVector2(), "IntVector2");
  148. return true;
  149. default:
  150. return false;
  151. }
  152. }
  153. bool LuaFunction::PushLuaTable(const String& tableName)
  154. {
  155. ++numArguments_;
  156. lua_getglobal(luaState_, tableName.CString());
  157. if (!lua_istable(luaState_, -1))
  158. {
  159. LOGERROR("Could not find lua table " + tableName);
  160. return false;
  161. }
  162. return true;
  163. }
  164. }