wrap_ThreadModule.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. // LOVE
  21. #include "wrap_ThreadModule.h"
  22. #include "wrap_LuaThread.h"
  23. #include "wrap_Channel.h"
  24. #include "ThreadModule.h"
  25. #include "filesystem/File.h"
  26. #include "filesystem/FileData.h"
  27. namespace love
  28. {
  29. namespace thread
  30. {
  31. static ThreadModule *instance = 0;
  32. int w_newThread(lua_State *L)
  33. {
  34. std::string name = "Thread code";
  35. love::Data *data = 0;
  36. if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
  37. luax_convobj(L, 1, "filesystem", "newFileData");
  38. if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
  39. {
  40. love::filesystem::FileData *fdata = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
  41. name = std::string("@") + fdata->getFilename();
  42. data = fdata;
  43. }
  44. else
  45. {
  46. data = luax_checktype<love::Data>(L, 1, "Data", DATA_T);
  47. }
  48. LuaThread *t = instance->newThread(name, data);
  49. luax_pushtype(L, "Thread", THREAD_THREAD_T, t);
  50. return 1;
  51. }
  52. int w_newChannel(lua_State *L)
  53. {
  54. Channel *c = instance->newChannel();
  55. luax_pushtype(L, "Channel", THREAD_CHANNEL_T, c);
  56. return 1;
  57. }
  58. int w_getChannel(lua_State *L)
  59. {
  60. std::string name = luax_checkstring(L, 1);
  61. Channel *c = instance->getChannel(name);
  62. luax_pushtype(L, "Channel", THREAD_CHANNEL_T, c);
  63. return 1;
  64. }
  65. // List of functions to wrap.
  66. static const luaL_Reg module_functions[] = {
  67. { "newThread", w_newThread },
  68. { "newChannel", w_newChannel },
  69. { "getChannel", w_getChannel },
  70. { 0, 0 }
  71. };
  72. static const lua_CFunction types[] = {
  73. luaopen_thread,
  74. luaopen_channel,
  75. 0
  76. };
  77. extern "C" int luaopen_love_thread(lua_State *L)
  78. {
  79. if (instance == 0)
  80. {
  81. luax_catchexcept(L, [&](){ instance = new love::thread::ThreadModule(); });
  82. }
  83. else
  84. instance->retain();
  85. WrappedModule w;
  86. w.module = instance;
  87. w.name = "thread";
  88. w.flags = MODULE_T;
  89. w.functions = module_functions;
  90. w.types = types;
  91. return luax_register_module(L, w);
  92. }
  93. } // thread
  94. } // love