runtime.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**
  2. * Copyright (c) 2006-2011 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 "runtime.h"
  21. // LOVE
  22. #include "Module.h"
  23. #include "Object.h"
  24. #include "Reference.h"
  25. #include "StringMap.h"
  26. #include <thread/threads.h>
  27. // STD
  28. #include <iostream>
  29. namespace love
  30. {
  31. static thread::Mutex *gcmutex = 0;
  32. void *_gcmutex = 0;
  33. unsigned int _gcthread = 0;
  34. /**
  35. * Called when an object is collected. The object is released
  36. * once in this function, possibly deleting it.
  37. **/
  38. static int w__gc(lua_State * L)
  39. {
  40. if (!gcmutex)
  41. {
  42. gcmutex = new thread::Mutex();
  43. _gcmutex = (void*) gcmutex;
  44. }
  45. Proxy * p = (Proxy *)lua_touserdata(L, 1);
  46. Object * t = (Object *)p->data;
  47. if (p->own)
  48. {
  49. thread::Lock lock(gcmutex);
  50. _gcthread = thread::ThreadBase::threadId();
  51. t->release();
  52. }
  53. return 0;
  54. }
  55. static int w__tostring(lua_State * L)
  56. {
  57. lua_pushvalue(L, lua_upvalueindex(1));
  58. return 1;
  59. }
  60. static int w__typeOf(lua_State * L)
  61. {
  62. Proxy * p = (Proxy *)lua_touserdata(L, 1);
  63. Type t = luax_type(L, 2);
  64. luax_pushboolean(L, p->flags[t]);
  65. return 1;
  66. }
  67. static int w__eq(lua_State * L)
  68. {
  69. Proxy * p1 = (Proxy *)lua_touserdata(L, 1);
  70. Proxy * p2 = (Proxy *)lua_touserdata(L, 2);
  71. luax_pushboolean(L, p1->data == p2->data);
  72. return 1;
  73. }
  74. Reference * luax_refif(lua_State * L, int type)
  75. {
  76. Reference * r = 0;
  77. // Create a reference only if the test succeeds.
  78. if (lua_type(L, -1) == type)
  79. r = new Reference(L);
  80. else // Pop the value even if it fails (but also if it succeeds).
  81. lua_pop(L, 1);
  82. return r;
  83. }
  84. void luax_printstack(lua_State * L)
  85. {
  86. for (int i = 1;i<=lua_gettop(L);i++)
  87. {
  88. std::cout << i << " - " << luaL_typename(L, i) << std::endl;
  89. }
  90. }
  91. bool luax_toboolean(lua_State * L, int idx)
  92. {
  93. return (lua_toboolean(L, idx) != 0);
  94. }
  95. void luax_pushboolean(lua_State * L, bool b)
  96. {
  97. lua_pushboolean(L, b ? 1 : 0);
  98. }
  99. bool luax_optboolean(lua_State * L, int idx, bool b)
  100. {
  101. if (lua_isboolean(L, idx) == 1)
  102. return (lua_toboolean(L, idx) == 1 ? true : false);
  103. return b;
  104. }
  105. std::string luax_checkstring(lua_State * L, int idx)
  106. {
  107. size_t len;
  108. const char * str = luaL_checklstring(L, idx, &len);
  109. return std::string(str, len);
  110. }
  111. void luax_pushstring(lua_State * L, std::string str)
  112. {
  113. lua_pushlstring(L, str.data(), str.size());
  114. }
  115. int luax_assert_argc(lua_State * L, int min)
  116. {
  117. int argc = lua_gettop(L);
  118. if ( argc < min )
  119. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  120. return 0;
  121. }
  122. int luax_assert_argc(lua_State * L, int min, int max)
  123. {
  124. int argc = lua_gettop(L);
  125. if ( argc < min || argc > max)
  126. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  127. return 0;
  128. }
  129. int luax_assert_function(lua_State * L, int n)
  130. {
  131. if (!lua_isfunction(L, n))
  132. return luaL_error(L, "Argument must be of type \"function\".");
  133. return 0;
  134. }
  135. int luax_register_module(lua_State * L, const WrappedModule & m)
  136. {
  137. // Put a reference to the C++ module in Lua.
  138. luax_getregistry(L, REGISTRY_MODULES);
  139. Proxy * p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  140. p->own = true;
  141. p->data = m.module;
  142. p->flags = m.flags;
  143. luaL_newmetatable(L, m.module->getName());
  144. lua_pushvalue(L, -1);
  145. lua_setfield(L, -2, "__index");
  146. lua_pushcfunction(L, w__gc);
  147. lua_setfield(L, -2, "__gc");
  148. lua_setmetatable(L, -2);
  149. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  150. lua_pop(L, 1);
  151. // Gets the love table.
  152. luax_insistglobal(L, "love");
  153. // Create new table for module.
  154. lua_newtable(L);
  155. // Register all the functions.
  156. luaL_register(L, 0, m.functions);
  157. // Register types.
  158. if (m.types != 0)
  159. for (const lua_CFunction * t = m.types; *t != 0; t++)
  160. (*t)(L);
  161. lua_pushvalue(L, -1);
  162. lua_setfield(L, -3, m.name); // love.graphics = table
  163. lua_remove(L, -2); // love
  164. return 1;
  165. }
  166. int luax_preload(lua_State * L, lua_CFunction f, const char * name)
  167. {
  168. lua_getglobal(L, "package");
  169. lua_getfield(L, -1, "preload");
  170. lua_pushcfunction(L, f);
  171. lua_setfield(L, -2, name);
  172. lua_pop(L, 2);
  173. return 0;
  174. }
  175. int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f)
  176. {
  177. luaL_newmetatable(L, tname);
  178. // m.__index = m
  179. lua_pushvalue(L, -1);
  180. lua_setfield(L, -2, "__index");
  181. // setup gc
  182. lua_pushcfunction(L, w__gc);
  183. lua_setfield(L, -2, "__gc");
  184. // Add equality
  185. lua_pushcfunction(L, w__eq);
  186. lua_setfield(L, -2, "__eq");
  187. // Add tostring function.
  188. lua_pushstring(L, tname);
  189. lua_pushcclosure(L, w__tostring, 1);
  190. lua_setfield(L, -2, "__tostring");
  191. // Add tostring to as type() as well.
  192. lua_pushstring(L, tname);
  193. lua_pushcclosure(L, w__tostring, 1);
  194. lua_setfield(L, -2, "type");
  195. // Add typeOf
  196. lua_pushcfunction(L, w__typeOf);
  197. lua_setfield(L, -2, "typeOf");
  198. if (f != 0)
  199. luaL_register(L, 0, f);
  200. lua_pop(L, 1); // Pops metatable.
  201. return 0;
  202. }
  203. int luax_table_insert(lua_State * L, int tindex, int vindex, int pos)
  204. {
  205. if (tindex < 0)
  206. tindex = lua_gettop(L)+1+tindex;
  207. if (vindex < 0)
  208. vindex = lua_gettop(L)+1+vindex;
  209. if (pos == -1)
  210. {
  211. lua_pushvalue(L, vindex);
  212. lua_rawseti(L, tindex, lua_objlen(L, tindex)+1);
  213. return 0;
  214. }
  215. else if (pos < 0)
  216. pos = lua_objlen(L, tindex)+1+pos;
  217. for (int i = lua_objlen(L, tindex)+1; i > pos; i--)
  218. {
  219. lua_rawgeti(L, tindex, i-1);
  220. lua_rawseti(L, tindex, i);
  221. }
  222. lua_pushvalue(L, vindex);
  223. lua_rawseti(L, tindex, pos);
  224. return 0;
  225. }
  226. int luax_register_searcher(lua_State * L, lua_CFunction f, int pos)
  227. {
  228. // Add the package loader to the package.loaders table.
  229. lua_getglobal(L, "package");
  230. if (lua_isnil(L, -1))
  231. return luaL_error(L, "Can't register searcher: package table does not exist.");
  232. lua_getfield(L, -1, "loaders");
  233. if (lua_isnil(L, -1))
  234. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  235. lua_pushcfunction(L, f);
  236. luax_table_insert(L, -2, -1, pos);
  237. lua_pop(L, 3);
  238. return 0;
  239. }
  240. void luax_newtype(lua_State * L, const char * name, bits flags, void * data, bool own)
  241. {
  242. Proxy * u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  243. u->data = data;
  244. u->flags = flags;
  245. u->own = own;
  246. luaL_newmetatable(L, name);
  247. lua_setmetatable(L, -2);
  248. }
  249. bool luax_istype(lua_State * L, int idx, love::bits type)
  250. {
  251. if (lua_isuserdata(L, idx) == 0)
  252. return false;
  253. return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
  254. }
  255. int luax_getfunction(lua_State * L, const char * mod, const char * fn)
  256. {
  257. lua_getglobal(L, "love");
  258. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  259. lua_getfield(L, -1, mod);
  260. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  261. lua_getfield(L, -1, fn);
  262. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  263. lua_remove(L, -2); // remove mod
  264. lua_remove(L, -2); // remove fn
  265. return 0;
  266. }
  267. int luax_convobj(lua_State * L, int idx, const char * mod, const char * fn)
  268. {
  269. // Convert string to a file.
  270. luax_getfunction(L, mod, fn);
  271. lua_pushvalue(L, idx); // The initial argument.
  272. lua_call(L, 1, 1); // Call the function, one arg, one return value.
  273. lua_replace(L, idx); // Replace the initial argument with the new object.
  274. return 0;
  275. }
  276. int luax_convobj(lua_State * L, int idxs[], int n, const char * mod, const char * fn)
  277. {
  278. luax_getfunction(L, mod, fn);
  279. for (int i = 0; i < n; i++)
  280. {
  281. lua_pushvalue(L, idxs[i]); // The arguments.
  282. }
  283. lua_call(L, n, 1); // Call the function, n args, one return value.
  284. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  285. return 0;
  286. }
  287. int luax_strtofile(lua_State * L, int idx)
  288. {
  289. return luax_convobj(L, idx, "filesystem", "newFile");
  290. }
  291. int luax_filetodata(lua_State * L, int idx)
  292. {
  293. return luax_convobj(L, idx, "filesystem", "read");
  294. }
  295. int luax_insist(lua_State * L, int idx, const char * k)
  296. {
  297. lua_getfield(L, idx, k);
  298. // Create if necessary.
  299. if (!lua_istable(L, -1))
  300. {
  301. lua_pop(L, 1); // Pop the non-table.
  302. lua_newtable(L);
  303. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  304. lua_setfield(L, -3, k); // k[idx] = table
  305. }
  306. return 1;
  307. }
  308. int luax_insistglobal(lua_State * L, const char * k)
  309. {
  310. lua_getglobal(L, k);
  311. if (!lua_istable(L, -1))
  312. {
  313. lua_pop(L, 1); // Pop the non-table.
  314. lua_newtable(L);
  315. lua_pushvalue(L, -1);
  316. lua_setglobal(L, k);
  317. }
  318. return 1;
  319. }
  320. int luax_insistlove(lua_State * L, const char * k)
  321. {
  322. luax_insistglobal(L, "love");
  323. luax_insist(L, -1, k);
  324. // The love table should be replaced with the top stack
  325. // item. Only the reqested table should remain on the stack.
  326. lua_replace(L, -2);
  327. return 1;
  328. }
  329. int luax_getregistry(lua_State * L, Registry r)
  330. {
  331. switch(r)
  332. {
  333. case REGISTRY_GC:
  334. return luax_insistlove(L, "_gc");
  335. case REGISTRY_MODULES:
  336. return luax_insistlove(L, "_modules");
  337. default:
  338. return luaL_error(L, "Attempted to use invalid registry.");
  339. }
  340. }
  341. StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] =
  342. {
  343. {"Invalid", INVALID_ID},
  344. {"Object", OBJECT_ID},
  345. {"Data", DATA_ID},
  346. {"Module", MODULE_ID},
  347. // Filesystem
  348. {"File", FILESYSTEM_FILE_ID},
  349. {"FileData", FILESYSTEM_FILE_DATA_ID},
  350. // Font
  351. {"GlyphData", FONT_GLYPH_DATA_ID},
  352. {"Rasterizer", FONT_RASTERIZER_ID},
  353. // Graphics
  354. {"Drawable", GRAPHICS_DRAWABLE_ID},
  355. {"Image", GRAPHICS_IMAGE_ID},
  356. {"Quad", GRAPHICS_QUAD_ID},
  357. {"Font", GRAPHICS_FONT_ID},
  358. {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID},
  359. {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID},
  360. {"Canvas", GRAPHICS_CANVAS_ID},
  361. // Image
  362. {"ImageData", IMAGE_IMAGE_DATA_ID},
  363. // Audio
  364. {"Source", AUDIO_SOURCE_ID},
  365. // Sound
  366. {"SoundData", SOUND_SOUND_DATA_ID},
  367. {"Decoder", SOUND_DECODER_ID},
  368. // Physics
  369. {"World", PHYSICS_WORLD_ID},
  370. {"Contact", PHYSICS_CONTACT_ID},
  371. {"Body", PHYSICS_BODY_ID},
  372. {"Shape", PHYSICS_SHAPE_ID},
  373. {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID},
  374. {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID},
  375. {"Joint", PHYSICS_JOINT_ID},
  376. {"MouseJoint", PHYSICS_MOUSE_JOINT_ID},
  377. {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID},
  378. {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID},
  379. {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID},
  380. {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID},
  381. {"GearJoint", PHYSICS_GEAR_JOINT_ID},
  382. // Thread
  383. {"Thread", THREAD_THREAD_ID},
  384. // The modules themselves. Only add abstracted modules here.
  385. {"filesystem", MODULE_FILESYSTEM_ID},
  386. {"image", MODULE_IMAGE_ID},
  387. {"sound", MODULE_SOUND_ID},
  388. };
  389. StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries));
  390. Type luax_type(lua_State * L, int idx)
  391. {
  392. Type t = INVALID_ID;
  393. types.find(luaL_checkstring(L, idx), t);
  394. return t;
  395. }
  396. } // love