Interpreter.cpp 3.9 KB

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