2
0

LuaStack.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #include "LuaStack.h"
  24. #include "Assert.h"
  25. #include "Vec2.h"
  26. #include "Vec3.h"
  27. #include "Mat4.h"
  28. #include "Quat.h"
  29. namespace crown
  30. {
  31. static const int32_t LUA_VEC2_BUFFER_SIZE = 4096;
  32. static Vec2 vec2_buffer[LUA_VEC2_BUFFER_SIZE];
  33. static uint32_t vec2_used = 0;
  34. static const int32_t LUA_VEC3_BUFFER_SIZE = 4096;
  35. static Vec3 vec3_buffer[LUA_VEC3_BUFFER_SIZE];
  36. static uint32_t vec3_used = 0;
  37. static const int32_t LUA_MAT4_BUFFER_SIZE = 4096;
  38. static Mat4 mat4_buffer[LUA_MAT4_BUFFER_SIZE];
  39. static uint32_t mat4_used = 0;
  40. static const int32_t LUA_QUAT_BUFFER_SIZE = 4096;
  41. static Quat quat_buffer[LUA_QUAT_BUFFER_SIZE];
  42. static uint32_t quat_used = 0;
  43. //-----------------------------------------------------------------------------
  44. static Vec2* next_vec2(const Vec2& v)
  45. {
  46. CE_ASSERT(vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
  47. return &(vec2_buffer[vec2_used++] = v);
  48. }
  49. //-----------------------------------------------------------------------------
  50. static Vec3* next_vec3(const Vec3& v)
  51. {
  52. CE_ASSERT(vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
  53. return &(vec3_buffer[vec3_used++] = v);
  54. }
  55. //-----------------------------------------------------------------------------
  56. static Mat4* next_mat4(const Mat4& m)
  57. {
  58. CE_ASSERT(mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
  59. return &(mat4_buffer[mat4_used++] = m);
  60. }
  61. //-----------------------------------------------------------------------------
  62. static Quat* next_quat(const Quat& q)
  63. {
  64. CE_ASSERT(quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
  65. return &(quat_buffer[quat_used++] = q);
  66. }
  67. //-----------------------------------------------------------------------------
  68. LuaStack::LuaStack(lua_State* L)
  69. {
  70. m_state = L;
  71. }
  72. //-----------------------------------------------------------------------------
  73. lua_State* LuaStack::state()
  74. {
  75. return m_state;
  76. }
  77. //-----------------------------------------------------------------------------
  78. int32_t LuaStack::num_args()
  79. {
  80. return lua_gettop(m_state);
  81. }
  82. //-----------------------------------------------------------------------------
  83. void LuaStack::push_bool(bool value)
  84. {
  85. lua_pushboolean(m_state, value);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void LuaStack::push_int32(int32_t value)
  89. {
  90. lua_pushinteger(m_state, value);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void LuaStack::push_uint32(uint32_t value)
  94. {
  95. lua_pushinteger(m_state, value);
  96. }
  97. //-----------------------------------------------------------------------------
  98. void LuaStack::push_int64(int64_t value)
  99. {
  100. lua_pushinteger(m_state, value);
  101. }
  102. //-----------------------------------------------------------------------------
  103. void LuaStack::push_uint64(uint64_t value)
  104. {
  105. lua_pushinteger(m_state, value);
  106. }
  107. //-----------------------------------------------------------------------------
  108. void LuaStack::push_float(float value)
  109. {
  110. lua_pushnumber(m_state, value);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void LuaStack::push_string(const char* str, size_t len)
  114. {
  115. lua_pushlstring(m_state, str, len);
  116. }
  117. //-----------------------------------------------------------------------------
  118. void LuaStack::push_lightdata(void* data)
  119. {
  120. lua_pushlightuserdata(m_state, data);
  121. }
  122. //-----------------------------------------------------------------------------
  123. void LuaStack::push_vec2(const Vec2& v)
  124. {
  125. lua_pushlightuserdata(m_state, next_vec2(v));
  126. }
  127. //-----------------------------------------------------------------------------
  128. void LuaStack::push_vec3(const Vec3& v)
  129. {
  130. lua_pushlightuserdata(m_state, next_vec3(v));
  131. }
  132. //-----------------------------------------------------------------------------
  133. void LuaStack::push_mat4(const Mat4& m)
  134. {
  135. lua_pushlightuserdata(m_state, next_mat4(m));
  136. }
  137. //-----------------------------------------------------------------------------
  138. void LuaStack::push_quat(const Quat& q)
  139. {
  140. lua_pushlightuserdata(m_state, next_quat(q));
  141. }
  142. //-----------------------------------------------------------------------------
  143. bool LuaStack::get_bool(int32_t index)
  144. {
  145. return (bool) luaL_checkinteger(m_state, index);
  146. }
  147. //-----------------------------------------------------------------------------
  148. int32_t LuaStack::get_int(int32_t index)
  149. {
  150. return luaL_checkinteger(m_state, index);
  151. }
  152. //-----------------------------------------------------------------------------
  153. float LuaStack::get_float(int32_t index)
  154. {
  155. return luaL_checknumber(m_state, index);
  156. }
  157. //-----------------------------------------------------------------------------
  158. const char* LuaStack::get_string(int32_t index)
  159. {
  160. return luaL_checkstring(m_state, index);
  161. }
  162. //-----------------------------------------------------------------------------
  163. void* LuaStack::get_lightdata(int32_t index)
  164. {
  165. return lua_touserdata(m_state, index);
  166. }
  167. //-----------------------------------------------------------------------------
  168. Vec2& LuaStack::get_vec2(int32_t index)
  169. {
  170. void* v = lua_touserdata(m_state, index);
  171. if (v < &vec2_buffer[0] || v > &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1])
  172. {
  173. luaL_typerror(m_state, index, "Vec2");
  174. }
  175. return *(Vec2*)v;
  176. }
  177. //-----------------------------------------------------------------------------
  178. Vec3& LuaStack::get_vec3(int32_t index)
  179. {
  180. void* v = lua_touserdata(m_state, index);
  181. if (v < &vec3_buffer[0] || v > &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1])
  182. {
  183. luaL_typerror(m_state, index, "Vec3");
  184. }
  185. return *(Vec3*)v;
  186. }
  187. //-----------------------------------------------------------------------------
  188. Mat4& LuaStack::get_mat4(int32_t index)
  189. {
  190. void* m = lua_touserdata(m_state, index);
  191. if (m < &mat4_buffer[0] || m > &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1])
  192. {
  193. luaL_typerror(m_state, index, "Mat4");
  194. }
  195. return *(Mat4*)m;
  196. }
  197. //-----------------------------------------------------------------------------
  198. Quat& LuaStack::get_quat(int32_t index)
  199. {
  200. void* q = lua_touserdata(m_state, index);
  201. if (q < &quat_buffer[0] || q > &quat_buffer[LUA_QUAT_BUFFER_SIZE-1])
  202. {
  203. luaL_typerror(m_state, index, "Quat");
  204. }
  205. return *(Quat*)q;
  206. }
  207. } // namespace crown