LuaThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Copyright (c) 2006-2014 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "LuaThread.h"
  21. #include "event/Event.h"
  22. #include "common/config.h"
  23. #ifdef LOVE_BUILD_STANDALONE
  24. extern "C" int luaopen_love(lua_State * L);
  25. #endif // LOVE_BUILD_STANDALONE
  26. extern "C" int luaopen_love_thread(lua_State *L);
  27. namespace love
  28. {
  29. namespace thread
  30. {
  31. LuaThread::LuaThread(const std::string &name, love::Data *code)
  32. : code(code)
  33. , name(name)
  34. , args(0)
  35. , nargs(0)
  36. {
  37. threadName = name;
  38. }
  39. LuaThread::~LuaThread()
  40. {
  41. // No args should still exist at this point,
  42. // but you never know.
  43. for (int i = 0; i < nargs; ++i)
  44. args[i]->release();
  45. }
  46. void LuaThread::threadFunction()
  47. {
  48. this->retain();
  49. error.clear();
  50. lua_State *L = luaL_newstate();
  51. luaL_openlibs(L);
  52. #ifdef LOVE_BUILD_STANDALONE
  53. love::luax_preload(L, luaopen_love, "love");
  54. luaopen_love(L);
  55. #endif // LOVE_BUILD_STANDALONE
  56. luaopen_love_thread(L);
  57. if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
  58. error = luax_tostring(L, -1);
  59. else
  60. {
  61. int pushedargs = nargs;
  62. for (int i = 0; i < nargs; ++i)
  63. {
  64. args[i]->toLua(L);
  65. args[i]->release();
  66. }
  67. // Set both args and nargs to nil, prevents the deconstructor from
  68. // accessing it again.
  69. nargs = 0;
  70. args = 0;
  71. if (lua_pcall(L, pushedargs, 0, 0) != 0)
  72. error = luax_tostring(L, -1);
  73. }
  74. lua_close(L);
  75. if (!error.empty())
  76. onError();
  77. this->release();
  78. }
  79. bool LuaThread::start(Variant **args, int nargs)
  80. {
  81. for (int i = 0; i < this->nargs; ++i)
  82. this->args[i]->release();
  83. this->args = args;
  84. this->nargs = nargs;
  85. return Threadable::start();
  86. }
  87. const std::string &LuaThread::getError() const
  88. {
  89. return error;
  90. }
  91. void LuaThread::onError()
  92. {
  93. if (error.empty())
  94. return;
  95. event::Event *event = Module::getInstance<event::Event>(Module::M_EVENT);
  96. if (!event)
  97. return;
  98. Proxy p;
  99. p.flags = THREAD_THREAD_T;
  100. p.data = this;
  101. Variant *arg1 = new Variant(THREAD_THREAD_ID, &p);
  102. Variant *arg2 = new Variant(error.c_str(), error.length());
  103. event::Message *msg = new event::Message("threaderror", arg1, arg2);
  104. arg1->release();
  105. arg2->release();
  106. event->push(msg);
  107. msg->release();
  108. }
  109. } // thread
  110. } // love