lua_stack.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "resource_types.h"
  7. #include "string_id.h"
  8. #include "types.h"
  9. #include "world_types.h"
  10. #include <lua.hpp>
  11. #if CROWN_DEBUG
  12. #define LUA_ASSERT(condition, stack, msg, ...) do { if (!(condition)) {\
  13. stack.push_fstring("\nLua assertion failed: %s\n\t" msg "\n", #condition, ##__VA_ARGS__);\
  14. lua_error(stack.state()); }} while (0);
  15. #else
  16. #define LUA_ASSERT(...) ((void)0)
  17. #endif // CROWN_DEBUG
  18. #define LIGHTDATA_TYPE_BITS 2
  19. #define LIGHTDATA_TYPE_MASK 0x3
  20. #define LIGHTDATA_TYPE_SHIFT 0
  21. #define POINTER_MARKER 0x0
  22. #define UNIT_MARKER 0x1
  23. namespace crown
  24. {
  25. /// Wrapper to manipulate Lua stack.
  26. ///
  27. /// @ingroup Lua
  28. class LuaStack
  29. {
  30. lua_State* L;
  31. public:
  32. LuaStack(lua_State* L)
  33. : L(L)
  34. {
  35. }
  36. lua_State* state()
  37. {
  38. return L;
  39. }
  40. /// Returns the number of elements in the stack.
  41. /// When called inside a function, it can be used to count
  42. /// the number of arguments passed to the function itself.
  43. int num_args()
  44. {
  45. return lua_gettop(L);
  46. }
  47. /// Removes the element at the given valid index, shifting down the elements
  48. /// above this index to fill the gap. Cannot be called with a pseudo-index,
  49. /// because a pseudo-index is not an actual stack position.
  50. void remove(int i)
  51. {
  52. lua_remove(L, i);
  53. }
  54. /// Pops @a n elements from the stack.
  55. void pop(int n)
  56. {
  57. lua_pop(L, n);
  58. }
  59. bool is_nil(int i)
  60. {
  61. return lua_isnil(L, i) == 1;
  62. }
  63. bool is_bool(int i)
  64. {
  65. return lua_isboolean(L, i) == 1;
  66. }
  67. bool is_number(int i)
  68. {
  69. return lua_isnumber(L, i) == 1;
  70. }
  71. bool is_pointer(int i)
  72. {
  73. return lua_islightuserdata(L, i) == 1
  74. && ((uintptr_t)lua_touserdata(L, i) & 0x3) == 0;
  75. }
  76. bool is_function(int i)
  77. {
  78. return lua_isfunction(L, i) == 1;
  79. }
  80. bool is_vector3(int i);
  81. bool is_quaternion(int i);
  82. bool is_matrix4x4(int i);
  83. /// Wraps lua_type.
  84. int value_type(int i)
  85. {
  86. return lua_type(L, i);
  87. }
  88. bool get_bool(int i)
  89. {
  90. return lua_toboolean(L, i) == 1;
  91. }
  92. int get_int(int i)
  93. {
  94. return (int)lua_tonumber(L, i);
  95. }
  96. float get_float(int i)
  97. {
  98. return (float)lua_tonumber(L, i);
  99. }
  100. const char* get_string(int i)
  101. {
  102. return lua_tostring(L, i);
  103. }
  104. void* get_pointer(int i)
  105. {
  106. if (!lua_isuserdata(L, i))
  107. luaL_typerror(L, i, "lightuserdata");
  108. void* p = lua_touserdata(L, i);
  109. CE_ASSERT_NOT_NULL(p);
  110. return p;
  111. }
  112. uint32_t get_id(int i)
  113. {
  114. return (uint32_t)lua_tonumber(L, i);
  115. }
  116. StringId32 get_string_id_32(int i)
  117. {
  118. return StringId32(get_string(i));
  119. }
  120. StringId64 get_string_id_64(int i)
  121. {
  122. return StringId64(get_string(i));
  123. }
  124. StringId64 get_resource_id(int i)
  125. {
  126. return StringId64(get_string(i));
  127. }
  128. DebugLine* get_debug_line(int i)
  129. {
  130. DebugLine* p = (DebugLine*)get_pointer(i);
  131. check_type(i, p);
  132. return p;
  133. }
  134. ResourcePackage* get_resource_package(int i)
  135. {
  136. ResourcePackage* p = (ResourcePackage*)get_pointer(i);
  137. check_type(i, p);
  138. return p;
  139. }
  140. World* get_world(int i)
  141. {
  142. World* p = (World*)get_pointer(i);
  143. check_type(i, p);
  144. return p;
  145. }
  146. SceneGraph* get_scene_graph(int i)
  147. {
  148. SceneGraph* p = (SceneGraph*)get_pointer(i);
  149. check_type(i, p);
  150. return p;
  151. }
  152. Level* get_level(int i)
  153. {
  154. Level* p = (Level*)get_pointer(i);
  155. check_type(i, p);
  156. return p;
  157. }
  158. RenderWorld* get_render_world(int i)
  159. {
  160. RenderWorld* p = (RenderWorld*)get_pointer(i);
  161. check_type(i, p);
  162. return p;
  163. }
  164. PhysicsWorld* get_physics_world(int i)
  165. {
  166. PhysicsWorld* p = (PhysicsWorld*)get_pointer(i);
  167. // if (*(uint32_t*)p != PhysicsWorld::MARKER)
  168. // luaL_typerror(L, i, "PhysicsWorld");
  169. return p;
  170. }
  171. SoundWorld* get_sound_world(int i)
  172. {
  173. SoundWorld* p = (SoundWorld*)get_pointer(i);
  174. // if (*(uint32_t*)p != SoundWorld::MARKER)
  175. // luaL_typerror(L, i, "SoundWorld");
  176. return p;
  177. }
  178. UnitId get_unit(int i)
  179. {
  180. uint32_t enc = (uint32_t)(uintptr_t)get_pointer(i);
  181. if ((enc & LIGHTDATA_TYPE_MASK) != UNIT_MARKER)
  182. luaL_typerror(L, i, "UnitId");
  183. UnitId id;
  184. id.decode(enc >> 2);
  185. return id;
  186. }
  187. CameraInstance get_camera(int i)
  188. {
  189. CameraInstance inst = { get_id(i) };
  190. return inst;
  191. }
  192. TransformInstance get_transform(int i)
  193. {
  194. TransformInstance inst = { get_id(i) };
  195. return inst;
  196. }
  197. MeshInstance get_mesh_instance(int i)
  198. {
  199. MeshInstance inst = { get_id(i) };
  200. return inst;
  201. }
  202. SpriteInstance get_sprite_instance(int i)
  203. {
  204. SpriteInstance inst = { get_id(i) };
  205. return inst;
  206. }
  207. LightInstance get_light_instance(int i)
  208. {
  209. LightInstance inst = { get_id(i) };
  210. return inst;
  211. }
  212. Material* get_material(int i)
  213. {
  214. return (Material*)get_pointer(i);
  215. }
  216. ActorInstance get_actor(int i)
  217. {
  218. ActorInstance inst = { get_id(i) };
  219. return inst;
  220. }
  221. ControllerInstance get_controller(int i)
  222. {
  223. ControllerInstance inst = { get_id(i) };
  224. return inst;
  225. }
  226. SoundInstanceId get_sound_instance_id(int i)
  227. {
  228. return get_id(i);
  229. }
  230. Gui* get_gui(int i)
  231. {
  232. return (Gui*)get_pointer(i);
  233. }
  234. Vector2 get_vector2(int i);
  235. Vector3& get_vector3(int i);
  236. Matrix4x4& get_matrix4x4(int i);
  237. Quaternion& get_quaternion(int i);
  238. Color4 get_color4(int i);
  239. Vector2& get_vector2box(int i)
  240. {
  241. Vector2* v = (Vector2*)luaL_checkudata(L, i, "Vector2Box");
  242. return *v;
  243. }
  244. Vector3& get_vector3box(int i)
  245. {
  246. Vector3* v = (Vector3*)luaL_checkudata(L, i, "Vector3Box");
  247. return *v;
  248. }
  249. Quaternion& get_quaternionbox(int i)
  250. {
  251. Quaternion* q = (Quaternion*)luaL_checkudata(L, i, "QuaternionBox");
  252. return *q;
  253. }
  254. Matrix4x4& get_matrix4x4box(int i)
  255. {
  256. Matrix4x4* m = (Matrix4x4*)luaL_checkudata(L, i, "Matrix4x4Box");
  257. return *m;
  258. }
  259. void push_nil()
  260. {
  261. lua_pushnil(L);
  262. }
  263. void push_bool(bool value)
  264. {
  265. lua_pushboolean(L, value);
  266. }
  267. void push_int(int value)
  268. {
  269. lua_pushnumber(L, value);
  270. }
  271. void push_float(float value)
  272. {
  273. lua_pushnumber(L, value);
  274. }
  275. void push_string(const char* s)
  276. {
  277. lua_pushstring(L, s);
  278. }
  279. void push_fstring(const char* fmt, ...)
  280. {
  281. va_list vl;
  282. va_start(vl, fmt);
  283. lua_pushvfstring(L, fmt, vl);
  284. va_end(vl);
  285. }
  286. void push_lstring(const char* s, uint32_t len)
  287. {
  288. lua_pushlstring(L, s, len);
  289. }
  290. void push_string_id(StringId32 value)
  291. {
  292. lua_pushnumber(L, value.id());
  293. }
  294. void push_pointer(void* p)
  295. {
  296. CE_ASSERT_NOT_NULL(p);
  297. lua_pushlightuserdata(L, p);
  298. }
  299. void push_function(lua_CFunction f)
  300. {
  301. lua_pushcfunction(L, f);
  302. }
  303. void push_id(uint32_t value)
  304. {
  305. lua_pushnumber(L, value);
  306. }
  307. /// Pushes an empty table onto the stack.
  308. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  309. /// and LuaStack::push_key_end() as in the following example:
  310. ///
  311. /// LuaStack stack(L)
  312. /// stack.push_table()
  313. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  314. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  315. /// return 1;
  316. void push_table(int narr = 0, int nrec = 0)
  317. {
  318. lua_createtable(L, narr, nrec);
  319. }
  320. /// See Stack::push_table()
  321. void push_key_begin(const char* key)
  322. {
  323. lua_pushstring(L, key);
  324. }
  325. /// See Stack::push_table()
  326. void push_key_begin(int i)
  327. {
  328. lua_pushnumber(L, i);
  329. }
  330. /// See Stack::push_table()
  331. void push_key_end()
  332. {
  333. lua_settable(L, -3);
  334. }
  335. int next(int i)
  336. {
  337. return lua_next(L, i);
  338. }
  339. void push_debug_line(DebugLine* line)
  340. {
  341. push_pointer(line);
  342. }
  343. void push_resource_package(ResourcePackage* package)
  344. {
  345. push_pointer(package);
  346. }
  347. void push_world(World* world)
  348. {
  349. push_pointer(world);
  350. };
  351. void push_scene_graph(SceneGraph* sg)
  352. {
  353. push_pointer(sg);
  354. }
  355. void push_level(Level* level)
  356. {
  357. push_pointer(level);
  358. }
  359. void push_render_world(RenderWorld* world)
  360. {
  361. push_pointer(world);
  362. }
  363. void push_physics_world(PhysicsWorld* world)
  364. {
  365. push_pointer(world);
  366. }
  367. void push_sound_world(SoundWorld* world)
  368. {
  369. push_pointer(world);
  370. }
  371. void push_unit(UnitId id)
  372. {
  373. uint32_t encoded = (id.encode() << 2) | UNIT_MARKER;
  374. push_pointer((void*)(uintptr_t)encoded);
  375. }
  376. void push_camera(CameraInstance i)
  377. {
  378. push_id(i.i);
  379. }
  380. void push_transform(TransformInstance i)
  381. {
  382. push_id(i.i);
  383. }
  384. void push_mesh_instance(MeshInstance i)
  385. {
  386. push_id(i.i);
  387. }
  388. void push_sprite_instance(SpriteInstance i)
  389. {
  390. push_id(i.i);
  391. }
  392. void push_light_instance(LightInstance i)
  393. {
  394. push_id(i.i);
  395. }
  396. void push_material(Material* material)
  397. {
  398. push_pointer(material);
  399. }
  400. void push_actor(ActorInstance i)
  401. {
  402. push_id(i.i);
  403. }
  404. void push_controller(ControllerInstance i)
  405. {
  406. push_id(i.i);
  407. }
  408. void push_sound_instance_id(const SoundInstanceId id)
  409. {
  410. push_id(id);
  411. }
  412. void push_gui(Gui* gui)
  413. {
  414. push_pointer(gui);
  415. }
  416. void push_vector2(const Vector2& v);
  417. void push_vector3(const Vector3& v);
  418. void push_matrix4x4(const Matrix4x4& m);
  419. void push_quaternion(const Quaternion& q);
  420. void push_color4(const Color4& c);
  421. void push_vector2box(const Vector2& v)
  422. {
  423. Vector2* vec = (Vector2*)lua_newuserdata(L, sizeof(Vector2));
  424. luaL_getmetatable(L, "Vector2Box");
  425. lua_setmetatable(L, -2);
  426. *vec = v;
  427. }
  428. void push_vector3box(const Vector3& v)
  429. {
  430. Vector3* vec = (Vector3*)lua_newuserdata(L, sizeof(Vector3));
  431. luaL_getmetatable(L, "Vector3Box");
  432. lua_setmetatable(L, -2);
  433. *vec = v;
  434. }
  435. void push_quaternionbox(const Quaternion& q)
  436. {
  437. Quaternion* quat = (Quaternion*)lua_newuserdata(L, sizeof(Quaternion));
  438. luaL_getmetatable(L, "QuaternionBox");
  439. lua_setmetatable(L, -2);
  440. *quat = q;
  441. }
  442. void push_matrix4x4box(const Matrix4x4& m)
  443. {
  444. Matrix4x4* mat = (Matrix4x4*)lua_newuserdata(L, sizeof(Matrix4x4));
  445. luaL_getmetatable(L, "Matrix4x4Box");
  446. lua_setmetatable(L, -2);
  447. *mat = m;
  448. }
  449. void check_temporary(int i, const Vector3* p);
  450. void check_temporary(int i, const Quaternion* p);
  451. void check_temporary(int i, const Matrix4x4* p);
  452. void check_type(int i, const DebugLine* p);
  453. void check_type(int i, const ResourcePackage* p);
  454. void check_type(int i, const World* p);
  455. void check_type(int i, const SceneGraph* p);
  456. void check_type(int i, const RenderWorld* p);
  457. void check_type(int i, const Level* p);
  458. };
  459. } // namespace crown