GlobalLuaFunctions.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "GlobalLuaFunctions.h"
  29. #include <RmlUi/Lua/LuaType.h>
  30. #include <RmlUi/Lua/IncludeLua.h>
  31. #include <RmlUi/Lua/Utilities.h>
  32. #include <RmlUi/Core/Types.h>
  33. #include <RmlUi/Core/Log.h>
  34. namespace Rml {
  35. namespace Lua {
  36. /*
  37. Below here are global functions and their helper functions that help overwrite the Lua global functions
  38. */
  39. //__pairs should return two values.
  40. //upvalue 1 is the __pairs function, upvalue 2 is the userdata created in rmlui_pairs
  41. //[1] is the object implementing __pairs
  42. //[2] is the key that was just read
  43. int rmlui_pairsaux(lua_State* L)
  44. {
  45. lua_pushvalue(L,lua_upvalueindex(1)); //push __pairs to top
  46. lua_insert(L,1); //move __pairs to the bottom
  47. lua_pushvalue(L,lua_upvalueindex(2)); //push userdata
  48. //stack looks like [1] = __pairs, [2] = object, [3] = latest key, [4] = userdata
  49. if(lua_pcall(L,lua_gettop(L)-1,LUA_MULTRET,0) != 0)
  50. Report(L,"__pairs");
  51. return lua_gettop(L);
  52. }
  53. //A version of paris that respects a __pairs metamethod.
  54. //"next" function is upvalue 1
  55. int rmlui_pairs(lua_State* L)
  56. {
  57. luaL_checkany(L,1); //[1] is the object given to us by pairs(object)
  58. if(luaL_getmetafield(L,1,"__pairs"))
  59. {
  60. void* ud = lua_newuserdata(L,sizeof(void*)); //create a new block of memory to be used as upvalue 1
  61. (*(int*)(ud)) = -1;
  62. lua_pushcclosure(L,rmlui_pairsaux,2); //uv 1 is __pairs, uv 2 is ud
  63. }
  64. else
  65. lua_pushvalue(L,lua_upvalueindex(1)); //generator
  66. lua_pushvalue(L,1); //state
  67. lua_pushnil(L); //initial value
  68. return 3;
  69. }
  70. //copy + pasted from Lua's lbaselib.c
  71. int ipairsaux (lua_State *L) {
  72. lua_Integer i = luaL_checkinteger(L, 2);
  73. luaL_checktype(L, 1, LUA_TTABLE);
  74. i++; /* next value */
  75. lua_pushinteger(L, i);
  76. lua_rawgeti(L, 1, i);
  77. return (lua_isnil(L, -1)) ? 0 : 2;
  78. }
  79. //__ipairs should return two values
  80. //upvalue 1 is the __ipairs function, upvalue 2 is the userdata created in rmlui_ipairs
  81. //[1] is the object implementing __ipairs, [2] is the key last used
  82. int rmlui_ipairsaux(lua_State* L)
  83. {
  84. lua_pushvalue(L,lua_upvalueindex(1)); //push __ipairs
  85. lua_insert(L,1); //move __ipairs to the bottom
  86. lua_pushvalue(L,lua_upvalueindex(2)); //push userdata
  87. //stack looks like [1] = __ipairs, [2] = object, [3] = latest key, [4] = userdata
  88. if(lua_pcall(L,lua_gettop(L)-1,LUA_MULTRET,0) != 0)
  89. Report(L,"__ipairs");
  90. return lua_gettop(L);
  91. }
  92. //A version of paris that respects a __pairs metamethod.
  93. //ipairsaux function is upvalue 1
  94. int rmlui_ipairs(lua_State* L)
  95. {
  96. luaL_checkany(L,1); //[1] is the object given to us by ipairs(object)
  97. if(luaL_getmetafield(L,1,"__ipairs"))
  98. {
  99. void* ud = lua_newuserdata(L,sizeof(void*)); //create a new block of memory to be used as upvalue 1
  100. (*(int*)(ud)) = -1;
  101. lua_pushcclosure(L,rmlui_pairsaux,2); //uv 1 is __ipairs, uv 2 is ud
  102. }
  103. else
  104. lua_pushvalue(L,lua_upvalueindex(1)); //generator
  105. lua_pushvalue(L,1); //state
  106. lua_pushnil(L); //initial value
  107. return 3;
  108. }
  109. //Based off of the luaB_print function from Lua's lbaselib.c
  110. int LuaPrint(lua_State* L)
  111. {
  112. int n = lua_gettop(L); /* number of arguments */
  113. int i;
  114. lua_getglobal(L, "tostring");
  115. StringList string_list = StringList();
  116. String output = "";
  117. for (i=1; i<=n; i++)
  118. {
  119. const char *s;
  120. lua_pushvalue(L, -1); /* function to be called */
  121. lua_pushvalue(L, i); /* value to print */
  122. lua_call(L, 1, 1);
  123. s = lua_tostring(L, -1); /* get result */
  124. if (s == nullptr)
  125. return luaL_error(L, "'tostring' must return a string to 'print'");
  126. if (i>1)
  127. output += "\t";
  128. output += String(s);
  129. lua_pop(L, 1); /* pop result */
  130. }
  131. output += "\n";
  132. Log::Message(Log::LT_INFO, output.c_str());
  133. return 0;
  134. }
  135. void OverrideLuaGlobalFunctions(lua_State* L)
  136. {
  137. lua_getglobal(L,"_G");
  138. lua_getglobal(L,"next");
  139. lua_pushcclosure(L,rmlui_pairs,1);
  140. lua_setfield(L,-2,"pairs");
  141. lua_pushcfunction(L,ipairsaux);
  142. lua_pushcclosure(L,rmlui_ipairs,1);
  143. lua_setfield(L,-2,"ipairs");
  144. lua_pushcfunction(L,LuaPrint);
  145. lua_setfield(L,-2,"print");
  146. lua_pop(L,1); //pop _G
  147. }
  148. } // namespace Lua
  149. } // namespace Rml