runtime.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /**
  2. * Copyright (c) 2006-2015 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. #ifndef LOVE_RUNTIME_H
  21. #define LOVE_RUNTIME_H
  22. // LOVE
  23. #include "types.h"
  24. #include "Object.h"
  25. // Lua
  26. extern "C" {
  27. #define LUA_COMPAT_ALL
  28. #include <lua.h>
  29. #include <lualib.h>
  30. #include <lauxlib.h>
  31. }
  32. // C++
  33. #include <exception>
  34. namespace love
  35. {
  36. // Forward declarations.
  37. class Module;
  38. class Reference;
  39. /**
  40. * Registries represent special tables which can be accessed with
  41. * luax_insistregistry and luax_getregistry.
  42. **/
  43. enum Registry
  44. {
  45. REGISTRY_GC,
  46. REGISTRY_MODULES,
  47. REGISTRY_TYPES
  48. };
  49. /**
  50. * This structure wraps all Lua-exposed objects. It exists in the
  51. * Lua state as a full userdata (so we can catch __gc "events"),
  52. * though the Object it refers to is light userdata in the sense
  53. * that it is not allocated by the Lua VM.
  54. **/
  55. struct Proxy
  56. {
  57. // Holds type information (see types.h).
  58. bits flags;
  59. // The light userdata (pointer to the love::Object).
  60. void *data;
  61. };
  62. /**
  63. * A Module with Lua wrapper functions and other data.
  64. **/
  65. struct WrappedModule
  66. {
  67. // The module containing the functions.
  68. Module *module;
  69. // The name for the table to put the functions in, without the 'love'-prefix.
  70. const char *name;
  71. // The type flags of this module.
  72. love::bits flags;
  73. // The functions of the module (last element {0,0}).
  74. const luaL_Reg *functions;
  75. // A list of functions which expose the types of the modules (last element 0).
  76. const lua_CFunction *types;
  77. };
  78. /**
  79. * Returns a reference to the top stack element (-1) if the value
  80. * is of the specified type. If the value is incorrect, zero is returned.
  81. *
  82. * In any case, the top stack element is popped, regardless of its type.
  83. **/
  84. Reference *luax_refif(lua_State *L, int type);
  85. /**
  86. * Prints the current contents of the stack. Only useful for debugging.
  87. * @param L The Lua state.
  88. **/
  89. void luax_printstack(lua_State *L);
  90. /**
  91. * Converts the value at idx to a bool. It follow the same rules
  92. * as lua_toboolean, but returns a bool instead of an int.
  93. * @param L The Lua state.
  94. * @param idx The index on the Lua stack.
  95. * @return True if the value evaluates to true, false otherwise.
  96. **/
  97. bool luax_toboolean(lua_State *L, int idx);
  98. /**
  99. * Pushes a bool onto the stack. It's the same as lua_pushboolean,
  100. * but with bool instead of int.
  101. * @param L The Lua state.
  102. * @param b The bool to push.
  103. **/
  104. void luax_pushboolean(lua_State *L, bool b);
  105. /**
  106. * Converts the value at idx to a bool, or if not present, b is returned.
  107. * @param L The Lua state.
  108. * @param idx The index of the Lua stack.
  109. * @param b The value to return if no value exist at the specified index.
  110. * @return True if the value evaluates to true, false otherwise.
  111. **/
  112. bool luax_optboolean(lua_State *L, int idx, bool b);
  113. /**
  114. * Converts the value at idx to a std::string. It takes care of the string
  115. * size and possible embedded nulls.
  116. * @param L The Lua state.
  117. * @param idx The index on the Lua stack.
  118. * @return Copy of the string at the specified index.
  119. **/
  120. std::string luax_tostring(lua_State *L, int idx);
  121. /**
  122. * Converts the value at idx to a std::string. It takes care of the string
  123. * size and possible embedded nulls.
  124. * @param L The Lua state.
  125. * @param idx The index on the Lua stack.
  126. * @return Copy of the string at the specified index.
  127. **/
  128. std::string luax_checkstring(lua_State *L, int idx);
  129. /**
  130. * Pushes a std::string onto the stack. It uses the length of the string
  131. * for lua_pushlstring's len argument.
  132. * @param L The Lua state.
  133. * @param str The string to push.
  134. **/
  135. void luax_pushstring(lua_State *L, const std::string &str);
  136. bool luax_boolflag(lua_State *L, int table_index, const char *key, bool defaultValue);
  137. int luax_intflag(lua_State *L, int table_index, const char *key, int defaultValue);
  138. /**
  139. * Convert the value at the specified index to an Lua number, and then
  140. * convert to a float.
  141. *
  142. * @param L The Lua state.
  143. * @param idx The index on the stack.
  144. */
  145. inline float luax_tofloat(lua_State *L, int idx)
  146. {
  147. return static_cast<float>(lua_tonumber(L, idx));
  148. }
  149. /**
  150. * Like luax_tofloat, but checks that the value is a number.
  151. *
  152. * @see luax_tofloat
  153. */
  154. inline float luax_checkfloat(lua_State *L, int idx)
  155. {
  156. return static_cast<float>(luaL_checknumber(L, idx));
  157. }
  158. /**
  159. * Require at least 'min' number of items on the stack.
  160. * @param L The Lua state.
  161. * @param min The minimum number of items on the stack.
  162. * @return Zero if conditions are met, otherwise a Lua error (longjmp).
  163. **/
  164. int luax_assert_argc(lua_State *L, int min);
  165. /**
  166. * Require at least 'min', but more than 'max' items on the stack.
  167. * @param L The Lua state.
  168. * @param min The minimum number of items on the stack.
  169. * @param max The maximum number of items on the stack.
  170. * @return Zero if conditions are met, otherwise a Lua error (longjmp).
  171. **/
  172. int luax_assert_argc(lua_State *L, int min, int max);
  173. /**
  174. * Require that the value at idx is a function.
  175. * @param L The Lua state.
  176. *@param idx The index on the stack.
  177. **/
  178. int luax_assert_function(lua_State *L, int idx);
  179. /**
  180. * Require that the value at idx is not nil. If it is, the function throws an
  181. * error using an optional error string at idx+1.
  182. * @param L The Lua state.
  183. * @param idx The index on the stack.
  184. **/
  185. int luax_assert_nilerror(lua_State *L, int idx);
  186. /**
  187. * Registers all functions in the array l (see luaL_Reg) into the table at the
  188. * top of the stack.
  189. * Similar to Lua 5.2's luaL_setfuncs without the upvalues, and to Lua 5.1's
  190. * luaL_register without the library name.
  191. **/
  192. void luax_setfuncs(lua_State *L, const luaL_Reg *l);
  193. /**
  194. * Register a module in the love table. The love table will be created if it does not exist.
  195. * NOTE: The module-object is expected to have a +1 reference count before calling
  196. * this function, as it doesn't retain the object itself but Lua will release it
  197. * upon garbage collection.
  198. * @param L The Lua state.
  199. **/
  200. int luax_register_module(lua_State *L, const WrappedModule &m);
  201. /**
  202. * Inserts a module with 'name' into the package.preloaded table.
  203. * @param f The function to be called when the module is opened.
  204. * @param name The name of the module, with 'love'-prefix, for instance 'love.graphics'.
  205. **/
  206. int luax_preload(lua_State *L, lua_CFunction f, const char *name);
  207. /**
  208. * Register a new type.
  209. * @param tname The name of the type. This must not conflict with other type names,
  210. * even from other modules.
  211. * @param f The list of member functions for the type.
  212. **/
  213. int luax_register_type(lua_State *L, const char *tname, const luaL_Reg *f = 0);
  214. /**
  215. * Do a table.insert from C
  216. * @param L the state
  217. * @param tindex the stack index of the table
  218. * @param vindex the stack index of the value
  219. * @param pos the position to insert it in
  220. **/
  221. int luax_table_insert(lua_State *L, int tindex, int vindex, int pos = -1);
  222. /**
  223. * Register a new searcher function for package.loaders. This can for instance enable
  224. * loading of files through love.filesystem using standard require.
  225. * @param L The Lua state.
  226. * @param f The searcher function.
  227. * @param pos The position to insert the loader in.
  228. **/
  229. int luax_register_searcher(lua_State *L, lua_CFunction f, int pos = -1);
  230. /**
  231. * Pushes a Lua representation of the given object onto the stack, creating and
  232. * storing the Lua representation in a weak table if it doesn't exist yet.
  233. * NOTE: The object will be retained by Lua and released upon garbage collection.
  234. * @param L The Lua state.
  235. * @param name The name of the type. This must match the name used with luax_register_type.
  236. * @param flags The type information of the object.
  237. * @param object The pointer to the actual object.
  238. **/
  239. void luax_pushtype(lua_State *L, const char *name, bits flags, love::Object *object);
  240. /**
  241. * Creates a new Lua representation of the given object *without* checking if it
  242. * exists yet, and *without* storing it in a weak table.
  243. * This should only be used when performance is an extreme concern and the
  244. * object is not ever expected to be pushed to Lua again, as it prevents the
  245. * Lua-side objects from working in some cases when used as keys in tables.
  246. * NOTE: The object will be retained by Lua and released upon garbage collection.
  247. * @param L The Lua state.
  248. * @param name The name of the type. This must match the name used with luax_register_type.
  249. * @param flags The type information of the object.
  250. * @param object The pointer to the actual object.
  251. **/
  252. void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *object);
  253. /**
  254. * Checks whether the value at idx is a certain type.
  255. * @param L The Lua state.
  256. * @param idx The index on the stack.
  257. * @param type The type to check for.
  258. * @return True if the value is Proxy of the specified type, false otherwise.
  259. **/
  260. bool luax_istype(lua_State *L, int idx, love::bits type);
  261. /**
  262. * Gets the function love.module.function and puts it on top of the stack (alone). If the
  263. * love table, the module, or the function does not exist, an error is returned.
  264. * @return An error if nonexistent, or 1 if successful.
  265. **/
  266. int luax_getfunction(lua_State *L, const char *module, const char *function);
  267. /**
  268. * Converts an object into another object by the specified function love.module.function.
  269. * The conversion function must accept a single object of the relevant type as a parameter,
  270. * and returnone value. If the function does not exist (see luax_getfunction), an error is returned.
  271. *
  272. * Note that the initial object at idx is replaced by the new object.
  273. *
  274. * @param L The Lua state.
  275. * @param idx The index on the stack.
  276. * @param module The module in the love table.
  277. * @param function The function in the module.
  278. **/
  279. int luax_convobj(lua_State *L, int idx, const char *module, const char *function);
  280. /**
  281. * Converts an object into another object by the specified function love.module.function.
  282. * The conversion function must accept a single object of the relevant type as its first parameter,
  283. * and return one value. If the function does not exist (see luax_getfunction), an error is returned.
  284. *
  285. * Note that the initial object at idx is replaced by the new object.
  286. *
  287. * @param L The Lua state.
  288. * @param idxs An array of indices on the stack.
  289. * @param n How many arguments are being passed.
  290. * @param module The module in the love table.
  291. * @param function The function in the module.
  292. **/
  293. int luax_convobj(lua_State *L, int idxs[], int n, const char *module, const char *function);
  294. // pcall versions of the above
  295. int luax_pconvobj(lua_State *L, int idx, const char *module, const char *function);
  296. int luax_pconvobj(lua_State *L, int idxs[], int n, const char *module, const char *function);
  297. /**
  298. * 'Insist' that a table 'k' exists in the table at idx. Insistence involves that the
  299. * table (k) is created if it does not exist in the table at idx. The table at idx must
  300. * pre-exist, however. Also note that if the a non-table value exists at the specified
  301. * location, it will be overwritten with a new table. The insisted table, and only the
  302. * insisted table, will be placed on top of the stack.
  303. *
  304. * @param idx The index on the stack containing a table.
  305. * @param k The name of the table we are insisting exist.
  306. **/
  307. int luax_insist(lua_State *L, int idx, const char *k);
  308. /**
  309. * Insist that a global table 'k' exists. See luax_insist.
  310. * @param k The name of the table we are insisting exist.
  311. **/
  312. int luax_insistglobal(lua_State *L, const char *k);
  313. /**
  314. * Insists that a table 'k' exists inside the 'love' table. See luax_insist.
  315. * @param k The name of the table we are insisting exist.
  316. **/
  317. int luax_insistlove(lua_State *L, const char *k);
  318. /**
  319. * Pushes the table 'k' in the love table onto the stack. Pushes nil if the
  320. * table doesn't exist.
  321. * @param k The name of the table we want to get.
  322. **/
  323. int luax_getlove(lua_State *L, const char *k);
  324. /**
  325. * Gets (creates if needed) the specified Registry, and pushes it into the
  326. * stack.
  327. * @param L The Lua state.
  328. * @param r The Registry to get.
  329. **/
  330. int luax_insistregistry(lua_State *L, Registry r);
  331. /**
  332. * Gets the specified Registry, and pushes it onto the stack. Pushes nil if the
  333. * registry hasn't been created (see luax_insistregistry.)
  334. * @param L The Lua state.
  335. * @param r The Registry to get.
  336. **/
  337. int luax_getregistry(lua_State *L, Registry r);
  338. extern "C" { // Also called from luasocket
  339. int luax_typerror(lua_State *L, int narg, const char *tname);
  340. }
  341. /**
  342. * Like luax_totype, but causes an error if the value at idx is not Proxy,
  343. * or is not the specified type.
  344. * @param L The Lua state.
  345. * @param idx The index on the stack.
  346. * @param name The name of the type.
  347. * @param type The type bit.
  348. **/
  349. template <typename T>
  350. T *luax_checktype(lua_State *L, int idx, const char *name, love::bits type)
  351. {
  352. if (lua_type(L, idx) != LUA_TUSERDATA)
  353. luax_typerror(L, idx, name);
  354. Proxy *u = (Proxy *)lua_touserdata(L, idx);
  355. if ((u->flags & type) != type)
  356. luax_typerror(L, idx, name);
  357. return (T *)u->data;
  358. }
  359. template <typename T>
  360. T *luax_getmodule(lua_State *L, const char *k, love::bits type)
  361. {
  362. luax_insistregistry(L, REGISTRY_MODULES);
  363. lua_getfield(L, -1, k);
  364. if (!lua_isuserdata(L, -1))
  365. luaL_error(L, "Tried to get nonexistant module %s.", k);
  366. Proxy *u = (Proxy *)lua_touserdata(L, -1);
  367. if ((u->flags & type) != type)
  368. luaL_error(L, "Incorrect module %s", k);
  369. lua_pop(L, 2);
  370. return (T *)u->data;
  371. }
  372. template <typename T>
  373. T *luax_optmodule(lua_State *L, const char *k, love::bits type)
  374. {
  375. luax_insistregistry(L, REGISTRY_MODULES);
  376. lua_getfield(L, -1, k);
  377. if (!lua_isuserdata(L, -1))
  378. {
  379. lua_pop(L, 2);
  380. return 0;
  381. }
  382. Proxy *u = (Proxy *)lua_touserdata(L, -1);
  383. if ((u->flags & type) != type)
  384. luaL_error(L, "Incorrect module %s", k);
  385. lua_pop(L, 2);
  386. return (T *) u->data;
  387. }
  388. /**
  389. * Converts the value at idx to the specified type without checking that
  390. * this conversion is valid. If the type has been previously verified with
  391. * luax_istype, then this can be safely used. Otherwise, use luax_checktype.
  392. * @param L The Lua state.
  393. * @param idx The index on the stack.
  394. * @param name The name of the type.
  395. * @param type The type bit.
  396. **/
  397. template <typename T>
  398. T *luax_totype(lua_State *L, int idx, const char * /* name */, love::bits /* type */)
  399. {
  400. return (T *)(((Proxy *)lua_touserdata(L, idx))->data);
  401. }
  402. Type luax_type(lua_State *L, int idx);
  403. /**
  404. * Converts any exceptions thrown by the passed lambda function into a Lua error.
  405. * lua_error (and luaL_error) cannot be called from inside the exception handler
  406. * because they use longjmp, which causes undefined behaviour when the
  407. * destructor of the exception would have been called.
  408. **/
  409. template <typename T>
  410. int luax_catchexcept(lua_State *L, const T& func)
  411. {
  412. bool should_error = false;
  413. try
  414. {
  415. func();
  416. }
  417. catch (const std::exception &e)
  418. {
  419. should_error = true;
  420. lua_pushstring(L, e.what());
  421. }
  422. if (should_error)
  423. return luaL_error(L, "%s", lua_tostring(L, -1));
  424. return 0;
  425. }
  426. template <typename T, typename F>
  427. int luax_catchexcept(lua_State *L, const T& func, const F& finallyfunc)
  428. {
  429. bool should_error = false;
  430. try
  431. {
  432. func();
  433. }
  434. catch (const std::exception &e)
  435. {
  436. should_error = true;
  437. lua_pushstring(L, e.what());
  438. }
  439. finallyfunc();
  440. if (should_error)
  441. return luaL_error(L, "%s", lua_tostring(L, -1));
  442. return 0;
  443. }
  444. } // love
  445. #endif // LOVE_RUNTIME_H