wrap_ThreadModule.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // C
  28. #include <cstring>
  29. namespace love
  30. {
  31. namespace thread
  32. {
  33. #define instance() (Module::getInstance<ThreadModule>(Module::M_THREAD))
  34. int w_newThread(lua_State *L)
  35. {
  36. std::string name = "Thread code";
  37. love::Data *data = nullptr;
  38. if (lua_isstring(L, 1))
  39. {
  40. size_t slen = 0;
  41. const char *str = lua_tolstring(L, 1, &slen);
  42. // Treat the string as Lua code if it's long or has a newline.
  43. if (slen >= 1024 || memchr(str, '\n', slen))
  44. {
  45. // Construct a FileData from the string.
  46. lua_pushvalue(L, 1);
  47. lua_pushstring(L, "string");
  48. int idxs[] = {lua_gettop(L) - 1, lua_gettop(L)};
  49. luax_convobj(L, idxs, 2, "filesystem", "newFileData");
  50. lua_pop(L, 1);
  51. lua_replace(L, 1);
  52. }
  53. else
  54. luax_convobj(L, 1, "filesystem", "newFileData");
  55. }
  56. else if (luax_istype(L, 1, love::filesystem::File::type))
  57. luax_convobj(L, 1, "filesystem", "newFileData");
  58. if (luax_istype(L, 1, love::filesystem::FileData::type))
  59. {
  60. love::filesystem::FileData *fdata = luax_checktype<love::filesystem::FileData>(L, 1);
  61. name = std::string("@") + fdata->getFilename();
  62. data = fdata;
  63. }
  64. else
  65. {
  66. data = luax_checktype<love::Data>(L, 1);
  67. }
  68. LuaThread *t = instance()->newThread(name, data);
  69. luax_pushtype(L, t);
  70. t->release();
  71. return 1;
  72. }
  73. int w_newChannel(lua_State *L)
  74. {
  75. Channel *c = instance()->newChannel();
  76. luax_pushtype(L, c);
  77. c->release();
  78. return 1;
  79. }
  80. int w_getChannel(lua_State *L)
  81. {
  82. std::string name = luax_checkstring(L, 1);
  83. Channel *c = instance()->getChannel(name);
  84. luax_pushtype(L, c);
  85. c->release();
  86. return 1;
  87. }
  88. // List of functions to wrap.
  89. static const luaL_Reg module_functions[] =
  90. {
  91. { "newThread", w_newThread },
  92. { "newChannel", w_newChannel },
  93. { "getChannel", w_getChannel },
  94. { 0, 0 }
  95. };
  96. static const lua_CFunction types[] = {
  97. luaopen_thread,
  98. luaopen_channel,
  99. 0
  100. };
  101. extern "C" int luaopen_love_thread(lua_State *L)
  102. {
  103. ThreadModule *instance = instance();
  104. if (instance == nullptr)
  105. {
  106. luax_catchexcept(L, [&](){ instance = new love::thread::ThreadModule(); });
  107. }
  108. else
  109. instance->retain();
  110. WrappedModule w;
  111. w.module = instance;
  112. w.name = "thread";
  113. w.type = &Module::type;
  114. w.functions = module_functions;
  115. w.types = types;
  116. return luax_register_module(L, w);
  117. }
  118. } // thread
  119. } // love