Browse Source

Merge branch 'feature-use-existing-lua-context' of https://github.com/dwimsey/libRocket into feature/lua-scripting

* 'feature-use-existing-lua-context' of https://github.com/dwimsey/libRocket:
  Allows use of an existing lua_State pointer instead of creating a new lua_State.  Useful if you are integrating with code that already supports Lua.
  Allows use of an existing lua_State pointer instead of creating a new lua_State.  Useful if you are integrating with code that already supports Lua.
David Wimsey 11 years ago
parent
commit
a170f13da5
2 changed files with 19 additions and 4 deletions
  1. 6 0
      Include/Rocket/Core/Lua/Interpreter.h
  2. 13 4
      Source/Core/Lua/Interpreter.cpp

+ 6 - 0
Include/Rocket/Core/Lua/Interpreter.h

@@ -96,6 +96,12 @@ public:
     exist again. Internally, it calls Interpreter::Startup() and registers the "body" tag to generate a LuaDocument
     exist again. Internally, it calls Interpreter::Startup() and registers the "body" tag to generate a LuaDocument
     rather than a Rocket::Core::ElementDocument    */
     rather than a Rocket::Core::ElementDocument    */
     static void Initialise();
     static void Initialise();
+    /** Creates the plugin and adds Rocket to an existing Lua context
+	 @remark Call this function only once, and special care must be taken when destroying the lua_State passed to this method.
+	 Interpreter::Shutdown() calles lua_close on the lua_State pointer provided here, do not call Interpreter::Shutdown if you
+	 must call lua_close yourself or if you need to continue to use the lua_State pointer provided here.  Internally, this
+	 method works as Initialise() except an existing lua_State pointer is used instead of creating a new lua_State context. */
+    static void Initialise(lua_State *_L = NULL);
     /** Stops the plugin by calling lua_close        */
     /** Stops the plugin by calling lua_close        */
 	static void Shutdown();
 	static void Shutdown();
     
     

+ 13 - 4
Source/Core/Lua/Interpreter.cpp

@@ -65,10 +65,12 @@ typedef Rocket::Core::ElementDocument Document;
 
 
 void Interpreter::Startup()
 void Interpreter::Startup()
 {
 {
-    Log::Message(Log::LT_INFO, "Loading Lua interpreter");
-    _L = luaL_newstate();
-    luaL_openlibs(_L);
-
+	if(_L == NULL)
+	{
+		Log::Message(Log::LT_INFO, "Loading Lua interpreter");
+		_L = luaL_newstate();
+		luaL_openlibs(_L);
+	}
     RegisterCoreTypes(_L);
     RegisterCoreTypes(_L);
 }
 }
 
 
@@ -223,6 +225,13 @@ void Interpreter::Initialise()
     Rocket::Core::RegisterPlugin(new Interpreter());
     Rocket::Core::RegisterPlugin(new Interpreter());
 }
 }
 
 
+void Interpreter::Initialise(lua_State *luaStatePointer)
+{
+	Interpreter *iPtr = new Interpreter();
+	iPtr->_L = luaStatePointer;
+	Rocket::Core::RegisterPlugin(iPtr);
+}
+
 void Interpreter::Shutdown()
 void Interpreter::Shutdown()
 {
 {
 	lua_close(_L);
 	lua_close(_L);