Interpreter.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/FileInterface.h>
  33. #include <Rocket/Core/Lua/LuaType.h>
  34. #include "LuaDocumentElementInstancer.h"
  35. #include <Rocket/Core/Factory.h>
  36. #include "LuaEventListenerInstancer.h"
  37. #include "Rocket.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 "ElementStyleProxy.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. #include "RocketContextsProxy.h"
  57. namespace Rocket {
  58. namespace Core {
  59. namespace Lua {
  60. lua_State* Interpreter::_L = NULL;
  61. //typedefs for nicer Lua names
  62. typedef Rocket::Core::ElementDocument Document;
  63. void Interpreter::Startup()
  64. {
  65. if(_L == NULL)
  66. {
  67. Log::Message(Log::LT_INFO, "Loading Lua interpreter");
  68. _L = luaL_newstate();
  69. luaL_openlibs(_L);
  70. }
  71. RegisterCoreTypes(_L);
  72. }
  73. void Interpreter::RegisterCoreTypes(lua_State* L)
  74. {
  75. LuaType<Vector2i>::Register(L);
  76. LuaType<Vector2f>::Register(L);
  77. LuaType<Colourf>::Register(L);
  78. LuaType<Colourb>::Register(L);
  79. LuaType<Log>::Register(L);
  80. LuaType<ElementStyleProxy>::Register(L);
  81. LuaType<Element>::Register(L);
  82. //things that inherit from Element
  83. LuaType<Document>::Register(L);
  84. LuaType<ElementText>::Register(L);
  85. LuaType<Event>::Register(L);
  86. LuaType<Context>::Register(L);
  87. LuaType<LuaRocket>::Register(L);
  88. LuaType<ElementInstancer>::Register(L);
  89. //Proxy tables
  90. LuaType<ContextDocumentsProxy>::Register(L);
  91. LuaType<EventParametersProxy>::Register(L);
  92. LuaType<ElementAttributesProxy>::Register(L);
  93. LuaType<ElementChildNodesProxy>::Register(L);
  94. LuaType<RocketContextsProxy>::Register(L);
  95. OverrideLuaGlobalFunctions(L);
  96. //push the global variable "rocket" to use the "Rocket" methods
  97. LuaRocketPushrocketGlobal(L);
  98. }
  99. void Interpreter::LoadFile(const String& file)
  100. {
  101. //use the file interface to get the contents of the script
  102. Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface();
  103. Rocket::Core::FileHandle handle = file_interface->Open(file);
  104. if(handle == 0) {
  105. lua_pushfstring(_L, "LoadFile: Unable to open file: %s", file.CString());
  106. Report(_L);
  107. return;
  108. }
  109. size_t size = file_interface->Length(handle);
  110. if(size == 0) {
  111. lua_pushfstring(_L, "LoadFile: File is 0 bytes in size: %s", file.CString());
  112. Report(_L);
  113. return;
  114. }
  115. char* file_contents = new char[size];
  116. file_interface->Read(file_contents,size,handle);
  117. file_interface->Close(handle);
  118. if(luaL_loadbuffer(_L,file_contents,size,file.CString()) != 0)
  119. Report(_L);
  120. else //if there were no errors loading, then the compiled function is on the top of the stack
  121. {
  122. if(lua_pcall(_L,0,0,0) != 0)
  123. Report(_L);
  124. }
  125. delete[] file_contents;
  126. }
  127. void Interpreter::DoString(const Rocket::Core::String& code, const Rocket::Core::String& name)
  128. {
  129. if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
  130. Report(_L);
  131. else
  132. {
  133. if(lua_pcall(_L,0,0,0) != 0)
  134. Report(_L);
  135. }
  136. }
  137. void Interpreter::LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name)
  138. {
  139. if(luaL_loadbuffer(_L,code.CString(),code.Length(), name.CString()) != 0)
  140. Report(_L);
  141. }
  142. void Interpreter::BeginCall(int funRef)
  143. {
  144. lua_settop(_L,0); //empty stack
  145. //lua_getref(_L,funRef);
  146. lua_rawgeti(_L, LUA_REGISTRYINDEX, (int)funRef);
  147. }
  148. bool Interpreter::ExecuteCall(int params, int res)
  149. {
  150. bool ret = true;
  151. int top = lua_gettop(_L);
  152. if(lua_type(_L,top-params) != LUA_TFUNCTION)
  153. {
  154. ret = false;
  155. //stack cleanup
  156. if(params > 0)
  157. {
  158. for(int i = top; i >= (top-params); i--)
  159. {
  160. if(!lua_isnone(_L,i))
  161. lua_remove(_L,i);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. if(lua_pcall(_L,params,res,0) != 0)
  168. {
  169. Report(_L);
  170. ret = false;
  171. }
  172. }
  173. return ret;
  174. }
  175. void Interpreter::EndCall(int res)
  176. {
  177. //stack cleanup
  178. for(int i = res; i > 0; i--)
  179. {
  180. if(!lua_isnone(_L,res))
  181. lua_remove(_L,res);
  182. }
  183. }
  184. lua_State* Interpreter::GetLuaState() { return _L; }
  185. //From Plugin
  186. int Interpreter::GetEventClasses()
  187. {
  188. return EVT_BASIC;
  189. }
  190. void Interpreter::OnInitialise()
  191. {
  192. Startup();
  193. Factory::RegisterElementInstancer("body",new LuaDocumentElementInstancer())->RemoveReference();
  194. Factory::RegisterEventListenerInstancer(new LuaEventListenerInstancer())->RemoveReference();
  195. }
  196. void Interpreter::OnShutdown()
  197. {
  198. }
  199. void Interpreter::Initialise()
  200. {
  201. Rocket::Core::Lua::Interpreter::Initialise(NULL);
  202. }
  203. void Interpreter::Initialise(lua_State *luaStatePointer)
  204. {
  205. Interpreter *iPtr = new Interpreter();
  206. iPtr->_L = luaStatePointer;
  207. Rocket::Core::RegisterPlugin(iPtr);
  208. }
  209. void Interpreter::Shutdown()
  210. {
  211. lua_close(_L);
  212. }
  213. }
  214. }
  215. }