Interpreter.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 = nullptr;
  62. //typedefs for nicer Lua names
  63. typedef Rml::Core::ElementDocument Document;
  64. void Interpreter::Startup()
  65. {
  66. if(_L == nullptr)
  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<ElementPtr>::Register(L);
  87. LuaType<Event>::Register(L);
  88. LuaType<Context>::Register(L);
  89. LuaType<LuaRmlUi>::Register(L);
  90. LuaType<ElementInstancer>::Register(L);
  91. //Proxy tables
  92. LuaType<ContextDocumentsProxy>::Register(L);
  93. LuaType<EventParametersProxy>::Register(L);
  94. LuaType<ElementAttributesProxy>::Register(L);
  95. LuaType<ElementChildNodesProxy>::Register(L);
  96. LuaType<RmlUiContextsProxy>::Register(L);
  97. OverrideLuaGlobalFunctions(L);
  98. //push the global variable "rmlui" to use the "RmlUi" methods
  99. LuaRmlUiPushrmluiGlobal(L);
  100. }
  101. void Interpreter::LoadFile(const String& file)
  102. {
  103. //use the file interface to get the contents of the script
  104. Rml::Core::FileInterface* file_interface = Rml::Core::GetFileInterface();
  105. Rml::Core::FileHandle handle = file_interface->Open(file);
  106. if(handle == 0) {
  107. lua_pushfstring(_L, "LoadFile: Unable to open file: %s", file.c_str());
  108. Report(_L);
  109. return;
  110. }
  111. size_t size = file_interface->Length(handle);
  112. if(size == 0) {
  113. lua_pushfstring(_L, "LoadFile: File is 0 bytes in size: %s", file.c_str());
  114. Report(_L);
  115. return;
  116. }
  117. char* file_contents = new char[size];
  118. file_interface->Read(file_contents,size,handle);
  119. file_interface->Close(handle);
  120. if(luaL_loadbuffer(_L,file_contents,size,file.c_str()) != 0)
  121. Report(_L);
  122. else //if there were no errors loading, then the compiled function is on the top of the stack
  123. {
  124. if(lua_pcall(_L,0,0,0) != 0)
  125. Report(_L);
  126. }
  127. delete[] file_contents;
  128. }
  129. void Interpreter::DoString(const Rml::Core::String& code, const Rml::Core::String& name)
  130. {
  131. if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
  132. Report(_L);
  133. else
  134. {
  135. if(lua_pcall(_L,0,0,0) != 0)
  136. Report(_L);
  137. }
  138. }
  139. void Interpreter::LoadString(const Rml::Core::String& code, const Rml::Core::String& name)
  140. {
  141. if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
  142. Report(_L);
  143. }
  144. void Interpreter::BeginCall(int funRef)
  145. {
  146. lua_settop(_L,0); //empty stack
  147. //lua_getref(_L,funRef);
  148. lua_rawgeti(_L, LUA_REGISTRYINDEX, (int)funRef);
  149. }
  150. bool Interpreter::ExecuteCall(int params, int res)
  151. {
  152. bool ret = true;
  153. int top = lua_gettop(_L);
  154. if(lua_type(_L,top-params) != LUA_TFUNCTION)
  155. {
  156. ret = false;
  157. //stack cleanup
  158. if(params > 0)
  159. {
  160. for(int i = top; i >= (top-params); i--)
  161. {
  162. if(!lua_isnone(_L,i))
  163. lua_remove(_L,i);
  164. }
  165. }
  166. }
  167. else
  168. {
  169. if(lua_pcall(_L,params,res,0) != 0)
  170. {
  171. Report(_L);
  172. ret = false;
  173. }
  174. }
  175. return ret;
  176. }
  177. void Interpreter::EndCall(int res)
  178. {
  179. //stack cleanup
  180. for(int i = res; i > 0; i--)
  181. {
  182. if(!lua_isnone(_L,res))
  183. lua_remove(_L,res);
  184. }
  185. }
  186. lua_State* Interpreter::GetLuaState() { return _L; }
  187. //From Plugin
  188. int Interpreter::GetEventClasses()
  189. {
  190. return EVT_BASIC;
  191. }
  192. static LuaDocumentElementInstancer* g_lua_document_element_instancer = nullptr;
  193. static LuaEventListenerInstancer* g_lua_event_listener_instancer = nullptr;
  194. void Interpreter::OnInitialise()
  195. {
  196. Startup();
  197. g_lua_document_element_instancer = new LuaDocumentElementInstancer();
  198. g_lua_event_listener_instancer = new LuaEventListenerInstancer();
  199. Factory::RegisterElementInstancer("body", g_lua_document_element_instancer);
  200. Factory::RegisterEventListenerInstancer(g_lua_event_listener_instancer);
  201. }
  202. void Interpreter::OnShutdown()
  203. {
  204. delete g_lua_document_element_instancer;
  205. delete g_lua_event_listener_instancer;
  206. g_lua_document_element_instancer = nullptr;
  207. g_lua_event_listener_instancer = nullptr;
  208. }
  209. void Interpreter::Initialise()
  210. {
  211. Rml::Core::Lua::Interpreter::Initialise(nullptr);
  212. }
  213. void Interpreter::Initialise(lua_State *luaStatePointer)
  214. {
  215. Interpreter *iPtr = new Interpreter();
  216. iPtr->_L = luaStatePointer;
  217. Rml::Core::RegisterPlugin(iPtr);
  218. }
  219. void Interpreter::Shutdown()
  220. {
  221. lua_close(_L);
  222. }
  223. }
  224. }
  225. }