LuaFunction.cpp 5.1 KB

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