Interpreter.cpp 6.8 KB

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