LuaFunction.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. tolua_pushurho3dstring(luaState_, string);
  101. }
  102. void LuaFunction::PushUserType(void* userType, const char* typeName)
  103. {
  104. ++numArguments_;
  105. tolua_pushusertype(luaState_, userType, typeName);
  106. }
  107. bool LuaFunction::PushVariant(const Variant& variant)
  108. {
  109. switch (variant.GetType())
  110. {
  111. case VAR_INT:
  112. PushInt(variant.GetInt());
  113. return true;
  114. case VAR_BOOL:
  115. PushBool(variant.GetBool());
  116. return true;
  117. case VAR_FLOAT:
  118. PushFloat(variant.GetFloat());
  119. return true;
  120. case VAR_VECTOR2:
  121. PushUserType(variant.GetVector2(), "Vector2");
  122. return true;
  123. case VAR_VECTOR3:
  124. PushUserType(variant.GetVector3(), "Vector3");
  125. return true;
  126. case VAR_VECTOR4:
  127. PushUserType(variant.GetVector4(), "Vector4");
  128. return true;
  129. case VAR_QUATERNION:
  130. PushUserType(variant.GetQuaternion(), "Quaternion");
  131. return true;
  132. case VAR_COLOR:
  133. PushUserType(variant.GetQuaternion(), "Color");
  134. return true;
  135. case VAR_STRING:
  136. PushString(variant.GetString());
  137. return true;
  138. case VAR_BUFFER:
  139. {
  140. VectorBuffer buffer(variant.GetBuffer());
  141. PushUserType(buffer, "VectorBuffer");
  142. }
  143. return true;
  144. case VAR_RESOURCEREF:
  145. PushUserType(variant.GetResourceRef(), "ResourceRef");
  146. return true;
  147. case VAR_INTRECT:
  148. PushUserType(variant.GetIntRect(), "IntRect");
  149. return true;
  150. case VAR_INTVECTOR2:
  151. PushUserType(variant.GetIntVector2(), "IntVector2");
  152. return true;
  153. }
  154. return false;
  155. }
  156. bool LuaFunction::PushLuaTable(const String& tableName)
  157. {
  158. ++numArguments_;
  159. lua_getglobal(luaState_, tableName.CString());
  160. if (!lua_istable(luaState_, -1))
  161. {
  162. LOGERROR("Could not find lua table " + tableName);
  163. return false;
  164. }
  165. return true;
  166. }
  167. }