Interpreter.cpp 3.4 KB

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