Interpreter.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "precompiled.h"
  2. #include "Interpreter.h"
  3. #include <Rocket/Core/Log.h>
  4. #include <Rocket/Core/String.h>
  5. #include "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. #include "Register.h" //think of it as a glorified macro file
  23. }
  24. void Interpreter::LoadFile(const String& file)
  25. {
  26. String msg = "Loading";
  27. if(luaL_loadfile(_L, file.CString()) != 0)
  28. {
  29. msg.Append(" failed. Could not load. ").Append(file);
  30. Log::Message(Log::LT_ERROR, msg.CString());
  31. Report();
  32. }
  33. else
  34. {
  35. if(lua_pcall(_L,0,0,0) != 0)
  36. {
  37. msg.Append(" failed. Could not run. ").Append(file);
  38. Log::Message(Log::LT_ERROR, msg.CString());
  39. Report();
  40. }
  41. else
  42. {
  43. msg.Append(" was successful. ").Append(file);
  44. Log::Message(Log::LT_INFO, msg.CString());
  45. }
  46. }
  47. }
  48. void Interpreter::DoString(const Rocket::Core::String& str)
  49. {
  50. if(luaL_dostring(_L,str.CString()) != 0)
  51. Report();
  52. }
  53. void Interpreter::Report()
  54. {
  55. const char * msg= lua_tostring(_L,-1);
  56. while(msg)
  57. {
  58. lua_pop(_L,-1);
  59. Log::Message(Log::LT_WARNING, msg);
  60. msg=lua_tostring(_L,-1);
  61. }
  62. }
  63. void Interpreter::BeginCall(int funRef)
  64. {
  65. lua_settop(_L,0); //empty stack
  66. lua_getref(_L,funRef);
  67. }
  68. bool Interpreter::ExecuteCall(int params, int res)
  69. {
  70. bool ret = true;
  71. int top = lua_gettop(_L);
  72. if(lua_type(_L,top-params) != LUA_TFUNCTION)
  73. {
  74. ret = false;
  75. //stack cleanup
  76. if(params > 0)
  77. {
  78. for(int i = top; i >= (top-params); i--)
  79. {
  80. if(!lua_isnone(_L,i))
  81. lua_remove(_L,i);
  82. }
  83. }
  84. }
  85. else
  86. {
  87. if(lua_pcall(_L,params,res,0))
  88. {
  89. Report();
  90. ret = false;
  91. }
  92. }
  93. return ret;
  94. }
  95. void Interpreter::EndCall(int res)
  96. {
  97. //stack cleanup
  98. for(int i = res; i > 0; i--)
  99. {
  100. if(!lua_isnone(_L,res))
  101. lua_remove(_L,res);
  102. }
  103. }
  104. lua_State* Interpreter::GetLuaState() { return _L; }
  105. //From Plugin
  106. int Interpreter::GetEventClasses()
  107. {
  108. return EVT_BASIC;
  109. }
  110. void Interpreter::OnInitialise()
  111. {
  112. Startup();
  113. Factory::RegisterElementInstancer("body",new LuaDocumentElementInstancer())->RemoveReference();
  114. Factory::RegisterEventListenerInstancer(new LuaEventListenerInstancer())->RemoveReference();
  115. }
  116. void Interpreter::OnShutdown()
  117. {
  118. lua_close(_L);
  119. }
  120. }
  121. }
  122. }