runtime.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. * Copyright (c) 2006-2017 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. #include <sstream>
  32. namespace love
  33. {
  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. Proxy *p = (Proxy *) lua_touserdata(L, 1);
  41. if (p->object != nullptr)
  42. {
  43. p->object->release();
  44. p->object = nullptr;
  45. }
  46. return 0;
  47. }
  48. static int w__tostring(lua_State *L)
  49. {
  50. Proxy *p = (Proxy *) lua_touserdata(L, 1);
  51. const char *typname = lua_tostring(L, lua_upvalueindex(1));
  52. lua_pushfstring(L, "%s: %p", typname, p->object);
  53. return 1;
  54. }
  55. static int w__type(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. if (!t)
  65. luax_pushboolean(L, false);
  66. else
  67. luax_pushboolean(L, p->type->isa(*t));
  68. return 1;
  69. }
  70. static int w__eq(lua_State *L)
  71. {
  72. Proxy *p1 = (Proxy *)lua_touserdata(L, 1);
  73. Proxy *p2 = (Proxy *)lua_touserdata(L, 2);
  74. luax_pushboolean(L, p1->object == p2->object && p1->object != nullptr);
  75. return 1;
  76. }
  77. static int w__release(lua_State *L)
  78. {
  79. Proxy *p = (Proxy *) lua_touserdata(L, 1);
  80. Object *object = p->object;
  81. if (object != nullptr)
  82. {
  83. p->object = nullptr;
  84. object->release();
  85. // Fetch the registry table of instantiated objects.
  86. luax_getregistry(L, REGISTRY_OBJECTS);
  87. if (lua_istable(L, -1))
  88. {
  89. // loveobjects[object] = nil
  90. lua_pushlightuserdata(L, object);
  91. lua_pushnil(L);
  92. lua_settable(L, -3);
  93. }
  94. lua_pop(L, 1);
  95. }
  96. luax_pushboolean(L, object != nullptr);
  97. return 1;
  98. }
  99. Reference *luax_refif(lua_State *L, int type)
  100. {
  101. Reference *r = nullptr;
  102. // Create a reference only if the test succeeds.
  103. if (lua_type(L, -1) == type)
  104. r = new Reference(L);
  105. else // Pop the value manually if it fails (done by Reference if it succeeds).
  106. lua_pop(L, 1);
  107. return r;
  108. }
  109. void luax_printstack(lua_State *L)
  110. {
  111. for (int i = 1; i <= lua_gettop(L); i++)
  112. std::cout << i << " - " << luaL_typename(L, i) << std::endl;
  113. }
  114. int luax_traceback(lua_State *L)
  115. {
  116. if (!lua_isstring(L, 1)) // 'message' not a string?
  117. return 1; // keep it intact
  118. lua_getglobal(L, "debug");
  119. if (!lua_istable(L, -1))
  120. {
  121. lua_pop(L, 1);
  122. return 1;
  123. }
  124. lua_getfield(L, -1, "traceback");
  125. if (!lua_isfunction(L, -1))
  126. {
  127. lua_pop(L, 2);
  128. return 1;
  129. }
  130. lua_pushvalue(L, 1); // pass error message
  131. lua_pushinteger(L, 2); // skip this function and traceback
  132. lua_call(L, 2, 1); // call debug.traceback
  133. return 1;
  134. }
  135. bool luax_isarrayoftables(lua_State *L, int idx)
  136. {
  137. if (!lua_istable(L, idx))
  138. return false;
  139. lua_rawgeti(L, idx, 1);
  140. bool tableoftables = lua_istable(L, -1);
  141. lua_pop(L, 1);
  142. return tableoftables;
  143. }
  144. bool luax_toboolean(lua_State *L, int idx)
  145. {
  146. return (lua_toboolean(L, idx) != 0);
  147. }
  148. bool luax_checkboolean(lua_State *L, int idx)
  149. {
  150. luaL_checktype(L, idx, LUA_TBOOLEAN);
  151. return luax_toboolean(L, idx);
  152. }
  153. void luax_pushboolean(lua_State *L, bool b)
  154. {
  155. lua_pushboolean(L, b ? 1 : 0);
  156. }
  157. bool luax_optboolean(lua_State *L, int idx, bool b)
  158. {
  159. if (lua_isboolean(L, idx) == 1)
  160. return (lua_toboolean(L, idx) == 1 ? true : false);
  161. return b;
  162. }
  163. std::string luax_tostring(lua_State *L, int idx)
  164. {
  165. size_t len;
  166. const char *str = lua_tolstring(L, idx, &len);
  167. return std::string(str, len);
  168. }
  169. std::string luax_checkstring(lua_State *L, int idx)
  170. {
  171. size_t len;
  172. const char *str = luaL_checklstring(L, idx, &len);
  173. return std::string(str, len);
  174. }
  175. void luax_pushstring(lua_State *L, const std::string &str)
  176. {
  177. lua_pushlstring(L, str.data(), str.size());
  178. }
  179. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
  180. {
  181. lua_getfield(L, table_index, key);
  182. bool retval;
  183. if (lua_isnoneornil(L, -1))
  184. retval = defaultValue;
  185. else
  186. retval = lua_toboolean(L, -1) != 0;
  187. lua_pop(L, 1);
  188. return retval;
  189. }
  190. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
  191. {
  192. lua_getfield(L, table_index, key);
  193. int retval;
  194. if (!lua_isnumber(L, -1))
  195. retval = defaultValue;
  196. else
  197. retval = (int) lua_tointeger(L, -1);
  198. lua_pop(L, 1);
  199. return retval;
  200. }
  201. double luax_numberflag(lua_State *L, int table_index, const char *key, double defaultValue)
  202. {
  203. lua_getfield(L, table_index, key);
  204. int retval;
  205. if (!lua_isnumber(L, -1))
  206. retval = defaultValue;
  207. else
  208. retval = lua_tonumber(L, -1);
  209. lua_pop(L, 1);
  210. return retval;
  211. }
  212. int luax_checkintflag(lua_State *L, int table_index, const char *key)
  213. {
  214. lua_getfield(L, table_index, key);
  215. int retval;
  216. if (!lua_isnumber(L, -1))
  217. {
  218. std::string err = "expected integer field " + std::string(key) + " in table";
  219. return luaL_argerror(L, table_index, err.c_str());
  220. }
  221. else
  222. retval = (int) luaL_checkinteger(L, -1);
  223. lua_pop(L, 1);
  224. return retval;
  225. }
  226. int luax_assert_argc(lua_State *L, int min)
  227. {
  228. int argc = lua_gettop(L);
  229. if (argc < min)
  230. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  231. return 0;
  232. }
  233. int luax_assert_argc(lua_State *L, int min, int max)
  234. {
  235. int argc = lua_gettop(L);
  236. if (argc < min || argc > max)
  237. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  238. return 0;
  239. }
  240. int luax_assert_function(lua_State *L, int idx)
  241. {
  242. if (!lua_isfunction(L, idx))
  243. return luaL_error(L, "Argument must be of type \"function\".");
  244. return 0;
  245. }
  246. int luax_assert_nilerror(lua_State *L, int idx)
  247. {
  248. if (lua_isnoneornil(L, idx))
  249. {
  250. if (lua_isstring(L, idx + 1))
  251. return luaL_error(L, lua_tostring(L, idx + 1));
  252. else
  253. return luaL_error(L, "assertion failed!");
  254. }
  255. return 0;
  256. }
  257. void luax_setfuncs(lua_State *L, const luaL_Reg *l)
  258. {
  259. if (l == nullptr)
  260. return;
  261. for (; l->name != nullptr; l++)
  262. {
  263. lua_pushcfunction(L, l->func);
  264. lua_setfield(L, -2, l->name);
  265. }
  266. }
  267. int luax_require(lua_State *L, const char *name)
  268. {
  269. lua_getglobal(L, "require");
  270. lua_pushstring(L, name);
  271. lua_call(L, 1, 1);
  272. return 1;
  273. }
  274. int luax_register_module(lua_State *L, const WrappedModule &m)
  275. {
  276. m.type->init();
  277. // Put a reference to the C++ module in Lua.
  278. luax_insistregistry(L, REGISTRY_MODULES);
  279. Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  280. p->object = m.module;
  281. p->type = m.type;
  282. luaL_newmetatable(L, m.module->getName());
  283. lua_pushvalue(L, -1);
  284. lua_setfield(L, -2, "__index");
  285. lua_pushcfunction(L, w__gc);
  286. lua_setfield(L, -2, "__gc");
  287. lua_setmetatable(L, -2);
  288. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  289. lua_pop(L, 1);
  290. // Gets the love table.
  291. luax_insistglobal(L, "love");
  292. // Create new table for module.
  293. lua_newtable(L);
  294. // Register all the functions.
  295. if (m.functions != nullptr)
  296. luax_setfuncs(L, m.functions);
  297. // Register types.
  298. if (m.types != nullptr)
  299. {
  300. for (const lua_CFunction *t = m.types; *t != nullptr; t++)
  301. (*t)(L);
  302. }
  303. lua_pushvalue(L, -1);
  304. lua_setfield(L, -3, m.name); // love.graphics = table
  305. lua_remove(L, -2); // love
  306. // Register module instance
  307. Module::registerInstance(m.module);
  308. return 1;
  309. }
  310. int luax_preload(lua_State *L, lua_CFunction f, const char *name)
  311. {
  312. lua_getglobal(L, "package");
  313. lua_getfield(L, -1, "preload");
  314. lua_pushcfunction(L, f);
  315. lua_setfield(L, -2, name);
  316. lua_pop(L, 2);
  317. return 0;
  318. }
  319. int luax_register_type(lua_State *L, love::Type *type, ...)
  320. {
  321. type->init();
  322. // Get the place for storing and re-using instantiated love types.
  323. luax_getregistry(L, REGISTRY_OBJECTS);
  324. // Create registry._loveobjects if it doesn't exist yet.
  325. if (!lua_istable(L, -1))
  326. {
  327. lua_newtable(L);
  328. lua_replace(L, -2);
  329. // Create a metatable.
  330. lua_newtable(L);
  331. // metatable.__mode = "v". Weak userdata values.
  332. lua_pushliteral(L, "v");
  333. lua_setfield(L, -2, "__mode");
  334. // setmetatable(newtable, metatable)
  335. lua_setmetatable(L, -2);
  336. // registry._loveobjects = newtable
  337. lua_setfield(L, LUA_REGISTRYINDEX, "_loveobjects");
  338. }
  339. else
  340. lua_pop(L, 1);
  341. luaL_newmetatable(L, type->getName());
  342. // m.__index = m
  343. lua_pushvalue(L, -1);
  344. lua_setfield(L, -2, "__index");
  345. // setup gc
  346. lua_pushcfunction(L, w__gc);
  347. lua_setfield(L, -2, "__gc");
  348. // Add equality
  349. lua_pushcfunction(L, w__eq);
  350. lua_setfield(L, -2, "__eq");
  351. // Add tostring function.
  352. lua_pushstring(L, type->getName());
  353. lua_pushcclosure(L, w__tostring, 1);
  354. lua_setfield(L, -2, "__tostring");
  355. // Add type
  356. lua_pushstring(L, type->getName());
  357. lua_pushcclosure(L, w__type, 1);
  358. lua_setfield(L, -2, "type");
  359. // Add typeOf
  360. lua_pushcfunction(L, w__typeOf);
  361. lua_setfield(L, -2, "typeOf");
  362. // Add release
  363. lua_pushcfunction(L, w__release);
  364. lua_setfield(L, -2, "release");
  365. va_list fs;
  366. va_start(fs, type);
  367. for (const luaL_Reg *f = va_arg(fs, const luaL_Reg *); f; f = va_arg(fs, const luaL_Reg *))
  368. luax_setfuncs(L, f);
  369. va_end(fs);
  370. lua_pop(L, 1); // Pops metatable.
  371. return 0;
  372. }
  373. void luax_gettypemetatable(lua_State *L, love::Type &type)
  374. {
  375. const char *name = type.getName();
  376. lua_getfield(L, LUA_REGISTRYINDEX, name);
  377. }
  378. int luax_table_insert(lua_State *L, int tindex, int vindex, int pos)
  379. {
  380. if (tindex < 0)
  381. tindex = lua_gettop(L)+1+tindex;
  382. if (vindex < 0)
  383. vindex = lua_gettop(L)+1+vindex;
  384. if (pos == -1)
  385. {
  386. lua_pushvalue(L, vindex);
  387. lua_rawseti(L, tindex, (int) luax_objlen(L, tindex)+1);
  388. return 0;
  389. }
  390. else if (pos < 0)
  391. pos = (int) luax_objlen(L, tindex)+1+pos;
  392. for (int i = (int) luax_objlen(L, tindex)+1; i > pos; i--)
  393. {
  394. lua_rawgeti(L, tindex, i-1);
  395. lua_rawseti(L, tindex, i);
  396. }
  397. lua_pushvalue(L, vindex);
  398. lua_rawseti(L, tindex, pos);
  399. return 0;
  400. }
  401. int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
  402. {
  403. // Add the package loader to the package.loaders table.
  404. lua_getglobal(L, "package");
  405. if (lua_isnil(L, -1))
  406. return luaL_error(L, "Can't register searcher: package table does not exist.");
  407. lua_getfield(L, -1, "loaders");
  408. // Lua 5.2 renamed package.loaders to package.searchers.
  409. if (lua_isnil(L, -1))
  410. {
  411. lua_pop(L, 1);
  412. lua_getfield(L, -1, "searchers");
  413. }
  414. if (lua_isnil(L, -1))
  415. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  416. lua_pushcfunction(L, f);
  417. luax_table_insert(L, -2, -1, pos);
  418. lua_pop(L, 3);
  419. return 0;
  420. }
  421. void luax_rawnewtype(lua_State *L, love::Type &type, love::Object *object)
  422. {
  423. Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  424. object->retain();
  425. u->object = object;
  426. u->type = &type;
  427. const char *name = type.getName();
  428. luaL_newmetatable(L, name);
  429. lua_getfield(L, -1, "__gc");
  430. bool has_gc = !lua_isnoneornil(L, -1);
  431. lua_pop(L, 1);
  432. // Make sure mt.__gc exists, so Lua states which don't have the object's
  433. // module loaded will still clean the object up when it's collected.
  434. if (!has_gc)
  435. {
  436. lua_pushcfunction(L, w__gc);
  437. lua_setfield(L, -2, "__gc");
  438. }
  439. lua_setmetatable(L, -2);
  440. }
  441. void luax_pushtype(lua_State *L, love::Type &type, love::Object *object)
  442. {
  443. if (object == nullptr)
  444. {
  445. lua_pushnil(L);
  446. return;
  447. }
  448. // Fetch the registry table of instantiated objects.
  449. luax_getregistry(L, REGISTRY_OBJECTS);
  450. // The table might not exist - it should be insisted in luax_register_type.
  451. if (!lua_istable(L, -1))
  452. {
  453. lua_pop(L, 1);
  454. return luax_rawnewtype(L, type, object);
  455. }
  456. // Get the value of loveobjects[object] on the stack.
  457. lua_pushlightuserdata(L, object);
  458. lua_gettable(L, -2);
  459. // If the Proxy userdata isn't in the instantiated types table yet, add it.
  460. if (lua_type(L, -1) != LUA_TUSERDATA)
  461. {
  462. lua_pop(L, 1);
  463. luax_rawnewtype(L, type, object);
  464. lua_pushlightuserdata(L, object);
  465. lua_pushvalue(L, -2);
  466. // loveobjects[object] = Proxy.
  467. lua_settable(L, -4);
  468. }
  469. // Remove the loveobjects table from the stack.
  470. lua_remove(L, -2);
  471. // Keep the Proxy userdata on the stack.
  472. }
  473. bool luax_istype(lua_State *L, int idx, love::Type &type)
  474. {
  475. if (lua_type(L, idx) != LUA_TUSERDATA)
  476. return false;
  477. Proxy *p = (Proxy *) lua_touserdata(L, idx);
  478. if (p->type != nullptr)
  479. return p->type->isa(type);
  480. else
  481. return false;
  482. }
  483. int luax_getfunction(lua_State *L, const char *mod, const char *fn)
  484. {
  485. lua_getglobal(L, "love");
  486. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  487. lua_getfield(L, -1, mod);
  488. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  489. lua_getfield(L, -1, fn);
  490. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  491. lua_remove(L, -2); // remove mod
  492. lua_remove(L, -2); // remove fn
  493. return 0;
  494. }
  495. int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
  496. {
  497. // Convert to absolute index if necessary.
  498. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  499. idx += lua_gettop(L) + 1;
  500. // Convert string to a file.
  501. luax_getfunction(L, mod, fn);
  502. lua_pushvalue(L, idx); // The initial argument.
  503. lua_call(L, 1, 2); // Call the function, one arg, one return value (plus optional errstring.)
  504. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  505. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  506. lua_replace(L, idx); // Replace the initial argument with the new object.
  507. return 0;
  508. }
  509. int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  510. {
  511. luax_getfunction(L, mod, fn);
  512. for (int i = 0; i < n; i++)
  513. {
  514. lua_pushvalue(L, idxs[i]); // The arguments.
  515. }
  516. lua_call(L, n, 2); // Call the function, n args, one return value (plus optional errstring.)
  517. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  518. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  519. if (n > 0)
  520. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  521. return 0;
  522. }
  523. int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
  524. {
  525. // Convert string to a file.
  526. luax_getfunction(L, mod, fn);
  527. lua_pushvalue(L, idx); // The initial argument.
  528. int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value.
  529. if (ret == 0)
  530. lua_replace(L, idx); // Replace the initial argument with the new object.
  531. return ret;
  532. }
  533. int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  534. {
  535. luax_getfunction(L, mod, fn);
  536. for (int i = 0; i < n; i++)
  537. {
  538. lua_pushvalue(L, idxs[i]); // The arguments.
  539. }
  540. int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value.
  541. if (ret == 0)
  542. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  543. return ret;
  544. }
  545. int luax_insist(lua_State *L, int idx, const char *k)
  546. {
  547. // Convert to absolute index if necessary.
  548. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  549. idx += lua_gettop(L) + 1;
  550. lua_getfield(L, idx, k);
  551. // Create if necessary.
  552. if (!lua_istable(L, -1))
  553. {
  554. lua_pop(L, 1); // Pop the non-table.
  555. lua_newtable(L);
  556. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  557. lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table)
  558. }
  559. return 1;
  560. }
  561. int luax_insistglobal(lua_State *L, const char *k)
  562. {
  563. lua_getglobal(L, k);
  564. if (!lua_istable(L, -1))
  565. {
  566. lua_pop(L, 1); // Pop the non-table.
  567. lua_newtable(L);
  568. lua_pushvalue(L, -1);
  569. lua_setglobal(L, k);
  570. }
  571. return 1;
  572. }
  573. int luax_c_insistglobal(lua_State *L, const char *k)
  574. {
  575. return luax_insistglobal(L, k);
  576. }
  577. int luax_insistlove(lua_State *L, const char *k)
  578. {
  579. luax_insistglobal(L, "love");
  580. luax_insist(L, -1, k);
  581. // The love table should be replaced with the top stack
  582. // item. Only the reqested table should remain on the stack.
  583. lua_replace(L, -2);
  584. return 1;
  585. }
  586. int luax_getlove(lua_State *L, const char *k)
  587. {
  588. lua_getglobal(L, "love");
  589. if (!lua_isnil(L, -1))
  590. {
  591. lua_getfield(L, -1, k);
  592. lua_replace(L, -2);
  593. }
  594. return 1;
  595. }
  596. int luax_insistregistry(lua_State *L, Registry r)
  597. {
  598. switch (r)
  599. {
  600. case REGISTRY_MODULES:
  601. return luax_insistlove(L, "_modules");
  602. case REGISTRY_OBJECTS:
  603. return luax_insist(L, LUA_REGISTRYINDEX, "_loveobjects");
  604. default:
  605. return luaL_error(L, "Attempted to use invalid registry.");
  606. }
  607. }
  608. int luax_getregistry(lua_State *L, Registry r)
  609. {
  610. switch (r)
  611. {
  612. case REGISTRY_MODULES:
  613. return luax_getlove(L, "_modules");
  614. case REGISTRY_OBJECTS:
  615. lua_getfield(L, LUA_REGISTRYINDEX, "_loveobjects");
  616. return 1;
  617. default:
  618. return luaL_error(L, "Attempted to use invalid registry.");
  619. }
  620. }
  621. static const char *MAIN_THREAD_KEY = "_love_mainthread";
  622. lua_State *luax_insistpinnedthread(lua_State *L)
  623. {
  624. lua_getfield(L, LUA_REGISTRYINDEX, MAIN_THREAD_KEY);
  625. if (lua_isnoneornil(L, -1))
  626. {
  627. lua_pop(L, 1);
  628. // lua_pushthread returns 1 if it's actually the main thread, but we
  629. // can't actually get the real main thread if lua_pushthread doesn't
  630. // return it (in Lua 5.1 at least), so we ignore that for now...
  631. // We do store a strong reference to the current thread/coroutine in
  632. // the registry, however.
  633. lua_pushthread(L);
  634. lua_pushvalue(L, -1);
  635. lua_setfield(L, LUA_REGISTRYINDEX, MAIN_THREAD_KEY);
  636. }
  637. lua_State *thread = lua_tothread(L, -1);
  638. lua_pop(L, 1);
  639. return thread;
  640. }
  641. lua_State *luax_getpinnedthread(lua_State *L)
  642. {
  643. lua_getfield(L, LUA_REGISTRYINDEX, MAIN_THREAD_KEY);
  644. lua_State *thread = lua_tothread(L, -1);
  645. lua_pop(L, 1);
  646. return thread;
  647. }
  648. void luax_markdeprecated(lua_State *L, const char *name, APIType api)
  649. {
  650. luax_markdeprecated(L, name, api, DEPRECATED_NO_REPLACEMENT, nullptr);
  651. }
  652. void luax_markdeprecated(lua_State *L, const char *name, APIType api, DeprecationType type, const char *replacement)
  653. {
  654. MarkDeprecated deprecated(name, api, type, replacement);
  655. if (deprecated.info != nullptr && deprecated.info->uses == 1)
  656. {
  657. luaL_where(L, 1);
  658. const char *where = lua_tostring(L, -1);
  659. if (where != nullptr)
  660. deprecated.info->where = where;
  661. lua_pop(L, 1);
  662. }
  663. }
  664. extern "C" int luax_typerror(lua_State *L, int narg, const char *tname)
  665. {
  666. int argtype = lua_type(L, narg);
  667. const char *argtname = nullptr;
  668. // We want to use the love type name for userdata, if possible.
  669. if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "type") != 0)
  670. {
  671. lua_pushvalue(L, narg);
  672. if (lua_pcall(L, 1, 1, 0) == 0 && lua_type(L, -1) == LUA_TSTRING)
  673. {
  674. argtname = lua_tostring(L, -1);
  675. // Non-love userdata might have a type metamethod which doesn't
  676. // describe its type properly, so we only use it for love types.
  677. if (!Type::byName(argtname))
  678. argtname = nullptr;
  679. }
  680. }
  681. if (argtname == nullptr)
  682. argtname = lua_typename(L, argtype);
  683. const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, argtname);
  684. return luaL_argerror(L, narg, msg);
  685. }
  686. int luax_enumerror(lua_State *L, const char *enumName, const char *value)
  687. {
  688. return luaL_error(L, "Invalid %s: %s", enumName, value);
  689. }
  690. int luax_enumerror(lua_State *L, const char *enumName, const std::vector<std::string> &values, const char *value)
  691. {
  692. std::stringstream valueStream;
  693. bool first = true;
  694. for (auto value : values)
  695. {
  696. valueStream << (first ? "'" : ", '") << value << "'";
  697. first = false;
  698. }
  699. std::string valueString = valueStream.str();
  700. return luaL_error(L, "Invalid %s '%s', expected one of: %s", enumName, value, valueString.c_str());
  701. }
  702. size_t luax_objlen(lua_State *L, int ndx)
  703. {
  704. #if LUA_VERSION_NUM == 501
  705. return lua_objlen(L, ndx);
  706. #else
  707. return lua_rawlen(L, ndx);
  708. #endif
  709. }
  710. void luax_register(lua_State *L, const char *name, const luaL_Reg *l)
  711. {
  712. if (name)
  713. lua_newtable(L);
  714. luax_setfuncs(L, l);
  715. if (name)
  716. {
  717. lua_pushvalue(L, -1);
  718. lua_setglobal(L, name);
  719. }
  720. }
  721. Type *luax_type(lua_State *L, int idx)
  722. {
  723. return Type::byName(luaL_checkstring(L, idx));
  724. }
  725. } // love