LuaDataModel.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "LuaDataModel.h"
  2. #include <RmlUi/Core/Context.h>
  3. #include <RmlUi/Core/DataModelHandle.h>
  4. #include <RmlUi/Core/DataVariable.h>
  5. #include <RmlUi/Lua/Utilities.h>
  6. #define RMLDATAMODEL "RMLDATAMODEL"
  7. namespace Rml {
  8. namespace Lua {
  9. namespace luabind {
  10. #if LUA_VERSION_NUM < 503
  11. static void lua_reverse(lua_State* L, int a, int b)
  12. {
  13. for (; a < b; ++a, --b)
  14. {
  15. lua_pushvalue(L, a);
  16. lua_pushvalue(L, b);
  17. lua_replace(L, a);
  18. lua_replace(L, b);
  19. }
  20. }
  21. void lua_rotate(lua_State* L, int idx, int n)
  22. {
  23. int n_elems = 0;
  24. idx = lua_absindex(L, idx);
  25. n_elems = lua_gettop(L) - idx + 1;
  26. if (n < 0)
  27. {
  28. n += n_elems;
  29. }
  30. if (n > 0 && n < n_elems)
  31. {
  32. luaL_checkstack(L, 2, "not enough stack slots available");
  33. n = n_elems - n;
  34. lua_reverse(L, idx, idx + n - 1);
  35. lua_reverse(L, idx + n, idx + n_elems - 1);
  36. lua_reverse(L, idx, idx + n_elems - 1);
  37. }
  38. }
  39. #endif
  40. using call_t = Rml::Function<void(void)>;
  41. inline int errhandler(lua_State* L)
  42. {
  43. const char* msg = lua_tostring(L, 1);
  44. if (msg == NULL)
  45. {
  46. if (luaL_callmeta(L, 1, "__tostring") && lua_type(L, -1) == LUA_TSTRING)
  47. return 1;
  48. else
  49. msg = lua_pushfstring(L, "(error object is a %s value)", luaL_typename(L, 1));
  50. }
  51. luaL_traceback(L, L, msg, 1);
  52. return 1;
  53. }
  54. inline void errfunc(const char* msg)
  55. {
  56. Log::Message(Log::LT_WARNING, "%s", msg);
  57. }
  58. inline int function_call(lua_State* L)
  59. {
  60. call_t& f = *(call_t*)lua_touserdata(L, 1);
  61. f();
  62. return 0;
  63. }
  64. inline bool invoke(lua_State* L, call_t f, int argn = 0)
  65. {
  66. if (!lua_checkstack(L, 3))
  67. {
  68. errfunc("stack overflow");
  69. lua_pop(L, argn);
  70. return false;
  71. }
  72. lua_pushcfunction(L, errhandler);
  73. lua_pushcfunction(L, function_call);
  74. lua_pushlightuserdata(L, &f);
  75. lua_rotate(L, -argn - 3, 3);
  76. if (lua_pcall(L, 1 + argn, 0, lua_gettop(L) - argn - 2) != LUA_OK)
  77. {
  78. errfunc(lua_tostring(L, -1));
  79. lua_pop(L, 2);
  80. return false;
  81. }
  82. lua_pop(L, 1);
  83. return true;
  84. }
  85. } // namespace luabind
  86. class LuaScalarDef;
  87. class LuaTableDef;
  88. struct LuaDataModel {
  89. DataModelConstructor constructor;
  90. DataModelHandle handle;
  91. lua_State* dataL;
  92. LuaScalarDef* scalarDef;
  93. LuaTableDef* tableDef;
  94. int top;
  95. };
  96. class LuaTableDef : public VariableDefinition {
  97. public:
  98. LuaTableDef(const struct LuaDataModel* model);
  99. bool Get(void* ptr, Variant& variant) override;
  100. bool Set(void* ptr, const Variant& variant) override;
  101. int Size(void* ptr) override;
  102. DataVariable Child(void* ptr, const DataAddressEntry& address) override;
  103. protected:
  104. const struct LuaDataModel* model;
  105. };
  106. class LuaScalarDef final : public LuaTableDef {
  107. public:
  108. LuaScalarDef(const struct LuaDataModel* model);
  109. DataVariable Child(void* ptr, const DataAddressEntry& address) override;
  110. };
  111. LuaTableDef::LuaTableDef(const struct LuaDataModel* model) : VariableDefinition(DataVariableType::Scalar), model(model) {}
  112. bool LuaTableDef::Get(void* ptr, Variant& variant)
  113. {
  114. lua_State* L = model->dataL;
  115. if (!L)
  116. return false;
  117. int id = (int)(intptr_t)ptr;
  118. GetVariant(L, id, &variant);
  119. return true;
  120. }
  121. bool LuaTableDef::Set(void* ptr, const Variant& variant)
  122. {
  123. int id = (int)(intptr_t)ptr;
  124. lua_State* L = model->dataL;
  125. if (!L)
  126. return false;
  127. PushVariant(L, &variant);
  128. lua_replace(L, id);
  129. return true;
  130. }
  131. static int lLuaTableDefSize(lua_State* L)
  132. {
  133. lua_pushinteger(L, luaL_len(L, 1));
  134. return 1;
  135. }
  136. static int lLuaTableDefChild(lua_State* L)
  137. {
  138. lua_gettable(L, 1);
  139. return 1;
  140. }
  141. int LuaTableDef::Size(void* ptr)
  142. {
  143. lua_State* L = model->dataL;
  144. if (!L)
  145. return 0;
  146. int id = (int)(intptr_t)ptr;
  147. if (lua_type(L, id) != LUA_TTABLE)
  148. {
  149. return 0;
  150. }
  151. if (!lua_checkstack(L, 4))
  152. {
  153. return 0;
  154. }
  155. lua_pushcfunction(L, lLuaTableDefSize);
  156. lua_pushvalue(L, id);
  157. if (LUA_OK != lua_pcall(L, 1, 1, 0))
  158. {
  159. lua_pop(L, 1);
  160. return 0;
  161. }
  162. int size = (int)lua_tointeger(L, -1);
  163. lua_pop(L, 1);
  164. return size;
  165. }
  166. DataVariable LuaTableDef::Child(void* ptr, const DataAddressEntry& address)
  167. {
  168. lua_State* L = model->dataL;
  169. if (!L)
  170. return DataVariable{};
  171. int id = (int)(intptr_t)ptr;
  172. if (lua_type(L, id) != LUA_TTABLE)
  173. {
  174. return DataVariable{};
  175. }
  176. if (!lua_checkstack(L, 4))
  177. {
  178. return DataVariable{};
  179. }
  180. lua_pushcfunction(L, lLuaTableDefChild);
  181. lua_pushvalue(L, id);
  182. if (address.index == -1)
  183. {
  184. lua_pushlstring(L, address.name.data(), address.name.size());
  185. }
  186. else
  187. {
  188. lua_pushinteger(L, (lua_Integer)address.index + 1);
  189. }
  190. if (LUA_OK != lua_pcall(L, 2, 1, 0))
  191. {
  192. lua_pop(L, 1);
  193. return DataVariable{};
  194. }
  195. return DataVariable(model->tableDef, (void*)(intptr_t)lua_gettop(L));
  196. }
  197. LuaScalarDef::LuaScalarDef(const struct LuaDataModel* model) : LuaTableDef(model) {}
  198. DataVariable LuaScalarDef::Child(void* ptr, const DataAddressEntry& address)
  199. {
  200. lua_State* L = model->dataL;
  201. if (!L)
  202. return DataVariable{};
  203. lua_settop(L, model->top);
  204. return LuaTableDef::Child(ptr, address);
  205. }
  206. static void BindVariable(struct LuaDataModel* D, lua_State* L)
  207. {
  208. lua_State* dataL = D->dataL;
  209. if (!lua_checkstack(dataL, 4))
  210. {
  211. luaL_error(L, "Memory Error");
  212. }
  213. int id = lua_gettop(dataL) + 1;
  214. D->top = id;
  215. // L top : key value
  216. lua_xmove(L, dataL, 1); // move value to dataL with index(id)
  217. lua_pushvalue(L, -1); // dup key
  218. lua_xmove(L, dataL, 1);
  219. lua_pushinteger(dataL, id);
  220. lua_rawset(dataL, 1);
  221. const char* key = lua_tostring(L, -1);
  222. if (lua_type(dataL, D->top) == LUA_TFUNCTION)
  223. {
  224. D->constructor.BindEventCallback(key, [=](DataModelHandle, Event& event, const VariantList& varlist) {
  225. lua_pushvalue(dataL, id);
  226. lua_xmove(dataL, L, 1);
  227. luabind::invoke(
  228. L,
  229. [&]() {
  230. LuaType<Event>::push(L, &event, false);
  231. for (auto const& variant : varlist)
  232. {
  233. PushVariant(L, &variant);
  234. }
  235. lua_call(L, (int)varlist.size() + 1, 0);
  236. },
  237. 1);
  238. });
  239. }
  240. else
  241. {
  242. D->constructor.BindCustomDataVariable(key, DataVariable(D->scalarDef, (void*)(intptr_t)id));
  243. }
  244. }
  245. static int getId(lua_State* L, lua_State* dataL)
  246. {
  247. lua_pushvalue(dataL, 1);
  248. lua_xmove(dataL, L, 1);
  249. lua_pushvalue(L, 2);
  250. lua_rawget(L, -2);
  251. if (lua_type(L, -1) != LUA_TNUMBER)
  252. {
  253. luaL_error(L, "DataModel has no key : %s", lua_tostring(L, 2));
  254. }
  255. int id = (int)lua_tointeger(L, -1);
  256. lua_pop(L, 2);
  257. return id;
  258. }
  259. static int lDataModelGet(lua_State* L)
  260. {
  261. struct LuaDataModel* D = (struct LuaDataModel*)lua_touserdata(L, 1);
  262. lua_State* dataL = D->dataL;
  263. if (dataL == nullptr)
  264. luaL_error(L, "DataModel closed");
  265. int id = getId(L, dataL);
  266. lua_pushvalue(dataL, id);
  267. lua_xmove(dataL, L, 1);
  268. return 1;
  269. }
  270. static int lDataModelSet(lua_State* L)
  271. {
  272. struct LuaDataModel* D = (struct LuaDataModel*)lua_touserdata(L, 1);
  273. lua_State* dataL = D->dataL;
  274. if (dataL == NULL)
  275. luaL_error(L, "DataModel released");
  276. lua_settop(dataL, D->top);
  277. lua_pushvalue(L, 2);
  278. lua_xmove(L, dataL, 1);
  279. lua_rawget(dataL, 1);
  280. if (lua_type(dataL, -1) == LUA_TNUMBER)
  281. {
  282. int id = (int)lua_tointeger(dataL, -1);
  283. lua_pop(dataL, 1);
  284. lua_xmove(L, dataL, 1);
  285. lua_replace(dataL, id);
  286. D->handle.DirtyVariable(lua_tostring(L, 2));
  287. return 0;
  288. }
  289. lua_pop(dataL, 1);
  290. BindVariable(D, L);
  291. return 0;
  292. }
  293. bool OpenLuaDataModel(lua_State* L, Context* context, int name_index, int table_index)
  294. {
  295. String name = luaL_checkstring(L, name_index);
  296. luaL_checktype(L, table_index, LUA_TTABLE);
  297. DataModelConstructor constructor = context->CreateDataModel(name);
  298. if (!constructor)
  299. {
  300. constructor = context->GetDataModel(name);
  301. if (!constructor)
  302. {
  303. return false;
  304. }
  305. }
  306. struct LuaDataModel* D = (struct LuaDataModel*)lua_newuserdata(L, sizeof(*D));
  307. D->dataL = nullptr;
  308. D->scalarDef = nullptr;
  309. D->tableDef = nullptr;
  310. D->constructor = constructor;
  311. D->handle = constructor.GetModelHandle();
  312. D->scalarDef = new LuaScalarDef(D);
  313. D->tableDef = new LuaTableDef(D);
  314. D->dataL = lua_newthread(L);
  315. D->top = 1;
  316. lua_newtable(D->dataL);
  317. lua_pushnil(L);
  318. while (lua_next(L, table_index) != 0)
  319. {
  320. BindVariable(D, L);
  321. }
  322. lua_setuservalue(L, -2);
  323. if (luaL_newmetatable(L, RMLDATAMODEL))
  324. {
  325. luaL_Reg l[] = {
  326. {"__index", lDataModelGet},
  327. {"__newindex", lDataModelSet},
  328. {nullptr, nullptr},
  329. };
  330. luaL_setfuncs(L, l, 0);
  331. }
  332. lua_setmetatable(L, -2);
  333. return true;
  334. }
  335. // If you create all the Data Models from lua, you can store these LuaDataModel objects in a table,
  336. // and call CloseLuaDataModel for each after Context released.
  337. // We don't put it in __gc, becuase LuaDataModel can be free before DataModel if you are not careful.
  338. // scalarDef will free by CloseLuaDataModel, but DataModel need it.
  339. void CloseLuaDataModel(lua_State* L)
  340. {
  341. luaL_checkudata(L, -1, RMLDATAMODEL);
  342. struct LuaDataModel* D = (struct LuaDataModel*)lua_touserdata(L, -1);
  343. D->dataL = nullptr;
  344. D->top = 0;
  345. delete D->scalarDef;
  346. D->scalarDef = nullptr;
  347. delete D->tableDef;
  348. D->tableDef = nullptr;
  349. lua_pushnil(L);
  350. lua_setuservalue(L, -2);
  351. }
  352. } // namespace Lua
  353. } // namespace Rml