LuaDataModel.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/Core/DataVariable.h>
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/DataModelHandle.h>
  32. #define RMLDATAMODEL "RMLDATAMODEL"
  33. namespace Rml {
  34. namespace Lua {
  35. class LuaScalarDef;
  36. struct LuaDataModel {
  37. DataModelHandle handle;
  38. lua_State *dataL;
  39. LuaScalarDef *scalarDef;
  40. };
  41. static void
  42. PushVariant(lua_State *L, const Rml::Variant &v) {
  43. switch (v.GetType()) {
  44. case Rml::Variant::Type::BOOL:
  45. lua_pushboolean(L, v.GetReference<bool>());
  46. break;
  47. case Rml::Variant::Type::BYTE:
  48. lua_pushinteger(L, v.GetReference<unsigned char>());
  49. break;
  50. case Rml::Variant::Type::CHAR: {
  51. char s[1] = {v.GetReference<char>() };
  52. lua_pushlstring(L, s, 1);
  53. break; }
  54. case Rml::Variant::Type::FLOAT:
  55. lua_pushnumber(L, v.GetReference<float>());
  56. break;
  57. case Rml::Variant::Type::DOUBLE:
  58. lua_pushnumber(L, v.GetReference<double>());
  59. break;
  60. case Rml::Variant::Type::INT:
  61. lua_pushinteger(L, v.GetReference<int>());
  62. break;
  63. case Rml::Variant::Type::INT64:
  64. lua_pushinteger(L, v.GetReference<int64_t>());
  65. break;
  66. case Rml::Variant::Type::STRING: {
  67. const Rml::String &s = v.GetReference<Rml::String>();
  68. lua_pushlstring(L, s.c_str(), s.length());
  69. break; }
  70. case Rml::Variant::Type::NONE:
  71. case Rml::Variant::Type::VECTOR2:
  72. case Rml::Variant::Type::VECTOR3:
  73. case Rml::Variant::Type::VECTOR4:
  74. case Rml::Variant::Type::COLOURF:
  75. case Rml::Variant::Type::COLOURB:
  76. case Rml::Variant::Type::SCRIPTINTERFACE:
  77. case Rml::Variant::Type::TRANSFORMPTR:
  78. case Rml::Variant::Type::TRANSITIONLIST:
  79. case Rml::Variant::Type::ANIMATIONLIST:
  80. case Rml::Variant::Type::DECORATORSPTR:
  81. case Rml::Variant::Type::FONTEFFECTSPTR:
  82. case Rml::Variant::Type::VOIDPTR:
  83. default:
  84. // todo : support other types
  85. lua_pushnil(L);
  86. break;
  87. }
  88. }
  89. static void
  90. GetVariant(lua_State *L, int index, Variant &variant) {
  91. switch(lua_type(L, index)) {
  92. case LUA_TBOOLEAN:
  93. variant = (bool)lua_toboolean(L, index);
  94. break;
  95. case LUA_TNUMBER:
  96. if (lua_isinteger(L, index)) {
  97. variant = (int64_t)lua_tointeger(L, index);
  98. } else {
  99. variant = (double)lua_tonumber(L, index);
  100. }
  101. break;
  102. case LUA_TSTRING:
  103. variant = Rml::String(lua_tostring(L, index));
  104. break;
  105. case LUA_TNIL:
  106. default: // todo: support other types
  107. variant = Variant();
  108. break;
  109. }
  110. }
  111. class LuaScalarDef final : public VariableDefinition {
  112. public:
  113. LuaScalarDef (const struct LuaDataModel *model) :
  114. VariableDefinition(DataVariableType::Scalar), model(model) {}
  115. private:
  116. virtual bool Get(void* ptr, Variant& variant) {
  117. lua_State *L = model->dataL;
  118. if (!L)
  119. return false;
  120. int id = (intptr_t)ptr;
  121. GetVariant(L, id, variant);
  122. return true;
  123. }
  124. virtual bool Set(void* ptr, const Rml::Variant& variant) {
  125. int id = (intptr_t)ptr;
  126. lua_State *L = model->dataL;
  127. if (!L)
  128. return false;
  129. PushVariant(L, variant);
  130. lua_replace(L, id);
  131. return true;
  132. }
  133. const struct LuaDataModel *model;
  134. };
  135. static int
  136. getId(lua_State *L, lua_State *dataL) {
  137. lua_pushvalue(dataL, 1);
  138. lua_xmove(dataL, L, 1);
  139. lua_pushvalue(L, 2);
  140. if (lua_rawget(L, -2) != LUA_TNUMBER) {
  141. luaL_error(L, "DataModel has no key : %s", lua_tostring(L, 2));
  142. }
  143. int id = lua_tointeger(L, -1);
  144. lua_pop(L, 2);
  145. return id;
  146. }
  147. static int
  148. lDataModelGet(lua_State *L) {
  149. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, 1);
  150. lua_State *dataL = D->dataL;
  151. if (dataL == nullptr)
  152. luaL_error(L, "DataModel closed");
  153. int id = getId(L, dataL);
  154. lua_pushvalue(dataL, id);
  155. lua_xmove(dataL, L, 1);
  156. return 1;
  157. }
  158. static int
  159. lDataModelSet(lua_State *L) {
  160. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, 1);
  161. lua_State *dataL = D->dataL;
  162. if (dataL == nullptr)
  163. luaL_error(L, "DataModel closed");
  164. int id = getId(L, dataL);
  165. lua_xmove(L, dataL, 1);
  166. lua_replace(dataL, id);
  167. D->handle.DirtyVariable(lua_tostring(L, 2));
  168. return 0;
  169. }
  170. // Construct a lua sub thread for LuaDataModel
  171. // stack 1 : { name(string) -> id(integer) }
  172. // stack 2- : values
  173. // For example : build from { str = "Hello", x = 0 }
  174. // 1: { str = 2 , x = 3 }
  175. // 2: "Hello"
  176. // 3: 0
  177. static lua_State *
  178. InitDataModelFromTable(lua_State *L, int index, Rml::DataModelConstructor &ctor, class LuaScalarDef *def) {
  179. lua_State *dataL = lua_newthread(L);
  180. lua_newtable(dataL);
  181. intptr_t id = 2;
  182. lua_pushnil(L);
  183. while (lua_next(L, index) != 0) {
  184. if (!lua_checkstack(dataL, 4)) {
  185. luaL_error(L, "Memory Error");
  186. }
  187. // L top : key value
  188. lua_xmove(L, dataL, 1); // move value to dataL with index(id)
  189. lua_pushvalue(L, -1); // dup key
  190. lua_xmove(L, dataL, 1);
  191. lua_pushinteger(dataL, id);
  192. lua_rawset(dataL, 1);
  193. const char *key = lua_tostring(L, -1);
  194. ctor.BindCustomDataVariable(key, Rml::DataVariable(def, (void *)id));
  195. ++id;
  196. }
  197. return dataL;
  198. }
  199. bool
  200. OpenLuaDataModel(lua_State *L, Rml::Context *context, int name_index, int table_index) {
  201. Rml::String name = luaL_checkstring(L, name_index);
  202. luaL_checktype(L, table_index, LUA_TTABLE);
  203. Rml::DataModelConstructor constructor = context->CreateDataModel(name);
  204. if (!constructor) {
  205. constructor = context->GetDataModel(name);
  206. if (!constructor) {
  207. return false;
  208. }
  209. }
  210. struct LuaDataModel *D = (struct LuaDataModel *)lua_newuserdata(L, sizeof(*D));
  211. D->dataL = nullptr;
  212. D->scalarDef = nullptr;
  213. D->handle = constructor.GetModelHandle();
  214. D->scalarDef = new LuaScalarDef(D);
  215. D->dataL = InitDataModelFromTable(L, table_index, constructor, D->scalarDef);
  216. lua_setuservalue(L, -2); // set D->dataL into uservalue of D
  217. if (luaL_newmetatable(L, RMLDATAMODEL)) {
  218. luaL_Reg l[] = {
  219. { "__index", lDataModelGet },
  220. { "__newindex", lDataModelSet },
  221. { nullptr, nullptr },
  222. };
  223. luaL_setfuncs(L, l, 0);
  224. }
  225. lua_setmetatable(L, -2);
  226. return true;
  227. }
  228. // If you create all the Data Models from lua, you can store these LuaDataModel objects in a table,
  229. // and call CloseLuaDataModel for each after Context released.
  230. // We don't put it in __gc, becuase LuaDataModel can be free before DataModel if you are not careful.
  231. // scalarDef will free by CloseLuaDataModel, but DataModel need it.
  232. void
  233. CloseLuaDataModel(lua_State *L) {
  234. luaL_checkudata(L, -1, RMLDATAMODEL);
  235. struct LuaDataModel *D = (struct LuaDataModel *)lua_touserdata(L, -1);
  236. D->dataL = nullptr;
  237. delete D->scalarDef;
  238. D->scalarDef = nullptr;
  239. lua_pushnil(L);
  240. lua_setuservalue(L, -2);
  241. }
  242. } // namespace Lua
  243. } // namespace Rml