GlobalLuaFunctions.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "GlobalLuaFunctions.h"
  29. #include <Rocket/Core/Lua/lua.hpp>
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Lua {
  33. /*
  34. Below here are global functions and their helper functions that help overwrite the Lua global functions
  35. */
  36. //__pairs should return two values.
  37. //upvalue 1 is the __pairs function, upvalue 2 is the userdata created in rocket_pairs
  38. //[1] is the object implementing __pairs
  39. //[2] is the key that was just read
  40. int rocket_pairsaux(lua_State* L)
  41. {
  42. lua_pushvalue(L,lua_upvalueindex(1)); //push __pairs to top
  43. lua_insert(L,1); //move __pairs to the bottom
  44. lua_pushvalue(L,lua_upvalueindex(2)); //push userdata
  45. //stack looks like [1] = __pairs, [2] = object, [3] = latest key, [4] = userdata
  46. if(lua_pcall(L,lua_gettop(L)-1,LUA_MULTRET,0) != 0)
  47. Report(L,"__pairs");
  48. return lua_gettop(L);
  49. }
  50. //A version of paris that respects a __pairs metamethod.
  51. //"next" function is upvalue 1
  52. int rocket_pairs(lua_State* L)
  53. {
  54. luaL_checkany(L,1); //[1] is the object given to us by pairs(object)
  55. if(luaL_getmetafield(L,1,"__pairs"))
  56. {
  57. void* ud = lua_newuserdata(L,sizeof(void*)); //create a new block of memory to be used as upvalue 1
  58. (*(int*)(ud)) = -1;
  59. lua_pushcclosure(L,rocket_pairsaux,2); //uv 1 is __pairs, uv 2 is ud
  60. }
  61. else
  62. lua_pushvalue(L,lua_upvalueindex(1)); //generator
  63. lua_pushvalue(L,1); //state
  64. lua_pushnil(L); //initial value
  65. return 3;
  66. }
  67. //copy + pasted from Lua's lbaselib.c
  68. int ipairsaux (lua_State *L) {
  69. int i = luaL_checkinteger(L, 2);
  70. luaL_checktype(L, 1, LUA_TTABLE);
  71. i++; /* next value */
  72. lua_pushinteger(L, i);
  73. lua_rawgeti(L, 1, i);
  74. return (lua_isnil(L, -1)) ? 0 : 2;
  75. }
  76. //__ipairs should return two values
  77. //upvalue 1 is the __ipairs function, upvalue 2 is the userdata created in rocket_ipairs
  78. //[1] is the object implementing __ipairs, [2] is the key last used
  79. int rocket_ipairsaux(lua_State* L)
  80. {
  81. lua_pushvalue(L,lua_upvalueindex(1)); //push __ipairs
  82. lua_insert(L,1); //move __ipairs to the bottom
  83. lua_pushvalue(L,lua_upvalueindex(2)); //push userdata
  84. //stack looks like [1] = __ipairs, [2] = object, [3] = latest key, [4] = userdata
  85. if(lua_pcall(L,lua_gettop(L)-1,LUA_MULTRET,0) != 0)
  86. Report(L,"__ipairs");
  87. return lua_gettop(L);
  88. }
  89. //A version of paris that respects a __pairs metamethod.
  90. //ipairsaux function is upvalue 1
  91. int rocket_ipairs(lua_State* L)
  92. {
  93. luaL_checkany(L,1); //[1] is the object given to us by ipairs(object)
  94. if(luaL_getmetafield(L,1,"__ipairs"))
  95. {
  96. void* ud = lua_newuserdata(L,sizeof(void*)); //create a new block of memory to be used as upvalue 1
  97. (*(int*)(ud)) = -1;
  98. lua_pushcclosure(L,rocket_pairsaux,2); //uv 1 is __ipairs, uv 2 is ud
  99. }
  100. else
  101. lua_pushvalue(L,lua_upvalueindex(1)); //generator
  102. lua_pushvalue(L,1); //state
  103. lua_pushnil(L); //initial value
  104. return 3;
  105. }
  106. //Based off of the luaB_print function from Lua's lbaselib.c
  107. int LuaPrint(lua_State* L)
  108. {
  109. int n = lua_gettop(L); /* number of arguments */
  110. int i;
  111. lua_getglobal(L, "tostring");
  112. StringList string_list = StringList();
  113. String output = "";
  114. for (i=1; i<=n; i++)
  115. {
  116. const char *s;
  117. lua_pushvalue(L, -1); /* function to be called */
  118. lua_pushvalue(L, i); /* value to print */
  119. lua_call(L, 1, 1);
  120. s = lua_tostring(L, -1); /* get result */
  121. if (s == NULL)
  122. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  123. LUA_QL("print"));
  124. if (i>1)
  125. output += "\t";
  126. output += String(s);
  127. lua_pop(L, 1); /* pop result */
  128. }
  129. output += "\n";
  130. Log::Message(Log::LT_INFO, output.CString());
  131. return 0;
  132. }
  133. void OverrideLuaGlobalFunctions(lua_State* L)
  134. {
  135. lua_getglobal(L,"_G");
  136. lua_getglobal(L,"next");
  137. lua_pushcclosure(L,rocket_pairs,1);
  138. lua_setfield(L,-2,"pairs");
  139. lua_pushcfunction(L,ipairsaux);
  140. lua_pushcclosure(L,rocket_ipairs,1);
  141. lua_setfield(L,-2,"ipairs");
  142. lua_pushcfunction(L,LuaPrint);
  143. lua_setfield(L,-2,"print");
  144. lua_pop(L,1); //pop _G
  145. }
  146. }
  147. }
  148. }