runtime.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /**
  2. * Copyright (c) 2006-2014 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. Object *object = (Object *) p->data;
  41. object->release();
  42. return 0;
  43. }
  44. static int w__tostring(lua_State *L)
  45. {
  46. lua_pushvalue(L, lua_upvalueindex(1));
  47. return 1;
  48. }
  49. static int w__typeOf(lua_State *L)
  50. {
  51. Proxy *p = (Proxy *)lua_touserdata(L, 1);
  52. Type t = luax_type(L, 2);
  53. luax_pushboolean(L, p->flags[t]);
  54. return 1;
  55. }
  56. static int w__eq(lua_State *L)
  57. {
  58. Proxy *p1 = (Proxy *)lua_touserdata(L, 1);
  59. Proxy *p2 = (Proxy *)lua_touserdata(L, 2);
  60. luax_pushboolean(L, p1->data == p2->data);
  61. return 1;
  62. }
  63. Reference *luax_refif(lua_State *L, int type)
  64. {
  65. Reference *r = 0;
  66. // Create a reference only if the test succeeds.
  67. if (lua_type(L, -1) == type)
  68. r = new Reference(L);
  69. else // Pop the value even if it fails (but also if it succeeds).
  70. lua_pop(L, 1);
  71. return r;
  72. }
  73. void luax_printstack(lua_State *L)
  74. {
  75. for (int i = 1; i<=lua_gettop(L); i++)
  76. {
  77. std::cout << i << " - " << luaL_typename(L, i) << std::endl;
  78. }
  79. }
  80. bool luax_toboolean(lua_State *L, int idx)
  81. {
  82. return (lua_toboolean(L, idx) != 0);
  83. }
  84. void luax_pushboolean(lua_State *L, bool b)
  85. {
  86. lua_pushboolean(L, b ? 1 : 0);
  87. }
  88. bool luax_optboolean(lua_State *L, int idx, bool b)
  89. {
  90. if (lua_isboolean(L, idx) == 1)
  91. return (lua_toboolean(L, idx) == 1 ? true : false);
  92. return b;
  93. }
  94. std::string luax_tostring(lua_State *L, int idx)
  95. {
  96. size_t len;
  97. const char *str = lua_tolstring(L, idx, &len);
  98. return std::string(str, len);
  99. }
  100. std::string luax_checkstring(lua_State *L, int idx)
  101. {
  102. size_t len;
  103. const char *str = luaL_checklstring(L, idx, &len);
  104. return std::string(str, len);
  105. }
  106. void luax_pushstring(lua_State *L, const std::string &str)
  107. {
  108. lua_pushlstring(L, str.data(), str.size());
  109. }
  110. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue)
  111. {
  112. lua_getfield(L, table_index, key);
  113. bool retval;
  114. if (lua_isnoneornil(L, -1))
  115. retval = defaultValue;
  116. else
  117. retval = lua_toboolean(L, -1);
  118. lua_pop(L, 1);
  119. return retval;
  120. }
  121. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue)
  122. {
  123. lua_getfield(L, table_index, key);
  124. int retval;
  125. if (!lua_isnumber(L, -1))
  126. retval = defaultValue;
  127. else
  128. retval = (int) lua_tointeger(L, -1);
  129. lua_pop(L, 1);
  130. return retval;
  131. }
  132. int luax_assert_argc(lua_State *L, int min)
  133. {
  134. int argc = lua_gettop(L);
  135. if (argc < min)
  136. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  137. return 0;
  138. }
  139. int luax_assert_argc(lua_State *L, int min, int max)
  140. {
  141. int argc = lua_gettop(L);
  142. if (argc < min || argc > max)
  143. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  144. return 0;
  145. }
  146. int luax_assert_function(lua_State *L, int idx)
  147. {
  148. if (!lua_isfunction(L, idx))
  149. return luaL_error(L, "Argument must be of type \"function\".");
  150. return 0;
  151. }
  152. int luax_assert_nilerror(lua_State *L, int idx)
  153. {
  154. if (lua_isnoneornil(L, idx))
  155. {
  156. if (lua_isstring(L, idx + 1))
  157. return luaL_error(L, lua_tostring(L, idx + 1));
  158. else
  159. return luaL_error(L, "assertion failed!");
  160. }
  161. return 0;
  162. }
  163. void luax_setfuncs(lua_State *L, const luaL_Reg *l)
  164. {
  165. if (l == 0)
  166. return;
  167. for (; l->name != 0; l++)
  168. {
  169. lua_pushcfunction(L, l->func);
  170. lua_setfield(L, -2, l->name);
  171. }
  172. }
  173. int luax_register_module(lua_State *L, const WrappedModule &m)
  174. {
  175. // Put a reference to the C++ module in Lua.
  176. luax_insistregistry(L, REGISTRY_MODULES);
  177. Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  178. p->data = m.module;
  179. p->flags = m.flags;
  180. luaL_newmetatable(L, m.module->getName());
  181. lua_pushvalue(L, -1);
  182. lua_setfield(L, -2, "__index");
  183. lua_pushcfunction(L, w__gc);
  184. lua_setfield(L, -2, "__gc");
  185. lua_setmetatable(L, -2);
  186. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  187. lua_pop(L, 1);
  188. // Gets the love table.
  189. luax_insistglobal(L, "love");
  190. // Create new table for module.
  191. lua_newtable(L);
  192. // Register all the functions.
  193. if (m.functions != 0)
  194. luax_setfuncs(L, m.functions);
  195. // Register types.
  196. if (m.types != 0)
  197. for (const lua_CFunction *t = m.types; *t != 0; t++)
  198. (*t)(L);
  199. lua_pushvalue(L, -1);
  200. lua_setfield(L, -3, m.name); // love.graphics = table
  201. lua_remove(L, -2); // love
  202. // Register module instance
  203. Module::registerInstance(m.module);
  204. return 1;
  205. }
  206. int luax_preload(lua_State *L, lua_CFunction f, const char *name)
  207. {
  208. lua_getglobal(L, "package");
  209. lua_getfield(L, -1, "preload");
  210. lua_pushcfunction(L, f);
  211. lua_setfield(L, -2, name);
  212. lua_pop(L, 2);
  213. return 0;
  214. }
  215. int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f)
  216. {
  217. // Verify that this type name has a matching Type ID and type name mapping.
  218. love::Type ltype;
  219. if (!love::getType(tname, ltype))
  220. printf("Missing type entry for type name: %s\n", tname);
  221. // Get the place for storing and re-using instantiated love types.
  222. luax_getregistry(L, REGISTRY_TYPES);
  223. // Create registry._lovetypes if it doesn't exist yet.
  224. if (!lua_istable(L, -1))
  225. {
  226. lua_newtable(L);
  227. lua_replace(L, -2);
  228. // Create a metatable.
  229. lua_newtable(L);
  230. // metatable.__mode = "v". Weak userdata values.
  231. lua_pushliteral(L, "v");
  232. lua_setfield(L, -2, "__mode");
  233. // setmetatable(newtable, metatable)
  234. lua_setmetatable(L, -2);
  235. // registry._lovetypes = newtable
  236. lua_setfield(L, LUA_REGISTRYINDEX, "_lovetypes");
  237. }
  238. else
  239. lua_pop(L, 1);
  240. luaL_newmetatable(L, tname);
  241. // m.__index = m
  242. lua_pushvalue(L, -1);
  243. lua_setfield(L, -2, "__index");
  244. // setup gc
  245. lua_pushcfunction(L, w__gc);
  246. lua_setfield(L, -2, "__gc");
  247. // Add equality
  248. lua_pushcfunction(L, w__eq);
  249. lua_setfield(L, -2, "__eq");
  250. // Add tostring function.
  251. lua_pushstring(L, tname);
  252. lua_pushcclosure(L, w__tostring, 1);
  253. lua_setfield(L, -2, "__tostring");
  254. // Add tostring to as type() as well.
  255. lua_pushstring(L, tname);
  256. lua_pushcclosure(L, w__tostring, 1);
  257. lua_setfield(L, -2, "type");
  258. // Add typeOf
  259. lua_pushcfunction(L, w__typeOf);
  260. lua_setfield(L, -2, "typeOf");
  261. if (f != 0)
  262. luax_setfuncs(L, f);
  263. lua_pop(L, 1); // Pops metatable.
  264. return 0;
  265. }
  266. int luax_table_insert(lua_State *L, int tindex, int vindex, int pos)
  267. {
  268. if (tindex < 0)
  269. tindex = lua_gettop(L)+1+tindex;
  270. if (vindex < 0)
  271. vindex = lua_gettop(L)+1+vindex;
  272. if (pos == -1)
  273. {
  274. lua_pushvalue(L, vindex);
  275. lua_rawseti(L, tindex, lua_objlen(L, tindex)+1);
  276. return 0;
  277. }
  278. else if (pos < 0)
  279. pos = lua_objlen(L, tindex)+1+pos;
  280. for (int i = lua_objlen(L, tindex)+1; i > pos; i--)
  281. {
  282. lua_rawgeti(L, tindex, i-1);
  283. lua_rawseti(L, tindex, i);
  284. }
  285. lua_pushvalue(L, vindex);
  286. lua_rawseti(L, tindex, pos);
  287. return 0;
  288. }
  289. int luax_register_searcher(lua_State *L, lua_CFunction f, int pos)
  290. {
  291. // Add the package loader to the package.loaders table.
  292. lua_getglobal(L, "package");
  293. if (lua_isnil(L, -1))
  294. return luaL_error(L, "Can't register searcher: package table does not exist.");
  295. lua_getfield(L, -1, "loaders");
  296. // Lua 5.2 renamed package.loaders to package.searchers.
  297. if (lua_isnil(L, -1))
  298. {
  299. lua_pop(L, 1);
  300. lua_getfield(L, -1, "searchers");
  301. }
  302. if (lua_isnil(L, -1))
  303. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  304. lua_pushcfunction(L, f);
  305. luax_table_insert(L, -2, -1, pos);
  306. lua_pop(L, 3);
  307. return 0;
  308. }
  309. void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object)
  310. {
  311. Proxy *u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  312. object->retain();
  313. u->data = (void *) object;
  314. u->flags = flags;
  315. luaL_newmetatable(L, name);
  316. lua_setmetatable(L, -2);
  317. }
  318. void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object)
  319. {
  320. if (object == nullptr)
  321. lua_pushnil(L);
  322. // Fetch the registry table of instantiated types.
  323. luax_getregistry(L, REGISTRY_TYPES);
  324. // The table might not exist - it should be insisted in luax_register_type.
  325. if (!lua_istable(L, -1))
  326. {
  327. lua_pop(L, 1);
  328. return luax_rawnewtype(L, name, flags, object);
  329. }
  330. // Get the value of lovetypes[object] on the stack.
  331. lua_pushlightuserdata(L, (void *) object);
  332. lua_gettable(L, -2);
  333. // If the Proxy userdata isn't in the instantiated types table yet, add it.
  334. if (lua_type(L, -1) != LUA_TUSERDATA)
  335. {
  336. lua_pop(L, 1);
  337. luax_rawnewtype(L, name, flags, object);
  338. lua_pushlightuserdata(L, (void *) object);
  339. lua_pushvalue(L, -2);
  340. // lovetypes[object] = Proxy.
  341. lua_settable(L, -4);
  342. }
  343. // Remove the lovetypes table from the stack.
  344. lua_remove(L, -2);
  345. // Keep the Proxy userdata on the stack.
  346. }
  347. bool luax_istype(lua_State *L, int idx, love::bits type)
  348. {
  349. if (lua_type(L, idx) != LUA_TUSERDATA)
  350. return false;
  351. return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
  352. }
  353. int luax_getfunction(lua_State *L, const char *mod, const char *fn)
  354. {
  355. lua_getglobal(L, "love");
  356. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  357. lua_getfield(L, -1, mod);
  358. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  359. lua_getfield(L, -1, fn);
  360. if (lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  361. lua_remove(L, -2); // remove mod
  362. lua_remove(L, -2); // remove fn
  363. return 0;
  364. }
  365. int luax_convobj(lua_State *L, int idx, const char *mod, const char *fn)
  366. {
  367. // Convert to absolute index if necessary.
  368. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  369. idx += lua_gettop(L) + 1;
  370. // Convert string to a file.
  371. luax_getfunction(L, mod, fn);
  372. lua_pushvalue(L, idx); // The initial argument.
  373. lua_call(L, 1, 2); // Call the function, one arg, one return value (plus optional errstring.)
  374. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  375. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  376. lua_replace(L, idx); // Replace the initial argument with the new object.
  377. return 0;
  378. }
  379. int luax_convobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  380. {
  381. luax_getfunction(L, mod, fn);
  382. for (int i = 0; i < n; i++)
  383. {
  384. lua_pushvalue(L, idxs[i]); // The arguments.
  385. }
  386. lua_call(L, n, 2); // Call the function, n args, one return value (plus optional errstring.)
  387. luax_assert_nilerror(L, -2); // Make sure the function returned something.
  388. lua_pop(L, 1); // Pop the second return value now that we don't need it.
  389. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  390. return 0;
  391. }
  392. int luax_pconvobj(lua_State *L, int idx, const char *mod, const char *fn)
  393. {
  394. // Convert string to a file.
  395. luax_getfunction(L, mod, fn);
  396. lua_pushvalue(L, idx); // The initial argument.
  397. int ret = lua_pcall(L, 1, 1, 0); // Call the function, one arg, one return value.
  398. if (ret == 0)
  399. lua_replace(L, idx); // Replace the initial argument with the new object.
  400. return ret;
  401. }
  402. int luax_pconvobj(lua_State *L, int idxs[], int n, const char *mod, const char *fn)
  403. {
  404. luax_getfunction(L, mod, fn);
  405. for (int i = 0; i < n; i++)
  406. {
  407. lua_pushvalue(L, idxs[i]); // The arguments.
  408. }
  409. int ret = lua_pcall(L, n, 1, 0); // Call the function, n args, one return value.
  410. if (ret == 0)
  411. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  412. return ret;
  413. }
  414. int luax_insist(lua_State *L, int idx, const char *k)
  415. {
  416. // Convert to absolute index if necessary.
  417. if (idx < 0 && idx > LUA_REGISTRYINDEX)
  418. idx += lua_gettop(L) + 1;
  419. lua_getfield(L, idx, k);
  420. // Create if necessary.
  421. if (!lua_istable(L, -1))
  422. {
  423. lua_pop(L, 1); // Pop the non-table.
  424. lua_newtable(L);
  425. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  426. lua_setfield(L, idx, k); // lua_stack[idx][k] = lua_stack[-1] (table)
  427. }
  428. return 1;
  429. }
  430. int luax_insistglobal(lua_State *L, const char *k)
  431. {
  432. lua_getglobal(L, k);
  433. if (!lua_istable(L, -1))
  434. {
  435. lua_pop(L, 1); // Pop the non-table.
  436. lua_newtable(L);
  437. lua_pushvalue(L, -1);
  438. lua_setglobal(L, k);
  439. }
  440. return 1;
  441. }
  442. int luax_insistlove(lua_State *L, const char *k)
  443. {
  444. luax_insistglobal(L, "love");
  445. luax_insist(L, -1, k);
  446. // The love table should be replaced with the top stack
  447. // item. Only the reqested table should remain on the stack.
  448. lua_replace(L, -2);
  449. return 1;
  450. }
  451. int luax_getlove(lua_State *L, const char *k)
  452. {
  453. lua_getglobal(L, "love");
  454. if (!lua_isnil(L, -1))
  455. {
  456. lua_getfield(L, -1, k);
  457. lua_replace(L, -2);
  458. }
  459. return 1;
  460. }
  461. int luax_insistregistry(lua_State *L, Registry r)
  462. {
  463. switch (r)
  464. {
  465. case REGISTRY_GC:
  466. return luax_insistlove(L, "_gc");
  467. case REGISTRY_MODULES:
  468. return luax_insistlove(L, "_modules");
  469. case REGISTRY_TYPES:
  470. return luax_insist(L, LUA_REGISTRYINDEX, "_lovetypes");
  471. default:
  472. return luaL_error(L, "Attempted to use invalid registry.");
  473. }
  474. }
  475. int luax_getregistry(lua_State *L, Registry r)
  476. {
  477. switch (r)
  478. {
  479. case REGISTRY_GC:
  480. return luax_getlove(L, "_gc");
  481. case REGISTRY_MODULES:
  482. return luax_getlove(L, "_modules");
  483. case REGISTRY_TYPES:
  484. lua_getfield(L, LUA_REGISTRYINDEX, "_lovetypes");
  485. return 1;
  486. default:
  487. return luaL_error(L, "Attempted to use invalid registry.");
  488. }
  489. }
  490. extern "C" int luax_typerror(lua_State *L, int narg, const char *tname)
  491. {
  492. int argtype = lua_type(L, narg);
  493. const char *argtname = 0;
  494. // We want to use the love type name for userdata, if possible.
  495. if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "__tostring") != 0)
  496. {
  497. lua_pushvalue(L, narg);
  498. if (lua_pcall(L, 1, 1, 0) == 0 && lua_type(L, -1) == LUA_TSTRING)
  499. {
  500. argtname = lua_tostring(L, -1);
  501. // Non-love userdata might have a tostring metamethod which doesn't
  502. // describe its type, so we only use __tostring for love types.
  503. love::Type t;
  504. if (!love::getType(argtname, t))
  505. argtname = 0;
  506. }
  507. }
  508. if (argtname == 0)
  509. argtname = lua_typename(L, argtype);
  510. const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, argtname);
  511. return luaL_argerror(L, narg, msg);
  512. }
  513. StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] =
  514. {
  515. {"Invalid", INVALID_ID},
  516. {"Object", OBJECT_ID},
  517. {"Data", DATA_ID},
  518. {"Module", MODULE_ID},
  519. // Filesystem
  520. {"File", FILESYSTEM_FILE_ID},
  521. {"FileData", FILESYSTEM_FILE_DATA_ID},
  522. // Font
  523. {"GlyphData", FONT_GLYPH_DATA_ID},
  524. {"Rasterizer", FONT_RASTERIZER_ID},
  525. // Graphics
  526. {"Drawable", GRAPHICS_DRAWABLE_ID},
  527. {"Texture", GRAPHICS_TEXTURE_ID},
  528. {"Image", GRAPHICS_IMAGE_ID},
  529. {"Quad", GRAPHICS_QUAD_ID},
  530. {"Font", GRAPHICS_FONT_ID},
  531. {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID},
  532. {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID},
  533. {"Canvas", GRAPHICS_CANVAS_ID},
  534. {"Shader", GRAPHICS_SHADER_ID},
  535. {"Mesh", GRAPHICS_MESH_ID},
  536. // Image
  537. {"ImageData", IMAGE_IMAGE_DATA_ID},
  538. {"CompressedData", IMAGE_COMPRESSED_DATA_ID},
  539. // Joystick
  540. {"Joystick", JOYSTICK_JOYSTICK_ID},
  541. // Math
  542. {"RandomGenerator", MATH_RANDOM_GENERATOR_ID},
  543. {"BezierCurve", MATH_BEZIER_CURVE_ID},
  544. // Audio
  545. {"Source", AUDIO_SOURCE_ID},
  546. // Sound
  547. {"SoundData", SOUND_SOUND_DATA_ID},
  548. {"Decoder", SOUND_DECODER_ID},
  549. // Mouse
  550. {"Cursor", MOUSE_CURSOR_ID},
  551. // Physics
  552. {"World", PHYSICS_WORLD_ID},
  553. {"Contact", PHYSICS_CONTACT_ID},
  554. {"Body", PHYSICS_BODY_ID},
  555. {"Fixture", PHYSICS_FIXTURE_ID},
  556. {"Shape", PHYSICS_SHAPE_ID},
  557. {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID},
  558. {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID},
  559. {"EdgeShape", PHYSICS_EDGE_SHAPE_ID},
  560. {"ChainShape", PHYSICS_CHAIN_SHAPE_ID},
  561. {"Joint", PHYSICS_JOINT_ID},
  562. {"MouseJoint", PHYSICS_MOUSE_JOINT_ID},
  563. {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID},
  564. {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID},
  565. {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID},
  566. {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID},
  567. {"GearJoint", PHYSICS_GEAR_JOINT_ID},
  568. {"FrictionJoint", PHYSICS_FRICTION_JOINT_ID},
  569. {"WeldJoint", PHYSICS_WELD_JOINT_ID},
  570. {"RopeJoint", PHYSICS_ROPE_JOINT_ID},
  571. {"WheelJoint", PHYSICS_WHEEL_JOINT_ID},
  572. {"MotorJoint", PHYSICS_MOTOR_JOINT_ID},
  573. // Thread
  574. {"Thread", THREAD_THREAD_ID},
  575. {"Channel", THREAD_CHANNEL_ID},
  576. // The modules themselves. Only add abstracted modules here.
  577. {"filesystem", MODULE_FILESYSTEM_ID},
  578. {"graphics", MODULE_GRAPHICS_ID},
  579. {"image", MODULE_IMAGE_ID},
  580. {"sound", MODULE_SOUND_ID},
  581. };
  582. StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries));
  583. bool getType(const char *in, love::Type &out)
  584. {
  585. return types.find(in, out);
  586. }
  587. bool getType(love::Type in, const char *&out)
  588. {
  589. return types.find(in, out);
  590. }
  591. Type luax_type(lua_State *L, int idx)
  592. {
  593. Type t = INVALID_ID;
  594. types.find(luaL_checkstring(L, idx), t);
  595. return t;
  596. }
  597. } // love