wrap_LuaThread.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, "Thread", THREAD_THREAD_T);
  28. }
  29. int w_Thread_start(lua_State *L)
  30. {
  31. LuaThread *t = luax_checkthread(L, 1);
  32. int nargs = lua_gettop(L) - 1;
  33. Variant **args = 0;
  34. if (nargs > 0)
  35. {
  36. args = new Variant*[nargs];
  37. for (int i = 0; i < nargs; ++i)
  38. {
  39. args[i] = Variant::fromLua(L, i+2);
  40. if (!args[i])
  41. {
  42. for (int j = i; j >= 0; j--)
  43. delete args[j];
  44. delete args;
  45. return luaL_argerror(L, i+2, "boolean, number, string, love type, or flat table expected");
  46. }
  47. }
  48. }
  49. luax_pushboolean(L, t->start(args, nargs));
  50. return 1;
  51. }
  52. int w_Thread_wait(lua_State *L)
  53. {
  54. LuaThread *t = luax_checkthread(L, 1);
  55. t->wait();
  56. return 0;
  57. }
  58. int w_Thread_getError(lua_State *L)
  59. {
  60. LuaThread *t = luax_checkthread(L, 1);
  61. std::string err = t->getError();
  62. if (err.empty())
  63. lua_pushnil(L);
  64. else
  65. luax_pushstring(L, err);
  66. return 1;
  67. }
  68. int w_Thread_isRunning(lua_State *L)
  69. {
  70. LuaThread *t = luax_checkthread(L, 1);
  71. luax_pushboolean(L, t->isRunning());
  72. return 1;
  73. }
  74. static const luaL_Reg type_functions[] = {
  75. { "start", w_Thread_start },
  76. { "wait", w_Thread_wait },
  77. { "getError", w_Thread_getError },
  78. { "isRunning", w_Thread_isRunning },
  79. { 0, 0 }
  80. };
  81. extern "C" int luaopen_thread(lua_State *L)
  82. {
  83. return luax_register_type(L, "Thread", type_functions);
  84. }
  85. } // thread
  86. } // love