Interpreter.cpp 5.6 KB

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