LuaBinder.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2014, 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 binder class. A wrapper on top of LUA
  18. class LuaBinder
  19. {
  20. public:
  21. template<typename T>
  22. using Allocator = HeapAllocator<T>;
  23. LuaBinder(Allocator<U8>& alloc, void* parent);
  24. ~LuaBinder();
  25. /// @privatesection
  26. /// {
  27. lua_State* _getLuaState()
  28. {
  29. return m_l;
  30. }
  31. Allocator<U8> _getAllocator() const
  32. {
  33. return m_alloc;
  34. }
  35. void* _getParent() const
  36. {
  37. return m_parent;
  38. }
  39. /// Make sure that the arguments match the argsCount number
  40. static void checkArgsCount(lua_State* l, I argsCount);
  41. /// Create a new LUA class
  42. static void createClass(lua_State* l, const char* className);
  43. /// Add new function in a class that it's already in the stack
  44. static void pushLuaCFuncMethod(lua_State* l, const char* name,
  45. lua_CFunction luafunc);
  46. /// Add a new static function in the class
  47. static void pushLuaCFuncStaticMethod(lua_State* l, const char* className,
  48. const char* name, lua_CFunction luafunc);
  49. template<typename TNumber>
  50. static ANKI_USE_RESULT Error checkNumber(
  51. lua_State* l, I stackIdx, TNumber& number)
  52. {
  53. lua_Number lnum;
  54. Error err = checkNumberInternal(l, stackIdx, lnum);
  55. if(!err)
  56. {
  57. number = lnum;
  58. }
  59. return err;
  60. }
  61. static void* luaAlloc(lua_State* l, size_t size);
  62. static void luaFree(lua_State* l, void* ptr);
  63. /// }
  64. /// Expose a variable to the lua state
  65. template<typename T>
  66. void exposeVariable(const char* name, T* y);
  67. /// Evaluate a file
  68. void evalFile(const CString& filename);
  69. /// Evaluate a string
  70. void evalString(const CString& str);
  71. /// For debugging purposes
  72. static void stackDump(lua_State* l);
  73. private:
  74. Allocator<U8> m_alloc;
  75. lua_State* m_l = nullptr;
  76. void* m_parent = nullptr; ///< Point to the ScriptManager
  77. static void* luaAllocCallback(
  78. void* userData, void* ptr, PtrSize osize, PtrSize nsize);
  79. static ANKI_USE_RESULT Error checkNumberInternal(
  80. lua_State* l, I stackIdx, lua_Number& number);
  81. };
  82. //==============================================================================
  83. /// lua userdata
  84. class UserData
  85. {
  86. public:
  87. void* m_data = nullptr;
  88. Bool8 m_gc = false; ///< Garbage collection on?
  89. };
  90. } // end namespace anki
  91. #endif