runtime.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /**
  2. * Copyright (c) 2006-2011 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 "runtime.h"
  21. // LOVE
  22. #include "Module.h"
  23. #include "Object.h"
  24. #include "Reference.h"
  25. #include "StringMap.h"
  26. // STD
  27. #include <iostream>
  28. // SDL
  29. #include <SDL_mutex.h>
  30. #include <SDL_thread.h>
  31. namespace love
  32. {
  33. static SDL_mutex *gcmutex = 0;
  34. void *_gcmutex = 0;
  35. unsigned int _gcthread = 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 = SDL_CreateMutex();
  45. _gcmutex = (void*) gcmutex;
  46. }
  47. Proxy * p = (Proxy *)lua_touserdata(L, 1);
  48. Object * t = (Object *)p->data;
  49. if(p->own)
  50. {
  51. SDL_mutexP(gcmutex);
  52. _gcthread = (unsigned int) SDL_ThreadID();
  53. t->release();
  54. SDL_mutexV(gcmutex);
  55. }
  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. int luax_assert_argc(lua_State * L, int min)
  109. {
  110. int argc = lua_gettop(L);
  111. if( argc < min )
  112. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected at least [%d]", argc, min);
  113. return 0;
  114. }
  115. int luax_assert_argc(lua_State * L, int min, int max)
  116. {
  117. int argc = lua_gettop(L);
  118. if( argc < min || argc > max)
  119. return luaL_error(L, "Incorrect number of arguments. Got [%d], expected [%d-%d]", argc, min, max);
  120. return 0;
  121. }
  122. int luax_assert_function(lua_State * L, int n)
  123. {
  124. if(!lua_isfunction(L, n))
  125. return luaL_error(L, "Argument must be of type \"function\".");
  126. return 0;
  127. }
  128. int luax_register_module(lua_State * L, const WrappedModule & m)
  129. {
  130. // Put a reference to the C++ module in Lua.
  131. luax_getregistry(L, REGISTRY_MODULES);
  132. Proxy * p = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  133. p->own = true;
  134. p->data = m.module;
  135. p->flags = m.flags;
  136. luaL_newmetatable(L, m.module->getName());
  137. lua_pushvalue(L, -1);
  138. lua_setfield(L, -2, "__index");
  139. lua_pushcfunction(L, w__gc);
  140. lua_setfield(L, -2, "__gc");
  141. lua_setmetatable(L, -2);
  142. lua_setfield(L, -2, m.name); // _modules[name] = proxy
  143. lua_pop(L, 1);
  144. // Gets the love table.
  145. luax_insistglobal(L, "love");
  146. // Create new table for module.
  147. lua_newtable(L);
  148. // Register all the functions.
  149. luaL_register(L, 0, m.functions);
  150. // Register types.
  151. if(m.types != 0)
  152. for(const lua_CFunction * t = m.types; *t != 0; t++)
  153. (*t)(L);
  154. lua_setfield(L, -2, m.name); // love.graphics = table
  155. lua_pop(L, 1); // love
  156. return 0;
  157. }
  158. int luax_preload(lua_State * L, lua_CFunction f, const char * name)
  159. {
  160. lua_getglobal(L, "package");
  161. lua_getfield(L, -1, "preload");
  162. lua_pushcfunction(L, f);
  163. lua_setfield(L, -2, name);
  164. lua_pop(L, 2);
  165. return 0;
  166. }
  167. int luax_register_type(lua_State * L, const char * tname, const luaL_Reg * f)
  168. {
  169. luaL_newmetatable(L, tname);
  170. // m.__index = m
  171. lua_pushvalue(L, -1);
  172. lua_setfield(L, -2, "__index");
  173. // setup gc
  174. lua_pushcfunction(L, w__gc);
  175. lua_setfield(L, -2, "__gc");
  176. // Add equality
  177. lua_pushcfunction(L, w__eq);
  178. lua_setfield(L, -2, "__eq");
  179. // Add tostring function.
  180. lua_pushstring(L, tname);
  181. lua_pushcclosure(L, w__tostring, 1);
  182. lua_setfield(L, -2, "__tostring");
  183. // Add tostring to as type() as well.
  184. lua_pushstring(L, tname);
  185. lua_pushcclosure(L, w__tostring, 1);
  186. lua_setfield(L, -2, "type");
  187. // Add typeOf
  188. lua_pushcfunction(L, w__typeOf);
  189. lua_setfield(L, -2, "typeOf");
  190. if(f != 0)
  191. luaL_register(L, 0, f);
  192. lua_pop(L, 1); // Pops metatable.
  193. return 0;
  194. }
  195. int luax_register_searcher(lua_State * L, lua_CFunction f)
  196. {
  197. // Add the package loader to the package.loaders table.
  198. lua_getglobal(L, "package");
  199. if(lua_isnil(L, -1))
  200. return luaL_error(L, "Can't register searcher: package table does not exist.");
  201. lua_getfield(L, -1, "loaders");
  202. if(lua_isnil(L, -1))
  203. return luaL_error(L, "Can't register searcher: package.loaders table does not exist.");
  204. int len = lua_objlen(L, -1);
  205. lua_pushinteger(L, len+1);
  206. lua_pushcfunction(L, f);
  207. lua_settable(L, -3);
  208. lua_pop(L, 2);
  209. return 0;
  210. }
  211. void luax_newtype(lua_State * L, const char * name, bits flags, void * data, bool own)
  212. {
  213. Proxy * u = (Proxy *)lua_newuserdata(L, sizeof(Proxy));
  214. u->data = data;
  215. u->flags = flags;
  216. u->own = own;
  217. luaL_newmetatable(L, name);
  218. lua_setmetatable(L, -2);
  219. }
  220. bool luax_istype(lua_State * L, int idx, love::bits type)
  221. {
  222. if(lua_isuserdata(L, idx) == 0)
  223. return false;
  224. return ((((Proxy *)lua_touserdata(L, idx))->flags & type) == type);
  225. }
  226. int luax_getfunction(lua_State * L, const char * mod, const char * fn)
  227. {
  228. lua_getglobal(L, "love");
  229. if(lua_isnil(L, -1)) return luaL_error(L, "Could not find global love!");
  230. lua_getfield(L, -1, mod);
  231. if(lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s!", mod);
  232. lua_getfield(L, -1, fn);
  233. if(lua_isnil(L, -1)) return luaL_error(L, "Could not find love.%s.%s!", mod, fn);
  234. lua_remove(L, -2); // remove mod
  235. lua_remove(L, -2); // remove fn
  236. return 0;
  237. }
  238. int luax_convobj(lua_State * L, int idx, const char * mod, const char * fn)
  239. {
  240. // Convert string to a file.
  241. luax_getfunction(L, mod, fn);
  242. lua_pushvalue(L, idx); // The initial argument.
  243. lua_call(L, 1, 1); // Call the function, one arg, one return value.
  244. lua_replace(L, idx); // Replace the initial argument with the new object.
  245. return 0;
  246. }
  247. int luax_convobj(lua_State * L, int idxs[], int n, const char * mod, const char * fn)
  248. {
  249. luax_getfunction(L, mod, fn);
  250. for (int i = 0; i < n; i++) {
  251. lua_pushvalue(L, idxs[i]); // The arguments.
  252. }
  253. lua_call(L, n, 1); // Call the function, n args, one return value.
  254. lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
  255. return 0;
  256. }
  257. int luax_strtofile(lua_State * L, int idx)
  258. {
  259. return luax_convobj(L, idx, "filesystem", "newFile");
  260. }
  261. int luax_filetodata(lua_State * L, int idx)
  262. {
  263. return luax_convobj(L, idx, "filesystem", "read");
  264. }
  265. int luax_insist(lua_State * L, int idx, const char * k)
  266. {
  267. lua_getfield(L, idx, k);
  268. // Create if necessary.
  269. if(!lua_istable(L, -1))
  270. {
  271. lua_pop(L, 1); // Pop the non-table.
  272. lua_newtable(L);
  273. lua_pushvalue(L, -1); // Duplicate the table to leave on top.
  274. lua_setfield(L, -3, k); // k[idx] = table
  275. }
  276. return 1;
  277. }
  278. int luax_insistglobal(lua_State * L, const char * k)
  279. {
  280. lua_getglobal(L, k);
  281. if(!lua_istable(L, -1))
  282. {
  283. lua_pop(L, 1); // Pop the non-table.
  284. lua_newtable(L);
  285. lua_pushvalue(L, -1);
  286. lua_setglobal(L, k);
  287. }
  288. return 1;
  289. }
  290. int luax_insistlove(lua_State * L, const char * k)
  291. {
  292. luax_insistglobal(L, "love");
  293. luax_insist(L, -1, k);
  294. // The love table should be replaced with the top stack
  295. // item. Only the reqested table should remain on the stack.
  296. lua_replace(L, -2);
  297. return 1;
  298. }
  299. int luax_getregistry(lua_State * L, Registry r)
  300. {
  301. switch(r)
  302. {
  303. case REGISTRY_GC:
  304. return luax_insistlove(L, "_gc");
  305. case REGISTRY_MODULES:
  306. return luax_insistlove(L, "_modules");
  307. default:
  308. return luaL_error(L, "Attempted to use invalid registry.");
  309. }
  310. }
  311. StringMap<Type, TYPE_MAX_ENUM>::Entry typeEntries[] =
  312. {
  313. {"Invalid", INVALID_ID},
  314. {"Object", OBJECT_ID},
  315. {"Data", DATA_ID},
  316. {"Module", MODULE_ID},
  317. // Filesystem
  318. {"File", FILESYSTEM_FILE_ID},
  319. {"FileData", FILESYSTEM_FILE_DATA_ID},
  320. // Font
  321. {"GlyphData", FONT_GLYPH_DATA_ID},
  322. {"Rasterizer", FONT_RASTERIZER_ID},
  323. // Graphics
  324. {"Drawable", GRAPHICS_DRAWABLE_ID},
  325. {"Image", GRAPHICS_IMAGE_ID},
  326. {"Quad", GRAPHICS_QUAD_ID},
  327. {"Glyph", GRAPHICS_GLYPH_ID},
  328. {"Font", GRAPHICS_FONT_ID},
  329. {"ParticleSystem", GRAPHICS_PARTICLE_SYSTEM_ID},
  330. {"SpriteBatch", GRAPHICS_SPRITE_BATCH_ID},
  331. {"VertexBuffer", GRAPHICS_VERTEX_BUFFER_ID},
  332. // Image
  333. {"ImageData", IMAGE_IMAGE_DATA_ID},
  334. {"EncodedImageData", IMAGE_ENCODED_IMAGE_DATA_ID},
  335. // Audio
  336. {"Source", AUDIO_SOURCE_ID},
  337. // Sound
  338. {"SoundData", SOUND_SOUND_DATA_ID},
  339. {"Decoder", SOUND_DECODER_ID},
  340. // Physics
  341. {"World", PHYSICS_WORLD_ID},
  342. {"Contact", PHYSICS_CONTACT_ID},
  343. {"Body", PHYSICS_BODY_ID},
  344. {"Shape", PHYSICS_SHAPE_ID},
  345. {"CircleShape", PHYSICS_CIRCLE_SHAPE_ID},
  346. {"PolygonShape", PHYSICS_POLYGON_SHAPE_ID},
  347. {"Joint", PHYSICS_JOINT_ID},
  348. {"MouseJoint", PHYSICS_MOUSE_JOINT_ID},
  349. {"DistanceJoint", PHYSICS_DISTANCE_JOINT_ID},
  350. {"PrismaticJoint", PHYSICS_PRISMATIC_JOINT_ID},
  351. {"RevoluteJoint", PHYSICS_REVOLUTE_JOINT_ID},
  352. {"PulleyJoint", PHYSICS_PULLEY_JOINT_ID},
  353. {"GearJoint", PHYSICS_GEAR_JOINT_ID},
  354. // Thread
  355. {"Thread", THREAD_THREAD_ID},
  356. // The modules themselves. Only add abstracted modules here.
  357. {"filesystem", MODULE_FILESYSTEM_ID},
  358. {"image", MODULE_IMAGE_ID},
  359. {"sound", MODULE_SOUND_ID},
  360. };
  361. StringMap<Type, TYPE_MAX_ENUM> types(typeEntries, sizeof(typeEntries));
  362. Type luax_type(lua_State * L, int idx)
  363. {
  364. Type t = INVALID_ID;
  365. types.find(luaL_checkstring(L, idx), t);
  366. return t;
  367. }
  368. } // love