LuaFunction.cpp 5.1 KB

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