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