Interpreter.cpp 6.5 KB

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