runtime.cpp 18 KB

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