LuaType.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <RmlUi/Core/Types.h>
  2. #include <RmlUi/Lua/LuaType.h>
  3. #include <RmlUi/Lua/Utilities.h>
  4. namespace Rml {
  5. namespace Lua {
  6. int LuaTypeImpl::index(lua_State* L, const char* class_name)
  7. {
  8. /*the table obj and the missing key are currently on the stack(index 1 & 2) as defined by the Lua language*/
  9. lua_getglobal(L, class_name); // stack pos [3] (fairly important, just refered to as [3])
  10. // string form of the key.
  11. const char* key = luaL_checkstring(L, 2);
  12. if (lua_istable(L, -1)) //[-1 = 3]
  13. {
  14. lua_pushvalue(L, 2); //[2] = key, [4] = copy of key
  15. lua_rawget(L, -2); //[-2 = 3] -> pop top and push the return value to top [4]
  16. // If the key were looking for is not in the table, retrieve its' metatables' index value.
  17. if (lua_isnil(L, -1)) //[-1 = 4] is value from rawget above
  18. {
  19. // try __getters
  20. lua_pop(L, 1); // remove top item (nil) from the stack
  21. lua_pushstring(L, "__getters");
  22. lua_rawget(L, -2); //[-2 = 3], <ClassName>._getters -> result to [4]
  23. lua_pushvalue(L, 2); //[2 = key] -> copy to [5]
  24. lua_rawget(L, -2); //[-2 = __getters] -> __getters[key], result to [5]
  25. if (lua_type(L, -1) == LUA_TFUNCTION) //[-1 = 5]
  26. {
  27. lua_pushvalue(L, 1); // push the userdata to the stack [6]
  28. lua_call(L, 1, 1); // remove one, result is at [6]
  29. }
  30. else
  31. {
  32. lua_settop(L, 4); // forget everything we did above
  33. lua_getmetatable(L, -2); //[-2 = 3] -> metatable from <ClassName> to top [5]
  34. if (lua_istable(L, -1)) //[-1 = 5] = the result of the above
  35. {
  36. lua_getfield(L, -1, "__index"); //[-1 = 5] = check the __index metamethod for the metatable-> push result to [6]
  37. if (lua_isfunction(L, -1)) //[-1 = 6] = __index metamethod
  38. {
  39. lua_pushvalue(L, 1); //[1] = object -> [7] = object
  40. lua_pushvalue(L, 2); //[2] = key -> [8] = key
  41. lua_call(L, 2, 1); // call function at top of stack (__index) -> pop top 2 as args; [7] = return value
  42. }
  43. else if (lua_istable(L, -1))
  44. lua_getfield(L, -1, key); // shorthand version of above -> [7] = return value
  45. else
  46. lua_pushnil(L); //[7] = nil
  47. }
  48. else
  49. lua_pushnil(L); //[6] = nil
  50. }
  51. }
  52. else if (lua_istable(L, -1)) //[-1 = 4] is value from rawget [3]
  53. {
  54. lua_pushvalue(L, 2); //[2] = key, [5] = key
  55. lua_rawget(L, -2); //[-2 = 3] = table of <ClassName> -> pop top and push the return value to top [5]
  56. }
  57. }
  58. else
  59. lua_pushnil(L); //[4] = nil
  60. lua_insert(L, 1); // top element to position 1 -> [1] = top element as calculated in the earlier rest of the function
  61. lua_settop(L, 1); // -> [1 = -1], removes the other elements
  62. return 1;
  63. }
  64. int LuaTypeImpl::newindex(lua_State* L, const char* class_name)
  65. {
  66. //[1] = obj, [2] = key, [3] = value
  67. // look for it in __setters
  68. lua_getglobal(L, class_name); //[4] = this table
  69. lua_pushstring(L, "__setters"); //[5]
  70. lua_rawget(L, -2); //[-2 = 4] -> <ClassName>.__setters to [5]
  71. lua_pushvalue(L, 2); //[2 = key] -> [6] = copy of key
  72. lua_rawget(L, -2); //[-2 = __setters] -> __setters[key] to [6]
  73. if (lua_type(L, -1) == LUA_TFUNCTION)
  74. {
  75. lua_pushvalue(L, 1); // userdata at [7]
  76. lua_pushvalue(L, 3); //[8] = copy of [3]
  77. lua_call(L, 2, 0); // call function, pop 2 off push 0 on
  78. }
  79. else
  80. lua_pop(L, 1); // not a setter function.
  81. lua_pop(L, 2); // pop __setters and the <Classname> table
  82. return 0;
  83. }
  84. } // namespace Lua
  85. } // namespace Rml