LuaDataSource.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "precompiled.h"
  2. #include "LuaDataSource.h"
  3. #include <Rocket/Core/Lua/Interpreter.h>
  4. #include <Rocket/Core/Log.h>
  5. namespace Rocket {
  6. namespace Core {
  7. namespace Lua {
  8. LuaDataSource::LuaDataSource(const String& name) : DataSource(name), getRowRef(LUA_NOREF), getNumRowsRef(LUA_NOREF)
  9. {
  10. }
  11. /// Fetches the contents of one row of a table within the data source.
  12. /// @param[out] row The list of values in the table.
  13. /// @param[in] table The name of the table to query.
  14. /// @param[in] row_index The index of the desired row.
  15. /// @param[in] columns The list of desired columns within the row.
  16. void LuaDataSource::GetRow(Rocket::Core::StringList& row, const Rocket::Core::String& table, int row_index, const Rocket::Core::StringList& columns)
  17. {
  18. if(getRowRef == LUA_NOREF || getRowRef == LUA_REFNIL) return;
  19. //setup the call
  20. Interpreter::BeginCall(getRowRef);
  21. lua_State* L = Interpreter::GetLuaState();
  22. lua_pushstring(L,table.CString());
  23. lua_pushinteger(L,row_index);
  24. lua_newtable(L);
  25. int index = 0;
  26. for(StringList::const_iterator itr = columns.begin(); itr != columns.end(); ++itr)
  27. {
  28. lua_pushstring(L,itr->CString());
  29. lua_rawseti(L,-2,index++);
  30. }
  31. Interpreter::ExecuteCall(3,1); //3 parameters, 1 return. After here, the top of the stack contains the return value
  32. int res = lua_gettop(L);
  33. if(lua_type(L,res) == LUA_TTABLE)
  34. {
  35. lua_pushnil(L);
  36. while(lua_next(L,res) != 0)
  37. {
  38. //key at -2, value at -1
  39. row.push_back(luaL_checkstring(L,-1));
  40. lua_pop(L,1); //pops value, leaves key for next iteration
  41. }
  42. lua_pop(L,1); //pop key
  43. }
  44. else
  45. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetRow must return a table, the function it called returned a %s", lua_typename(L,res));
  46. Interpreter::EndCall(1);
  47. }
  48. /// Fetches the number of rows within one of this data source's tables.
  49. /// @param[in] table The name of the table to query.
  50. /// @return The number of rows within the specified table. Returns -1 in case of an incorrect Lua function.
  51. int LuaDataSource::GetNumRows(const Rocket::Core::String& table)
  52. {
  53. if(getNumRowsRef == LUA_NOREF || getNumRowsRef == LUA_REFNIL) return -1;
  54. lua_State* L = Interpreter::GetLuaState();
  55. Interpreter::BeginCall(getNumRowsRef);
  56. lua_pushstring(L,table.CString());
  57. Interpreter::ExecuteCall(1,1); //1 parameter, 1 return. After this, the top of the stack contains the return value
  58. int res = lua_gettop(L);
  59. if(lua_type(L,res) == LUA_TNUMBER)
  60. {
  61. return luaL_checkint(L,res);
  62. }
  63. else
  64. {
  65. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetNumRows must return an integer, the function it called returned a %s", lua_typename(L,res));
  66. return -1;
  67. }
  68. }
  69. }
  70. }
  71. }