Utilities.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/Variant.h>
  29. #include <RmlUi/Lua/Utilities.h>
  30. namespace Rml {
  31. namespace Lua {
  32. #if LUA_VERSION_NUM < 502
  33. void lua_len(lua_State* L, int i)
  34. {
  35. switch (lua_type(L, i))
  36. {
  37. case LUA_TSTRING: lua_pushnumber(L, (lua_Number)lua_objlen(L, i)); break;
  38. case LUA_TTABLE:
  39. if (!luaL_callmeta(L, i, "__len"))
  40. lua_pushnumber(L, (lua_Number)lua_objlen(L, i));
  41. break;
  42. case LUA_TUSERDATA:
  43. if (luaL_callmeta(L, i, "__len"))
  44. break;
  45. /* FALLTHROUGH */
  46. default: luaL_error(L, "attempt to get length of a %s value", lua_typename(L, lua_type(L, i)));
  47. }
  48. }
  49. lua_Integer luaL_len(lua_State* L, int i)
  50. {
  51. lua_Integer res = 0;
  52. int isnum = 0;
  53. luaL_checkstack(L, 1, "not enough stack slots");
  54. lua_len(L, i);
  55. res = lua_tointegerx(L, -1, &isnum);
  56. lua_pop(L, 1);
  57. if (!isnum)
  58. luaL_error(L, "object length is not an integer");
  59. return res;
  60. }
  61. #endif
  62. void PushVariant(lua_State* L, const Variant* var)
  63. {
  64. if (!var)
  65. {
  66. lua_pushnil(L);
  67. return;
  68. }
  69. switch (var->GetType())
  70. {
  71. case Variant::BOOL: lua_pushboolean(L, var->Get<bool>()); break;
  72. case Variant::BYTE:
  73. case Variant::CHAR:
  74. case Variant::INT: lua_pushinteger(L, var->Get<int>()); break;
  75. case Variant::INT64: lua_pushinteger(L, var->Get<int64_t>()); break;
  76. case Variant::UINT: lua_pushinteger(L, var->Get<unsigned int>()); break;
  77. case Variant::UINT64: lua_pushinteger(L, var->Get<uint64_t>()); break;
  78. case Variant::FLOAT:
  79. case Variant::DOUBLE: lua_pushnumber(L, var->Get<double>()); break;
  80. case Variant::COLOURB: LuaType<Colourb>::push(L, new Colourb(var->Get<Colourb>()), true); break;
  81. case Variant::COLOURF: LuaType<Colourf>::push(L, new Colourf(var->Get<Colourf>()), true); break;
  82. case Variant::STRING:
  83. {
  84. const String& s = var->GetReference<Rml::String>();
  85. lua_pushlstring(L, s.c_str(), s.length());
  86. }
  87. break;
  88. case Variant::VECTOR2:
  89. // according to Variant.inl, it is going to be a Vector2f
  90. LuaType<Vector2f>::push(L, new Vector2f(var->Get<Vector2f>()), true);
  91. break;
  92. case Variant::VOIDPTR: lua_pushlightuserdata(L, var->Get<void*>()); break;
  93. default: lua_pushnil(L); break;
  94. }
  95. }
  96. void GetVariant(lua_State* L, int index, Variant* variant)
  97. {
  98. if (!variant)
  99. return;
  100. switch (lua_type(L, index))
  101. {
  102. case LUA_TBOOLEAN: *variant = (bool)lua_toboolean(L, index); break;
  103. case LUA_TNUMBER: *variant = lua_tonumber(L, index); break;
  104. case LUA_TSTRING: *variant = Rml::String(lua_tostring(L, index)); break;
  105. case LUA_TLIGHTUSERDATA: *variant = lua_touserdata(L, index); break;
  106. case LUA_TNIL:
  107. default: // todo: support other types
  108. *variant = Variant();
  109. break;
  110. }
  111. }
  112. void PushIndex(lua_State* L, int index)
  113. {
  114. lua_pushinteger(L, index + 1);
  115. }
  116. int GetIndex(lua_State* L, int index)
  117. {
  118. return (int)luaL_checkinteger(L, index) - 1;
  119. }
  120. } // namespace Lua
  121. } // namespace Rml