Utilities.cpp 4.2 KB

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