|
@@ -60,50 +60,29 @@
|
|
|
namespace Rml {
|
|
namespace Rml {
|
|
|
namespace Core {
|
|
namespace Core {
|
|
|
namespace Lua {
|
|
namespace Lua {
|
|
|
-lua_State* Interpreter::_L = nullptr;
|
|
|
|
|
|
|
+
|
|
|
|
|
+static lua_State* g_L = nullptr;
|
|
|
|
|
+
|
|
|
//typedefs for nicer Lua names
|
|
//typedefs for nicer Lua names
|
|
|
typedef Rml::Core::ElementDocument Document;
|
|
typedef Rml::Core::ElementDocument Document;
|
|
|
|
|
|
|
|
-void Interpreter::Startup()
|
|
|
|
|
|
|
+
|
|
|
|
|
+void Interpreter::Initialise()
|
|
|
{
|
|
{
|
|
|
- if(_L == nullptr)
|
|
|
|
|
- {
|
|
|
|
|
- Log::Message(Log::LT_INFO, "Loading Lua interpreter");
|
|
|
|
|
- _L = luaL_newstate();
|
|
|
|
|
- luaL_openlibs(_L);
|
|
|
|
|
- }
|
|
|
|
|
- RegisterCoreTypes(_L);
|
|
|
|
|
|
|
+ Rml::Core::Lua::Interpreter::Initialise(nullptr);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-void Interpreter::RegisterCoreTypes(lua_State* L)
|
|
|
|
|
|
|
+void Interpreter::Initialise(lua_State* luaStatePointer)
|
|
|
{
|
|
{
|
|
|
- LuaType<Vector2i>::Register(L);
|
|
|
|
|
- LuaType<Vector2f>::Register(L);
|
|
|
|
|
- LuaType<Colourf>::Register(L);
|
|
|
|
|
- LuaType<Colourb>::Register(L);
|
|
|
|
|
- LuaType<Log>::Register(L);
|
|
|
|
|
- LuaType<ElementStyleProxy>::Register(L);
|
|
|
|
|
- LuaType<Element>::Register(L);
|
|
|
|
|
- //things that inherit from Element
|
|
|
|
|
- LuaType<Document>::Register(L);
|
|
|
|
|
- LuaType<ElementText>::Register(L);
|
|
|
|
|
- LuaType<ElementPtr>::Register(L);
|
|
|
|
|
- LuaType<Event>::Register(L);
|
|
|
|
|
- LuaType<Context>::Register(L);
|
|
|
|
|
- LuaType<LuaRmlUi>::Register(L);
|
|
|
|
|
- LuaType<ElementInstancer>::Register(L);
|
|
|
|
|
- //Proxy tables
|
|
|
|
|
- LuaType<ContextDocumentsProxy>::Register(L);
|
|
|
|
|
- LuaType<EventParametersProxy>::Register(L);
|
|
|
|
|
- LuaType<ElementAttributesProxy>::Register(L);
|
|
|
|
|
- LuaType<ElementChildNodesProxy>::Register(L);
|
|
|
|
|
- LuaType<RmlUiContextsProxy>::Register(L);
|
|
|
|
|
- OverrideLuaGlobalFunctions(L);
|
|
|
|
|
- //push the global variable "rmlui" to use the "RmlUi" methods
|
|
|
|
|
- LuaRmlUiPushrmluiGlobal(L);
|
|
|
|
|
|
|
+ RMLUI_ASSERT(g_L == nullptr);
|
|
|
|
|
+ g_L = luaStatePointer;
|
|
|
|
|
+ Rml::Core::RegisterPlugin(new Interpreter());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+lua_State* Interpreter::GetLuaState()
|
|
|
|
|
+{
|
|
|
|
|
+ return g_L;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
|
|
|
void Interpreter::LoadFile(const String& file)
|
|
void Interpreter::LoadFile(const String& file)
|
|
@@ -112,27 +91,27 @@ void Interpreter::LoadFile(const String& file)
|
|
|
Rml::Core::FileInterface* file_interface = Rml::Core::GetFileInterface();
|
|
Rml::Core::FileInterface* file_interface = Rml::Core::GetFileInterface();
|
|
|
Rml::Core::FileHandle handle = file_interface->Open(file);
|
|
Rml::Core::FileHandle handle = file_interface->Open(file);
|
|
|
if(handle == 0) {
|
|
if(handle == 0) {
|
|
|
- lua_pushfstring(_L, "LoadFile: Unable to open file: %s", file.c_str());
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ lua_pushfstring(g_L, "LoadFile: Unable to open file: %s", file.c_str());
|
|
|
|
|
+ Report(g_L);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
size_t size = file_interface->Length(handle);
|
|
size_t size = file_interface->Length(handle);
|
|
|
if(size == 0) {
|
|
if(size == 0) {
|
|
|
- lua_pushfstring(_L, "LoadFile: File is 0 bytes in size: %s", file.c_str());
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ lua_pushfstring(g_L, "LoadFile: File is 0 bytes in size: %s", file.c_str());
|
|
|
|
|
+ Report(g_L);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
char* file_contents = new char[size];
|
|
char* file_contents = new char[size];
|
|
|
file_interface->Read(file_contents,size,handle);
|
|
file_interface->Read(file_contents,size,handle);
|
|
|
file_interface->Close(handle);
|
|
file_interface->Close(handle);
|
|
|
|
|
|
|
|
- if(luaL_loadbuffer(_L,file_contents,size,file.c_str()) != 0)
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ if(luaL_loadbuffer(g_L,file_contents,size,file.c_str()) != 0)
|
|
|
|
|
+ Report(g_L);
|
|
|
else //if there were no errors loading, then the compiled function is on the top of the stack
|
|
else //if there were no errors loading, then the compiled function is on the top of the stack
|
|
|
{
|
|
{
|
|
|
- if(lua_pcall(_L,0,0,0) != 0)
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ if(lua_pcall(g_L,0,0,0) != 0)
|
|
|
|
|
+ Report(g_L);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
delete[] file_contents;
|
|
delete[] file_contents;
|
|
@@ -141,34 +120,34 @@ void Interpreter::LoadFile(const String& file)
|
|
|
|
|
|
|
|
void Interpreter::DoString(const Rml::Core::String& code, const Rml::Core::String& name)
|
|
void Interpreter::DoString(const Rml::Core::String& code, const Rml::Core::String& name)
|
|
|
{
|
|
{
|
|
|
- if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ if(luaL_loadbuffer(g_L,code.c_str(),code.length(), name.c_str()) != 0)
|
|
|
|
|
+ Report(g_L);
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if(lua_pcall(_L,0,0,0) != 0)
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ if(lua_pcall(g_L,0,0,0) != 0)
|
|
|
|
|
+ Report(g_L);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::LoadString(const Rml::Core::String& code, const Rml::Core::String& name)
|
|
void Interpreter::LoadString(const Rml::Core::String& code, const Rml::Core::String& name)
|
|
|
{
|
|
{
|
|
|
- if(luaL_loadbuffer(_L,code.c_str(),code.length(), name.c_str()) != 0)
|
|
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ if(luaL_loadbuffer(g_L,code.c_str(),code.length(), name.c_str()) != 0)
|
|
|
|
|
+ Report(g_L);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
void Interpreter::BeginCall(int funRef)
|
|
void Interpreter::BeginCall(int funRef)
|
|
|
{
|
|
{
|
|
|
- lua_settop(_L,0); //empty stack
|
|
|
|
|
- //lua_getref(_L,funRef);
|
|
|
|
|
- lua_rawgeti(_L, LUA_REGISTRYINDEX, (int)funRef);
|
|
|
|
|
|
|
+ lua_settop(g_L,0); //empty stack
|
|
|
|
|
+ //lua_getref(g_L,funRef);
|
|
|
|
|
+ lua_rawgeti(g_L, LUA_REGISTRYINDEX, (int)funRef);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool Interpreter::ExecuteCall(int params, int res)
|
|
bool Interpreter::ExecuteCall(int params, int res)
|
|
|
{
|
|
{
|
|
|
bool ret = true;
|
|
bool ret = true;
|
|
|
- int top = lua_gettop(_L);
|
|
|
|
|
- if(lua_type(_L,top-params) != LUA_TFUNCTION)
|
|
|
|
|
|
|
+ int top = lua_gettop(g_L);
|
|
|
|
|
+ if(lua_type(g_L,top-params) != LUA_TFUNCTION)
|
|
|
{
|
|
{
|
|
|
ret = false;
|
|
ret = false;
|
|
|
//stack cleanup
|
|
//stack cleanup
|
|
@@ -176,16 +155,16 @@ bool Interpreter::ExecuteCall(int params, int res)
|
|
|
{
|
|
{
|
|
|
for(int i = top; i >= (top-params); i--)
|
|
for(int i = top; i >= (top-params); i--)
|
|
|
{
|
|
{
|
|
|
- if(!lua_isnone(_L,i))
|
|
|
|
|
- lua_remove(_L,i);
|
|
|
|
|
|
|
+ if(!lua_isnone(g_L,i))
|
|
|
|
|
+ lua_remove(g_L,i);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if(lua_pcall(_L,params,res,0) != 0)
|
|
|
|
|
|
|
+ if(lua_pcall(g_L,params,res,0) != 0)
|
|
|
{
|
|
{
|
|
|
- Report(_L);
|
|
|
|
|
|
|
+ Report(g_L);
|
|
|
ret = false;
|
|
ret = false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -197,13 +176,11 @@ void Interpreter::EndCall(int res)
|
|
|
//stack cleanup
|
|
//stack cleanup
|
|
|
for(int i = res; i > 0; i--)
|
|
for(int i = res; i > 0; i--)
|
|
|
{
|
|
{
|
|
|
- if(!lua_isnone(_L,res))
|
|
|
|
|
- lua_remove(_L,res);
|
|
|
|
|
|
|
+ if(!lua_isnone(g_L,res))
|
|
|
|
|
+ lua_remove(g_L,res);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-lua_State* Interpreter::GetLuaState() { return _L; }
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
//From Plugin
|
|
//From Plugin
|
|
|
int Interpreter::GetEventClasses()
|
|
int Interpreter::GetEventClasses()
|
|
@@ -211,46 +188,72 @@ int Interpreter::GetEventClasses()
|
|
|
return EVT_BASIC;
|
|
return EVT_BASIC;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static LuaDocumentElementInstancer* g_lua_document_element_instancer = nullptr;
|
|
|
|
|
-static LuaEventListenerInstancer* g_lua_event_listener_instancer = nullptr;
|
|
|
|
|
-
|
|
|
|
|
void Interpreter::OnInitialise()
|
|
void Interpreter::OnInitialise()
|
|
|
{
|
|
{
|
|
|
- Startup();
|
|
|
|
|
- g_lua_document_element_instancer = new LuaDocumentElementInstancer();
|
|
|
|
|
- g_lua_event_listener_instancer = new LuaEventListenerInstancer();
|
|
|
|
|
- Factory::RegisterElementInstancer("body", g_lua_document_element_instancer);
|
|
|
|
|
- Factory::RegisterEventListenerInstancer(g_lua_event_listener_instancer);
|
|
|
|
|
|
|
+ if (g_L == nullptr)
|
|
|
|
|
+ {
|
|
|
|
|
+ Log::Message(Log::LT_INFO, "Loading Lua interpreter");
|
|
|
|
|
+ g_L = luaL_newstate();
|
|
|
|
|
+ luaL_openlibs(g_L);
|
|
|
|
|
+ owns_lua_state = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ owns_lua_state = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ RegisterCoreTypes(g_L);
|
|
|
|
|
+
|
|
|
|
|
+ lua_document_element_instancer = new LuaDocumentElementInstancer();
|
|
|
|
|
+ lua_event_listener_instancer = new LuaEventListenerInstancer();
|
|
|
|
|
+ Factory::RegisterElementInstancer("body", lua_document_element_instancer);
|
|
|
|
|
+ Factory::RegisterEventListenerInstancer(lua_event_listener_instancer);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Interpreter::OnShutdown()
|
|
void Interpreter::OnShutdown()
|
|
|
{
|
|
{
|
|
|
- delete g_lua_document_element_instancer;
|
|
|
|
|
- delete g_lua_event_listener_instancer;
|
|
|
|
|
- g_lua_document_element_instancer = nullptr;
|
|
|
|
|
- g_lua_event_listener_instancer = nullptr;
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ delete lua_document_element_instancer;
|
|
|
|
|
+ delete lua_event_listener_instancer;
|
|
|
|
|
+ lua_document_element_instancer = nullptr;
|
|
|
|
|
+ lua_event_listener_instancer = nullptr;
|
|
|
|
|
|
|
|
-void Interpreter::Initialise()
|
|
|
|
|
-{
|
|
|
|
|
- Rml::Core::Lua::Interpreter::Initialise(nullptr);
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ if (owns_lua_state)
|
|
|
|
|
+ lua_close(g_L);
|
|
|
|
|
+
|
|
|
|
|
+ g_L = nullptr;
|
|
|
|
|
|
|
|
-void Interpreter::Initialise(lua_State *luaStatePointer)
|
|
|
|
|
-{
|
|
|
|
|
- Interpreter *iPtr = new Interpreter();
|
|
|
|
|
- iPtr->_L = luaStatePointer;
|
|
|
|
|
- Rml::Core::RegisterPlugin(iPtr);
|
|
|
|
|
|
|
+ delete this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void Interpreter::Shutdown()
|
|
|
|
|
|
|
+void Interpreter::RegisterCoreTypes(lua_State* L)
|
|
|
{
|
|
{
|
|
|
- lua_close(_L);
|
|
|
|
|
|
|
+ LuaType<Vector2i>::Register(L);
|
|
|
|
|
+ LuaType<Vector2f>::Register(L);
|
|
|
|
|
+ LuaType<Colourf>::Register(L);
|
|
|
|
|
+ LuaType<Colourb>::Register(L);
|
|
|
|
|
+ LuaType<Log>::Register(L);
|
|
|
|
|
+ LuaType<ElementStyleProxy>::Register(L);
|
|
|
|
|
+ LuaType<Element>::Register(L);
|
|
|
|
|
+ //things that inherit from Element
|
|
|
|
|
+ LuaType<Document>::Register(L);
|
|
|
|
|
+ LuaType<ElementText>::Register(L);
|
|
|
|
|
+ LuaType<ElementPtr>::Register(L);
|
|
|
|
|
+ LuaType<Event>::Register(L);
|
|
|
|
|
+ LuaType<Context>::Register(L);
|
|
|
|
|
+ LuaType<LuaRmlUi>::Register(L);
|
|
|
|
|
+ LuaType<ElementInstancer>::Register(L);
|
|
|
|
|
+ //Proxy tables
|
|
|
|
|
+ LuaType<ContextDocumentsProxy>::Register(L);
|
|
|
|
|
+ LuaType<EventParametersProxy>::Register(L);
|
|
|
|
|
+ LuaType<ElementAttributesProxy>::Register(L);
|
|
|
|
|
+ LuaType<ElementChildNodesProxy>::Register(L);
|
|
|
|
|
+ LuaType<RmlUiContextsProxy>::Register(L);
|
|
|
|
|
+ OverrideLuaGlobalFunctions(L);
|
|
|
|
|
+ //push the global variable "rmlui" to use the "RmlUi" methods
|
|
|
|
|
+ LuaRmlUiPushrmluiGlobal(L);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|