Interpreter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORELUAINTERPRETER_H
  28. #define ROCKETCORELUAINTERPRETER_H
  29. #include "Header.h"
  30. #include <Rocket/Core/Lua/lua.hpp>
  31. #include <Rocket/Core/Plugin.h>
  32. namespace Rocket {
  33. namespace Core {
  34. namespace Lua {
  35. /**
  36. This initializes the Lua interpreter, and has functions to load the scripts or
  37. call functions that exist in Lua.
  38. @author Nathan Starkey
  39. */
  40. class ROCKETLUA_API Interpreter : public Plugin
  41. {
  42. public:
  43. /** This function calls luaL_loadfile and then lua_pcall, reporting the errors (if any)
  44. @param[in] file Fully qualified file name to execute.
  45. @remark Somewhat misleading name if you are used to the Lua meaning of "load file". It behaves
  46. exactly as luaL_dofile does. */
  47. static void LoadFile(const Rocket::Core::String& file);
  48. /** Calls lua_dostring and reports the errors.
  49. @param[in] code String to execute
  50. @param[in] name Name for the code that will show up in the Log */
  51. static void DoString(const Rocket::Core::String& code, const Rocket::Core::String& name = "");
  52. /** Same as DoString, except does NOT call pcall on it. It will leave the compiled (but not executed) string
  53. on top of the stack. It behaves exactly like luaL_loadstring, but you get to specify the name
  54. @param[in] code String to compile
  55. @param[in] name Name for the code that will show up in the Log */
  56. static void LoadString(const Rocket::Core::String& code, const Rocket::Core::String& name = "");
  57. /** Clears all of the items on the stack, and pushes the function from funRef on top of the stack. Only use
  58. this if you used lua_ref instead of luaL_ref
  59. @param[in] funRef Lua reference that you would recieve from calling lua_ref */
  60. static void BeginCall(int funRef);
  61. /** Uses lua_pcall on a function, which executes the function with params number of parameters and pushes
  62. res number of return values on to the stack.
  63. @pre Before you call this, your stack should look like:
  64. [1] function to call;
  65. [2...top] parameters to pass to the function (if any).
  66. Or, in words, make sure to push the function on the stack before the parameters.
  67. @post After this function, the params and function will be popped off, and 'res'
  68. number of items will be pushed. */
  69. static bool ExecuteCall(int params = 0, int res = 0);
  70. /** removes 'res' number of items from the stack
  71. @param[in] res Number of results to remove from the stack. */
  72. static void EndCall(int res = 0);
  73. /** This will populate the global Lua table with all of the Lua core types by calling LuaType<T>::Register
  74. @param[in] L The lua_State to use to register the types
  75. @remark This is called automatically inside of Interpreter::Startup(), so you do not have to
  76. call this function upon initialization of the Interpreter. If you are using RocketControlsLua, then you
  77. \em will need to call Rocket::Controls::Lua::RegisterTypes(lua_State*) */
  78. static void RegisterCoreTypes(lua_State* L);
  79. /**
  80. @return The lua_State that the Interpreter created in Interpreter::Startup()
  81. @remark This class lacks a SetLuaState for a reason. If you have to use a seperate Lua binding and want to keep the types
  82. from libRocket, then use this lua_State; it will already have all of the libraries loaded, and all of the types defined.
  83. Alternatively, you can call RegisterCoreTypes(lua_State*) with your own Lua state if you need them defined in it. */
  84. static lua_State* GetLuaState();
  85. /** Creates the plugin.
  86. @remark This is equivilent to calling Initialise(NULL).
  87. */
  88. static void Initialise();
  89. /** Creates the plugin and adds Rocket to an existing Lua context if one is provided.
  90. @remark Call this function only once, and special care must be taken when destroying the lua_State passed to this method.
  91. Interpreter::Shutdown() calles lua_close on the lua_State pointer provided here, do not call Interpreter::Shutdown if you
  92. must call lua_close yourself or if you need to continue to use the lua_State pointer provided here. Internally, it calls
  93. Interpreter::Startup() and registers the "body" tag to generate a LuaDocument rather than a Rocket::Core::ElementDocument.
  94. If the argument provided is NULL, a Lua context is created automatically instead. */
  95. static void Initialise(lua_State *_L);
  96. /** Stops the plugin by calling lua_close
  97. @remark Shutdown calls lua_Close on the lua_State associated with the Interpreter. If a lua_State was provided in the
  98. original call to Initialise, Shutdown should not be called OR you must not call lua_Close from within your code. */
  99. static void Shutdown();
  100. /** @sa Rocket::Core::Plugin::GetEventClasses */
  101. virtual int GetEventClasses();
  102. /** @sa Rocket::Core::Plugin::OnInitialise */
  103. virtual void OnInitialise();
  104. /** Currently does nothing. You must call Interpreter::Shutdown yourself at the appropriate time.
  105. @sa Rocket::Core::Plugin::OnShutdown */
  106. virtual void OnShutdown();
  107. private:
  108. /** Creates a lua_State for @var _L and calls luaL_openlibs, then calls Interpreter::RegisterCoreTypes(lua_State*)
  109. @remark called by Interpreter::Initialise() */
  110. void Startup();
  111. /** Lua state that Interpreter::Initialise() creates. */
  112. static lua_State* _L;
  113. };
  114. }
  115. }
  116. }
  117. #endif