LuaThread.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) 2006-2017 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. love::Type LuaThread::type("Thread", &Threadable::type);
  31. LuaThread::LuaThread(const std::string &name, love::Data *code)
  32. : code(code)
  33. , name(name)
  34. {
  35. threadName = name;
  36. }
  37. LuaThread::~LuaThread()
  38. {
  39. }
  40. void LuaThread::threadFunction()
  41. {
  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. lua_pushcfunction(L, luax_traceback);
  58. int tracebackidx = lua_gettop(L);
  59. if (luaL_loadbuffer(L, (const char *) code->getData(), code->getSize(), name.c_str()) != 0)
  60. error = luax_tostring(L, -1);
  61. else
  62. {
  63. int pushedargs = (int) args.size();
  64. for (int i = 0; i < pushedargs; i++)
  65. args[i].toLua(L);
  66. args.clear();
  67. if (lua_pcall(L, pushedargs, 0, tracebackidx) != 0)
  68. error = luax_tostring(L, -1);
  69. }
  70. lua_close(L);
  71. if (!error.empty())
  72. onError();
  73. }
  74. bool LuaThread::start(const std::vector<Variant> &args)
  75. {
  76. this->args = args;
  77. return Threadable::start();
  78. }
  79. const std::string &LuaThread::getError() const
  80. {
  81. return error;
  82. }
  83. void LuaThread::onError()
  84. {
  85. if (error.empty())
  86. return;
  87. auto eventmodule = Module::getInstance<event::Event>(Module::M_EVENT);
  88. if (!eventmodule)
  89. return;
  90. Proxy p;
  91. p.type = &LuaThread::type;
  92. p.object = this;
  93. std::vector<Variant> vargs = {
  94. Variant(p.type, &p),
  95. Variant(error.c_str(), error.length())
  96. };
  97. StrongRef<event::Message> msg(new event::Message("threaderror", vargs), Acquire::NORETAIN);
  98. eventmodule->push(msg);
  99. }
  100. } // thread
  101. } // love