lua_stack.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "lua.hpp"
  25. #include "types.h"
  26. #include "lua_system.h"
  27. #include "vector3.h"
  28. #include "vector2.h"
  29. #include "quaternion.h"
  30. #include "matrix4x4.h"
  31. #include "string_utils.h"
  32. #include "color4.h"
  33. #if defined(CROWN_DEBUG)
  34. static void* checkudata(lua_State* L, int index, const char* expected)
  35. {
  36. luaL_checktype(L, index, LUA_TUSERDATA);
  37. return luaL_checkudata(L, index, expected);
  38. }
  39. typedef bool (*checkfn)(int);
  40. static void* checklightdata(lua_State* L, int index, checkfn cf, const char* expected)
  41. {
  42. luaL_checktype(L, index, LUA_TLIGHTUSERDATA);
  43. if (!cf(index)) luaL_typerror(L, index, expected);
  44. return lua_touserdata(L, index);
  45. }
  46. static bool always_true(int index)
  47. {
  48. return index == index;
  49. }
  50. #define CHECKUDATA(stack, index, expected) checkudata(stack, index, expected)
  51. #define CHECKLIGHTDATA(stack, index, cf, expected) checklightdata(stack, index, cf, expected)
  52. #define CHECKBOOLEAN(stack, index) lua_toboolean(stack, index)
  53. #define CHECKINTEGER(stack, index) luaL_checkinteger(stack, index)
  54. #define CHECKNUMBER(stack, index) luaL_checknumber(stack, index)
  55. #define CHECKSTRING(stack, index) luaL_checkstring(stack, index)
  56. #else
  57. #define CHECKUDATA(stack, index, expected) lua_touserdata(stack, index)
  58. #define CHECKLIGHTDATA(stack, index, cf, expected) lua_touserdata(stack, index)
  59. #define CHECKBOOLEAN(stack, index) lua_toboolean(stack, index)
  60. #define CHECKINTEGER(stack, index) lua_tointeger(stack, index)
  61. #define CHECKNUMBER(stack, index) lua_tonumber(stack, index)
  62. #define CHECKSTRING(stack, index) lua_tostring(stack, index)
  63. #endif
  64. namespace crown
  65. {
  66. class PhysicsWorld;
  67. class SoundWorld;
  68. class World;
  69. struct Actor;
  70. struct Camera;
  71. struct Controller;
  72. struct Gui;
  73. struct Mesh;
  74. struct ResourcePackage;
  75. struct Sprite;
  76. struct Unit;
  77. struct DebugLine;
  78. struct Raycast;
  79. struct Material;
  80. typedef Id SoundInstanceId;
  81. typedef Id GuiId;
  82. typedef int (*MetamethodFunction)(lua_State*);
  83. struct LuaStack
  84. {
  85. LuaStack(lua_State* L)
  86. : _L(L)
  87. {
  88. }
  89. lua_State* state()
  90. {
  91. return _L;
  92. }
  93. /// Returns the number of elements in the stack.
  94. /// When called inside a function, it can be used to count
  95. /// the number of arguments passed to the function itself.
  96. int32_t num_args()
  97. {
  98. return lua_gettop(_L);
  99. }
  100. /// Removes the element at the given valid index, shifting down the elements
  101. /// above this index to fill the gap. Cannot be called with a pseudo-index,
  102. /// because a pseudo-index is not an actual stack position.
  103. void remove(int32_t index)
  104. {
  105. lua_remove(_L, index);
  106. }
  107. /// Pops @a n elements from the stack.
  108. void pop(int32_t n)
  109. {
  110. lua_pop(_L, n);
  111. }
  112. bool is_nil(int32_t index)
  113. {
  114. return lua_isnil(_L, index) == 1;
  115. }
  116. bool is_number(int32_t index)
  117. {
  118. return lua_isnumber(_L, index) == 1;
  119. }
  120. /// Wraps lua_type.
  121. int value_type(uint32_t index)
  122. {
  123. return lua_type(_L, index);
  124. }
  125. void push_nil()
  126. {
  127. lua_pushnil(_L);
  128. }
  129. void push_bool(bool value)
  130. {
  131. lua_pushboolean(_L, value);
  132. }
  133. void push_int32(int32_t value)
  134. {
  135. lua_pushinteger(_L, value);
  136. }
  137. void push_uint32(uint32_t value)
  138. {
  139. lua_pushinteger(_L, value);
  140. }
  141. void push_float(float value)
  142. {
  143. lua_pushnumber(_L, value);
  144. }
  145. void push_string(const char* s)
  146. {
  147. lua_pushstring(_L, s);
  148. }
  149. void push_fstring(const char* fmt, ...)
  150. {
  151. va_list vl;
  152. va_start(vl, fmt);
  153. lua_pushvfstring(_L, fmt, vl);
  154. va_end(vl);
  155. }
  156. void push_literal(const char* s, size_t len)
  157. {
  158. lua_pushlstring(_L, s, len);
  159. }
  160. bool get_bool(int32_t index)
  161. {
  162. return CHECKBOOLEAN(_L, index) == 1;
  163. }
  164. int32_t get_int(int32_t index)
  165. {
  166. return CHECKINTEGER(_L, index);
  167. }
  168. float get_float(int32_t index)
  169. {
  170. return (float) CHECKNUMBER(_L, index);
  171. }
  172. const char* get_string(int32_t index)
  173. {
  174. return CHECKSTRING(_L, index);
  175. }
  176. /// Pushes an empty table onto the stack.
  177. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  178. /// and LuaStack::push_key_end() as in the following example:
  179. ///
  180. /// LuaStack stack(L)
  181. /// stack.push_table()
  182. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  183. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  184. /// return 1;
  185. void push_table()
  186. {
  187. lua_newtable(_L);
  188. }
  189. /// See Stack::push_table()
  190. void push_key_begin(const char* key)
  191. {
  192. lua_pushstring(_L, key);
  193. }
  194. /// See Stack::push_table()
  195. void push_key_begin(int32_t i)
  196. {
  197. lua_pushnumber(_L, i);
  198. }
  199. /// See Stack::push_table()
  200. void push_key_end()
  201. {
  202. lua_settable(_L, -3);
  203. }
  204. int next(int32_t i)
  205. {
  206. return lua_next(_L, i);
  207. }
  208. void push_resource_package(ResourcePackage* package)
  209. {
  210. ResourcePackage** p = (ResourcePackage**) lua_newuserdata(_L, sizeof(ResourcePackage*));
  211. *p = package;
  212. luaL_getmetatable(_L, "ResourcePackage");
  213. lua_setmetatable(_L, -2);
  214. }
  215. ResourcePackage* get_resource_package(int32_t index)
  216. {
  217. ResourcePackage* pkg = *(ResourcePackage**) CHECKUDATA(_L, index, "ResourcePackage");
  218. return pkg;
  219. }
  220. void push_world(World* world)
  221. {
  222. World** w = (World**) lua_newuserdata(_L, sizeof(World*));
  223. *w = world;
  224. luaL_getmetatable(_L, "World");
  225. lua_setmetatable(_L, -2);
  226. };
  227. World* get_world(int32_t index)
  228. {
  229. World* w = *(World**) CHECKUDATA(_L, index, "World");
  230. return w;
  231. };
  232. void push_physics_world(PhysicsWorld* world)
  233. {
  234. PhysicsWorld** w = (PhysicsWorld**) lua_newuserdata(_L, sizeof(PhysicsWorld*));
  235. luaL_getmetatable(_L, "PhysicsWorld");
  236. lua_setmetatable(_L, -2);
  237. *w = world;
  238. }
  239. PhysicsWorld* get_physics_world(int32_t index)
  240. {
  241. PhysicsWorld* w = *(PhysicsWorld**) CHECKUDATA(_L, index, "PhysicsWorld");
  242. return w;
  243. }
  244. void push_sound_world(SoundWorld* world)
  245. {
  246. SoundWorld** w = (SoundWorld**) lua_newuserdata(_L, sizeof(SoundWorld*));
  247. *w = world;
  248. luaL_getmetatable(_L, "SoundWorld");
  249. lua_setmetatable(_L, -2);
  250. }
  251. SoundWorld* get_sound_world(int32_t index)
  252. {
  253. SoundWorld* w = *(SoundWorld**) CHECKUDATA(_L, index, "SoundWorld");
  254. return w;
  255. }
  256. void push_unit(Unit* unit)
  257. {
  258. lua_pushlightuserdata(_L, unit);
  259. }
  260. Unit* get_unit(int32_t index)
  261. {
  262. return (Unit*) CHECKLIGHTDATA(_L, index, always_true, "Unit");
  263. }
  264. void push_camera(Camera* camera)
  265. {
  266. lua_pushlightuserdata(_L, camera);
  267. }
  268. Camera* get_camera(int32_t index)
  269. {
  270. return (Camera*) CHECKLIGHTDATA(_L, index, always_true, "Camera");
  271. }
  272. void push_mesh(Mesh* mesh)
  273. {
  274. lua_pushlightuserdata(_L, mesh);
  275. }
  276. Mesh* get_mesh(int32_t index)
  277. {
  278. return (Mesh*) CHECKLIGHTDATA(_L, index, always_true, "Mesh");
  279. }
  280. void push_sprite(Sprite* sprite)
  281. {
  282. lua_pushlightuserdata(_L, sprite);
  283. }
  284. Sprite* get_sprite(int32_t index)
  285. {
  286. return (Sprite*) CHECKLIGHTDATA(_L, index, always_true, "Sprite");
  287. }
  288. void push_material(Material* material)
  289. {
  290. lua_pushlightuserdata(_L, material);
  291. }
  292. Material* get_material(int32_t index)
  293. {
  294. return (Material*) CHECKLIGHTDATA(_L, index, always_true, "Material");
  295. }
  296. void push_actor(Actor* actor)
  297. {
  298. lua_pushlightuserdata(_L, actor);
  299. }
  300. Actor* get_actor(int32_t index)
  301. {
  302. return (Actor*) CHECKLIGHTDATA(_L, index, always_true, "Actor");
  303. }
  304. void push_controller(Controller* controller)
  305. {
  306. lua_pushlightuserdata(_L, controller);
  307. }
  308. Controller* get_controller(int32_t index)
  309. {
  310. return (Controller*) CHECKLIGHTDATA(_L, index, always_true, "Controller");
  311. }
  312. void push_raycast(Raycast* raycast)
  313. {
  314. lua_pushlightuserdata(_L, raycast);
  315. }
  316. Raycast* get_raycast(int32_t index)
  317. {
  318. return (Raycast*) CHECKLIGHTDATA(_L, index, always_true, "Raycast");
  319. }
  320. void push_sound_instance_id(const SoundInstanceId id)
  321. {
  322. uintptr_t enc = id.encode();
  323. lua_pushlightuserdata(_L, (void*)enc);
  324. }
  325. SoundInstanceId get_sound_instance_id(int32_t index)
  326. {
  327. uint32_t enc = (uintptr_t) CHECKLIGHTDATA(_L, index, always_true, "SoundInstanceId");
  328. SoundInstanceId id;
  329. id.decode(enc);
  330. return id;
  331. }
  332. void push_gui(Gui* gui)
  333. {
  334. lua_pushlightuserdata(_L, gui);
  335. }
  336. Gui* get_gui(int32_t index)
  337. {
  338. return (Gui*) CHECKLIGHTDATA(_L, index, always_true, "Gui");
  339. }
  340. void push_debug_line(DebugLine* line)
  341. {
  342. DebugLine** l = (DebugLine**) lua_newuserdata(_L, sizeof(DebugLine*));
  343. *l = line;
  344. luaL_getmetatable(_L, "DebugLine");
  345. lua_setmetatable(_L, -2);
  346. }
  347. DebugLine* get_debug_line(int32_t index)
  348. {
  349. DebugLine* l = *(DebugLine**) CHECKUDATA(_L, index, "DebugLine");
  350. return l;
  351. }
  352. Vector2& get_vector2(int32_t index)
  353. {
  354. void* v = CHECKLIGHTDATA(_L, index, lua_globals::is_vector2, "Vector2");
  355. return *(Vector2*)v;
  356. }
  357. Vector3& get_vector3(int32_t index)
  358. {
  359. void* v = CHECKLIGHTDATA(_L, index, lua_globals::is_vector3, "Vector3");
  360. return *(Vector3*)v;
  361. }
  362. Matrix4x4& get_matrix4x4(int32_t index)
  363. {
  364. void* m = CHECKLIGHTDATA(_L, index, lua_globals::is_matrix4x4, "Matrix4x4");
  365. return *(Matrix4x4*)m;
  366. }
  367. Quaternion& get_quaternion(int32_t index)
  368. {
  369. void* q = CHECKLIGHTDATA(_L, index, lua_globals::is_quaternion, "Quaternion");
  370. return *(Quaternion*)q;
  371. }
  372. Color4 get_color4(int32_t index)
  373. {
  374. // Color4 represented as Quaternion
  375. void* c = CHECKLIGHTDATA(_L, index, lua_globals::is_quaternion, "Color4");
  376. Quaternion& q = *(Quaternion*)c;
  377. return Color4(q.x, q.y, q.z, q.w);
  378. }
  379. void push_vector2(const Vector2& v)
  380. {
  381. lua_pushlightuserdata(_L, lua_globals::next_vector2(v));
  382. luaL_getmetatable(_L, "Lightuserdata_mt");
  383. lua_setmetatable(_L, -2);
  384. }
  385. void push_vector3(const Vector3& v)
  386. {
  387. lua_pushlightuserdata(_L, lua_globals::next_vector3(v));
  388. luaL_getmetatable(_L, "Lightuserdata_mt");
  389. lua_setmetatable(_L, -2);
  390. }
  391. void push_matrix4x4(const Matrix4x4& m)
  392. {
  393. lua_pushlightuserdata(_L, lua_globals::next_matrix4x4(m));
  394. luaL_getmetatable(_L, "Lightuserdata_mt");
  395. lua_setmetatable(_L, -2);
  396. }
  397. void push_quaternion(const Quaternion& q)
  398. {
  399. lua_pushlightuserdata(_L, lua_globals::next_quaternion(q));
  400. luaL_getmetatable(_L, "Lightuserdata_mt");
  401. lua_setmetatable(_L, -2);
  402. }
  403. void push_vector2box(const Vector2& v)
  404. {
  405. Vector2* vec = (Vector2*) lua_newuserdata(_L, sizeof(Vector2));
  406. luaL_getmetatable(_L, "Vector2Box");
  407. lua_setmetatable(_L, -2);
  408. *vec = v;
  409. }
  410. Vector2& get_vector2box(uint32_t index)
  411. {
  412. Vector2* v = (Vector2*) CHECKUDATA(_L, index, "Vector2Box");
  413. return *v;
  414. }
  415. void push_vector3box(const Vector3& v)
  416. {
  417. Vector3* vec = (Vector3*) lua_newuserdata(_L, sizeof(Vector3));
  418. luaL_getmetatable(_L, "Vector3Box");
  419. lua_setmetatable(_L, -2);
  420. *vec = v;
  421. }
  422. Vector3& get_vector3box(uint32_t index)
  423. {
  424. Vector3* v = (Vector3*) CHECKUDATA(_L, index, "Vector3Box");
  425. return *v;
  426. }
  427. void push_quaternionbox(const Quaternion& q)
  428. {
  429. Quaternion* quat = (Quaternion*) lua_newuserdata(_L, sizeof(Quaternion));
  430. luaL_getmetatable(_L, "QuaternionBox");
  431. lua_setmetatable(_L, -2);
  432. *quat = q;
  433. }
  434. Quaternion& get_quaternionbox(uint32_t index)
  435. {
  436. Quaternion* q = (Quaternion*) CHECKUDATA(_L, index, "QuaternionBox");
  437. return *q;
  438. }
  439. void push_matrix4x4box(const Matrix4x4& m)
  440. {
  441. Matrix4x4* mat = (Matrix4x4*) lua_newuserdata(_L, sizeof(Matrix4x4));
  442. luaL_getmetatable(_L, "Matrix4x4Box");
  443. lua_setmetatable(_L, -2);
  444. *mat = m;
  445. }
  446. Matrix4x4& get_matrix4x4box(uint32_t index)
  447. {
  448. Matrix4x4* m = (Matrix4x4*) CHECKUDATA(_L, index, "Matrix4x4Box");
  449. return *m;
  450. }
  451. private:
  452. lua_State* _L;
  453. };
  454. } // namespace crown