runtime.cpp 17 KB

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