LuaWrapper.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * LuaWrapper.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "api/Registry.h"
  12. #include "LuaFunctor.h"
  13. #include "LuaStack.h"
  14. /*
  15. * Original code is LunaWrapper by nornagon.
  16. * https://lua-users.org/wiki/LunaWrapper
  17. * Published under the BSD 2-clause license
  18. * https://opensource.org/licenses/BSD-2-Clause
  19. *
  20. */
  21. namespace scripting
  22. {
  23. namespace detail
  24. {
  25. template<typename T>
  26. struct RegType
  27. {
  28. const char * name;
  29. std::function<int(lua_State *, T)> functor;
  30. };
  31. struct CustomRegType
  32. {
  33. const char * name;
  34. lua_CFunction functor;
  35. bool isStatic;
  36. };
  37. template <typename P, typename U>
  38. struct Dispatcher
  39. {
  40. using ProxyType = P;
  41. using UDataType = U;
  42. static int invoke(lua_State * L)
  43. {
  44. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  45. int i = (int)lua_tonumber(L, lua_upvalueindex(1));
  46. void * raw = luaL_checkudata(L, 1, KEY);
  47. if(!raw)
  48. {
  49. lua_settop(L, 0);
  50. return 0;
  51. }
  52. lua_remove(L, 1);
  53. auto obj = *(static_cast<UDataType *>(raw));
  54. return (ProxyType::REGISTER[i].functor)(L, obj);
  55. }
  56. static void setIndexTable(lua_State * L)
  57. {
  58. lua_pushstring(L, "__index");
  59. lua_newtable(L);
  60. lua_Integer index = 0;
  61. for(auto & reg : ProxyType::REGISTER)
  62. {
  63. lua_pushstring(L, reg.name);
  64. lua_pushnumber(L, index);
  65. lua_pushcclosure(L, &invoke, 1);
  66. lua_rawset(L, -3);
  67. index++;
  68. }
  69. for(auto & reg : ProxyType::REGISTER_CUSTOM)
  70. {
  71. if(!reg.isStatic)
  72. {
  73. lua_pushstring(L, reg.name);
  74. lua_pushcclosure(L, reg.functor, 0);
  75. lua_rawset(L, -3);
  76. }
  77. }
  78. lua_rawset(L, -3);
  79. }
  80. static void pushStaticTable(lua_State * L)
  81. {
  82. lua_newtable(L);
  83. lua_newtable(L);
  84. lua_pushstring(L, "__index");
  85. {
  86. lua_newtable(L);
  87. for(auto & reg : ProxyType::REGISTER_CUSTOM)
  88. {
  89. if(reg.isStatic)
  90. {
  91. lua_pushstring(L, reg.name);
  92. lua_pushcclosure(L, reg.functor, 0);
  93. lua_rawset(L, -3);
  94. }
  95. }
  96. }
  97. lua_rawset(L, -3);
  98. lua_pushstring(L, "__newindex");
  99. lua_pushnil(L);
  100. lua_rawset(L, -3);
  101. lua_setmetatable(L, -2);
  102. }
  103. static int destructor(lua_State * L)
  104. {
  105. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  106. void * objPtr = luaL_checkudata(L, 1, KEY);
  107. if(objPtr)
  108. {
  109. auto obj = static_cast<UDataType *>(objPtr);
  110. obj->reset();
  111. }
  112. lua_settop(L, 0);
  113. return 0;
  114. }
  115. };
  116. }
  117. class RegistarBase : public api::Registar
  118. {
  119. public:
  120. protected:
  121. virtual void adjustMetatable(lua_State * L) const
  122. {
  123. }
  124. virtual void adjustStaticTable(lua_State * L) const
  125. {
  126. }
  127. };
  128. template<class T, class Proxy = T>
  129. class OpaqueWrapper : public RegistarBase
  130. {
  131. public:
  132. using ObjectType = typename std::remove_cv<T>::type;
  133. using UDataType = ObjectType *;
  134. using CUDataType = const ObjectType *;
  135. using RegType = detail::RegType<UDataType>;
  136. using CustomRegType = detail::CustomRegType;
  137. void pushMetatable(lua_State * L) const override final
  138. {
  139. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  140. static auto S_KEY = api::TypeRegistry::get()->getKey<CUDataType>();
  141. LuaStack S(L);
  142. if(luaL_newmetatable(L, KEY) != 0)
  143. adjustMetatable(L);
  144. S.balance();
  145. if(luaL_newmetatable(L, S_KEY) != 0)
  146. adjustMetatable(L);
  147. S.balance();
  148. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  149. adjustStaticTable(L);
  150. }
  151. protected:
  152. void adjustMetatable(lua_State * L) const override
  153. {
  154. detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  155. }
  156. };
  157. template<class T, class Proxy = T>
  158. class SharedWrapper : public RegistarBase
  159. {
  160. public:
  161. using ObjectType = typename std::remove_cv<T>::type;
  162. using UDataType = std::shared_ptr<T>;
  163. using RegType = detail::RegType<UDataType>;
  164. using CustomRegType = detail::CustomRegType;
  165. static int constructor(lua_State * L)
  166. {
  167. LuaStack S(L);
  168. S.clear();//we do not accept any parameters in constructor
  169. auto obj = std::make_shared<T>();
  170. S.push(obj);
  171. return 1;
  172. }
  173. void pushMetatable(lua_State * L) const override final
  174. {
  175. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  176. LuaStack S(L);
  177. if(luaL_newmetatable(L, KEY) != 0)
  178. {
  179. adjustMetatable(L);
  180. S.push("__gc");
  181. lua_pushcfunction(L, &(detail::Dispatcher<Proxy, UDataType>::destructor));
  182. lua_rawset(L, -3);
  183. }
  184. S.balance();
  185. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  186. adjustStaticTable(L);
  187. }
  188. protected:
  189. void adjustMetatable(lua_State * L) const override
  190. {
  191. detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  192. }
  193. };
  194. template<class T, class Proxy = T>
  195. class UniqueOpaqueWrapper : public api::Registar
  196. {
  197. public:
  198. using ObjectType = typename std::remove_cv<T>::type;
  199. using UDataType = std::unique_ptr<T>;
  200. using RegType = detail::RegType<UDataType>;
  201. using CustomRegType = detail::CustomRegType;
  202. void pushMetatable(lua_State * L) const override final
  203. {
  204. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  205. LuaStack S(L);
  206. if(luaL_newmetatable(L, KEY) != 0)
  207. {
  208. // detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  209. S.push("__gc");
  210. lua_pushcfunction(L, &(detail::Dispatcher<Proxy, UDataType>::destructor));
  211. lua_rawset(L, -3);
  212. }
  213. S.balance();
  214. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  215. }
  216. };
  217. }