Interpreter.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "precompiled.h"
  2. #include "Interpreter.h"
  3. #include <Rocket/Core/Log.h>
  4. #include <Rocket/Core/String.h>
  5. #include <Rocket/Core/Lua/LuaType.h>
  6. #include "LuaDocumentElementInstancer.h"
  7. #include <Rocket/Core/Factory.h>
  8. #include "LuaEventListenerInstancer.h"
  9. #include "Rocket.h"
  10. namespace Rocket {
  11. namespace Core {
  12. namespace Lua {
  13. lua_State* Interpreter::_L = NULL;
  14. typedef Rocket::Core::ElementDocument Document;
  15. void Interpreter::Startup()
  16. {
  17. Log::Message(Log::LT_INFO, "Loading Lua interpreter");
  18. _L = luaL_newstate();
  19. luaL_openlibs(_L);
  20. RegisterEverything(_L);
  21. }
  22. void Interpreter::RegisterEverything(lua_State* L)
  23. {
  24. LuaType<Vector2i>::Register(L);
  25. LuaType<Vector2f>::Register(L);
  26. LuaType<Colourf>::Register(L);
  27. LuaType<Colourb>::Register(L);
  28. LuaType<Log>::Register(L);
  29. LuaType<ElementStyle>::Register(L);
  30. LuaType<Element>::Register(L);
  31. //things that inherit from Element
  32. LuaType<Document>::Register(L);
  33. //controls that inherit from Element
  34. LuaType<Rocket::Controls::ElementTabSet>::Register(L);
  35. LuaType<Rocket::Controls::ElementDataGrid>::Register(L);
  36. LuaType<Rocket::Controls::ElementDataGridRow>::Register(L);
  37. LuaType<Rocket::Controls::ElementForm>::Register(L);
  38. LuaType<Rocket::Controls::ElementFormControl>::Register(L);
  39. //inherits from ElementFormControl
  40. LuaType<Rocket::Controls::ElementFormControlSelect>::Register(L);
  41. LuaType<Rocket::Controls::ElementFormControlDataSelect>::Register(L);
  42. LuaType<Rocket::Controls::ElementFormControlInput>::Register(L);
  43. LuaType<Rocket::Controls::ElementFormControlTextArea>::Register(L);
  44. LuaType<Event>::Register(L);
  45. LuaType<Context>::Register(L);
  46. LuaType<rocket>::Register(L);
  47. }
  48. void Interpreter::LoadFile(const String& file)
  49. {
  50. String msg = "Loading";
  51. if(luaL_loadfile(_L, file.CString()) != 0)
  52. {
  53. msg.Append(" failed. Could not load. ").Append(file);
  54. Log::Message(Log::LT_ERROR, msg.CString());
  55. Report();
  56. }
  57. else
  58. {
  59. if(lua_pcall(_L,0,0,0) != 0)
  60. {
  61. msg.Append(" failed. Could not run. ").Append(file);
  62. Log::Message(Log::LT_ERROR, msg.CString());
  63. Report();
  64. }
  65. else
  66. {
  67. msg.Append(" was successful. ").Append(file);
  68. Log::Message(Log::LT_INFO, msg.CString());
  69. }
  70. }
  71. }
  72. void Interpreter::DoString(const Rocket::Core::String& str)
  73. {
  74. if(luaL_dostring(_L,str.CString()) != 0)
  75. Report();
  76. }
  77. void Interpreter::Report()
  78. {
  79. const char * msg= lua_tostring(_L,-1);
  80. while(msg)
  81. {
  82. lua_pop(_L,1);
  83. Log::Message(Log::LT_WARNING, msg);
  84. msg=lua_tostring(_L,-1);
  85. }
  86. }
  87. void Interpreter::BeginCall(int funRef)
  88. {
  89. lua_settop(_L,0); //empty stack
  90. lua_getref(_L,funRef);
  91. }
  92. bool Interpreter::ExecuteCall(int params, int res)
  93. {
  94. bool ret = true;
  95. int top = lua_gettop(_L);
  96. //String strtype = lua_typename(_L,top-params);
  97. String strtype;
  98. for(int i = top; i >= 1; i--)
  99. strtype = lua_typename(_L,lua_type(_L,i));
  100. if(lua_type(_L,top-params) != LUA_TFUNCTION)
  101. {
  102. ret = false;
  103. //stack cleanup
  104. if(params > 0)
  105. {
  106. for(int i = top; i >= (top-params); i--)
  107. {
  108. if(!lua_isnone(_L,i))
  109. lua_remove(_L,i);
  110. }
  111. }
  112. }
  113. else
  114. {
  115. if(lua_pcall(_L,params,res,0))
  116. {
  117. Report();
  118. ret = false;
  119. }
  120. }
  121. return ret;
  122. }
  123. void Interpreter::EndCall(int res)
  124. {
  125. //stack cleanup
  126. for(int i = res; i > 0; i--)
  127. {
  128. if(!lua_isnone(_L,res))
  129. lua_remove(_L,res);
  130. }
  131. }
  132. lua_State* Interpreter::GetLuaState() { return _L; }
  133. //From Plugin
  134. int Interpreter::GetEventClasses()
  135. {
  136. return EVT_BASIC;
  137. }
  138. void Interpreter::OnInitialise()
  139. {
  140. Startup();
  141. Factory::RegisterElementInstancer("body",new LuaDocumentElementInstancer())->RemoveReference();
  142. Factory::RegisterEventListenerInstancer(new LuaEventListenerInstancer())->RemoveReference();
  143. }
  144. void Interpreter::OnShutdown()
  145. {
  146. //causing crashes
  147. //lua_close(_L);
  148. }
  149. void Interpreter::Initialise()
  150. {
  151. Rocket::Core::RegisterPlugin(new Interpreter());
  152. }
  153. void Interpreter::Shutdown()
  154. {
  155. lua_close(_L);
  156. }
  157. }
  158. }
  159. }