LuaThread.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * Copyright (c) 2006-2016 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. namespace love
  27. {
  28. namespace thread
  29. {
  30. LuaThread::LuaThread(const std::string &name, love::Data *code)
  31. : code(code)
  32. , name(name)
  33. {
  34. threadName = name;
  35. }
  36. LuaThread::~LuaThread()
  37. {
  38. }
  39. void LuaThread::threadFunction()
  40. {
  41. this->retain();
  42. error.clear();
  43. lua_State *L = luaL_newstate();
  44. luaL_openlibs(L);
  45. #ifdef LOVE_BUILD_STANDALONE
  46. luax_preload(L, luaopen_love, "love");
  47. luax_require(L, "love");
  48. lua_pop(L, 1);
  49. #endif // LOVE_BUILD_STANDALONE
  50. luax_require(L, "love.thread");
  51. lua_pop(L, 1);
  52. // We load love.filesystem by default, since require still exists without it
  53. // but won't load files from the proper paths. love.filesystem also must be
  54. // loaded before using any love function that can take a filepath argument.
  55. luax_require(L, "love.filesystem");
  56. lua_pop(L, 1);
  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 = (int) args.size();
  62. for (int i = 0; i < pushedargs; i++)
  63. args[i].toLua(L);
  64. args.clear();
  65. if (lua_pcall(L, pushedargs, 0, 0) != 0)
  66. error = luax_tostring(L, -1);
  67. }
  68. lua_close(L);
  69. if (!error.empty())
  70. onError();
  71. this->release();
  72. }
  73. bool LuaThread::start(const std::vector<Variant> &args)
  74. {
  75. this->args = args;
  76. return Threadable::start();
  77. }
  78. const std::string &LuaThread::getError() const
  79. {
  80. return error;
  81. }
  82. void LuaThread::onError()
  83. {
  84. if (error.empty())
  85. return;
  86. auto eventmodule = Module::getInstance<event::Event>(Module::M_EVENT);
  87. if (!eventmodule)
  88. return;
  89. Proxy p;
  90. p.type = THREAD_THREAD_ID;
  91. p.object = this;
  92. std::vector<Variant> vargs = {
  93. Variant(p.type, &p),
  94. Variant(error.c_str(), error.length())
  95. };
  96. StrongRef<event::Message> msg(new event::Message("threaderror", vargs), Acquire::NORETAIN);
  97. eventmodule->push(msg);
  98. }
  99. } // thread
  100. } // love