ContextDocumentsProxy.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "ContextDocumentsProxy.h"
  2. #include <RmlUi/Core/ElementDocument.h>
  3. namespace Rml {
  4. namespace Lua {
  5. typedef ElementDocument Document;
  6. template <>
  7. void ExtraInit<ContextDocumentsProxy>(lua_State* L, int metatable_index)
  8. {
  9. lua_pushcfunction(L, ContextDocumentsProxy__index);
  10. lua_setfield(L, metatable_index, "__index");
  11. lua_pushcfunction(L, ContextDocumentsProxy__pairs);
  12. lua_setfield(L, metatable_index, "__pairs");
  13. }
  14. int ContextDocumentsProxy__index(lua_State* L)
  15. {
  16. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  17. int type = lua_type(L, 2);
  18. if (type == LUA_TNUMBER || type == LUA_TSTRING) // only valid key types
  19. {
  20. ContextDocumentsProxy* proxy = LuaType<ContextDocumentsProxy>::check(L, 1);
  21. Document* ret = nullptr;
  22. if (type == LUA_TSTRING)
  23. ret = proxy->owner->GetDocument(luaL_checkstring(L, 2));
  24. else
  25. ret = proxy->owner->GetDocument((int)luaL_checkinteger(L, 2) - 1);
  26. LuaType<Document>::push(L, ret, false);
  27. return 1;
  28. }
  29. else
  30. return LuaType<ContextDocumentsProxy>::index(L);
  31. }
  32. struct ContextDocumentsProxyPairs {
  33. static int next(lua_State* L)
  34. {
  35. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L, 1);
  36. ContextDocumentsProxyPairs* self = static_cast<ContextDocumentsProxyPairs*>(lua_touserdata(L, lua_upvalueindex(1)));
  37. Document* doc = nullptr;
  38. int num_docs = obj->owner->GetNumDocuments();
  39. // because there can be missing indexes, make sure to continue until there
  40. // is actually a document at the index
  41. while (self->m_cur < num_docs)
  42. {
  43. doc = obj->owner->GetDocument(self->m_cur++);
  44. if (doc != nullptr)
  45. break;
  46. }
  47. if (doc == nullptr)
  48. {
  49. return 0;
  50. }
  51. lua_pushstring(L, doc->GetId().c_str());
  52. LuaType<Document>::push(L, doc);
  53. return 2;
  54. }
  55. static int destroy(lua_State* L)
  56. {
  57. static_cast<ContextDocumentsProxyPairs*>(lua_touserdata(L, 1))->~ContextDocumentsProxyPairs();
  58. return 0;
  59. }
  60. static int constructor(lua_State* L)
  61. {
  62. void* storage = lua_newuserdata(L, sizeof(ContextDocumentsProxyPairs));
  63. if (luaL_newmetatable(L, "RmlUi::Lua::ContextDocumentsProxyPairs"))
  64. {
  65. static luaL_Reg mt[] = {
  66. {"__gc", destroy},
  67. {NULL, NULL},
  68. };
  69. luaL_setfuncs(L, mt, 0);
  70. }
  71. lua_setmetatable(L, -2);
  72. lua_pushcclosure(L, next, 1);
  73. new (storage) ContextDocumentsProxyPairs();
  74. return 1;
  75. }
  76. ContextDocumentsProxyPairs() : m_cur(0) {}
  77. int m_cur;
  78. };
  79. int ContextDocumentsProxy__pairs(lua_State* L)
  80. {
  81. ContextDocumentsProxyPairs::constructor(L);
  82. lua_pushvalue(L, 1);
  83. return 2;
  84. }
  85. RegType<ContextDocumentsProxy> ContextDocumentsProxyMethods[] = {
  86. {nullptr, nullptr},
  87. };
  88. luaL_Reg ContextDocumentsProxyGetters[] = {
  89. {nullptr, nullptr},
  90. };
  91. luaL_Reg ContextDocumentsProxySetters[] = {
  92. {nullptr, nullptr},
  93. };
  94. RMLUI_LUATYPE_DEFINE(ContextDocumentsProxy)
  95. } // namespace Lua
  96. } // namespace Rml