LuaType.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <RmlUi/Core/Types.h>
  29. #include <RmlUi/Lua/LuaType.h>
  30. #include <RmlUi/Lua/Utilities.h>
  31. namespace Rml {
  32. namespace Lua {
  33. int LuaTypeImpl::index(lua_State* L, const char* class_name)
  34. {
  35. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  36. lua_getglobal(L, class_name); // stack pos [3] (fairly important, just refered to as [3])
  37. // string form of the key.
  38. const char* key = luaL_checkstring(L, 2);
  39. if (lua_istable(L, -1)) //[-1 = 3]
  40. {
  41. lua_pushvalue(L, 2); //[2] = key, [4] = copy of key
  42. lua_rawget(L, -2); //[-2 = 3] -> pop top and push the return value to top [4]
  43. // If the key were looking for is not in the table, retrieve its' metatables' index value.
  44. if (lua_isnil(L, -1)) //[-1 = 4] is value from rawget above
  45. {
  46. // try __getters
  47. lua_pop(L, 1); // remove top item (nil) from the stack
  48. lua_pushstring(L, "__getters");
  49. lua_rawget(L, -2); //[-2 = 3], <ClassName>._getters -> result to [4]
  50. lua_pushvalue(L, 2); //[2 = key] -> copy to [5]
  51. lua_rawget(L, -2); //[-2 = __getters] -> __getters[key], result to [5]
  52. if (lua_type(L, -1) == LUA_TFUNCTION) //[-1 = 5]
  53. {
  54. lua_pushvalue(L, 1); // push the userdata to the stack [6]
  55. lua_call(L, 1, 1); // remove one, result is at [6]
  56. }
  57. else
  58. {
  59. lua_settop(L, 4); // forget everything we did above
  60. lua_getmetatable(L, -2); //[-2 = 3] -> metatable from <ClassName> to top [5]
  61. if (lua_istable(L, -1)) //[-1 = 5] = the result of the above
  62. {
  63. lua_getfield(L, -1, "__index"); //[-1 = 5] = check the __index metamethod for the metatable-> push result to [6]
  64. if (lua_isfunction(L, -1)) //[-1 = 6] = __index metamethod
  65. {
  66. lua_pushvalue(L, 1); //[1] = object -> [7] = object
  67. lua_pushvalue(L, 2); //[2] = key -> [8] = key
  68. lua_call(L, 2, 1); // call function at top of stack (__index) -> pop top 2 as args; [7] = return value
  69. }
  70. else if (lua_istable(L, -1))
  71. lua_getfield(L, -1, key); // shorthand version of above -> [7] = return value
  72. else
  73. lua_pushnil(L); //[7] = nil
  74. }
  75. else
  76. lua_pushnil(L); //[6] = nil
  77. }
  78. }
  79. else if (lua_istable(L, -1)) //[-1 = 4] is value from rawget [3]
  80. {
  81. lua_pushvalue(L, 2); //[2] = key, [5] = key
  82. lua_rawget(L, -2); //[-2 = 3] = table of <ClassName> -> pop top and push the return value to top [5]
  83. }
  84. }
  85. else
  86. lua_pushnil(L); //[4] = nil
  87. lua_insert(L, 1); // top element to position 1 -> [1] = top element as calculated in the earlier rest of the function
  88. lua_settop(L, 1); // -> [1 = -1], removes the other elements
  89. return 1;
  90. }
  91. int LuaTypeImpl::newindex(lua_State* L, const char* class_name)
  92. {
  93. //[1] = obj, [2] = key, [3] = value
  94. // look for it in __setters
  95. lua_getglobal(L, class_name); //[4] = this table
  96. lua_pushstring(L, "__setters"); //[5]
  97. lua_rawget(L, -2); //[-2 = 4] -> <ClassName>.__setters to [5]
  98. lua_pushvalue(L, 2); //[2 = key] -> [6] = copy of key
  99. lua_rawget(L, -2); //[-2 = __setters] -> __setters[key] to [6]
  100. if (lua_type(L, -1) == LUA_TFUNCTION)
  101. {
  102. lua_pushvalue(L, 1); // userdata at [7]
  103. lua_pushvalue(L, 3); //[8] = copy of [3]
  104. lua_call(L, 2, 0); // call function, pop 2 off push 0 on
  105. }
  106. else
  107. lua_pop(L, 1); // not a setter function.
  108. lua_pop(L, 2); // pop __setters and the <Classname> table
  109. return 0;
  110. }
  111. } // namespace Lua
  112. } // namespace Rml