summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rw-r--r--source/LuaScript.cpp232
-rw-r--r--source/LuaScript.h36
-rw-r--r--source/LuaState.cpp2
-rw-r--r--source/WebAdmin.cpp8
4 files changed, 36 insertions, 242 deletions
diff --git a/source/LuaScript.cpp b/source/LuaScript.cpp
index 8d92c238f..f27d1793f 100644
--- a/source/LuaScript.cpp
+++ b/source/LuaScript.cpp
@@ -9,31 +9,16 @@
extern "C"
{
-#include "lualib.h"
+ #include "lualib.h"
}
-
#include "tolua++.h"
-#include "Bindings.h"
-#include "ManualBindings.h"
-
-// fwd: SQLite/lsqlite3.c
-extern "C"
-{
- LUALIB_API int luaopen_lsqlite3(lua_State * L);
-}
-
-// fwd: LuaExpat/lxplib.c:
-extern "C"
-{
- int luaopen_lxp(lua_State * L);
-}
cLuaScript::cLuaScript()
- : m_LuaState(NULL)
+ : m_LuaState("cLuaScript")
{
}
@@ -42,166 +27,45 @@ cLuaScript::cLuaScript()
-cLuaScript::~cLuaScript()
-{
- if( m_LuaState )
- {
- lua_close( m_LuaState );
- m_LuaState = 0;
- }
-}
-
-
-
-
-
void cLuaScript::Initialize()
{
// Check to see if this script has not been initialized before
- ASSERT(!m_LuaState);
+ ASSERT(!m_LuaState.IsValid());
// Create a Lua state and bind all libraries to it
- m_LuaState = lua_open();
- luaL_openlibs(m_LuaState);
- tolua_AllToLua_open(m_LuaState);
- ManualBindings::Bind(m_LuaState);
- luaopen_lsqlite3(m_LuaState);
- luaopen_lxp(m_LuaState);
+ m_LuaState.Create();
}
-bool cLuaScript::LoadFile( const char* a_FilePath )
+bool cLuaScript::LoadFile(const char * a_FilePath)
{
// Make sure the plugin is initialized
- ASSERT(m_LuaState);
-
- // Load the file into the Lua state
- int s = luaL_loadfile(m_LuaState, a_FilePath );
- if (ReportErrors(s))
- {
- return false;
- }
- return true;
-}
-
-
-
-
-
-bool cLuaScript::Execute()
-{
- // Make sure we got a Lua state
- ASSERT(m_LuaState);
-
- // Execute the script as it is right now
- int s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
- if( ReportErrors( s ) )
- {
- return false;
- }
- return true;
-}
-
-
+ ASSERT(m_LuaState.IsValid());
-
-
-bool cLuaScript::ReportErrors( int a_Status )
-{
- if (a_Status == 0)
- {
- // No error to report
- return false;
- }
-
- // Status was set to error so get the error from the Lua state and log it
- LOGERROR("LUA: %s", lua_tostring(m_LuaState, -1));
- lua_pop(m_LuaState, 1);
-
- // Return true to indicate that an error was returned
- return true;
+ return m_LuaState.LoadFile(a_FilePath);
}
-bool cLuaScript::LuaPushFunction( const char * a_FunctionName, bool a_bLogError /*= true*/ )
+bool cLuaScript::CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString)
{
- ASSERT(m_LuaState);
-
- // Find and push the function on the Lua stack
- lua_getglobal(m_LuaState, a_FunctionName);
-
- // Make sure we found a function
- if (!lua_isfunction(m_LuaState, -1))
- {
- if (a_bLogError)
- {
- LOGWARN("LUA: Could not find function %s()", a_FunctionName);
- }
-
- // Pop the pushed 'object' back
- lua_pop(m_LuaState, 1);
- return false;
- }
-
- // Successfully pushed a function to the Lua stack
- return true;
-}
-
-
-
-
-
-bool cLuaScript::LuaCallFunction( int a_NumArgs, int a_NumResults, const char * a_FunctionName )
-{
- ASSERT(m_LuaState);
-
- // Make sure there's a lua function on the stack
- ASSERT(lua_isfunction(m_LuaState, -a_NumArgs - 1));
-
- // Call the desired function
- int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);
-
- // Check for errors
- if (ReportErrors(s))
+ ASSERT(m_LuaState.IsValid());
+
+ m_LuaState.PushFunction("ShowPage");
+ m_LuaState.PushUserType(&a_WebAdmin, "cWebAdmin");
+ m_LuaState.PushUserType(&a_Request, "HTTPTemplateRequest");
+ if (!m_LuaState.CallFunction(1))
{
- LOGWARN("LUA: Error calling function %s()", a_FunctionName);
return false;
}
-
- // Successfully executed function
- return true;
-}
-
-
-
-
-
-bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString )
-{
- // Make sure we have the required things to call a function
- ASSERT(m_LuaState);
- ASSERT(a_Function);
-
- // Push the desired function on the stack
- if (!LuaPushFunction(a_Function))
- {
- return false;
- }
-
- if (!LuaCallFunction(0, 1, a_Function))
- {
- return false;
- }
-
if (lua_isstring(m_LuaState, -1))
{
- ReturnedString = tolua_tostring(m_LuaState, -1, "");
+ a_ReturnedString.assign(tolua_tostring(m_LuaState, -1, ""));
}
lua_pop(m_LuaState, 1);
return true;
@@ -210,69 +74,3 @@ bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString )
-
-bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString )
-{
- // Make sure we have the required things to call a function
- ASSERT(m_LuaState);
- ASSERT(a_Function);
-
- // Push the desired function on the stack
- if (!LuaPushFunction(a_Function))
- {
- return false;
- }
-
- tolua_pushusertype(m_LuaState, a_UserType.Object, a_UserType.ClassName);
-
- if (!LuaCallFunction(1, 1, a_Function))
- {
- return false;
- }
-
- if (lua_isstring(m_LuaState, -1))
- {
- ReturnedString = tolua_tostring(m_LuaState, -1, "");
- }
- lua_pop(m_LuaState, 1);
- return true;
-}
-
-
-
-
-
-bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString )
-{
- // Make sure we have the required things to call a function
- ASSERT(m_LuaState);
- ASSERT(a_Function);
-
- // Push the desired function on the stack
- if (!LuaPushFunction(a_Function))
- {
- return false;
- }
-
- tolua_pushusertype(m_LuaState, a_UserType1.Object, a_UserType1.ClassName);
- tolua_pushusertype(m_LuaState, a_UserType2.Object, a_UserType2.ClassName);
-
- if (!LuaCallFunction(2, 1, a_Function))
- {
- return false;
- }
-
- if (lua_isstring(m_LuaState, -1))
- {
- ReturnedString = tolua_tostring(m_LuaState, -1, "");
- }
- lua_pop(m_LuaState, 1);
- return true;
-}
-
-
-
-
-
-
-
diff --git a/source/LuaScript.h b/source/LuaScript.h
index f98b2e65b..6c224e115 100644
--- a/source/LuaScript.h
+++ b/source/LuaScript.h
@@ -9,12 +9,13 @@
#pragma once
-struct lua_State;
+#include "LuaState.h"
+/*
struct sLuaUsertype
{
sLuaUsertype(void* a_pObject, const char* a_pClassName) : Object(a_pObject), ClassName(a_pClassName) {}
@@ -22,6 +23,15 @@ struct sLuaUsertype
void* Object;
const char* ClassName;
} ;
+*/
+
+
+
+
+
+// fwd:
+class cWebAdmin;
+struct HTTPTemplateRequest;
@@ -30,32 +40,18 @@ struct sLuaUsertype
class cLuaScript
{
public:
- cLuaScript();
- ~cLuaScript();
+ cLuaScript(void);
/// Prepares a Lua state
- void Initialize();
+ void Initialize();
/// Load a Lua script on the given path
- bool LoadFile(const char* a_FilePath);
-
- /// Execute the loaded Lua script
- bool Execute();
+ bool LoadFile(const char * a_FilePath);
- /// Call a function on the Lua script. Put all overloads here
- bool CallFunction(const char* a_Function, AString& ReturnedString);
- bool CallFunction(const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString);
- bool CallFunction(const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString);
+ bool CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString);
protected:
- /// Reports an error in the log if a_Status is flagged as an error. Returns true when a_Status is flagged as error, returns false when no error occured.
- bool ReportErrors(int a_Status);
-
- /// Helper functions for calling functions in Lua
- bool LuaPushFunction(const char * a_FunctionName, bool a_bLogError = true);
- bool LuaCallFunction(int a_NumArgs, int a_NumResults, const char * a_FunctionName ); // a_FunctionName is only used for error messages, nothing else
-private:
- lua_State* m_LuaState;
+ cLuaState m_LuaState;
} ;
diff --git a/source/LuaState.cpp b/source/LuaState.cpp
index e60a0f80c..aa9d796c3 100644
--- a/source/LuaState.cpp
+++ b/source/LuaState.cpp
@@ -420,7 +420,7 @@ bool cLuaState::CallFunction(int a_NumResults)
int s = lua_pcall(m_LuaState, m_NumCurrentFunctionArgs, a_NumResults, 0);
if (ReportErrors(s))
{
- LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName);
+ LOGWARNING("Error in %s calling function %s()", m_SubsystemName.c_str(), m_CurrentFunctionName.c_str());
m_NumCurrentFunctionArgs = -1;
m_CurrentFunctionName.clear();
return false;
diff --git a/source/WebAdmin.cpp b/source/WebAdmin.cpp
index 3d04ce8f3..be7efa18d 100644
--- a/source/WebAdmin.cpp
+++ b/source/WebAdmin.cpp
@@ -178,7 +178,7 @@ void cWebAdmin::Request_Handler(webserver::http_request* r)
if (!bDontShowTemplate)
{
// New Lua web template
- bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallFunction("ShowPage", sLuaUsertype(WebAdmin, "cWebAdmin"), sLuaUsertype(&TemplateRequest, "HTTPTemplateRequest"), Template);
+ bLuaTemplateSuccessful = WebAdmin->m_pTemplate->CallShowPage(*WebAdmin, TemplateRequest, Template);
}
if (!bLuaTemplateSuccessful)
@@ -274,14 +274,14 @@ bool cWebAdmin::Init( int a_Port )
m_Port = a_Port;
m_IniFile = new cIniFile("webadmin.ini");
- if( m_IniFile->ReadFile() )
+ if (m_IniFile->ReadFile())
{
- m_Port = m_IniFile->GetValueI("WebAdmin", "Port", 8080 );
+ m_Port = m_IniFile->GetValueI("WebAdmin", "Port", 8080);
}
// Initialize the WebAdmin template script and load the file
m_pTemplate->Initialize();
- if (!m_pTemplate->LoadFile( FILE_IO_PREFIX "webadmin/template.lua") || !m_pTemplate->Execute())
+ if (!m_pTemplate->LoadFile(FILE_IO_PREFIX "webadmin/template.lua"))
{
LOGWARN("Could not load WebAdmin template.");
}