runtime.cpp 18 KB

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