LuaFunction.cpp 5.1 KB

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