mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
- Refactored Lua C++ bindings
- Removed tolua++
This commit is contained in:
@ -25,55 +25,55 @@
|
||||
** The constructor
|
||||
**
|
||||
*/
|
||||
LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state),
|
||||
m_iRef(LUA_NOREF),
|
||||
LuaScript::LuaScript(const char* file) :
|
||||
m_Ref(LUA_NOREF),
|
||||
m_Initialized(true)
|
||||
{
|
||||
int result = luaL_loadfile(m_State, file);
|
||||
int result = luaL_loadfile(LuaManager::GetState(), file);
|
||||
|
||||
// If the file loaded okay.
|
||||
if (result == 0)
|
||||
{
|
||||
// Create the table this script will reside in
|
||||
lua_newtable(m_State);
|
||||
lua_newtable(LuaManager::GetState());
|
||||
|
||||
// Create the metatable that will store the global table
|
||||
lua_createtable(m_State, 0, 1);
|
||||
lua_createtable(LuaManager::GetState(), 0, 1);
|
||||
|
||||
// Push the global teble
|
||||
lua_pushvalue(m_State, LUA_GLOBALSINDEX);
|
||||
lua_pushvalue(LuaManager::GetState(), LUA_GLOBALSINDEX);
|
||||
|
||||
// Set the __index of the table to be the global table
|
||||
lua_setfield(m_State, -2, "__index");
|
||||
lua_setfield(LuaManager::GetState(), -2, "__index");
|
||||
|
||||
// Set the metatable for the script's table
|
||||
lua_setmetatable(m_State, -2);
|
||||
lua_setmetatable(LuaManager::GetState(), -2);
|
||||
|
||||
// Put the table into the global table
|
||||
m_iRef = luaL_ref(m_State, LUA_GLOBALSINDEX);
|
||||
m_Ref = luaL_ref(LuaManager::GetState(), LUA_GLOBALSINDEX);
|
||||
|
||||
PushTable();
|
||||
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
|
||||
// Set the environment for the function to be run in to be the table that
|
||||
// has been created for the script/
|
||||
lua_setfenv(m_State, -2);
|
||||
lua_setfenv(LuaManager::GetState(), -2);
|
||||
|
||||
// Execute the Lua script
|
||||
result = lua_pcall(m_State, 0, 0, 0);
|
||||
result = lua_pcall(LuaManager::GetState(), 0, 0, 0);
|
||||
|
||||
if (result)
|
||||
{
|
||||
m_Initialized = false;
|
||||
LuaManager::ReportErrors(m_State);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
|
||||
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
||||
m_iRef = LUA_NOREF;
|
||||
luaL_unref(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
m_Ref = LUA_NOREF;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Initialized = false;
|
||||
LuaManager::ReportErrors(m_State);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
}
|
||||
}
|
||||
|
||||
@ -83,7 +83,7 @@ LuaScript::LuaScript(lua_State* state, const char* file) : m_State(state),
|
||||
*/
|
||||
LuaScript::~LuaScript()
|
||||
{
|
||||
luaL_unref(m_State, LUA_GLOBALSINDEX, m_iRef);
|
||||
luaL_unref(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -97,18 +97,18 @@ bool LuaScript::IsFunction(const char* funcName)
|
||||
if (m_Initialized)
|
||||
{
|
||||
// Push our table onto the stack
|
||||
PushTable();
|
||||
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
|
||||
// Push the function onto the stack
|
||||
lua_getfield(m_State, -1, funcName);
|
||||
lua_getfield(LuaManager::GetState(), -1, funcName);
|
||||
|
||||
if (lua_isfunction(m_State, -1))
|
||||
if (lua_isfunction(LuaManager::GetState(), -1))
|
||||
{
|
||||
bExists = true;
|
||||
}
|
||||
|
||||
// Pop both the table and the function off the stack.
|
||||
lua_pop(m_State, 2);
|
||||
lua_pop(LuaManager::GetState(), 2);
|
||||
}
|
||||
|
||||
return bExists;
|
||||
@ -123,17 +123,17 @@ void LuaScript::RunFunction(const char* funcName)
|
||||
if (m_Initialized)
|
||||
{
|
||||
// Push our table onto the stack
|
||||
PushTable();
|
||||
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
|
||||
// Push the function onto the stack
|
||||
lua_getfield(m_State, -1, funcName);
|
||||
lua_getfield(LuaManager::GetState(), -1, funcName);
|
||||
|
||||
if (lua_pcall(m_State, 0, 0, 0))
|
||||
if (lua_pcall(LuaManager::GetState(), 0, 0, 0))
|
||||
{
|
||||
LuaManager::ReportErrors(m_State);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
}
|
||||
|
||||
lua_pop(m_State, 1);
|
||||
lua_pop(LuaManager::GetState(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,31 +148,31 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std
|
||||
if (m_Initialized)
|
||||
{
|
||||
// Push our table onto the stack
|
||||
PushTable();
|
||||
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
|
||||
// Push the function onto the stack
|
||||
lua_getfield(m_State, -1, funcName);
|
||||
lua_getfield(LuaManager::GetState(), -1, funcName);
|
||||
|
||||
if (lua_pcall(m_State, 0, 1, 0))
|
||||
if (lua_pcall(LuaManager::GetState(), 0, 1, 0))
|
||||
{
|
||||
LuaManager::ReportErrors(m_State);
|
||||
lua_pop(m_State, 1);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
lua_pop(LuaManager::GetState(), 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
type = lua_type(m_State, -1);
|
||||
type = lua_type(LuaManager::GetState(), -1);
|
||||
if (type == LUA_TNUMBER)
|
||||
{
|
||||
numValue = lua_tonumber(m_State, -1);
|
||||
numValue = lua_tonumber(LuaManager::GetState(), -1);
|
||||
}
|
||||
else if (type == LUA_TSTRING)
|
||||
{
|
||||
const char* str = lua_tostring(m_State, -1);
|
||||
const char* str = lua_tostring(LuaManager::GetState(), -1);
|
||||
strValue = ConvertToWide(str);
|
||||
numValue = strtod(str, NULL);
|
||||
}
|
||||
|
||||
lua_pop(m_State, 2);
|
||||
lua_pop(LuaManager::GetState(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,20 +188,20 @@ void LuaScript::RunString(const char* str)
|
||||
if (m_Initialized)
|
||||
{
|
||||
// Load the string as a Lua chunk
|
||||
if (luaL_loadstring(m_State, str))
|
||||
if (luaL_loadstring(LuaManager::GetState(), str))
|
||||
{
|
||||
LuaManager::ReportErrors(m_State);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
}
|
||||
|
||||
// Push our table onto the stack
|
||||
PushTable();
|
||||
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_Ref);
|
||||
|
||||
// Pop table and set the environment of the loaded chunk to it
|
||||
lua_setfenv(m_State, -2);
|
||||
lua_setfenv(LuaManager::GetState(), -2);
|
||||
|
||||
if (lua_pcall(m_State, 0, 0, 0))
|
||||
if (lua_pcall(LuaManager::GetState(), 0, 0, 0))
|
||||
{
|
||||
LuaManager::ReportErrors(m_State);
|
||||
LuaManager::ReportErrors(LuaManager::GetState());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user