2
0

LuaStack.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "LuaStack.h"
  2. #include <cassert>
  3. namespace crown
  4. {
  5. //-------------------------------------------------------
  6. LuaStack::LuaStack(lua_State* L)
  7. {
  8. m_state = L;
  9. }
  10. //-------------------------------------------------------
  11. void LuaStack::push_bool(bool value)
  12. {
  13. lua_pushboolean(m_state, value);
  14. }
  15. //-------------------------------------------------------
  16. void LuaStack::push_int(int32_t value)
  17. {
  18. lua_pushinteger(m_state, value);
  19. }
  20. //-------------------------------------------------------
  21. void LuaStack::push_float(float value)
  22. {
  23. lua_pushnumber(m_state, value);
  24. }
  25. //-------------------------------------------------------
  26. void LuaStack::push_string(const char* str, size_t len)
  27. {
  28. lua_pushlstring(m_state, str, len);
  29. }
  30. //-------------------------------------------------------
  31. void LuaStack::push_lightudata(void* ptr)
  32. {
  33. lua_pushlightuserdata(m_state, ptr);
  34. }
  35. //-------------------------------------------------------
  36. bool LuaStack::get_bool(int32_t index)
  37. {
  38. return (bool) luaL_checkinteger(m_state, index);
  39. }
  40. //-------------------------------------------------------
  41. int32_t LuaStack::get_int(int32_t index)
  42. {
  43. return luaL_checkinteger(m_state, index);
  44. }
  45. //-------------------------------------------------------
  46. float LuaStack::get_float(int32_t index)
  47. {
  48. return luaL_checknumber(m_state, index);
  49. }
  50. //-------------------------------------------------------
  51. const char* LuaStack::get_string(int32_t index)
  52. {
  53. return luaL_checkstring(m_state, index);
  54. }
  55. const void* LuaStack::get_lightudata(int32_t index)
  56. {
  57. if (!lua_islightuserdata(m_state, index))
  58. {
  59. assert(0);
  60. }
  61. return lua_touserdata(m_state, index);
  62. }
  63. } // namespace crown