runtime.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**
  2. * Copyright (c) 2006-2015 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 "config.h"
  21. #include "runtime.h"
  22. // LOVE
  23. #include "Module.h"
  24. #include "Object.h"
  25. #include "Reference.h"
  26. #include "StringMap.h"
  27. // C++
  28. #include <algorithm>
  29. #include <iostream>
  30. #include <cstdio>
  31. namespace love
  32. {
  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. Proxy *p = (Proxy *) lua_touserdata(L, 1);
  40. p->object->release();
  41. return 0;
  42. }
  43. static int w__tostring(lua_State *L)
  44. {
  45. lua_pushvalue(L, lua_upvalueindex(1));
  46. return 1;
  47. }
  48. static int w__typeOf(lua_State *L)
  49. {
  50. Proxy *p = (Proxy *)lua_touserdata(L, 1);
  51. Type t = luax_type(L, 2);
  52. luax_pushboolean(L, typeFlags[p->type][t]);
  53. return 1;
  54. }
  55. static int w__eq(lua_State *L)
  56. {
  57. Proxy *p1 = (Proxy *)lua_touserdata(L, 1);
  58. Proxy *p2 = (Proxy *)lua_touserdata(L, 2);
  59. luax_pushboolean(L, p1->object == p2->object);
  60. return 1;
  61. }
  62. Reference *luax_refif(lua_State *L, int type)
  63. {
  64. Reference *r = 0;
  65. // Create a reference only if the test succeeds.
  66. if (lua_type(L, -1) == type)
  67. r = new Reference(L);
  68. else // Pop the value even if it fails (but also if it succeeds).
  69. lua_pop(L, 1);
  70. return r;
  71. }
  72. void luax_printstack(lua_State *L)
  73. {
  74. for (int i = 1; i<=lua_gettop(L); i++)
  75. {
  76. std::cout << i << " - " << luaL_typename(L, i) << std::endl;
  77. }
  78. }
  79. bool luax_toboolean(lua_State *L, int idx)
  80. {
  81. return (lua_toboolean(L, idx) != 0);
  82. }
  83. void luax_pushboolean(lua_State *L, bool b)
  84. {
  85. lua_pushboolean(L, b ? 1 : 0);
  86. }
  87. bool luax_optboolean(lua_State *L, int idx, bool b)
  88. {
  89. if (lua_isboolean(L, idx) == 1)
  90. return (lua_toboolean(L, idx) == 1 ? true : false);
  91. return b;
  92. }
  93. std::string luax_tostring(lua_State *L, int idx)
  94. {
  95. size_t len;
  96. const char *str = lua_tolstring(L, idx, &len);
  97. return std::string(str, len);
  98. }
  99. std::string luax_checkstring(lua_State *L, int idx)
  100. {
  101. size_t len;
  102. const char *str = luaL_checklstring(L, idx, &len);
  103. return std::string(str, len);
  104. }
  105. void luax_pushstring(lua_State *L, const std::string &str)
  106. {
  107. lua_pushlstring(L, str.data(), str.size());
  108. }
  109. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
  110. {
  111. lua_getfield(L, table_index, key);
  112. bool retval;
  113. if (lua_isnoneornil(L, -1))
  114. retval = defaultValue;
  115. else
  116. retval = lua_toboolean(L, -1);
  117. lua_pop(L, 1);
  118. return retval;
  119. }
  120. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
  121. {
  122. lua_getfield(L, table_index, key);
  123. int retval;
  124. if (!lua_isnumber(L, -1))
  125. retval = defaultValue;
  126. else
  127. retval = (int) lua_tointeger(L, -1);
  128. lua_pop(L, 1);
  129. return retval;
  130. }
  131. int luax_assert_argc(lua_State *L, int min)
  132. {
  133. int argc = lua_gettop(L);
  134. if (argc < min)
  135. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  136. return 0;
  137. }
  138. int luax_assert_argc(lua_State *L, int min, int max)
  139. {
  140. int argc = lua_gettop(L);
  141. if (argc < min || argc > max)
  142. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  143. return 0;
  144. }
  145. int luax_assert_function(lua_State *L, int idx)
  146. {
  147. if (!lua_isfunction(L, idx))
  148. return luaL_error(L, "Argument must be of type \"function\".");
  149. return 0;
  150. }
  151. int luax_assert_nilerror(lua_State *L, int idx)
  152. {
  153. if (lua_isnoneornil(L, idx))
  154. {
  155. if (lua_isstring(L, idx + 1))
  156. return luaL_error(L, lua_tostring(L, idx + 1));
  157. else
  158. return luaL_error(L, "assertion failed!");
  159. }
  160. return 0;
  161. }
  162. void luax_setfuncs(lua_State *L, const luaL_Reg *l)
  163. {
  164. if (l == 0)
  165. return;
  166. for (; l->name != 0; l++)
  167. {
  168. lua_pushcfunction(L, l->func);
  169. lua_setfield(L, -2, l->name);
  170. }
  171. }
  172. int luax_register_module(lua_State *L, const WrappedModule &m)
  173. {
  174. // Put a reference to the C++ module in Lua.
  175. luax_insistregistry(L, REGISTRY_MODULES);
  176. Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  177. p->object = m.module;
  178. p->type = m.type;
  179. luaL_newmetatable(L, m.module->getName());
  180. lua_pushvalue(L, -1);
  181. lua_setfield(L, -2, "__index");
  182. lua_pushcfunction(L, w__gc);
  183. lua_setfield(L, -2, "__gc");
  184. lua_setmetatable(L, -2);
  185. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  186. lua_pop(L, 1);
  187. // Gets the love table.
  188. luax_insistglobal(L, "love");
  189. // Create new table for module.
  190. lua_newtable(L);
  191. // Register all the functions.
  192. if (m.functions != 0)
  193. luax_setfuncs(L, m.functions);
  194. // Register types.
  195. if (m.types != 0)
  196. for (const lua_CFunction *t = m.types; *t != 0; t++)
  197. (*t)(L);
  198. lua_pushvalue(L, -1);
  199. lua_setfield(L, -3, m.name); // love.graphics = table
  200. lua_remove(L, -2); // love
  201. // Register module instance
  202. Module::registerInstance(m.module);
  203. return 1;
  204. }
  205. int luax_preload(lua_State *L, lua_CFunction f, const char *name)
  206. {
  207. lua_getglobal(L, "package");
  208. lua_getfield(L, -1, "preload");
  209. lua_pushcfunction(L, f);
  210. lua_setfield(L, -2, name);
  211. lua_pop(L, 2);
  212. return 0;
  213. }
  214. int luax_register_type(lua_State *L, love::Type type, const luaL_Reg *f)
  215. {
  216. // Verify that this type name has a matching Type ID and type name mapping.
  217. const char *tname = "Invalid";
  218. if (!love::getType(type, tname))
  219. printf("Missing type name entry for type ID %d\n", type);
  220. // Get the place for storing and re-using instantiated love types.
  221. luax_getregistry(L, REGISTRY_TYPES);
  222. // Create registry._lovetypes if it doesn't exist yet.
  223. if (!lua_istable(L, -1))
  224. {
  225. lua_newtable(L);
  226. lua_replace(L, -2);
  227. // Create a metatable.
  228. lua_newtable(L);
  229. // metatable.__mode = "v". Weak userdata values.
  230. lua_pushliteral(L, "v");
  231. lua_setfield(L, -2, "__mode");
  232. // setmetatable(newtable, metatable)
  233. lua_setmetatable(L, -2);
  234. // registry._lovetypes = newtable
  235. lua_setfield(L, LUA_REGISTRYINDEX, "_lovetypes");
  236. }
  237. else
  238. lua_pop(L, 1);
  239. luaL_newmetatable(L, tname);
  240. // m.__index = m
  241. lua_pushvalue(L, -1);
  242. lua_setfield(L, -2, "__index");
  243. // setup gc
  244. lua_pushcfunction(L, w__gc);
  245. lua_setfield(L, -2, "__gc");
  246. // Add equality
  247. lua_pushcfunction(L, w__eq);
  248. lua_setfield(L, -2, "__eq");
  249. // Add tostring function.
  250. lua_pushstring(L, tname);
  251. lua_pushcclosure(L, w__tostring, 1);
  252. lua_setfield(L, -2, "__tostring");
  253. // Add tostring to as type() as well.
  254. lua_pushstring(L, tname);
  255. lua_pushcclosure(L, w__tostring, 1);
  256. lua_setfield(L, -2, "type");
  257. // Add typeOf
  258. lua_pushcfunction(L, w__typeOf);
  259. lua_setfield(L, -2, "typeOf");
  260. if (f != 0)
  261. luax_setfuncs(L, f);
  262. lua_pop(L, 1); // Pops metatable.
  263. return 0;
  264. }
  265. int luax_table_insert(lua_State *L, int tindex, int vindex, int pos)
  266. {
  267. if (tindex < 0)
  268. tindex = lua_gettop(L)+1+tindex;
  269. if (vindex < 0)
  270. vindex = lua_gettop(L)+1+vindex;
  271. if (pos == -1)
  272. {
  273. lua_pushvalue(L, vindex);
  274. lua_rawseti(L, tindex, (int) lua_objlen(L, tindex)+1);
  275. return 0;
  276. }
  277. else if (pos < 0)
  278. pos = (int) lua_objlen(L, tindex)+1+pos;
  279. for (int i = (int) lua_objlen(L, tindex)+1; i > pos; i--)
  280. {
  281. lua_rawgeti(L, tindex, i-1);
  282. lua_rawseti(L, tindex, i);
  283. }
  284. lua_pushvalue(L, vindex);
  285. lua_rawseti(L, tindex, pos);
  286. return 0;
  287. }
  288. int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
  289. {
  290. // Add the package loader to the package.loaders table.
  291. lua_getglobal(L, "package");
  292. if (lua_isnil(L, -1))
  293. return luaL_error(L, "Can't register searcher: package table does not exist.");
  294. lua_getfield(L, -1, "loaders");
  295. // Lua 5.2 renamed package.loaders to package.searchers.
  296. if (lua_isnil(L, -1))
  297. {
  298. lua_pop(L, 1);
  299. lua_getfield(L, -1, "searchers");
  300. }
  301. if (lua_isnil(L, -1))
  302. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  303. lua_pushcfunction(L, f);
  304. luax_table_insert(L, -2, -1, pos);
  305. lua_pop(L, 3);
  306. return 0;
  307. }
  308. void luax_rawnewtype(lua_State *L, love::Type type, love::Object *object)
  309. {
  310. Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  311. object->retain();
  312. u->object = object;
  313. u->type = type;
  314. const char *name = "Invalid";
  315. getType(type, name);
  316. luaL_newmetatable(L, name);
  317. lua_setmetatable(L, -2);
  318. }
  319. void luax_pushtype(lua_State *L, love::Type type, love::Object *object)
  320. {
  321. if (object == nullptr)
  322. {
  323. lua_pushnil(L);
  324. return;
  325. }
  326. // Fetch the registry table of instantiated types.
  327. luax_getregistry(L, REGISTRY_TYPES);
  328. // The table might not exist - it should be insisted in luax_register_type.
  329. if (!lua_istable(L, -1))
  330. {
  331. lua_pop(L, 1);
  332. return luax_rawnewtype(L, type, object);
  333. }
  334. // Get the value of lovetypes[object] on the stack.
  335. lua_pushlightuserdata(L, object);
  336. lua_gettable(L, -2);
  337. // If the Proxy userdata isn't in the instantiated types table yet, add it.
  338. if (lua_type(L, -1) != LUA_TUSERDATA)
  339. {
  340. lua_pop(L, 1);
  341. luax_rawnewtype(L, type, object);
  342. lua_pushlightuserdata(L, object);
  343. lua_pushvalue(L, -2);
  344. // lovetypes[object] = Proxy.
  345. lua_settable(L, -4);
  346. }
  347. // Remove the lovetypes table from the stack.
  348. lua_remove(L, -2);
  349. // Keep the Proxy userdata on the stack.
  350. }
  351. bool luax_istype(lua_State *L, int idx, love::Type type)
  352. {
  353. if (lua_type(L, idx) != LUA_TUSERDATA)
  354. return false;
  355. Proxy *p = (Proxy *) lua_touserdata(L, idx);
  356. return typeFlags[p->type][type];
  357. }
  358. int luax_getfunction(lua_State *L, const char *mod, const char *fn)
  359. {
  360. lua_getglobal(L, "love");
  361. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  362. lua_getfield(L, -1, mod);
  363. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  364. lua_getfield(L, -1, fn);
  365. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  366. lua_remove(L, -2); // remove mod
  367. lua_remove(L, -2); // remove fn
  368. return 0;
  369. }
  370. int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
  371. {
  372. // Convert to absolute index if necessary.
  373. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  374. idx += lua_gettop(L) + 1;
  375. // Convert string to a file.
  376. luax_getfunction(L, mod, fn);
  377. lua_pushvalue(L, idx); // The initial argument.
  378. lua_call(L, 1, 2); // Call the function, one arg, one return value (plus optional errstring.)
  379. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  380. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  381. lua_replace(L, idx); // Replace the initial argument with the new object.
  382. return 0;
  383. }
  384. int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  385. {
  386. luax_getfunction(L, mod, fn);
  387. for (int i = 0; i < n; i++)
  388. {
  389. lua_pushvalue(L, idxs[i]); // The arguments.
  390. }
  391. lua_call(L, n, 2); // Call the function, n args, one return value (plus optional errstring.)
  392. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  393. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  394. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  395. return 0;
  396. }
  397. int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
  398. {
  399. // Convert string to a file.
  400. luax_getfunction(L, mod, fn);
  401. lua_pushvalue(L, idx); // The initial argument.
  402. int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value.
  403. if (ret == 0)
  404. lua_replace(L, idx); // Replace the initial argument with the new object.
  405. return ret;
  406. }
  407. int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  408. {
  409. luax_getfunction(L, mod, fn);
  410. for (int i = 0; i < n; i++)
  411. {
  412. lua_pushvalue(L, idxs[i]); // The arguments.
  413. }
  414. int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value.
  415. if (ret == 0)
  416. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  417. return ret;
  418. }
  419. int luax_insist(lua_State *L, int idx, const char *k)
  420. {
  421. // Convert to absolute index if necessary.
  422. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  423. idx += lua_gettop(L) + 1;
  424. lua_getfield(L, idx, k);
  425. // Create if necessary.
  426. if (!lua_istable(L, -1))
  427. {
  428. lua_pop(L, 1); // Pop the non-table.
  429. lua_newtable(L);
  430. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  431. lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table)
  432. }
  433. return 1;
  434. }
  435. int luax_insistglobal(lua_State *L, const char *k)
  436. {
  437. lua_getglobal(L, k);
  438. if (!lua_istable(L, -1))
  439. {
  440. lua_pop(L, 1); // Pop the non-table.
  441. lua_newtable(L);
  442. lua_pushvalue(L, -1);
  443. lua_setglobal(L, k);
  444. }
  445. return 1;
  446. }
  447. int luax_insistlove(lua_State *L, const char *k)
  448. {
  449. luax_insistglobal(L, "love");
  450. luax_insist(L, -1, k);
  451. // The love table should be replaced with the top stack
  452. // item. Only the reqested table should remain on the stack.
  453. lua_replace(L, -2);
  454. return 1;
  455. }
  456. int luax_getlove(lua_State *L, const char *k)
  457. {
  458. lua_getglobal(L, "love");
  459. if (!lua_isnil(L, -1))
  460. {
  461. lua_getfield(L, -1, k);
  462. lua_replace(L, -2);
  463. }
  464. return 1;
  465. }
  466. int luax_insistregistry(lua_State *L, Registry r)
  467. {
  468. switch (r)
  469. {
  470. case REGISTRY_GC:
  471. return luax_insistlove(L, "_gc");
  472. case REGISTRY_MODULES:
  473. return luax_insistlove(L, "_modules");
  474. case REGISTRY_TYPES:
  475. return luax_insist(L, LUA_REGISTRYINDEX, "_lovetypes");
  476. default:
  477. return luaL_error(L, "Attempted to use invalid registry.");
  478. }
  479. }
  480. int luax_getregistry(lua_State *L, Registry r)
  481. {
  482. switch (r)
  483. {
  484. case REGISTRY_GC:
  485. return luax_getlove(L, "_gc");
  486. case REGISTRY_MODULES:
  487. return luax_getlove(L, "_modules");
  488. case REGISTRY_TYPES:
  489. lua_getfield(L, LUA_REGISTRYINDEX, "_lovetypes");
  490. return 1;
  491. default:
  492. return luaL_error(L, "Attempted to use invalid registry.");
  493. }
  494. }
  495. extern "C" int luax_typerror(lua_State *L, int narg, const char *tname)
  496. {
  497. int argtype = lua_type(L, narg);
  498. const char *argtname = 0;
  499. // We want to use the love type name for userdata, if possible.
  500. if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "__tostring") != 0)
  501. {
  502. lua_pushvalue(L, narg);
  503. if (lua_pcall(L, 1, 1, 0) == 0 && lua_type(L, -1) == LUA_TSTRING)
  504. {
  505. argtname = lua_tostring(L, -1);
  506. // Non-love userdata might have a tostring metamethod which doesn't
  507. // describe its type, so we only use __tostring for love types.
  508. love::Type t;
  509. if (!love::getType(argtname, t))
  510. argtname = 0;
  511. }
  512. }
  513. if (argtname == 0)
  514. argtname = lua_typename(L, argtype);
  515. const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, argtname);
  516. return luaL_argerror(L, narg, msg);
  517. }
  518. Type luax_type(lua_State *L, int idx)
  519. {
  520. Type t = INVALID_ID;
  521. getType(luaL_checkstring(L, idx), t);
  522. return t;
  523. }
  524. } // love