LuaBinder.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_SCRIPT_LUA_BINDER_H
  6. #define ANKI_SCRIPT_LUA_BINDER_H
  7. #include "anki/util/Assert.h"
  8. #include "anki/util/StdTypes.h"
  9. #include "anki/util/Allocator.h"
  10. #include "anki/util/String.h"
  11. #include <lua.hpp>
  12. #ifndef ANKI_LUA_HPP
  13. # error "Wrong LUA header included"
  14. #endif
  15. #include <functional>
  16. namespace anki {
  17. /// LUA userdata
  18. class UserData
  19. {
  20. public:
  21. I64 m_sig = 0; ///< Signature to identify the user data.
  22. void* m_data = nullptr;
  23. Bool8 m_gc = false; ///< Garbage collection on?
  24. };
  25. /// Lua binder class. A wrapper on top of LUA
  26. class LuaBinder
  27. {
  28. public:
  29. template<typename T>
  30. using Allocator = ChainAllocator<T>;
  31. LuaBinder();
  32. ~LuaBinder();
  33. ANKI_USE_RESULT Error create(Allocator<U8>& alloc, void* parent);
  34. lua_State* getLuaState()
  35. {
  36. return m_l;
  37. }
  38. Allocator<U8> getAllocator() const
  39. {
  40. return m_alloc;
  41. }
  42. void* getParent() const
  43. {
  44. return m_parent;
  45. }
  46. /// Expose a variable to the lua state
  47. template<typename T>
  48. void exposeVariable(const char* name, T* y);
  49. /// Evaluate a string
  50. ANKI_USE_RESULT Error evalString(const CString& str);
  51. /// For debugging purposes
  52. static void stackDump(lua_State* l);
  53. /// Make sure that the arguments match the argsCount number
  54. static void checkArgsCount(lua_State* l, I argsCount);
  55. /// Create a new LUA class
  56. static void createClass(lua_State* l, const char* className);
  57. /// Add new function in a class that it's already in the stack
  58. static void pushLuaCFuncMethod(lua_State* l, const char* name,
  59. lua_CFunction luafunc);
  60. /// Add a new static function in the class.
  61. static void pushLuaCFuncStaticMethod(lua_State* l, const char* className,
  62. const char* name, lua_CFunction luafunc);
  63. /// Add a new function.
  64. static void pushLuaCFunc(
  65. lua_State* l, const char* name, lua_CFunction luafunc);
  66. /// Get a number from the stack.
  67. template<typename TNumber>
  68. static ANKI_USE_RESULT Error checkNumber(
  69. lua_State* l, I stackIdx, TNumber& number);
  70. /// Get a string from the stack.
  71. static ANKI_USE_RESULT Error checkString(
  72. lua_State* l, I32 stackIdx, const char*& out);
  73. /// Get some user data from the stack.
  74. /// The function uses the type signature to validate the type and not the
  75. /// typeName. That is supposed to be faster.
  76. static ANKI_USE_RESULT Error checkUserData(
  77. lua_State* l, I32 stackIdx, const char* typeName, I64 typeSignature,
  78. UserData*& out);
  79. /// Allocate memory.
  80. static void* luaAlloc(lua_State* l, size_t size);
  81. /// Free memory.
  82. static void luaFree(lua_State* l, void* ptr);
  83. template<typename TWrapedType>
  84. static I64 getWrappedTypeSignature();
  85. template<typename TWrapedType>
  86. static const char* getWrappedTypeName();
  87. private:
  88. Allocator<U8> m_alloc;
  89. lua_State* m_l = nullptr;
  90. void* m_parent = nullptr; ///< Point to the ScriptManager
  91. static void* luaAllocCallback(
  92. void* userData, void* ptr, PtrSize osize, PtrSize nsize);
  93. static ANKI_USE_RESULT Error checkNumberInternal(
  94. lua_State* l, I32 stackIdx, lua_Number& number);
  95. };
  96. //==============================================================================
  97. template<typename TNumber>
  98. inline Error LuaBinder::checkNumber(
  99. lua_State* l, I stackIdx, TNumber& number)
  100. {
  101. lua_Number lnum;
  102. Error err = checkNumberInternal(l, stackIdx, lnum);
  103. if(!err)
  104. {
  105. number = lnum;
  106. }
  107. return err;
  108. }
  109. //==============================================================================
  110. template<typename T>
  111. inline void LuaBinder::exposeVariable(const char* name, T* y)
  112. {
  113. void* ptr = lua_newuserdata(m_l, sizeof(UserData));
  114. UserData* ud = reinterpret_cast<UserData*>(ptr);
  115. ud->m_data = y;
  116. ud->m_gc = false;
  117. ud->m_sig = getWrappedTypeSignature<T>();
  118. luaL_setmetatable(m_l, getWrappedTypeName<T>());
  119. lua_setglobal(m_l, name);
  120. }
  121. } // end namespace anki
  122. #endif