runtime.cpp 12 KB

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