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