LuaThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Copyright (c) 2006-2013 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. code->retain();
  38. }
  39. LuaThread::~LuaThread()
  40. {
  41. code->release();
  42. // No args should still exist at this point,
  43. // but you never know.
  44. for (int i = 0; i < nargs; ++i)
  45. args[i]->release();
  46. }
  47. void LuaThread::threadFunction()
  48. {
  49. this->retain();
  50. error.clear();
  51. lua_State *L = luaL_newstate();
  52. luaL_openlibs(L);
  53. #ifdef LOVE_BUILD_STANDALONE
  54. love::luax_preload(L, luaopen_love, "love");
  55. luaopen_love(L);
  56. #endif // LOVE_BUILD_STANDALONE
  57. luaopen_love_thread(L);
  58. if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
  59. error = luax_tostring(L, -1);
  60. else
  61. {
  62. int pushedargs = nargs;
  63. for (int i = 0; i < nargs; ++i)
  64. {
  65. args[i]->toLua(L);
  66. args[i]->release();
  67. }
  68. // Set both args and nargs to nil, prevents the deconstructor from
  69. // accessing it again.
  70. nargs = 0;
  71. args = 0;
  72. if (lua_pcall(L, pushedargs, 0, 0) != 0)
  73. error = luax_tostring(L, -1);
  74. }
  75. lua_close(L);
  76. if (!error.empty())
  77. onError();
  78. this->release();
  79. }
  80. bool LuaThread::start(Variant **args, int nargs)
  81. {
  82. for (int i = 0; i < this->nargs; ++i)
  83. this->args[i]->release();
  84. this->args = args;
  85. this->nargs = nargs;
  86. return Threadable::start();
  87. }
  88. const std::string &LuaThread::getError() const
  89. {
  90. return error;
  91. }
  92. void LuaThread::onError()
  93. {
  94. if (error.empty())
  95. return;
  96. event::Event *event = (event::Event *) Module::findInstance("love.event.");
  97. if (!event)
  98. return;
  99. Proxy p;
  100. p.flags = THREAD_THREAD_T;
  101. p.data = this;
  102. Variant *arg1 = new Variant(THREAD_THREAD_ID, &p);
  103. Variant *arg2 = new Variant(error.c_str(), error.length());
  104. event::Message *msg = new event::Message("threaderror", arg1, arg2);
  105. arg1->release();
  106. arg2->release();
  107. event->push(msg);
  108. msg->release();
  109. }
  110. } // thread
  111. } // love