LuaDataSource.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "precompiled.h"
  29. #include "LuaDataSource.h"
  30. #include <RmlUi/Core/Lua/Interpreter.h>
  31. #include <RmlUi/Core/Log.h>
  32. #include <RmlUi/Core/String.h>
  33. using Rml::Core::Lua::Interpreter;
  34. using Rml::Core::Log;
  35. using Rml::Core::Lua::LuaType;
  36. namespace Rml {
  37. namespace Controls {
  38. namespace Lua {
  39. LuaDataSource::LuaDataSource(const Rml::Core::String& name) : DataSource(name), getRowRef(LUA_NOREF), getNumRowsRef(LUA_NOREF)
  40. {
  41. }
  42. /// Fetches the contents of one row of a table within the data source.
  43. /// @param[out] row The list of values in the table.
  44. /// @param[in] table The name of the table to query.
  45. /// @param[in] row_index The index of the desired row.
  46. /// @param[in] columns The list of desired columns within the row.
  47. void LuaDataSource::GetRow(Rml::Core::StringList& row, const Rml::Core::String& table, int row_index, const Rml::Core::StringList& columns)
  48. {
  49. if(getRowRef == LUA_NOREF || getRowRef == LUA_REFNIL) return;
  50. //setup the call
  51. Interpreter::BeginCall(getRowRef);
  52. lua_State* L = Interpreter::GetLuaState();
  53. lua_pushstring(L,table.c_str());
  54. lua_pushinteger(L,row_index);
  55. lua_newtable(L);
  56. int index = 0;
  57. for(Rml::Core::StringList::const_iterator itr = columns.begin(); itr != columns.end(); ++itr)
  58. {
  59. lua_pushstring(L,itr->c_str());
  60. lua_rawseti(L,-2,index++);
  61. }
  62. Interpreter::ExecuteCall(3,1); //3 parameters, 1 return. After here, the top of the stack contains the return value
  63. int res = lua_gettop(L);
  64. if(lua_type(L,res) == LUA_TTABLE)
  65. {
  66. lua_pushnil(L);
  67. while(lua_next(L,res) != 0)
  68. {
  69. //key at -2, value at -1
  70. row.push_back(luaL_checkstring(L,-1));
  71. lua_pop(L,1); //pops value, leaves key for next iteration
  72. }
  73. lua_pop(L,1); //pop key
  74. }
  75. else
  76. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetRow must return a table, the function it called returned a %s", lua_typename(L,res));
  77. Interpreter::EndCall(1);
  78. }
  79. /// Fetches the number of rows within one of this data source's tables.
  80. /// @param[in] table The name of the table to query.
  81. /// @return The number of rows within the specified table. Returns -1 in case of an incorrect Lua function.
  82. int LuaDataSource::GetNumRows(const Rml::Core::String& table)
  83. {
  84. if(getNumRowsRef == LUA_NOREF || getNumRowsRef == LUA_REFNIL) return -1;
  85. lua_State* L = Interpreter::GetLuaState();
  86. Interpreter::BeginCall(getNumRowsRef);
  87. lua_pushstring(L,table.c_str());
  88. Interpreter::ExecuteCall(1,1); //1 parameter, 1 return. After this, the top of the stack contains the return value
  89. int res = lua_gettop(L);
  90. if(lua_type(L,res) == LUA_TNUMBER)
  91. {
  92. return (int)luaL_checkinteger(L,res);
  93. }
  94. else
  95. {
  96. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetNumRows must return an integer, the function it called returned a %s", lua_typename(L,res));
  97. return -1;
  98. }
  99. }
  100. }
  101. }
  102. }