wrap_LuaThread.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2006-2010 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 "wrap_LuaThread.h"
  21. namespace love
  22. {
  23. namespace thread
  24. {
  25. LuaThread *luax_checkthread(lua_State *L, int idx)
  26. {
  27. return luax_checktype<LuaThread>(L, idx);
  28. }
  29. int w_Thread_start(lua_State *L)
  30. {
  31. LuaThread *t = luax_checkthread(L, 1);
  32. std::vector<Variant> args;
  33. int nargs = lua_gettop(L) - 1;
  34. for (int i = 0; i < nargs; ++i)
  35. {
  36. args.push_back(Variant::fromLua(L, i+2));
  37. if (args.back().getType() == Variant::UNKNOWN)
  38. {
  39. args.clear();
  40. return luaL_argerror(L, i+2, "boolean, number, string, love type, or flat table expected");
  41. }
  42. }
  43. luax_pushboolean(L, t->start(args));
  44. return 1;
  45. }
  46. int w_Thread_wait(lua_State *L)
  47. {
  48. LuaThread *t = luax_checkthread(L, 1);
  49. t->wait();
  50. return 0;
  51. }
  52. int w_Thread_getError(lua_State *L)
  53. {
  54. LuaThread *t = luax_checkthread(L, 1);
  55. std::string err = t->getError();
  56. if (err.empty())
  57. lua_pushnil(L);
  58. else
  59. luax_pushstring(L, err);
  60. return 1;
  61. }
  62. int w_Thread_isRunning(lua_State *L)
  63. {
  64. LuaThread *t = luax_checkthread(L, 1);
  65. luax_pushboolean(L, t->isRunning());
  66. return 1;
  67. }
  68. static const luaL_Reg w_Thread_functions[] =
  69. {
  70. { "start", w_Thread_start },
  71. { "wait", w_Thread_wait },
  72. { "getError", w_Thread_getError },
  73. { "isRunning", w_Thread_isRunning },
  74. { 0, 0 }
  75. };
  76. extern "C" int luaopen_thread(lua_State *L)
  77. {
  78. return luax_register_type(L, LuaThread::type, "Thread", w_Thread_functions, nullptr);
  79. }
  80. } // thread
  81. } // love