wrap_Thread.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_Thread.h"
  21. namespace love
  22. {
  23. namespace thread
  24. {
  25. namespace sdl
  26. {
  27. Thread *luax_checkthread(lua_State *L, int idx)
  28. {
  29. return luax_checktype<Thread>(L, idx, "Thread", THREAD_THREAD_T);
  30. }
  31. int w_Thread_start(lua_State *L)
  32. {
  33. Thread *t = luax_checkthread(L, 1);
  34. t->start();
  35. return 0;
  36. }
  37. int w_Thread_kill(lua_State *L)
  38. {
  39. Thread *t = luax_checkthread(L, 1);
  40. t->kill();
  41. return 0;
  42. }
  43. int w_Thread_wait(lua_State *L)
  44. {
  45. Thread *t = luax_checkthread(L, 1);
  46. t->wait();
  47. return 0;
  48. }
  49. int w_Thread_getName(lua_State *L)
  50. {
  51. Thread *t = luax_checkthread(L, 1);
  52. lua_pushstring(L, t->getName().c_str());
  53. return 1;
  54. }
  55. int w_Thread_receive(lua_State *L)
  56. {
  57. Thread *t = luax_checkthread(L, 1);
  58. std::string name = luaL_checkstring(L, 2);
  59. ThreadVariant *v = t->receive(name);
  60. if (!v)
  61. {
  62. lua_pushnil(L);
  63. return 1;
  64. }
  65. v->retain();
  66. t->clear(name);
  67. switch(v->type)
  68. {
  69. case BOOLEAN:
  70. lua_pushboolean(L, v->data.boolean);
  71. break;
  72. case NUMBER:
  73. lua_pushnumber(L, v->data.number);
  74. break;
  75. case STRING:
  76. lua_pushstring(L, v->data.string);
  77. break;
  78. case LUSERDATA:
  79. lua_pushlightuserdata(L, v->data.userdata);
  80. break;
  81. case FUSERDATA:
  82. const char *name;
  83. love::types.find(v->udatatype, name);
  84. ((love::Object *) v->data.userdata)->retain();
  85. luax_newtype(L, name, v->flags, v->data.userdata);
  86. break;
  87. default:
  88. lua_pushnil(L);
  89. break;
  90. }
  91. v->release();
  92. return 1;
  93. }
  94. int w_Thread_demand(lua_State *L)
  95. {
  96. Thread *t = luax_checkthread(L, 1);
  97. std::string name = luaL_checkstring(L, 2);
  98. ThreadVariant *v = t->demand(name);
  99. if (!v)
  100. {
  101. lua_pushnil(L);
  102. return 1;
  103. }
  104. v->retain();
  105. t->clear(name);
  106. switch(v->type)
  107. {
  108. case BOOLEAN:
  109. lua_pushboolean(L, v->data.boolean);
  110. break;
  111. case NUMBER:
  112. lua_pushnumber(L, v->data.number);
  113. break;
  114. case STRING:
  115. lua_pushstring(L, v->data.string);
  116. break;
  117. case LUSERDATA:
  118. lua_pushlightuserdata(L, v->data.userdata);
  119. break;
  120. case FUSERDATA:
  121. const char *name;
  122. types.find(v->udatatype, name);
  123. ((love::Object *) v->data.userdata)->retain();
  124. luax_newtype(L, name, v->flags, v->data.userdata);
  125. break;
  126. default:
  127. lua_pushnil(L);
  128. break;
  129. }
  130. v->release();
  131. return 1;
  132. }
  133. int w_Thread_peek(lua_State *L)
  134. {
  135. Thread *t = luax_checkthread(L, 1);
  136. std::string name = luaL_checkstring(L, 2);
  137. ThreadVariant *v = t->receive(name);
  138. if (!v)
  139. {
  140. lua_pushnil(L);
  141. return 1;
  142. }
  143. v->retain();
  144. switch(v->type)
  145. {
  146. case BOOLEAN:
  147. lua_pushboolean(L, v->data.boolean);
  148. break;
  149. case NUMBER:
  150. lua_pushnumber(L, v->data.number);
  151. break;
  152. case STRING:
  153. lua_pushstring(L, v->data.string);
  154. break;
  155. case LUSERDATA:
  156. lua_pushlightuserdata(L, v->data.userdata);
  157. break;
  158. case FUSERDATA:
  159. const char *name;
  160. types.find(v->udatatype, name);
  161. ((love::Object *) v->data.userdata)->retain();
  162. luax_newtype(L, name, v->flags, v->data.userdata);
  163. break;
  164. default:
  165. lua_pushnil(L);
  166. break;
  167. }
  168. v->release();
  169. return 1;
  170. }
  171. Type extractudatatype(lua_State * L, int idx)
  172. {
  173. Type t = INVALID_ID;
  174. if (!lua_isuserdata(L, idx))
  175. return t;
  176. if (luaL_getmetafield (L, idx, "__tostring") == 0)
  177. return t;
  178. lua_pushvalue(L, idx);
  179. int result = lua_pcall(L, 1, 1, 0);
  180. if (result == 0)
  181. types.find(lua_tostring(L, -1), t);
  182. if (result == 0 || result == LUA_ERRRUN)
  183. lua_pop(L, 1);
  184. return t;
  185. }
  186. int w_Thread_send(lua_State *L)
  187. {
  188. Thread *t = luax_checkthread(L, 1);
  189. std::string name = luaL_checkstring(L, 2);
  190. ThreadVariant *v;
  191. if (lua_isboolean(L, 3))
  192. {
  193. v = new ThreadVariant((bool) lua_toboolean(L, 3));
  194. }
  195. else if (lua_isnumber(L, 3))
  196. {
  197. v = new ThreadVariant(lua_tonumber(L, 3));
  198. }
  199. else if (lua_isstring(L, 3))
  200. {
  201. v = new ThreadVariant(lua_tostring(L, 3));
  202. }
  203. else if (lua_islightuserdata(L, 3))
  204. {
  205. v = new ThreadVariant(lua_touserdata(L, 3));
  206. }
  207. else if (lua_isuserdata(L, 3))
  208. {
  209. v = new ThreadVariant(extractudatatype(L, 3), lua_touserdata(L, 3));
  210. }
  211. else
  212. {
  213. return luaL_error(L, "Expected boolean, number, string or userdata");
  214. }
  215. t->send(name, v);
  216. v->release();
  217. return 0;
  218. }
  219. static const luaL_Reg type_functions[] = {
  220. { "start", w_Thread_start },
  221. { "kill", w_Thread_kill },
  222. { "wait", w_Thread_wait },
  223. { "getName", w_Thread_getName },
  224. { "receive", w_Thread_receive },
  225. { "demand", w_Thread_demand },
  226. { "peek", w_Thread_peek },
  227. { "send", w_Thread_send },
  228. { 0, 0 }
  229. };
  230. int luaopen_thread(lua_State *L)
  231. {
  232. return luax_register_type(L, "Thread", type_functions);
  233. }
  234. static ThreadModule *instance;
  235. int w_newThread(lua_State *L)
  236. {
  237. std::string name = luaL_checkstring(L, 1);
  238. if (lua_isstring(L, 2))
  239. luax_convobj(L, 2, "filesystem", "read");
  240. love::Data *data = luax_checktype<love::Data>(L, 2, "Data", DATA_T);
  241. Thread *t = instance->newThread(name, data);
  242. luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)t);
  243. return 1;
  244. }
  245. int w_getThreads(lua_State *L)
  246. {
  247. unsigned count = instance->getThreadCount();
  248. Thread **list = new Thread*[count];
  249. instance->getThreads(list);
  250. lua_newtable(L);
  251. for (unsigned int i = 0; i<count; i++)
  252. {
  253. luax_newtype(L, "Thread", THREAD_THREAD_T, (void*) list[i]);
  254. list[i]->lock();
  255. list[i]->retain();
  256. list[i]->unlock();
  257. lua_setfield(L, -2, list[i]->getName().c_str());
  258. }
  259. delete[] list;
  260. return 1;
  261. }
  262. int w_getThread(lua_State *L)
  263. {
  264. std::string name = luaL_checkstring(L, 1);
  265. Thread *t = instance->getThread(name);
  266. if (t)
  267. {
  268. luax_newtype(L, "Thread", THREAD_THREAD_T, (void*)t);
  269. t->lock();
  270. t->retain();
  271. t->unlock();
  272. }
  273. else
  274. lua_pushnil(L);
  275. return 1;
  276. }
  277. // List of functions to wrap.
  278. static const luaL_Reg module_functions[] = {
  279. { "newThread", w_newThread },
  280. { "getThread", w_getThread },
  281. { "getThreads", w_getThreads },
  282. { 0, 0 }
  283. };
  284. static const lua_CFunction types[] = {
  285. luaopen_thread,
  286. 0
  287. };
  288. int luaopen_love_thread(lua_State *L)
  289. {
  290. if(instance == 0)
  291. {
  292. try
  293. {
  294. instance = new ThreadModule();
  295. }
  296. catch(Exception & e)
  297. {
  298. return luaL_error(L, e.what());
  299. }
  300. }
  301. else
  302. instance->retain();
  303. WrappedModule w;
  304. w.module = instance;
  305. w.name = "thread";
  306. w.flags = MODULE_T;
  307. w.functions = module_functions;
  308. w.types = types;
  309. return luax_register_module(L, w);
  310. }
  311. }
  312. }
  313. }