LuaDataModel.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LuaDataModel.h"
  29. #include <RmlUi/Lua/Utilities.h>
  30. #include <RmlUi/Core/DataVariable.h>
  31. #include <RmlUi/Core/Context.h>
  32. #include <RmlUi/Core/DataModelHandle.h>
  33. #define RMLDATAMODEL "RMLDATAMODEL"
  34. namespace Rml {
  35. namespace Lua {
  36. class LuaScalarDef;
  37. struct LuaDataModel {
  38. DataModelHandle handle;
  39. lua_State *dataL;
  40. LuaScalarDef *scalarDef;
  41. };
  42. class LuaScalarDef final : public VariableDefinition {
  43. public:
  44. LuaScalarDef (const struct LuaDataModel *model) :
  45. VariableDefinition(DataVariableType::Scalar), model(model) {}
  46. private:
  47. bool Get(void* ptr, Variant& variant) override {
  48. lua_State *L = model->dataL;
  49. if (!L)
  50. return false;
  51. int id = int((intptr_t)ptr);
  52. GetVariant(L, id, &variant);
  53. return true;
  54. }
  55. bool Set(void* ptr, const Rml::Variant& variant) override {
  56. int id = int((intptr_t)ptr);
  57. lua_State *L = model->dataL;
  58. if (!L)
  59. return false;
  60. PushVariant(L, &variant);
  61. lua_replace(L, id);
  62. return true;
  63. }
  64. const struct LuaDataModel *model;
  65. };
  66. static int
  67. getId(lua_State *L, lua_State *dataL) {
  68. lua_pushvalue(dataL, 1);
  69. lua_xmove(dataL, L, 1);
  70. lua_pushvalue(L, 2);
  71. lua_rawget(L, -2);
  72. if (lua_type(L, -1) != LUA_TNUMBER) {
  73. luaL_error(L, "DataModel has no key : %s", lua_tostring(L, 2));
  74. }
  75. int id = (int)lua_tointeger(L, -1);
  76. lua_pop(L, 2);
  77. return id;
  78. }
  79. static int
  80. lDataModelGet(lua_State *L) {
  81. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, 1);
  82. lua_State *dataL = D->dataL;
  83. if (dataL == nullptr)
  84. luaL_error(L, "DataModel closed");
  85. int id = getId(L, dataL);
  86. lua_pushvalue(dataL, id);
  87. lua_xmove(dataL, L, 1);
  88. return 1;
  89. }
  90. static int
  91. lDataModelSet(lua_State *L) {
  92. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, 1);
  93. lua_State *dataL = D->dataL;
  94. if (dataL == nullptr)
  95. luaL_error(L, "DataModel closed");
  96. int id = getId(L, dataL);
  97. lua_xmove(L, dataL, 1);
  98. lua_replace(dataL, id);
  99. D->handle.DirtyVariable(lua_tostring(L, 2));
  100. return 0;
  101. }
  102. // Construct a lua sub thread for LuaDataModel
  103. // stack 1 : { name(string) -> id(integer) }
  104. // stack 2- : values
  105. // For example : build from { str = "Hello", x = 0 }
  106. // 1: { str = 2 , x = 3 }
  107. // 2: "Hello"
  108. // 3: 0
  109. static lua_State *
  110. InitDataModelFromTable(lua_State *L, int index, Rml::DataModelConstructor &ctor, class LuaScalarDef *def) {
  111. lua_State *dataL = lua_newthread(L);
  112. lua_newtable(dataL);
  113. intptr_t id = 2;
  114. lua_pushnil(L);
  115. while (lua_next(L, index) != 0) {
  116. if (!lua_checkstack(dataL, 4)) {
  117. luaL_error(L, "Memory Error");
  118. }
  119. // L top : key value
  120. lua_xmove(L, dataL, 1); // move value to dataL with index(id)
  121. lua_pushvalue(L, -1); // dup key
  122. lua_xmove(L, dataL, 1);
  123. lua_pushinteger(dataL, id);
  124. lua_rawset(dataL, 1);
  125. const char *key = lua_tostring(L, -1);
  126. ctor.BindCustomDataVariable(key, Rml::DataVariable(def, (void *)id));
  127. ++id;
  128. }
  129. return dataL;
  130. }
  131. bool
  132. OpenLuaDataModel(lua_State *L, Rml::Context *context, int name_index, int table_index) {
  133. Rml::String name = luaL_checkstring(L, name_index);
  134. luaL_checktype(L, table_index, LUA_TTABLE);
  135. Rml::DataModelConstructor constructor = context->CreateDataModel(name);
  136. if (!constructor) {
  137. constructor = context->GetDataModel(name);
  138. if (!constructor) {
  139. return false;
  140. }
  141. }
  142. struct LuaDataModel *D = (struct LuaDataModel *)lua_newuserdata(L, sizeof(*D));
  143. D->dataL = nullptr;
  144. D->scalarDef = nullptr;
  145. D->handle = constructor.GetModelHandle();
  146. D->scalarDef = new LuaScalarDef(D);
  147. D->dataL = InitDataModelFromTable(L, table_index, constructor, D->scalarDef);
  148. lua_setuservalue(L, -2); // set D->dataL into uservalue of D
  149. if (luaL_newmetatable(L, RMLDATAMODEL)) {
  150. luaL_Reg l[] = {
  151. { "__index", lDataModelGet },
  152. { "__newindex", lDataModelSet },
  153. { nullptr, nullptr },
  154. };
  155. luaL_setfuncs(L, l, 0);
  156. }
  157. lua_setmetatable(L, -2);
  158. return true;
  159. }
  160. // If you create all the Data Models from lua, you can store these LuaDataModel objects in a table,
  161. // and call CloseLuaDataModel for each after Context released.
  162. // We don't put it in __gc, becuase LuaDataModel can be free before DataModel if you are not careful.
  163. // scalarDef will free by CloseLuaDataModel, but DataModel need it.
  164. void
  165. CloseLuaDataModel(lua_State *L) {
  166. luaL_checkudata(L, -1, RMLDATAMODEL);
  167. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, -1);
  168. D->dataL = nullptr;
  169. delete D->scalarDef;
  170. D->scalarDef = nullptr;
  171. lua_pushnil(L);
  172. lua_setuservalue(L, -2);
  173. }
  174. } // namespace Lua
  175. } // namespace Rml