Interpreter.cpp 6.7 KB

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