2
0

runtime.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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, const std::string &str)
  116. {
  117. lua_pushlstring(L, str.data(), str.size());
  118. }
  119. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
  120. {
  121. lua_getfield(L, table_index, key);
  122. bool retval;
  123. if (lua_isnoneornil(L, -1))
  124. retval = defaultValue;
  125. else
  126. retval = lua_toboolean(L, -1);
  127. lua_pop(L, 1);
  128. return retval;
  129. }
  130. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
  131. {
  132. lua_getfield(L, table_index, key);
  133. int retval;
  134. if (!lua_isnumber(L, -1))
  135. retval = defaultValue;
  136. else
  137. retval = lua_tonumber(L, -1);
  138. lua_pop(L, 1);
  139. return retval;
  140. }
  141. int luax_assert_argc(lua_State *L, int min)
  142. {
  143. int argc = lua_gettop(L);
  144. if (argc < min)
  145. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  146. return 0;
  147. }
  148. int luax_assert_argc(lua_State *L, int min, int max)
  149. {
  150. int argc = lua_gettop(L);
  151. if (argc < min || argc > max)
  152. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  153. return 0;
  154. }
  155. int luax_assert_function(lua_State *L, int n)
  156. {
  157. if (!lua_isfunction(L, n))
  158. return luaL_error(L, "Argument must be of type \"function\".");
  159. return 0;
  160. }
  161. int luax_register_module(lua_State *L, const WrappedModule &m)
  162. {
  163. // Put a reference to the C++ module in Lua.
  164. luax_getregistry(L, REGISTRY_MODULES);
  165. Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  166. p->own = true;
  167. p->data = m.module;
  168. p->flags = m.flags;
  169. luaL_newmetatable(L, m.module->getName());
  170. lua_pushvalue(L, -1);
  171. lua_setfield(L, -2, "__index");
  172. lua_pushcfunction(L, w__gc);
  173. lua_setfield(L, -2, "__gc");
  174. lua_setmetatable(L, -2);
  175. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  176. lua_pop(L, 1);
  177. // Gets the love table.
  178. luax_insistglobal(L, "love");
  179. // Create new table for module.
  180. lua_newtable(L);
  181. // Register all the functions.
  182. luaL_register(L, 0, m.functions);
  183. // Register types.
  184. if (m.types != 0)
  185. for (const lua_CFunction *t = m.types; *t != 0; t++)
  186. (*t)(L);
  187. lua_pushvalue(L, -1);
  188. lua_setfield(L, -3, m.name); // love.graphics = table
  189. lua_remove(L, -2); // love
  190. // Register module instance
  191. Module::registerInstance(m.module);
  192. return 1;
  193. }
  194. int luax_preload(lua_State *L, lua_CFunction f, const char *name)
  195. {
  196. lua_getglobal(L, "package");
  197. lua_getfield(L, -1, "preload");
  198. lua_pushcfunction(L, f);
  199. lua_setfield(L, -2, name);
  200. lua_pop(L, 2);
  201. return 0;
  202. }
  203. int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f)
  204. {
  205. luaL_newmetatable(L, tname);
  206. // m.__index = m
  207. lua_pushvalue(L, -1);
  208. lua_setfield(L, -2, "__index");
  209. // setup gc
  210. lua_pushcfunction(L, w__gc);
  211. lua_setfield(L, -2, "__gc");
  212. // Add equality
  213. lua_pushcfunction(L, w__eq);
  214. lua_setfield(L, -2, "__eq");
  215. // Add tostring function.
  216. lua_pushstring(L, tname);
  217. lua_pushcclosure(L, w__tostring, 1);
  218. lua_setfield(L, -2, "__tostring");
  219. // Add tostring to as type() as well.
  220. lua_pushstring(L, tname);
  221. lua_pushcclosure(L, w__tostring, 1);
  222. lua_setfield(L, -2, "type");
  223. // Add typeOf
  224. lua_pushcfunction(L, w__typeOf);
  225. lua_setfield(L, -2, "typeOf");
  226. if (f != 0)
  227. luaL_register(L, 0, f);
  228. lua_pop(L, 1); // Pops metatable.
  229. return 0;
  230. }
  231. int luax_table_insert(lua_State *L, int tindex, int vindex, int pos)
  232. {
  233. if (tindex < 0)
  234. tindex = lua_gettop(L)+1+tindex;
  235. if (vindex < 0)
  236. vindex = lua_gettop(L)+1+vindex;
  237. if (pos == -1)
  238. {
  239. lua_pushvalue(L, vindex);
  240. lua_rawseti(L, tindex, lua_objlen(L, tindex)+1);
  241. return 0;
  242. }
  243. else if (pos < 0)
  244. pos = lua_objlen(L, tindex)+1+pos;
  245. for (int i = lua_objlen(L, tindex)+1; i > pos; i--)
  246. {
  247. lua_rawgeti(L, tindex, i-1);
  248. lua_rawseti(L, tindex, i);
  249. }
  250. lua_pushvalue(L, vindex);
  251. lua_rawseti(L, tindex, pos);
  252. return 0;
  253. }
  254. int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
  255. {
  256. // Add the package loader to the package.loaders table.
  257. lua_getglobal(L, "package");
  258. if (lua_isnil(L, -1))
  259. return luaL_error(L, "Can't register searcher: package table does not exist.");
  260. lua_getfield(L, -1, "loaders");
  261. if (lua_isnil(L, -1))
  262. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  263. lua_pushcfunction(L, f);
  264. luax_table_insert(L, -2, -1, pos);
  265. lua_pop(L, 3);
  266. return 0;
  267. }
  268. void luax_newtype(lua_State *L, const char *name, bits flags, void *data, bool own)
  269. {
  270. Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  271. u->data = data;
  272. u->flags = flags;
  273. u->own = own;
  274. luaL_newmetatable(L, name);
  275. lua_setmetatable(L, -2);
  276. }
  277. bool luax_istype(lua_State *L, int idx, love::bits type)
  278. {
  279. if (lua_isuserdata(L, idx) == 0)
  280. return false;
  281. return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
  282. }
  283. int luax_getfunction(lua_State *L, const char *mod, const char *fn)
  284. {
  285. lua_getglobal(L, "love");
  286. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  287. lua_getfield(L, -1, mod);
  288. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  289. lua_getfield(L, -1, fn);
  290. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  291. lua_remove(L, -2); // remove mod
  292. lua_remove(L, -2); // remove fn
  293. return 0;
  294. }
  295. int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
  296. {
  297. // Convert string to a file.
  298. luax_getfunction(L, mod, fn);
  299. lua_pushvalue(L, idx); // The initial argument.
  300. lua_call(L, 1, 1); // Call the function, one arg, one return value.
  301. lua_replace(L, idx); // Replace the initial argument with the new object.
  302. return 0;
  303. }
  304. int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  305. {
  306. luax_getfunction(L, mod, fn);
  307. for (int i = 0; i < n; i++)
  308. {
  309. lua_pushvalue(L, idxs[i]); // The arguments.
  310. }
  311. lua_call(L, n, 1); // Call the function, n args, one return value.
  312. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  313. return 0;
  314. }
  315. int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
  316. {
  317. // Convert string to a file.
  318. luax_getfunction(L, mod, fn);
  319. lua_pushvalue(L, idx); // The initial argument.
  320. int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value.
  321. if (ret == 0)
  322. lua_replace(L, idx); // Replace the initial argument with the new object.
  323. return ret;
  324. }
  325. int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  326. {
  327. luax_getfunction(L, mod, fn);
  328. for (int i = 0; i < n; i++)
  329. {
  330. lua_pushvalue(L, idxs[i]); // The arguments.
  331. }
  332. int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value.
  333. if (ret == 0)
  334. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  335. return ret;
  336. }
  337. int luax_strtofile(lua_State *L, int idx)
  338. {
  339. return luax_convobj(L, idx, "filesystem", "newFile");
  340. }
  341. int luax_filetodata(lua_State *L, int idx)
  342. {
  343. return luax_convobj(L, idx, "filesystem", "read");
  344. }
  345. int luax_insist(lua_State *L, int idx, const char *k)
  346. {
  347. // Convert to absolute index if necessary.
  348. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  349. idx = lua_gettop(L) + ++idx;
  350. lua_getfield(L, idx, k);
  351. // Create if necessary.
  352. if (!lua_istable(L, -1))
  353. {
  354. lua_pop(L, 1); // Pop the non-table.
  355. lua_newtable(L);
  356. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  357. lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table)
  358. }
  359. return 1;
  360. }
  361. int luax_insistglobal(lua_State *L, const char *k)
  362. {
  363. lua_getglobal(L, k);
  364. if (!lua_istable(L, -1))
  365. {
  366. lua_pop(L, 1); // Pop the non-table.
  367. lua_newtable(L);
  368. lua_pushvalue(L, -1);
  369. lua_setglobal(L, k);
  370. }
  371. return 1;
  372. }
  373. int luax_insistlove(lua_State *L, const char *k)
  374. {
  375. luax_insistglobal(L, "love");
  376. luax_insist(L, -1, k);
  377. // The love table should be replaced with the top stack
  378. // item. Only the reqested table should remain on the stack.
  379. lua_replace(L, -2);
  380. return 1;
  381. }
  382. int luax_getregistry(lua_State *L, Registry r)
  383. {
  384. switch (r)
  385. {
  386. case REGISTRY_GC:
  387. return luax_insistlove(L, "_gc");
  388. case REGISTRY_MODULES:
  389. return luax_insistlove(L, "_modules");
  390. default:
  391. return luaL_error(L, "Attempted to use invalid registry.");
  392. }
  393. }
  394. StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] =
  395. {
  396. {"Invalid", INVALID_ID},
  397. {"Object", OBJECT_ID},
  398. {"Data", DATA_ID},
  399. {"Module", MODULE_ID},
  400. // Filesystem
  401. {"File", FILESYSTEM_FILE_ID},
  402. {"FileData", FILESYSTEM_FILE_DATA_ID},
  403. // Font
  404. {"GlyphData", FONT_GLYPH_DATA_ID},
  405. {"Rasterizer", FONT_RASTERIZER_ID},
  406. // Graphics
  407. {"Drawable", GRAPHICS_DRAWABLE_ID},
  408. {"DrawGable", GRAPHICS_DRAWGABLE_ID},
  409. {"Image", GRAPHICS_IMAGE_ID},
  410. {"Geometry", GRAPHICS_GEOMETRY_ID},
  411. {"Font", GRAPHICS_FONT_ID},
  412. {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID},
  413. {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID},
  414. {"Canvas", GRAPHICS_CANVAS_ID},
  415. {"Shader", GRAPHICS_SHADER_ID},
  416. // Image
  417. {"ImageData", IMAGE_IMAGE_DATA_ID},
  418. {"CompressedData", IMAGE_COMPRESSED_DATA_ID},
  419. // Math
  420. {"RandomGenerator", MATH_RANDOM_GENERATOR_ID},
  421. // Audio
  422. {"Source", AUDIO_SOURCE_ID},
  423. // Sound
  424. {"SoundData", SOUND_SOUND_DATA_ID},
  425. {"Decoder", SOUND_DECODER_ID},
  426. // Physics
  427. {"World", PHYSICS_WORLD_ID},
  428. {"Contact", PHYSICS_CONTACT_ID},
  429. {"Body", PHYSICS_BODY_ID},
  430. {"Fixture", PHYSICS_FIXTURE_ID},
  431. {"Shape", PHYSICS_SHAPE_ID},
  432. {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID},
  433. {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID},
  434. {"EdgeShape", PHYSICS_EDGE_SHAPE_ID},
  435. {"ChainShape", PHYSICS_CHAIN_SHAPE_ID},
  436. {"Joint", PHYSICS_JOINT_ID},
  437. {"MouseJoint", PHYSICS_MOUSE_JOINT_ID},
  438. {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID},
  439. {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID},
  440. {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID},
  441. {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID},
  442. {"GearJoint", PHYSICS_GEAR_JOINT_ID},
  443. {"WeldJoint", PHYSICS_WELD_JOINT_ID},
  444. {"RopeJoint", PHYSICS_ROPE_JOINT_ID},
  445. {"WheelJoint", PHYSICS_WHEEL_JOINT_ID},
  446. // Thread
  447. {"Thread", THREAD_THREAD_ID},
  448. {"Channel", THREAD_CHANNEL_ID},
  449. // The modules themselves. Only add abstracted modules here.
  450. {"filesystem", MODULE_FILESYSTEM_ID},
  451. {"graphics", MODULE_GRAPHICS_ID},
  452. {"image", MODULE_IMAGE_ID},
  453. {"sound", MODULE_SOUND_ID},
  454. };
  455. StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries));
  456. Type luax_type(lua_State *L, int idx)
  457. {
  458. Type t = INVALID_ID;
  459. types.find(luaL_checkstring(L, idx), t);
  460. return t;
  461. }
  462. } // love