Interpreter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <Rocket/Core/Lua/Interpreter.h>
  29. #include <Rocket/Core/Log.h>
  30. #include <Rocket/Core/String.h>
  31. #include <Rocket/Core/Lua/LuaType.h>
  32. #include "LuaDocumentElementInstancer.h"
  33. #include <Rocket/Core/Factory.h>
  34. #include "LuaEventListenerInstancer.h"
  35. #include "LuaDataFormatter.h"
  36. #include "Rocket.h"
  37. namespace Rocket {
  38. namespace Core {
  39. namespace Lua {
  40. lua_State* Interpreter::_L = NULL;
  41. typedef Rocket::Core::ElementDocument Document;
  42. typedef Rocket::Core::Lua::LuaDataFormatter DataFormatter;
  43. void Interpreter::Startup()
  44. {
  45. Log::Message(Log::LT_INFO, "Loading Lua interpreter");
  46. _L = luaL_newstate();
  47. luaL_openlibs(_L);
  48. RegisterEverything(_L);
  49. }
  50. void Interpreter::RegisterEverything(lua_State* L)
  51. {
  52. LuaType<Vector2i>::Register(L);
  53. LuaType<Vector2f>::Register(L);
  54. LuaType<Colourf>::Register(L);
  55. LuaType<Colourb>::Register(L);
  56. LuaType<Log>::Register(L);
  57. LuaType<ElementStyle>::Register(L);
  58. LuaType<Element>::Register(L);
  59. //things that inherit from Element
  60. LuaType<Document>::Register(L);
  61. //controls that inherit from Element
  62. LuaType<Rocket::Controls::ElementTabSet>::Register(L);
  63. LuaType<Rocket::Controls::ElementDataGrid>::Register(L);
  64. LuaType<Rocket::Controls::ElementDataGridRow>::Register(L);
  65. LuaType<Rocket::Controls::ElementForm>::Register(L);
  66. LuaType<Rocket::Controls::ElementFormControl>::Register(L);
  67. //inherits from ElementFormControl
  68. LuaType<Rocket::Controls::ElementFormControlSelect>::Register(L);
  69. LuaType<Rocket::Controls::ElementFormControlDataSelect>::Register(L);
  70. LuaType<Rocket::Controls::ElementFormControlInput>::Register(L);
  71. LuaType<Rocket::Controls::ElementFormControlTextArea>::Register(L);
  72. LuaType<Event>::Register(L);
  73. LuaType<Context>::Register(L);
  74. LuaType<DataFormatter>::Register(L);
  75. LuaType<rocket>::Register(L);
  76. }
  77. void Interpreter::LoadFile(const String& file)
  78. {
  79. String msg = "Loading";
  80. if(luaL_loadfile(_L, file.CString()) != 0)
  81. {
  82. msg.Append(" failed. Could not load. ").Append(file);
  83. Log::Message(Log::LT_ERROR, msg.CString());
  84. Report();
  85. }
  86. else
  87. {
  88. if(lua_pcall(_L,0,0,0) != 0)
  89. {
  90. msg.Append(" failed. Could not run. ").Append(file);
  91. Log::Message(Log::LT_ERROR, msg.CString());
  92. Report();
  93. }
  94. else
  95. {
  96. msg.Append(" was successful. ").Append(file);
  97. Log::Message(Log::LT_INFO, msg.CString());
  98. }
  99. }
  100. }
  101. void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core::String& name)
  102. {
  103. luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
  104. if(lua_pcall(_L,0,0,0) != 0)
  105. Report();
  106. }
  107. void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name)
  108. {
  109. luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString());
  110. }
  111. void Interpreter::Report(lua_State* L, const Rocket::Core::String& place)
  112. {
  113. if(L == NULL)
  114. L = _L; //use the original state of Interpreter
  115. const char * msg= lua_tostring(_L,-1);
  116. String strmsg;
  117. while(msg)
  118. {
  119. lua_pop(_L,1);
  120. if(place == "")
  121. strmsg = msg;
  122. else
  123. strmsg = String(place).Append(" ").Append(msg);
  124. Log::Message(Log::LT_WARNING, strmsg.CString());
  125. msg=lua_tostring(_L,-1);
  126. }
  127. }
  128. void Interpreter::BeginCall(int funRef)
  129. {
  130. lua_settop(_L,0); //empty stack
  131. lua_getref(_L,funRef);
  132. }
  133. bool Interpreter::ExecuteCall(int params, int res)
  134. {
  135. bool ret = true;
  136. int top = lua_gettop(_L);
  137. //String strtype = lua_typename(_L,top-params);
  138. String strtype;
  139. for(int i = top; i >= 1; i--)
  140. strtype = lua_typename(_L,lua_type(_L,i));
  141. if(lua_type(_L,top-params) != LUA_TFUNCTION)
  142. {
  143. ret = false;
  144. //stack cleanup
  145. if(params > 0)
  146. {
  147. for(int i = top; i >= (top-params); i--)
  148. {
  149. if(!lua_isnone(_L,i))
  150. lua_remove(_L,i);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if(lua_pcall(_L,params,res,0) != 0)
  157. {
  158. Report();
  159. ret = false;
  160. }
  161. }
  162. return ret;
  163. }
  164. void Interpreter::EndCall(int res)
  165. {
  166. //stack cleanup
  167. for(int i = res; i > 0; i--)
  168. {
  169. if(!lua_isnone(_L,res))
  170. lua_remove(_L,res);
  171. }
  172. }
  173. lua_State* Interpreter::GetLuaState() { return _L; }
  174. //From Plugin
  175. int Interpreter::GetEventClasses()
  176. {
  177. return EVT_BASIC;
  178. }
  179. void Interpreter::OnInitialise()
  180. {
  181. Startup();
  182. Factory::RegisterElementInstancer("body",new LuaDocumentElementInstancer())->RemoveReference();
  183. Factory::RegisterEventListenerInstancer(new LuaEventListenerInstancer())->RemoveReference();
  184. }
  185. void Interpreter::OnShutdown()
  186. {
  187. //causing crashes
  188. //lua_close(_L);
  189. }
  190. void Interpreter::Initialise()
  191. {
  192. Rocket::Core::RegisterPlugin(new Interpreter());
  193. }
  194. void Interpreter::Shutdown()
  195. {
  196. lua_close(_L);
  197. }
  198. }
  199. }
  200. }