LuaFunction.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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(const LuaScriptInstance* instance)
  49. {
  50. if (!IsValid())
  51. return false;
  52. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, functionRef_);
  53. if (instance)
  54. {
  55. lua_rawgeti(luaState_, LUA_REGISTRYINDEX, instance->GetScriptObjectRef());
  56. numArguments_ = 1;
  57. }
  58. else
  59. numArguments_ = 0;
  60. return true;
  61. }
  62. bool LuaFunction::EndCall(int numReturns)
  63. {
  64. if (lua_pcall(luaState_, numArguments_, numReturns, 0) != 0)
  65. {
  66. const char* message = lua_tostring(luaState_, -1);
  67. LOGERROR("Execute Lua function failed: " + String(message));
  68. lua_pop(luaState_, 1);
  69. return false;
  70. }
  71. return true;
  72. }
  73. void LuaFunction::PushInt(int value)
  74. {
  75. ++numArguments_;
  76. lua_pushinteger(luaState_, value);
  77. }
  78. void LuaFunction::PushBool(bool value)
  79. {
  80. ++numArguments_;
  81. lua_pushboolean(luaState_, value);
  82. }
  83. void LuaFunction::PushFloat(float value)
  84. {
  85. ++numArguments_;
  86. lua_pushnumber(luaState_, value);
  87. }
  88. void LuaFunction::PushDouble(double 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. void LuaFunction::PushVariant(const Variant& variant, const char* asType)
  104. {
  105. ++numArguments_;
  106. ToluaPushVariant(luaState_, &variant, asType);
  107. }
  108. void LuaFunction::PushLuaTable(const String& tableName)
  109. {
  110. ++numArguments_;
  111. lua_getglobal(luaState_, tableName.CString());
  112. if (!lua_istable(luaState_, -1))
  113. LOGERROR("Could not find lua table " + tableName); // nil is pushed instead
  114. }
  115. }