ContextDocumentsProxy.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2023 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 "ContextDocumentsProxy.h"
  29. #include <RmlUi/Core/ElementDocument.h>
  30. namespace Rml {
  31. namespace Lua {
  32. typedef ElementDocument Document;
  33. template <>
  34. void ExtraInit<ContextDocumentsProxy>(lua_State* L, int metatable_index)
  35. {
  36. lua_pushcfunction(L, ContextDocumentsProxy__index);
  37. lua_setfield(L, metatable_index, "__index");
  38. lua_pushcfunction(L, ContextDocumentsProxy__pairs);
  39. lua_setfield(L, metatable_index, "__pairs");
  40. }
  41. int ContextDocumentsProxy__index(lua_State* L)
  42. {
  43. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  44. int type = lua_type(L, 2);
  45. if (type == LUA_TNUMBER || type == LUA_TSTRING) // only valid key types
  46. {
  47. ContextDocumentsProxy* proxy = LuaType<ContextDocumentsProxy>::check(L, 1);
  48. Document* ret = nullptr;
  49. if (type == LUA_TSTRING)
  50. ret = proxy->owner->GetDocument(luaL_checkstring(L, 2));
  51. else
  52. ret = proxy->owner->GetDocument((int)luaL_checkinteger(L, 2) - 1);
  53. LuaType<Document>::push(L, ret, false);
  54. return 1;
  55. }
  56. else
  57. return LuaType<ContextDocumentsProxy>::index(L);
  58. }
  59. struct ContextDocumentsProxyPairs {
  60. static int next(lua_State* L)
  61. {
  62. ContextDocumentsProxy* obj = LuaType<ContextDocumentsProxy>::check(L, 1);
  63. ContextDocumentsProxyPairs* self = static_cast<ContextDocumentsProxyPairs*>(lua_touserdata(L, lua_upvalueindex(1)));
  64. Document* doc = nullptr;
  65. int num_docs = obj->owner->GetNumDocuments();
  66. // because there can be missing indexes, make sure to continue until there
  67. // is actually a document at the index
  68. while (self->m_cur < num_docs)
  69. {
  70. doc = obj->owner->GetDocument(self->m_cur++);
  71. if (doc != nullptr)
  72. break;
  73. }
  74. if (doc == nullptr)
  75. {
  76. return 0;
  77. }
  78. lua_pushstring(L, doc->GetId().c_str());
  79. LuaType<Document>::push(L, doc);
  80. return 2;
  81. }
  82. static int destroy(lua_State* L)
  83. {
  84. static_cast<ContextDocumentsProxyPairs*>(lua_touserdata(L, 1))->~ContextDocumentsProxyPairs();
  85. return 0;
  86. }
  87. static int constructor(lua_State* L)
  88. {
  89. void* storage = lua_newuserdata(L, sizeof(ContextDocumentsProxyPairs));
  90. if (luaL_newmetatable(L, "RmlUi::Lua::ContextDocumentsProxyPairs"))
  91. {
  92. static luaL_Reg mt[] = {
  93. {"__gc", destroy},
  94. {NULL, NULL},
  95. };
  96. luaL_setfuncs(L, mt, 0);
  97. }
  98. lua_setmetatable(L, -2);
  99. lua_pushcclosure(L, next, 1);
  100. new (storage) ContextDocumentsProxyPairs();
  101. return 1;
  102. }
  103. ContextDocumentsProxyPairs() : m_cur(0) {}
  104. int m_cur;
  105. };
  106. int ContextDocumentsProxy__pairs(lua_State* L)
  107. {
  108. ContextDocumentsProxyPairs::constructor(L);
  109. lua_pushvalue(L, 1);
  110. return 2;
  111. }
  112. RegType<ContextDocumentsProxy> ContextDocumentsProxyMethods[] = {
  113. {nullptr, nullptr},
  114. };
  115. luaL_Reg ContextDocumentsProxyGetters[] = {
  116. {nullptr, nullptr},
  117. };
  118. luaL_Reg ContextDocumentsProxySetters[] = {
  119. {nullptr, nullptr},
  120. };
  121. RMLUI_LUATYPE_DEFINE(ContextDocumentsProxy)
  122. } // namespace Lua
  123. } // namespace Rml