- Refactored Lua C++ bindings

- Removed tolua++
This commit is contained in:
Birunthan Mohanathas 2012-03-09 10:28:25 +00:00
parent cea569c0b6
commit 7767300379
18 changed files with 255 additions and 2659 deletions

View File

@ -783,11 +783,6 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Use</PrecompiledHeader>
<PrecompiledHeaderFile Condition="'$(Configuration)|$(Platform)'=='Release|x64'">../../StdAfx.h</PrecompiledHeaderFile>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_event.c" />
<ClCompile Include="lua\tolua++\tolua_is.c" />
<ClCompile Include="lua\tolua++\tolua_map.c" />
<ClCompile Include="lua\tolua++\tolua_push.c" />
<ClCompile Include="lua\tolua++\tolua_to.c" />
<ClCompile Include="lua\lua\lapi.c" />
<ClCompile Include="lua\lua\lauxlib.c" />
<ClCompile Include="lua\lua\lbaselib.c" />
@ -883,8 +878,6 @@
<ClInclude Include="lua\LuaScript.h" />
<ClInclude Include="lua\include\lua.hpp" />
<ClInclude Include="lua\lua\luaconf.h" />
<ClInclude Include="lua\include\tolua++.h" />
<ClInclude Include="lua\tolua++\tolua_event.h" />
<ClInclude Include="lua\lua\lapi.h" />
<ClInclude Include="lua\lua\lauxlib.h" />
<ClInclude Include="lua\lua\lcode.h" />

View File

@ -22,9 +22,6 @@
<Filter Include="Lua\Lua Headers">
<UniqueIdentifier>{45f9e20c-cd34-4e18-84f2-9090ee108656}</UniqueIdentifier>
</Filter>
<Filter Include="Lua\tolua">
<UniqueIdentifier>{b3b45d6d-b775-470e-9882-82a231d0b209}</UniqueIdentifier>
</Filter>
<Filter Include="Lua\Lua">
<UniqueIdentifier>{c8550e7b-009b-4e12-a55a-ed458d05bdad}</UniqueIdentifier>
</Filter>
@ -159,21 +156,6 @@
<ClCompile Include="lua\glue\LuaGlobal.cpp">
<Filter>Lua\glue</Filter>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_event.c">
<Filter>Lua\tolua</Filter>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_is.c">
<Filter>Lua\tolua</Filter>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_map.c">
<Filter>Lua\tolua</Filter>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_push.c">
<Filter>Lua\tolua</Filter>
</ClCompile>
<ClCompile Include="lua\tolua++\tolua_to.c">
<Filter>Lua\tolua</Filter>
</ClCompile>
<ClCompile Include="lua\lua\lapi.c">
<Filter>Lua\Lua</Filter>
</ClCompile>
@ -476,12 +458,6 @@
<ClInclude Include="lua\lua\luaconf.h">
<Filter>Lua\Lua Headers</Filter>
</ClInclude>
<ClInclude Include="lua\include\tolua++.h">
<Filter>Lua\tolua</Filter>
</ClInclude>
<ClInclude Include="lua\tolua++\tolua_event.h">
<Filter>Lua\tolua</Filter>
</ClInclude>
<ClInclude Include="lua\lua\lapi.h">
<Filter>Lua\Lua</Filter>
</ClInclude>

View File

@ -139,7 +139,7 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
lua_State* L = LuaManager::GetState();
m_ScriptFile = scriptFile;
m_LuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str());
m_LuaScript = new LuaScript(m_ScriptFile.c_str());
if (m_LuaScript->IsInitialized())
{
@ -152,25 +152,25 @@ void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
LogWithArgs(LOG_WARNING, L"Script: Using deprecated GetStringValue() in [%s]", m_Name.c_str());
}
m_LuaScript->PushTable();
lua_rawgeti(LuaManager::GetState(), LUA_GLOBALSINDEX, m_LuaScript->GetRef());
// Push the variable name we want to put a value in.
lua_pushstring(L, "SELF");
// Push the value
tolua_pushusertype(L, this, "CMeasure");
// Bind the variable
lua_settable(L, -3);
*(CMeterWindow**)lua_newuserdata(L, sizeof(CMeterWindow*)) = m_MeterWindow;
lua_getglobal(L, "CMeterWindow");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "SKIN");
lua_pushstring(L, "SKIN");
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
lua_settable(L, -3);
*(CMeasure**)lua_newuserdata(L, sizeof(CMeasure*)) = this;
lua_getglobal(L, "CMeasure");
lua_setmetatable(L, -2);
lua_setfield(L, -2, "SELF");
// Look in the properties table for values to read from the section.
// For backwards compatibility
lua_getfield(L, -1, "PROPERTIES");
if (lua_isnil(L, -1) == 0)
{
lua_pushnil(L);
// Look in the table for values to read from the section
while (lua_next(L, -2))
{
lua_pop(L, 1);

View File

@ -33,18 +33,12 @@ void LuaManager::Initialize()
// Load Lua base libraries
luaL_openlibs(c_State);
// Initialize tolua
tolua_open(c_State);
// Register custom types and functions
tolua_module(c_State, NULL, 0);
tolua_beginmodule(c_State, NULL);
RegisterGlobal(c_State);
RegisterMeasure(c_State);
RegisterMeasure(c_State);
RegisterMeter(c_State);
RegisterMeterWindow(c_State);
tolua_endmodule(c_State);
}
++c_RefCount;

View File

@ -20,7 +20,6 @@
#define __LUAMANAGER_H__
#include "lua.hpp"
#include "tolua++.h"
class LuaManager
{

View File

@ -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());
}
}
}

View File

@ -20,17 +20,16 @@
#define __LUASCRIPT_H__
#include "lua.hpp"
#include "LuaManager.h"
class LuaScript
{
public:
LuaScript(lua_State* state, const char* file);
LuaScript(const char* file);
~LuaScript();
bool IsInitialized() { return m_Initialized; }
lua_State* GetState() { return m_State; }
void PushTable() { lua_rawgeti(m_State, LUA_GLOBALSINDEX, m_iRef); }
int GetRef() { return m_Ref; }
bool IsFunction(const char* funcName);
void RunFunction(const char* funcName);
@ -38,9 +37,7 @@ public:
void RunString(const char* str);
protected:
lua_State* m_State;
int m_iRef;
int m_Ref;
bool m_Initialized;
};

View File

@ -20,7 +20,7 @@
#include "../LuaManager.h"
#include "../../Litestep.h"
static int Global_Log(lua_State* L)
static int Global_Print(lua_State* L)
{
// Modified version of luaB_print()
std::string message;
@ -57,14 +57,7 @@ static int Global_Log(lua_State* L)
return 0;
}
static const luaL_reg TO_funcs[] =
{
{ "LuaLog", Global_Log },
{ NULL, NULL }
};
void LuaManager::RegisterGlobal(lua_State* L)
{
lua_register(L, "print", Global_Log);
luaL_register(L, "TO", TO_funcs); // For backwards compatibility
lua_register(L, "print", Global_Print);
}

View File

@ -21,18 +21,23 @@
#include "../../Measure.h"
#include "../../MeterWindow.h"
static int Measure_GetName(lua_State* L)
inline CMeasure* GetSelf(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
return *(CMeasure**)lua_touserdata(L, 1);
}
static int GetName(lua_State* L)
{
CMeasure* self = GetSelf(L);
const WCHAR* val = (const WCHAR*)self->GetName();
LuaManager::PushWide(L, val);
return 1;
}
static int Measure_GetOption(lua_State* L)
static int GetOption(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
CMeterWindow* meterWindow = self->GetMeterWindow();
CConfigParser& parser = meterWindow->GetParser();
@ -43,9 +48,9 @@ static int Measure_GetOption(lua_State* L)
return 1;
}
static int Measure_ReadString(lua_State* L)
static int ReadString(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
CMeterWindow* meterWindow = self->GetMeterWindow();
CConfigParser& parser = meterWindow->GetParser();
@ -56,87 +61,89 @@ static int Measure_ReadString(lua_State* L)
return 1;
}
static int Measure_ReadNumber(lua_State* L)
static int ReadNumber(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
CMeterWindow* meterWindow = self->GetMeterWindow();
CConfigParser& parser = meterWindow->GetParser();
std::wstring strTmp = LuaManager::ToWide(L, 2);
double value = parser.ReadFormula(self->GetName(), strTmp.c_str(), tolua_tonumber(L, 3, 0));
double value = parser.ReadFormula(self->GetName(), strTmp.c_str(), lua_tonumber(L, 3));
lua_pushnumber(L, value);
return 1;
}
static int Measure_Disable(lua_State* L)
static int Disable(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
self->Disable();
return 0;
}
static int Measure_Enable(lua_State* L)
static int Enable(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
self->Enable();
return 0;
}
static int Measure_GetValue(lua_State* L)
static int GetValue(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
double val = (double)self->GetValue();
lua_pushnumber(L, (lua_Number)val);
return 1;
}
static int Measure_GetRelativeValue(lua_State* L)
static int GetRelativeValue(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
double val = (double)self->GetRelativeValue();
lua_pushnumber(L, (lua_Number)val);
return 1;
}
static int Measure_GetValueRange(lua_State* L)
static int GetValueRange(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
double val = (double)self->GetValueRange();
lua_pushnumber(L, (lua_Number)val);
return 1;
}
static int Measure_GetMinValue(lua_State* L)
static int GetMinValue(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
double val = (double)self->GetMinValue();
lua_pushnumber(L, (lua_Number)val);
return 1;
}
static int Measure_GetMaxValue(lua_State* L)
static int GetMaxValue(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
CMeasure* self = GetSelf(L);
double val = (double)self->GetMaxValue();
lua_pushnumber(L, (lua_Number)val);
return 1;
}
static int Measure_GetStringValue(lua_State* L)
static int GetStringValue(lua_State* L)
{
CMeasure* self = (CMeasure*)tolua_tousertype(L, 1, 0);
AUTOSCALE autoScale = ((AUTOSCALE)(int)tolua_tonumber(L, 2, AUTOSCALE_OFF));
double scale = (double)tolua_tonumber(L, 3, 1.0);
int decimals = (int)tolua_tonumber(L, 4, 0);
bool percentual = (bool)tolua_toboolean(L, 5, false);
CMeasure* self = GetSelf(L);
int top = lua_gettop(L);
AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF;
double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0;
int decimals = (int)lua_tonumber(L, 4);
bool percentual = lua_toboolean(L, 5);
const WCHAR* val = self->GetStringValue(autoScale, scale, decimals, percentual);
LuaManager::PushWide(L, val);
@ -146,21 +153,24 @@ static int Measure_GetStringValue(lua_State* L)
void LuaManager::RegisterMeasure(lua_State* L)
{
tolua_usertype(L, "CMeasure");
tolua_cclass(L, "CMeasure", "CMeasure", "", NULL);
const luaL_reg functions[] =
{
{ "GetName", GetName },
{ "GetOption", GetOption },
{ "ReadString", ReadString },
{ "ReadNumber", ReadNumber },
{ "Disable", Disable },
{ "Enable", Enable },
{ "GetValue", GetValue },
{ "GetRelativeValue", GetRelativeValue },
{ "GetValueRange", GetValueRange },
{ "GetMinValue", GetMinValue },
{ "GetMaxValue", GetMaxValue },
{ "GetStringValue", GetStringValue },
{ NULL, NULL }
};
tolua_beginmodule(L, "CMeasure");
tolua_function(L, "GetName", Measure_GetName);
tolua_function(L, "GetOption", Measure_GetOption);
tolua_function(L, "ReadString", Measure_ReadString);
tolua_function(L, "ReadNumber", Measure_ReadNumber);
tolua_function(L, "Disable", Measure_Disable);
tolua_function(L, "Enable", Measure_Enable);
tolua_function(L, "GetValue", Measure_GetValue);
tolua_function(L, "GetRelativeValue", Measure_GetRelativeValue);
tolua_function(L, "GetValueRange", Measure_GetValueRange);
tolua_function(L, "GetMinValue", Measure_GetMinValue);
tolua_function(L, "GetMaxValue", Measure_GetMaxValue);
tolua_function(L, "GetStringValue", Measure_GetStringValue);
tolua_endmodule(L);
luaL_register(L, "CMeasure", functions);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}

View File

@ -21,18 +21,23 @@
#include "../../Meter.h"
#include "../../MeterString.h"
static int Meter_GetName(lua_State* L)
inline CMeter* GetSelf(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
return *(CMeter**)lua_touserdata(L, 1);
}
static int GetName(lua_State* L)
{
CMeter* self = GetSelf(L);
const WCHAR* val = (const WCHAR*)self->GetName();
LuaManager::PushWide(L, val);
return 1;
}
static int Meter_GetOption(lua_State* L)
static int GetOption(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
CMeter* self = GetSelf(L);
CMeterWindow* meterWindow = self->GetMeterWindow();
CConfigParser& parser = meterWindow->GetParser();
@ -43,100 +48,96 @@ static int Meter_GetOption(lua_State* L)
return 1;
}
static int Meter_GetW(lua_State* L)
static int GetW(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetW();
lua_pushnumber(L, (lua_Number)val);
CMeter* self = GetSelf(L);
lua_pushnumber(L, self->GetW());
return 1;
}
static int Meter_GetH(lua_State* L)
static int GetH(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetH();
lua_pushnumber(L, (lua_Number)val);
CMeter* self = GetSelf(L);
lua_pushnumber(L, self->GetH());
return 1;
}
static int Meter_GetX(lua_State* L)
static int GetX(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
bool abs = ((bool)tolua_toboolean(L, 2, false));
int val = (int)self->GetX(abs);
lua_pushnumber(L, (lua_Number)val);
CMeter* self = GetSelf(L);
bool abs = (bool)lua_toboolean(L, 2);
lua_pushnumber(L, self->GetX(abs));
return 1;
}
static int Meter_GetY(lua_State* L)
static int GetY(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
bool abs = ((bool)tolua_toboolean(L, 2, false));
int val = (int)self->GetY(abs);
lua_pushnumber(L, (lua_Number)val);
CMeter* self = GetSelf(L);
bool abs = (bool)lua_toboolean(L, 2);
lua_pushnumber(L, self->GetY(abs));
return 1;
}
static int Meter_SetW(lua_State* L)
static int SetW(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int w = (int)tolua_tonumber(L, 2, 0);
CMeter* self = GetSelf(L);
int w = (int)lua_tonumber(L, 2);
self->SetW(w);
return 0;
}
static int Meter_SetH(lua_State* L)
static int SetH(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int h = (int)tolua_tonumber(L, 2, 0);
CMeter* self = GetSelf(L);
int h = (int)lua_tonumber(L, 2);
self->SetH(h);
return 0;
}
static int Meter_SetX(lua_State* L)
static int SetX(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int x = (int)tolua_tonumber(L, 2, 0);
CMeter* self = GetSelf(L);
int x = (int)lua_tonumber(L, 2);
self->SetX(x);
return 0;
}
static int Meter_SetY(lua_State* L)
static int SetY(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
int y = (int)tolua_tonumber(L, 2, 0);
CMeter* self = GetSelf(L);
int y = (int)lua_tonumber(L, 2);
self->SetY(y);
return 0;
}
static int Meter_Hide(lua_State* L)
static int Hide(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
CMeter* self = GetSelf(L);
self->Hide();
return 0;
}
static int Meter_Show(lua_State* L)
static int Show(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
CMeter* self = GetSelf(L);
self->Show();
return 0;
}
static int Meter_SetText(lua_State* L)
static int SetText(lua_State* L)
{
CMeter* self = (CMeter*)tolua_tousertype(L, 1, 0);
CMeter* self = GetSelf(L);
if (CMeterString* stringMeter = dynamic_cast<CMeterString*>(self))
{
@ -149,22 +150,25 @@ static int Meter_SetText(lua_State* L)
void LuaManager::RegisterMeter(lua_State* L)
{
tolua_usertype(L, "CMeter");
tolua_cclass(L, "CMeter", "CMeter", "", NULL);
const luaL_reg functions[] =
{
{ "GetName", GetName },
{ "GetOption", GetOption },
{ "GetW", GetW },
{ "GetH", GetH },
{ "GetX", GetX },
{ "GetY", GetY },
{ "SetW", SetW },
{ "SetH", SetH },
{ "SetX", SetX },
{ "SetY", SetY },
{ "Hide", Hide },
{ "Show", Show },
{ "SetText", SetText },
{ NULL, NULL }
};
tolua_beginmodule(L, "CMeter");
tolua_function(L, "GetName", Meter_GetName);
tolua_function(L, "GetOption", Meter_GetOption);
tolua_function(L, "GetW", Meter_GetW);
tolua_function(L, "GetH", Meter_GetH);
tolua_function(L, "GetX", Meter_GetX);
tolua_function(L, "GetY", Meter_GetY);
tolua_function(L, "SetW", Meter_SetW);
tolua_function(L, "SetH", Meter_SetH);
tolua_function(L, "SetX", Meter_SetX);
tolua_function(L, "SetY", Meter_SetY);
tolua_function(L, "Hide", Meter_Hide);
tolua_function(L, "Show", Meter_Show);
tolua_function(L, "SetText", Meter_SetText);
tolua_endmodule(L);
luaL_register(L, "CMeter", functions);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}

View File

@ -22,51 +22,62 @@
#include "../../MeterWindow.h"
#include "../../MeterString.h"
static int MeterWindow_Bang(lua_State* L)
extern CRainmeter* Rainmeter;
inline CMeterWindow* GetSelf(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
return *(CMeterWindow**)lua_touserdata(L, 1);
}
static int Bang(lua_State* L)
{
CMeterWindow* self = GetSelf(L);
std::wstring strTmp = LuaManager::ToWide(L, 2);
CConfigParser& parser = self->GetParser();
parser.ReplaceVariables(strTmp);
self->GetMainObject()->ExecuteCommand(strTmp.c_str(), self);
Rainmeter->ExecuteCommand(strTmp.c_str(), self);
return 0;
}
static int MeterWindow_GetMeter(lua_State* L)
static int GetMeter(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = GetSelf(L);
const std::wstring meterName = LuaManager::ToWide(L, 2);
CMeter* meter = self->GetMeter(meterName);
if (!meter)
if (meter)
{
std::wstring error = L"Script: No such meter as ";
error += meterName;
Log(LOG_ERROR, error.c_str());
return 0;
*(CMeter**)lua_newuserdata(L, sizeof(CMeter*)) = meter;
lua_getglobal(L, "CMeter");
lua_setmetatable(L, -2);
return 1;
}
tolua_pushusertype(L, meter, "CMeter");
return 1;
return 0;
}
static int MeterWindow_GetMeasure(lua_State* L)
static int GetMeasure(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = GetSelf(L);
const std::wstring measureName = LuaManager::ToWide(L, 2);
CMeasure* val = self->GetMeasure(measureName);
tolua_pushusertype(L, (void*)val, "CMeasure");
CMeasure* measure = self->GetMeasure(measureName);
if (measure)
{
*(CMeasure**)lua_newuserdata(L, sizeof(CMeasure*)) = measure;
lua_getglobal(L, "CMeasure");
lua_setmetatable(L, -2);
return 1;
}
return 1;
return 0;
}
static int MeterWindow_GetVariable(lua_State* L)
static int GetVariable(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = GetSelf(L);
std::wstring strTmp = LuaManager::ToWide(L, 2);
if (self->GetParser().GetVariable(strTmp, strTmp))
@ -74,15 +85,13 @@ static int MeterWindow_GetVariable(lua_State* L)
LuaManager::PushWide(L, strTmp.c_str());
return 1;
}
else
{
return 0;
}
return 0;
}
static int MeterWindow_ReplaceVariables(lua_State* L)
static int ReplaceVariables(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = GetSelf(L);
std::wstring strTmp = LuaManager::ToWide(L, 2);
self->GetParser().ReplaceVariables(strTmp);
@ -91,9 +100,9 @@ static int MeterWindow_ReplaceVariables(lua_State* L)
return 1;
}
static int MeterWindow_ParseFormula(lua_State* L)
static int ParseFormula(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = GetSelf(L);
std::wstring strTmp = LuaManager::ToWide(L, 2);
double result;
@ -106,68 +115,62 @@ static int MeterWindow_ParseFormula(lua_State* L)
return 0;
}
static int MeterWindow_MoveWindow(lua_State* L)
static int MoveWindow(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int x = (int)tolua_tonumber(L, 2, 0);
int y = (int)tolua_tonumber(L, 3, 0);
CMeterWindow* self = GetSelf(L);
int x = (int)lua_tonumber(L, 2);
int y = (int)lua_tonumber(L, 3);
self->MoveWindow(x, y);
return 0;
}
static int MeterWindow_FadeWindow(lua_State* L)
static int FadeWindow(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int from = (int)tolua_tonumber(L, 2, 0);
int to = (int)tolua_tonumber(L, 3, 0);
CMeterWindow* self = GetSelf(L);
int from = (int)lua_tonumber(L, 2);
int to = (int)lua_tonumber(L, 3);
self->FadeWindow(from, to);
return 0;
}
static int MeterWindow_GetW(lua_State* L)
static int GetW(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetW();
lua_pushnumber(L, (lua_Number)val);
CMeterWindow* self = GetSelf(L);
lua_pushnumber(L, self->GetW());
return 1;
}
static int MeterWindow_GetH(lua_State* L)
static int GetH(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetH();
lua_pushnumber(L, (lua_Number)val);
CMeterWindow* self = GetSelf(L);
lua_pushnumber(L, self->GetH());
return 1;
}
static int MeterWindow_GetX(lua_State* L)
static int GetX(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetX();
lua_pushnumber(L, (lua_Number)val);
CMeterWindow* self = GetSelf(L);
lua_pushnumber(L, self->GetX());
return 1;
}
static int MeterWindow_GetY(lua_State* L)
static int GetY(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
int val = (int)self->GetY();
lua_pushnumber(L, (lua_Number)val);
CMeterWindow* self = GetSelf(L);
lua_pushnumber(L, self->GetY());
return 1;
}
static int MeterWindow_MakePathAbsolute(lua_State* L)
static int MakePathAbsolute(lua_State* L)
{
CMeterWindow* self = (CMeterWindow*)tolua_tousertype(L, 1, 0);
CMeterWindow* self = *(CMeterWindow**)lua_touserdata(L, 1);
std::wstring path = LuaManager::ToWide(L, 2);
self->MakePathAbsolute(path);
LuaManager::PushWide(L, path.c_str());
@ -176,22 +179,25 @@ static int MeterWindow_MakePathAbsolute(lua_State* L)
void LuaManager::RegisterMeterWindow(lua_State* L)
{
tolua_usertype(L, "CMeterWindow");
tolua_cclass(L, "CMeterWindow", "CMeterWindow", "", NULL);
const luaL_reg functions[] =
{
{ "Bang", Bang },
{ "GetMeter", GetMeter },
{ "GetMeasure", GetMeasure },
{ "GetVariable", GetVariable },
{ "ReplaceVariables", ReplaceVariables },
{ "ParseFormula", ParseFormula },
{ "MoveWindow", MoveWindow },
{ "FadeWindow", FadeWindow },
{ "GetW", GetW },
{ "GetH", GetH },
{ "GetX", GetX },
{ "GetY", GetY },
{ "MakePathAbsolute", MakePathAbsolute },
{ NULL, NULL }
};
tolua_beginmodule(L, "CMeterWindow");
tolua_function(L, "Bang", MeterWindow_Bang);
tolua_function(L, "GetMeter", MeterWindow_GetMeter);
tolua_function(L, "GetMeasure", MeterWindow_GetMeasure);
tolua_function(L, "GetVariable", MeterWindow_GetVariable);
tolua_function(L, "ReplaceVariables", MeterWindow_ReplaceVariables);
tolua_function(L, "ParseFormula", MeterWindow_ParseFormula);
tolua_function(L, "MoveWindow", MeterWindow_MoveWindow);
tolua_function(L, "FadeWindow", MeterWindow_FadeWindow);
tolua_function(L, "GetW", MeterWindow_GetW);
tolua_function(L, "GetH", MeterWindow_GetH);
tolua_function(L, "GetX", MeterWindow_GetX);
tolua_function(L, "GetY", MeterWindow_GetY);
tolua_function(L, "MakePathAbsolute", MeterWindow_MakePathAbsolute);
tolua_endmodule(L);
luaL_register(L, "CMeterWindow", functions);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}

View File

@ -1,186 +0,0 @@
/* tolua
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#ifndef TOLUA_H
#define TOLUA_H
#ifndef TOLUA_API
#define TOLUA_API extern
#endif
#define TOLUA_VERSION "tolua++-1.0.92"
#ifdef __cplusplus
extern "C" {
#endif
#define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str())
#define tolua_iscppstring tolua_isstring
#define tolua_iscppstringarray tolua_isstringarray
#define tolua_pushfieldcppstring(L,lo,idx,s) tolua_pushfieldstring(L, lo, idx, s.c_str())
#ifndef TEMPLATE_BIND
#define TEMPLATE_BIND(p)
#endif
#define TOLUA_TEMPLATE_BIND(p)
#define TOLUA_PROTECTED_DESTRUCTOR
#define TOLUA_PROPERTY_TYPE(p)
typedef int lua_Object;
#include "lua.h"
#include "lauxlib.h"
struct tolua_Error
{
int index;
int array;
const char* type;
};
typedef struct tolua_Error tolua_Error;
#define TOLUA_NOPEER LUA_REGISTRYINDEX /* for lua 5.1 */
TOLUA_API const char* tolua_typename (lua_State* L, int lo);
TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err);
TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err);
TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isvaluenil (lua_State* L, int lo, tolua_Error* err);
TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err);
TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err);
TOLUA_API int tolua_isvaluearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isbooleanarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isnumberarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isstringarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_istablearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isuserdataarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err);
TOLUA_API int tolua_isusertypearray
(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err);
TOLUA_API void tolua_open (lua_State* L);
TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size);
TOLUA_API int tolua_register_gc (lua_State* L, int lo);
TOLUA_API int tolua_default_collect (lua_State* tolua_S);
TOLUA_API void tolua_usertype (lua_State* L, const char* type);
TOLUA_API void tolua_beginmodule (lua_State* L, const char* name);
TOLUA_API void tolua_endmodule (lua_State* L);
TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar);
TOLUA_API void tolua_class (lua_State* L, const char* name, const char* base);
TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col);
TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func);
TOLUA_API void tolua_constant (lua_State* L, const char* name, lua_Number value);
TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set);
TOLUA_API void tolua_array (lua_State* L,const char* name, lua_CFunction get, lua_CFunction set);
/* TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type); */
/* TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base); */
TOLUA_API void tolua_pushvalue (lua_State* L, int lo);
TOLUA_API void tolua_pushboolean (lua_State* L, int value);
TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value);
TOLUA_API void tolua_pushstring (lua_State* L, const char* value);
TOLUA_API void tolua_pushuserdata (lua_State* L, void* value);
TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type);
TOLUA_API void tolua_pushusertype_and_takeownership(lua_State* L, void* value, const char* type);
TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v);
TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v);
TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v);
TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v);
TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v);
TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type);
TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type);
TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def);
TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def);
TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def);
TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def);
TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def);
TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def);
TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def);
TOLUA_API const char* tolua_tofieldstring (lua_State* L, int lo, int index, const char* def);
TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def);
TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def);
TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def);
TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def);
TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name);
TOLUA_API int class_gc_event (lua_State* L);
#ifdef __cplusplus
static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def) {
const char* s = tolua_tostring(L, narg, def);
return s?s:"";
};
static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def) {
const char* s = tolua_tofieldstring(L, lo, index, def);
return s?s:"";
};
#else
#define tolua_tocppstring tolua_tostring
#define tolua_tofieldcppstring tolua_tofieldstring
#endif
TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index);
#ifndef Mtolua_new
#define Mtolua_new(EXP) new EXP
#endif
#ifndef Mtolua_delete
#define Mtolua_delete(EXP) delete EXP
#endif
#ifndef Mtolua_new_dim
#define Mtolua_new_dim(EXP, len) new EXP[len]
#endif
#ifndef Mtolua_delete_dim
#define Mtolua_delete_dim(EXP) delete [] EXP
#endif
#ifndef tolua_outside
#define tolua_outside
#endif
#ifndef tolua_owned
#define tolua_owned
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,536 +0,0 @@
/* tolua: event functions
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include <stdio.h>
#include "tolua++.h"
/* Store at ubox
* It stores, creating the corresponding table if needed,
* the pair key/value in the corresponding ubox table
*/
static void storeatubox (lua_State* L, int lo)
{
#ifdef LUA_VERSION_NUM
lua_getfenv(L, lo);
if (lua_rawequal(L, -1, TOLUA_NOPEER)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfenv(L, lo); /* stack: k,v,table */
};
lua_insert(L, -3);
lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */
lua_pop(L, 1);
#else
/* stack: key value (to be stored) */
lua_pushstring(L,"tolua_peers");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */
lua_pushvalue(L,lo);
lua_rawget(L,-2); /* stack: k v ubox ubox[u] */
if (!lua_istable(L,-1))
{
lua_pop(L,1); /* stack: k v ubox */
lua_newtable(L); /* stack: k v ubox table */
lua_pushvalue(L,1);
lua_pushvalue(L,-2); /* stack: k v ubox table u table */
lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */
}
lua_insert(L,-4); /* put table before k */
lua_pop(L,1); /* pop ubox */
lua_rawset(L,-3); /* store at table */
lua_pop(L,1); /* pop ubox[u] */
#endif
}
/* Module index function
*/
static int module_index_event (lua_State* L)
{
lua_pushstring(L,".get");
lua_rawget(L,-3);
if (lua_istable(L,-1))
{
lua_pushvalue(L,2); /* key */
lua_rawget(L,-2);
if (lua_iscfunction(L,-1))
{
lua_call(L,0,1);
return 1;
}
else if (lua_istable(L,-1))
return 1;
}
/* call old index meta event */
if (lua_getmetatable(L,1))
{
lua_pushstring(L,"__index");
lua_rawget(L,-2);
lua_pushvalue(L,1);
lua_pushvalue(L,2);
if (lua_isfunction(L,-1))
{
lua_call(L,2,1);
return 1;
}
else if (lua_istable(L,-1))
{
lua_gettable(L,-3);
return 1;
}
}
lua_pushnil(L);
return 1;
}
/* Module newindex function
*/
static int module_newindex_event (lua_State* L)
{
lua_pushstring(L,".set");
lua_rawget(L,-4);
if (lua_istable(L,-1))
{
lua_pushvalue(L,2); /* key */
lua_rawget(L,-2);
if (lua_iscfunction(L,-1))
{
lua_pushvalue(L,1); /* only to be compatible with non-static vars */
lua_pushvalue(L,3); /* value */
lua_call(L,2,0);
return 0;
}
}
/* call old newindex meta event */
if (lua_getmetatable(L,1) && lua_getmetatable(L,-1))
{
lua_pushstring(L,"__newindex");
lua_rawget(L,-2);
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_pushvalue(L,3);
lua_call(L,3,0);
}
}
lua_settop(L,3);
lua_rawset(L,-3);
return 0;
}
/* Class index function
* If the object is a userdata (ie, an object), it searches the field in
* the alternative table stored in the corresponding "ubox" table.
*/
static int class_index_event (lua_State* L)
{
int t = lua_type(L,1);
if (t == LUA_TUSERDATA)
{
/* Access alternative table */
#ifdef LUA_VERSION_NUM /* new macro on version 5.1 */
lua_getfenv(L,1);
if (!lua_rawequal(L, -1, TOLUA_NOPEER)) {
lua_pushvalue(L, 2); /* key */
lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */
if (!lua_isnil(L, -1))
return 1;
};
#else
lua_pushstring(L,"tolua_peers");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */
lua_pushvalue(L,1);
lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */
if (lua_istable(L,-1))
{
lua_pushvalue(L,2); /* key */
lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */
if (!lua_isnil(L,-1))
return 1;
}
#endif
lua_settop(L,2); /* stack: obj key */
/* Try metatables */
lua_pushvalue(L,1); /* stack: obj key obj */
while (lua_getmetatable(L,-1))
{ /* stack: obj key obj mt */
lua_remove(L,-2); /* stack: obj key mt */
if (lua_isnumber(L,2)) /* check if key is a numeric value */
{
/* try operator[] */
lua_pushstring(L,".geti");
lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_call(L,2,1);
return 1;
}
}
else
{
lua_pushvalue(L,2); /* stack: obj key mt key */
lua_rawget(L,-2); /* stack: obj key mt value */
if (!lua_isnil(L,-1))
return 1;
else
lua_pop(L,1);
/* try C/C++ variable */
lua_pushstring(L,".get");
lua_rawget(L,-2); /* stack: obj key mt tget */
if (lua_istable(L,-1))
{
lua_pushvalue(L,2);
lua_rawget(L,-2); /* stack: obj key mt value */
if (lua_iscfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_call(L,2,1);
return 1;
}
else if (lua_istable(L,-1))
{
/* deal with array: create table to be returned and cache it in ubox */
void* u = *((void**)lua_touserdata(L,1));
lua_newtable(L); /* stack: obj key mt value table */
lua_pushstring(L,".self");
lua_pushlightuserdata(L,u);
lua_rawset(L,-3); /* store usertype in ".self" */
lua_insert(L,-2); /* stack: obj key mt table value */
lua_setmetatable(L,-2); /* set stored value as metatable */
lua_pushvalue(L,-1); /* stack: obj key met table table */
lua_pushvalue(L,2); /* stack: obj key mt table table key */
lua_insert(L,-2); /* stack: obj key mt table key table */
storeatubox(L,1); /* stack: obj key mt table */
return 1;
}
}
}
lua_settop(L,3);
}
lua_pushnil(L);
return 1;
}
else if (t== LUA_TTABLE)
{
module_index_event(L);
return 1;
}
lua_pushnil(L);
return 1;
}
/* Newindex function
* It first searches for a C/C++ varaible to be set.
* Then, it either stores it in the alternative ubox table (in the case it is
* an object) or in the own table (that represents the class or module).
*/
static int class_newindex_event (lua_State* L)
{
int t = lua_type(L,1);
if (t == LUA_TUSERDATA)
{
/* Try accessing a C/C++ variable to be set */
lua_getmetatable(L,1);
while (lua_istable(L,-1)) /* stack: t k v mt */
{
if (lua_isnumber(L,2)) /* check if key is a numeric value */
{
/* try operator[] */
lua_pushstring(L,".seti");
lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_pushvalue(L,3);
lua_call(L,3,0);
return 0;
}
}
else
{
lua_pushstring(L,".set");
lua_rawget(L,-2); /* stack: t k v mt tset */
if (lua_istable(L,-1))
{
lua_pushvalue(L,2);
lua_rawget(L,-2); /* stack: t k v mt tset func */
if (lua_iscfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,3);
lua_call(L,2,0);
return 0;
}
lua_pop(L,1); /* stack: t k v mt tset */
}
lua_pop(L,1); /* stack: t k v mt */
if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */
lua_pushnil(L);
lua_remove(L,-2); /* stack: t k v mt */
}
}
lua_settop(L,3); /* stack: t k v */
/* then, store as a new field */
storeatubox(L,1);
}
else if (t== LUA_TTABLE)
{
module_newindex_event(L);
}
return 0;
}
static int class_call_event(lua_State* L) {
if (lua_istable(L, 1)) {
lua_pushstring(L, ".call");
lua_rawget(L, 1);
if (lua_isfunction(L, -1)) {
lua_insert(L, 1);
lua_call(L, lua_gettop(L)-1, 1);
return 1;
};
};
tolua_error(L,"Attempt to call a non-callable object.",NULL);
return 0;
};
static int do_operator (lua_State* L, const char* op)
{
if (lua_isuserdata(L,1))
{
/* Try metatables */
lua_pushvalue(L,1); /* stack: op1 op2 */
while (lua_getmetatable(L,-1))
{ /* stack: op1 op2 op1 mt */
lua_remove(L,-2); /* stack: op1 op2 mt */
lua_pushstring(L,op); /* stack: op1 op2 mt key */
lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_call(L,2,1);
return 1;
}
lua_settop(L,3);
}
}
tolua_error(L,"Attempt to perform operation on an invalid operand",NULL);
return 0;
}
static int class_add_event (lua_State* L)
{
return do_operator(L,".add");
}
static int class_sub_event (lua_State* L)
{
return do_operator(L,".sub");
}
static int class_mul_event (lua_State* L)
{
return do_operator(L,".mul");
}
static int class_div_event (lua_State* L)
{
return do_operator(L,".div");
}
static int class_lt_event (lua_State* L)
{
return do_operator(L,".lt");
}
static int class_le_event (lua_State* L)
{
return do_operator(L,".le");
}
static int class_eq_event (lua_State* L)
{
/* copying code from do_operator here to return false when no operator is found */
if (lua_isuserdata(L,1))
{
/* Try metatables */
lua_pushvalue(L,1); /* stack: op1 op2 */
while (lua_getmetatable(L,-1))
{ /* stack: op1 op2 op1 mt */
lua_remove(L,-2); /* stack: op1 op2 mt */
lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */
lua_rawget(L,-2); /* stack: obj key mt func */
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_pushvalue(L,2);
lua_call(L,2,1);
return 1;
}
lua_settop(L,3);
}
}
lua_settop(L, 3);
lua_pushboolean(L, 0);
return 1;
}
/*
static int class_gc_event (lua_State* L)
{
void* u = *((void**)lua_touserdata(L,1));
fprintf(stderr, "collecting: looking at %p\n", u);
lua_pushstring(L,"tolua_gc");
lua_rawget(L,LUA_REGISTRYINDEX);
lua_pushlightuserdata(L,u);
lua_rawget(L,-2);
if (lua_isfunction(L,-1))
{
lua_pushvalue(L,1);
lua_call(L,1,0);
lua_pushlightuserdata(L,u);
lua_pushnil(L);
lua_rawset(L,-3);
}
lua_pop(L,2);
return 0;
}
*/
TOLUA_API int class_gc_event (lua_State* L)
{
void* u = *((void**)lua_touserdata(L,1));
int top;
/*fprintf(stderr, "collecting: looking at %p\n", u);*/
/*
lua_pushstring(L,"tolua_gc");
lua_rawget(L,LUA_REGISTRYINDEX);
*/
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushlightuserdata(L,u);
lua_rawget(L,-2); /* stack: gc umt */
lua_getmetatable(L,1); /* stack: gc umt mt */
/*fprintf(stderr, "checking type\n");*/
top = lua_gettop(L);
if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */
{
/*fprintf(stderr, "Found type!\n");*/
/* get gc function */
lua_pushliteral(L,".collector");
lua_rawget(L,-2); /* stack: gc umt mt collector */
if (lua_isfunction(L,-1)) {
/*fprintf(stderr, "Found .collector!\n");*/
}
else {
lua_pop(L,1);
/*fprintf(stderr, "Using default cleanup\n");*/
lua_pushcfunction(L,tolua_default_collect);
}
lua_pushvalue(L,1); /* stack: gc umt mt collector u */
lua_call(L,1,0);
lua_pushlightuserdata(L,u); /* stack: gc umt mt u */
lua_pushnil(L); /* stack: gc umt mt u nil */
lua_rawset(L,-5); /* stack: gc umt mt */
}
lua_pop(L,3);
return 0;
}
/* Register module events
* It expects the metatable on the top of the stack
*/
TOLUA_API void tolua_moduleevents (lua_State* L)
{
lua_pushstring(L,"__index");
lua_pushcfunction(L,module_index_event);
lua_rawset(L,-3);
lua_pushstring(L,"__newindex");
lua_pushcfunction(L,module_newindex_event);
lua_rawset(L,-3);
}
/* Check if the object on the top has a module metatable
*/
TOLUA_API int tolua_ismodulemetatable (lua_State* L)
{
int r = 0;
if (lua_getmetatable(L,-1))
{
lua_pushstring(L,"__index");
lua_rawget(L,-2);
r = (lua_tocfunction(L,-1) == module_index_event);
lua_pop(L,2);
}
return r;
}
/* Register class events
* It expects the metatable on the top of the stack
*/
TOLUA_API void tolua_classevents (lua_State* L)
{
lua_pushstring(L,"__index");
lua_pushcfunction(L,class_index_event);
lua_rawset(L,-3);
lua_pushstring(L,"__newindex");
lua_pushcfunction(L,class_newindex_event);
lua_rawset(L,-3);
lua_pushstring(L,"__add");
lua_pushcfunction(L,class_add_event);
lua_rawset(L,-3);
lua_pushstring(L,"__sub");
lua_pushcfunction(L,class_sub_event);
lua_rawset(L,-3);
lua_pushstring(L,"__mul");
lua_pushcfunction(L,class_mul_event);
lua_rawset(L,-3);
lua_pushstring(L,"__div");
lua_pushcfunction(L,class_div_event);
lua_rawset(L,-3);
lua_pushstring(L,"__lt");
lua_pushcfunction(L,class_lt_event);
lua_rawset(L,-3);
lua_pushstring(L,"__le");
lua_pushcfunction(L,class_le_event);
lua_rawset(L,-3);
lua_pushstring(L,"__eq");
lua_pushcfunction(L,class_eq_event);
lua_rawset(L,-3);
lua_pushstring(L,"__call");
lua_pushcfunction(L,class_call_event);
lua_rawset(L,-3);
lua_pushstring(L,"__gc");
lua_pushstring(L, "tolua_gc_event");
lua_rawget(L, LUA_REGISTRYINDEX);
/*lua_pushcfunction(L,class_gc_event);*/
lua_rawset(L,-3);
}

View File

@ -1,24 +0,0 @@
/* tolua: event functions
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#ifndef TOLUA_EVENT_H
#define TOLUA_EVENT_H
#include "tolua++.h"
TOLUA_API void tolua_moduleevents (lua_State* L);
TOLUA_API int tolua_ismodulemetatable (lua_State* L);
TOLUA_API void tolua_classevents (lua_State* L);
#endif

View File

@ -1,622 +0,0 @@
/* tolua: functions to check types.
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include "tolua++.h"
#include "lauxlib.h"
#include <stdlib.h>
#include <string.h>
/* a fast check if a is b, without parameter validation
i.e. if b is equal to a or a superclass of a. */
TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index)
{
int result;
if (lua_rawequal(L,mt_indexa,mt_indexb))
result = 1;
else
{
if (super_index) {
lua_pushvalue(L, super_index);
} else {
lua_pushliteral(L,"tolua_super");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */
};
lua_pushvalue(L,mt_indexa); /* stack: super mta */
lua_rawget(L,-2); /* stack: super super[mta] */
lua_pushvalue(L,mt_indexb); /* stack: super super[mta] mtb */
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super super[mta] typenameB */
lua_rawget(L,-2); /* stack: super super[mta] bool */
result = lua_toboolean(L,-1);
lua_pop(L,3);
}
return result;
}
/* Push and returns the corresponding object typename */
TOLUA_API const char* tolua_typename (lua_State* L, int lo)
{
int tag = lua_type(L,lo);
if (tag == LUA_TNONE)
lua_pushstring(L,"[no object]");
else if (tag != LUA_TUSERDATA && tag != LUA_TTABLE)
lua_pushstring(L,lua_typename(L,tag));
else if (tag == LUA_TUSERDATA)
{
if (!lua_getmetatable(L,lo))
lua_pushstring(L,lua_typename(L,tag));
else
{
lua_rawget(L,LUA_REGISTRYINDEX);
if (!lua_isstring(L,-1))
{
lua_pop(L,1);
lua_pushstring(L,"[undefined]");
}
}
}
else /* is table */
{
lua_pushvalue(L,lo);
lua_rawget(L,LUA_REGISTRYINDEX);
if (!lua_isstring(L,-1))
{
lua_pop(L,1);
lua_pushstring(L,"table");
}
else
{
lua_pushstring(L,"class ");
lua_insert(L,-2);
lua_concat(L,2);
}
}
return lua_tostring(L,-1);
}
TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err)
{
if (msg[0] == '#')
{
const char* expected = err->type;
const char* provided = tolua_typename(L,err->index);
if (msg[1]=='f')
{
int narg = err->index;
if (err->array)
luaL_error(L,"%s\n argument #%d is array of '%s'; array of '%s' expected.\n",
msg+2,narg,provided,expected);
else
luaL_error(L,"%s\n argument #%d is '%s'; '%s' expected.\n",
msg+2,narg,provided,expected);
}
else if (msg[1]=='v')
{
if (err->array)
luaL_error(L,"%s\n value is array of '%s'; array of '%s' expected.\n",
msg+2,provided,expected);
else
luaL_error(L,"%s\n value is '%s'; '%s' expected.\n",
msg+2,provided,expected);
}
}
else
luaL_error(L,msg);
}
/* the equivalent of lua_is* for usertable */
static int lua_isusertable (lua_State* L, int lo, const const char* type)
{
int r = 0;
if (lo < 0) lo = lua_gettop(L)+lo+1;
lua_pushvalue(L,lo);
lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[t] */
if (lua_isstring(L,-1))
{
r = strcmp(lua_tostring(L,-1),type)==0;
if (!r)
{
/* try const */
lua_pushstring(L,"const ");
lua_insert(L,-2);
lua_concat(L,2);
r = lua_isstring(L,-1) && strcmp(lua_tostring(L,-1),type)==0;
}
}
lua_pop(L, 1);
return r;
}
int push_table_instance(lua_State* L, int lo) {
if (lua_istable(L, lo)) {
lua_pushstring(L, ".c_instance");
lua_gettable(L, lo);
if (lua_isuserdata(L, -1)) {
lua_replace(L, lo);
return 1;
} else {
lua_pop(L, 1);
return 0;
};
} else {
return 0;
};
return 0;
};
/* the equivalent of lua_is* for usertype */
static int lua_isusertype (lua_State* L, int lo, const char* type)
{
if (!lua_isuserdata(L,lo)) {
if (!push_table_instance(L, lo)) {
return 0;
};
};
{
/* check if it is of the same type */
int r;
const char *tn;
if (lua_getmetatable(L,lo)) /* if metatable? */
{
lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[mt] */
tn = lua_tostring(L,-1);
r = tn && (strcmp(tn,type) == 0);
lua_pop(L, 1);
if (r)
return 1;
else
{
/* check if it is a specialized class */
lua_pushstring(L,"tolua_super");
lua_rawget(L,LUA_REGISTRYINDEX); /* get super */
lua_getmetatable(L,lo);
lua_rawget(L,-2); /* get super[mt] */
if (lua_istable(L,-1))
{
int b;
lua_pushstring(L,type);
lua_rawget(L,-2); /* get super[mt][type] */
b = lua_toboolean(L,-1);
lua_pop(L,3);
if (b)
return 1;
}
}
}
}
return 0;
}
TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err)
{
if (lua_gettop(L)<abs(lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "[no object]";
return 0;
}
TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isnil(L,lo) || lua_isboolean(L,lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "boolean";
return 0;
}
TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isnumber(L,lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "number";
return 0;
}
TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isnil(L,lo) || lua_isstring(L,lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "string";
return 0;
}
TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_istable(L,lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "table";
return 0;
}
TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isusertable(L,lo,type))
return 1;
err->index = lo;
err->array = 0;
err->type = type;
return 0;
}
TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isnil(L,lo) || lua_isuserdata(L,lo))
return 1;
err->index = lo;
err->array = 0;
err->type = "userdata";
return 0;
}
TOLUA_API int tolua_isvaluenil (lua_State* L, int lo, tolua_Error* err) {
if (lua_gettop(L)<abs(lo))
return 0; /* somebody else should chack this */
if (!lua_isnil(L, lo))
return 0;
err->index = lo;
err->array = 0;
err->type = "value";
return 1;
};
TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err)
{
if (def || abs(lo)<=lua_gettop(L)) /* any valid index */
return 1;
err->index = lo;
err->array = 0;
err->type = "value";
return 0;
}
TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err)
{
if (def && lua_gettop(L)<abs(lo))
return 1;
if (lua_isnil(L,lo) || lua_isusertype(L,lo,type))
return 1;
err->index = lo;
err->array = 0;
err->type = type;
return 0;
}
TOLUA_API int tolua_isvaluearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
return 1;
}
TOLUA_API int tolua_isbooleanarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "boolean";
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
TOLUA_API int tolua_isnumberarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!lua_isnumber(L,-1) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "number";
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
TOLUA_API int tolua_isstringarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "string";
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
TOLUA_API int tolua_istablearray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (! lua_istable(L,-1) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "table";
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
TOLUA_API int tolua_isuserdataarray
(lua_State* L, int lo, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "userdata";
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
TOLUA_API int tolua_isusertypearray
(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err)
{
if (!tolua_istable(L,lo,def,err))
return 0;
else
{
int i;
for (i=1; i<=dim; ++i)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->type = type;
err->array = 1;
return 0;
}
lua_pop(L,1);
}
}
return 1;
}
#if 0
int tolua_isbooleanfield
(lua_State* L, int lo, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "boolean";
return 0;
}
lua_pop(L,1);
return 1;
}
int tolua_isnumberfield
(lua_State* L, int lo, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!lua_isnumber(L,-1) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "number";
return 0;
}
lua_pop(L,1);
return 1;
}
int tolua_isstringfield
(lua_State* L, int lo, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "string";
return 0;
}
lua_pop(L,1);
return 1;
}
int tolua_istablefield
(lua_State* L, int lo, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i+1);
lua_gettable(L,lo);
if (! lua_istable(L,-1) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "table";
return 0;
}
lua_pop(L,1);
}
int tolua_isusertablefield
(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (! lua_isusertable(L,-1,type) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = type;
return 0;
}
lua_pop(L,1);
return 1;
}
int tolua_isuserdatafield
(lua_State* L, int lo, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->array = 1;
err->type = "userdata";
return 0;
}
lua_pop(L,1);
return 1;
}
int tolua_isusertypefield
(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err)
{
lua_pushnumber(L,i);
lua_gettable(L,lo);
if (!(lua_isnil(L,-1) || lua_isusertype(L,-1,type)) &&
!(def && lua_isnil(L,-1))
)
{
err->index = lo;
err->type = type;
err->array = 1;
return 0;
}
lua_pop(L,1);
return 1;
}
#endif

View File

@ -1,704 +0,0 @@
/* tolua: functions to map features
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include "tolua++.h"
#include "tolua_event.h"
#include "lauxlib.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Create metatable
* Create and register new metatable
*/
static int tolua_newmetatable (lua_State* L, char* name)
{
int r = luaL_newmetatable(L,name);
#ifdef LUA_VERSION_NUM /* only lua 5.1 */
if (r) {
lua_pushvalue(L, -1);
lua_pushstring(L, name);
lua_settable(L, LUA_REGISTRYINDEX); /* reg[mt] = type_name */
};
#endif
if (r)
tolua_classevents(L); /* set meta events */
lua_pop(L,1);
return r;
}
/* Map super classes
* It sets 'name' as being also a 'base', mapping all super classes of 'base' in 'name'
*/
static void mapsuper (lua_State* L, const char* name, const char* base)
{
/* push registry.super */
lua_pushstring(L,"tolua_super");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */
luaL_getmetatable(L,name); /* stack: super mt */
lua_rawget(L,-2); /* stack: super table */
if (lua_isnil(L,-1))
{
/* create table */
lua_pop(L,1);
lua_newtable(L); /* stack: super table */
luaL_getmetatable(L,name); /* stack: super table mt */
lua_pushvalue(L,-2); /* stack: super table mt table */
lua_rawset(L,-4); /* stack: super table */
}
/* set base as super class */
lua_pushstring(L,base);
lua_pushboolean(L,1);
lua_rawset(L,-3); /* stack: super table */
/* set all super class of base as super class of name */
luaL_getmetatable(L,base); /* stack: super table base_mt */
lua_rawget(L,-3); /* stack: super table base_table */
if (lua_istable(L,-1))
{
/* traverse base table */
lua_pushnil(L); /* first key */
while (lua_next(L,-2) != 0)
{
/* stack: ... base_table key value */
lua_pushvalue(L,-2); /* stack: ... base_table key value key */
lua_insert(L,-2); /* stack: ... base_table key key value */
lua_rawset(L,-5); /* stack: ... base_table key */
}
}
lua_pop(L,3); /* stack: <empty> */
}
/* creates a 'tolua_ubox' table for base clases, and
// expects the metatable and base metatable on the stack */
static void set_ubox(lua_State* L) {
/* mt basemt */
if (!lua_isnil(L, -1)) {
lua_pushstring(L, "tolua_ubox");
lua_rawget(L,-2);
} else {
lua_pushnil(L);
};
/* mt basemt base_ubox */
if (!lua_isnil(L,-1)) {
lua_pushstring(L, "tolua_ubox");
lua_insert(L, -2);
/* mt basemt key ubox */
lua_rawset(L,-4);
/* (mt with ubox) basemt */
} else {
/* mt basemt nil */
lua_pop(L, 1);
lua_pushstring(L,"tolua_ubox"); lua_newtable(L);
/* make weak value metatable for ubox table to allow userdata to be
garbage-collected */
lua_newtable(L); lua_pushliteral(L, "__mode"); lua_pushliteral(L, "v"); lua_rawset(L, -3); /* stack: string ubox mt */
lua_setmetatable(L, -2); /* stack:mt basemt string ubox */
lua_rawset(L,-4);
};
};
/* Map inheritance
* It sets 'name' as derived from 'base' by setting 'base' as metatable of 'name'
*/
static void mapinheritance (lua_State* L, const char* name, const char* base)
{
/* set metatable inheritance */
luaL_getmetatable(L,name);
if (base && *base)
luaL_getmetatable(L,base);
else {
if (lua_getmetatable(L, -1)) { /* already has a mt, we don't overwrite it */
lua_pop(L, 2);
return;
};
luaL_getmetatable(L,"tolua_commonclass");
};
set_ubox(L);
lua_setmetatable(L,-2);
lua_pop(L,1);
}
/* Object type
*/
static int tolua_bnd_type (lua_State* L)
{
tolua_typename(L,lua_gettop(L));
return 1;
}
/* Take ownership
*/
static int tolua_bnd_takeownership (lua_State* L)
{
int success = 0;
if (lua_isuserdata(L,1))
{
if (lua_getmetatable(L,1)) /* if metatable? */
{
lua_pop(L,1); /* clear metatable off stack */
/* force garbage collection to avoid C to reuse a to-be-collected address */
#ifdef LUA_VERSION_NUM
lua_gc(L, LUA_GCCOLLECT, 0);
#else
lua_setgcthreshold(L,0);
#endif
success = tolua_register_gc(L,1);
}
}
lua_pushboolean(L,success!=0);
return 1;
}
/* Release ownership
*/
static int tolua_bnd_releaseownership (lua_State* L)
{
int done = 0;
if (lua_isuserdata(L,1))
{
void* u = *((void**)lua_touserdata(L,1));
/* force garbage collection to avoid releasing a to-be-collected address */
#ifdef LUA_VERSION_NUM
lua_gc(L, LUA_GCCOLLECT, 0);
#else
lua_setgcthreshold(L,0);
#endif
lua_pushstring(L,"tolua_gc");
lua_rawget(L,LUA_REGISTRYINDEX);
lua_pushlightuserdata(L,u);
lua_rawget(L,-2);
lua_getmetatable(L,1);
if (lua_rawequal(L,-1,-2)) /* check that we are releasing the correct type */
{
lua_pushlightuserdata(L,u);
lua_pushnil(L);
lua_rawset(L,-5);
done = 1;
}
}
lua_pushboolean(L,done!=0);
return 1;
}
/* Type casting
*/
static int tolua_bnd_cast (lua_State* L)
{
/* // old code
void* v = tolua_tousertype(L,1,NULL);
const char* s = tolua_tostring(L,2,NULL);
if (v && s)
tolua_pushusertype(L,v,s);
else
lua_pushnil(L);
return 1;
*/
void* v;
const char* s;
if (lua_islightuserdata(L, 1)) {
v = tolua_touserdata(L, 1, NULL);
} else {
v = tolua_tousertype(L, 1, 0);
};
s = tolua_tostring(L,2,NULL);
if (v && s)
tolua_pushusertype(L,v,s);
else
lua_pushnil(L);
return 1;
}
/* Inheritance
*/
static int tolua_bnd_inherit (lua_State* L) {
/* stack: lua object, c object */
lua_pushstring(L, ".c_instance");
lua_pushvalue(L, -2);
lua_rawset(L, -4);
/* l_obj[".c_instance"] = c_obj */
return 0;
};
#ifdef LUA_VERSION_NUM /* lua 5.1 */
static int tolua_bnd_setpeer(lua_State* L) {
/* stack: userdata, table */
if (!lua_isuserdata(L, -2)) {
lua_pushstring(L, "Invalid argument #1 to setpeer: userdata expected.");
lua_error(L);
};
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_pushvalue(L, TOLUA_NOPEER);
};
lua_setfenv(L, -2);
return 0;
};
static int tolua_bnd_getpeer(lua_State* L) {
/* stack: userdata */
lua_getfenv(L, -1);
if (lua_rawequal(L, -1, TOLUA_NOPEER)) {
lua_pop(L, 1);
lua_pushnil(L);
};
return 1;
};
#endif
/* static int class_gc_event (lua_State* L); */
TOLUA_API void tolua_open (lua_State* L)
{
int top = lua_gettop(L);
lua_pushstring(L,"tolua_opened");
lua_rawget(L,LUA_REGISTRYINDEX);
if (!lua_isboolean(L,-1))
{
lua_pushstring(L,"tolua_opened"); lua_pushboolean(L,1); lua_rawset(L,LUA_REGISTRYINDEX);
#ifndef LUA_VERSION_NUM /* only prior to lua 5.1 */
/* create peer object table */
lua_pushstring(L, "tolua_peers"); lua_newtable(L);
/* make weak key metatable for peers indexed by userdata object */
lua_newtable(L); lua_pushliteral(L, "__mode"); lua_pushliteral(L, "k"); lua_rawset(L, -3); /* stack: string peers mt */
lua_setmetatable(L, -2); /* stack: string peers */
lua_rawset(L,LUA_REGISTRYINDEX);
#endif
/* create object ptr -> udata mapping table */
lua_pushstring(L,"tolua_ubox"); lua_newtable(L);
/* make weak value metatable for ubox table to allow userdata to be
garbage-collected */
lua_newtable(L); lua_pushliteral(L, "__mode"); lua_pushliteral(L, "v"); lua_rawset(L, -3); /* stack: string ubox mt */
lua_setmetatable(L, -2); /* stack: string ubox */
lua_rawset(L,LUA_REGISTRYINDEX);
lua_pushstring(L,"tolua_super"); lua_newtable(L); lua_rawset(L,LUA_REGISTRYINDEX);
lua_pushstring(L,"tolua_gc"); lua_newtable(L);lua_rawset(L,LUA_REGISTRYINDEX);
/* create gc_event closure */
lua_pushstring(L, "tolua_gc_event");
lua_pushstring(L, "tolua_gc");
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushstring(L, "tolua_super");
lua_rawget(L, LUA_REGISTRYINDEX);
lua_pushcclosure(L, class_gc_event, 2);
lua_rawset(L, LUA_REGISTRYINDEX);
tolua_newmetatable(L,"tolua_commonclass");
tolua_module(L,NULL,0);
tolua_beginmodule(L,NULL);
tolua_module(L,"tolua",0);
tolua_beginmodule(L,"tolua");
tolua_function(L,"type",tolua_bnd_type);
tolua_function(L,"takeownership",tolua_bnd_takeownership);
tolua_function(L,"releaseownership",tolua_bnd_releaseownership);
tolua_function(L,"cast",tolua_bnd_cast);
tolua_function(L,"inherit", tolua_bnd_inherit);
#ifdef LUA_VERSION_NUM /* lua 5.1 */
tolua_function(L, "setpeer", tolua_bnd_setpeer);
tolua_function(L, "getpeer", tolua_bnd_getpeer);
#endif
tolua_endmodule(L);
tolua_endmodule(L);
}
lua_settop(L,top);
}
/* Copy a C object
*/
TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size)
{
void* clone = (void*)malloc(size);
if (clone)
memcpy(clone,value,size);
else
tolua_error(L,"insuficient memory",NULL);
return clone;
}
/* Default collect function
*/
TOLUA_API int tolua_default_collect (lua_State* tolua_S)
{
void* self = tolua_tousertype(tolua_S,1,0);
free(self);
return 0;
}
/* Do clone
*/
TOLUA_API int tolua_register_gc (lua_State* L, int lo)
{
int success = 1;
void *value = *(void **)lua_touserdata(L,lo);
lua_pushstring(L,"tolua_gc");
lua_rawget(L,LUA_REGISTRYINDEX);
lua_pushlightuserdata(L,value);
lua_rawget(L,-2);
if (!lua_isnil(L,-1)) /* make sure that object is not already owned */
success = 0;
else
{
lua_pushlightuserdata(L,value);
lua_getmetatable(L,lo);
lua_rawset(L,-4);
}
lua_pop(L,2);
return success;
}
/* Register a usertype
* It creates the correspoding metatable in the registry, for both 'type' and 'const type'.
* It maps 'const type' as being also a 'type'
*/
TOLUA_API void tolua_usertype (lua_State* L, const char* type)
{
char ctype[128] = "const ";
strncat(ctype,type,120);
/* create both metatables */
if (tolua_newmetatable(L,ctype) && tolua_newmetatable(L,type))
mapsuper(L,type,ctype); /* 'type' is also a 'const type' */
}
/* Begin module
* It pushes the module (or class) table on the stack
*/
TOLUA_API void tolua_beginmodule (lua_State* L, const char* name)
{
if (name)
{
lua_pushstring(L,name);
lua_rawget(L,-2);
}
else
lua_pushvalue(L,LUA_GLOBALSINDEX);
}
/* End module
* It pops the module (or class) from the stack
*/
TOLUA_API void tolua_endmodule (lua_State* L)
{
lua_pop(L,1);
}
/* Map module
* It creates a new module
*/
#if 1
TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
{
if (name)
{
/* tolua module */
lua_pushstring(L,name);
lua_rawget(L,-2);
if (!lua_istable(L,-1)) /* check if module already exists */
{
lua_pop(L,1);
lua_newtable(L);
lua_pushstring(L,name);
lua_pushvalue(L,-2);
lua_rawset(L,-4); /* assing module into module */
}
}
else
{
/* global table */
lua_pushvalue(L,LUA_GLOBALSINDEX);
}
if (hasvar)
{
if (!tolua_ismodulemetatable(L)) /* check if it already has a module metatable */
{
/* create metatable to get/set C/C++ variable */
lua_newtable(L);
tolua_moduleevents(L);
if (lua_getmetatable(L,-2))
lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */
lua_setmetatable(L,-2);
}
}
lua_pop(L,1); /* pop module */
}
#else
TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
{
if (name)
{
/* tolua module */
lua_pushstring(L,name);
lua_newtable(L);
}
else
{
/* global table */
lua_pushvalue(L,LUA_GLOBALSINDEX);
}
if (hasvar)
{
/* create metatable to get/set C/C++ variable */
lua_newtable(L);
tolua_moduleevents(L);
if (lua_getmetatable(L,-2))
lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */
lua_setmetatable(L,-2);
}
if (name)
lua_rawset(L,-3); /* assing module into module */
else
lua_pop(L,1); /* pop global table */
}
#endif
static void push_collector(lua_State* L, const char* type, lua_CFunction col) {
/* push collector function, but only if it's not NULL, or if there's no
collector already */
if (!col) return;
luaL_getmetatable(L,type);
lua_pushstring(L,".collector");
/*
if (!col) {
lua_pushvalue(L, -1);
lua_rawget(L, -3);
if (!lua_isnil(L, -1)) {
lua_pop(L, 3);
return;
};
lua_pop(L, 1);
};
// */
lua_pushcfunction(L,col);
lua_rawset(L,-3);
lua_pop(L, 1);
};
/* Map C class
* It maps a C class, setting the appropriate inheritance and super classes.
*/
TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col)
{
char cname[128] = "const ";
char cbase[128] = "const ";
strncat(cname,name,120);
strncat(cbase,base,120);
mapinheritance(L,name,base);
mapinheritance(L,cname,name);
mapsuper(L,cname,cbase);
mapsuper(L,name,base);
lua_pushstring(L,lname);
push_collector(L, name, col);
/*
luaL_getmetatable(L,name);
lua_pushstring(L,".collector");
lua_pushcfunction(L,col);
lua_rawset(L,-3);
*/
luaL_getmetatable(L,name);
lua_rawset(L,-3); /* assign class metatable to module */
/* now we also need to store the collector table for the const
instances of the class */
push_collector(L, cname, col);
/*
luaL_getmetatable(L,cname);
lua_pushstring(L,".collector");
lua_pushcfunction(L,col);
lua_rawset(L,-3);
lua_pop(L,1);
*/
}
/* Add base
* It adds additional base classes to a class (for multiple inheritance)
* (not for now)
TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base) {
char cname[128] = "const ";
char cbase[128] = "const ";
strncat(cname,name,120);
strncat(cbase,base,120);
mapsuper(L,cname,cbase);
mapsuper(L,name,base);
};
*/
/* Map function
* It assigns a function into the current module (or class)
*/
TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func)
{
lua_pushstring(L,name);
lua_pushcfunction(L,func);
lua_rawset(L,-3);
}
/* sets the __call event for the class (expects the class' main table on top) */
/* never really worked :(
TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type) {
lua_getmetatable(L, -1);
//luaL_getmetatable(L, type);
lua_pushstring(L,"__call");
lua_pushcfunction(L,func);
lua_rawset(L,-3);
lua_pop(L, 1);
};
*/
/* Map constant number
* It assigns a constant number into the current module (or class)
*/
TOLUA_API void tolua_constant (lua_State* L, const char* name, lua_Number value)
{
lua_pushstring(L,name);
tolua_pushnumber(L,value);
lua_rawset(L,-3);
}
/* Map variable
* It assigns a variable into the current module (or class)
*/
TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
{
/* get func */
lua_pushstring(L,".get");
lua_rawget(L,-2);
if (!lua_istable(L,-1))
{
/* create .get table, leaving it at the top */
lua_pop(L,1);
lua_newtable(L);
lua_pushstring(L,".get");
lua_pushvalue(L,-2);
lua_rawset(L,-4);
}
lua_pushstring(L,name);
lua_pushcfunction(L,get);
lua_rawset(L,-3); /* store variable */
lua_pop(L,1); /* pop .get table */
/* set func */
if (set)
{
lua_pushstring(L,".set");
lua_rawget(L,-2);
if (!lua_istable(L,-1))
{
/* create .set table, leaving it at the top */
lua_pop(L,1);
lua_newtable(L);
lua_pushstring(L,".set");
lua_pushvalue(L,-2);
lua_rawset(L,-4);
}
lua_pushstring(L,name);
lua_pushcfunction(L,set);
lua_rawset(L,-3); /* store variable */
lua_pop(L,1); /* pop .set table */
}
}
/* Access const array
* It reports an error when trying to write into a const array
*/
static int const_array (lua_State* L)
{
luaL_error(L,"value of const array cannot be changed");
return 0;
}
/* Map an array
* It assigns an array into the current module (or class)
*/
TOLUA_API void tolua_array (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
{
lua_pushstring(L,".get");
lua_rawget(L,-2);
if (!lua_istable(L,-1))
{
/* create .get table, leaving it at the top */
lua_pop(L,1);
lua_newtable(L);
lua_pushstring(L,".get");
lua_pushvalue(L,-2);
lua_rawset(L,-4);
}
lua_pushstring(L,name);
lua_newtable(L); /* create array metatable */
lua_pushvalue(L,-1);
lua_setmetatable(L,-2); /* set the own table as metatable (for modules) */
lua_pushstring(L,"__index");
lua_pushcfunction(L,get);
lua_rawset(L,-3);
lua_pushstring(L,"__newindex");
lua_pushcfunction(L,set?set:const_array);
lua_rawset(L,-3);
lua_rawset(L,-3); /* store variable */
lua_pop(L,1); /* pop .get table */
}
TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name) {
#ifdef LUA_VERSION_NUM /* lua 5.1 */
luaL_loadbuffer(L, B, size, name) || lua_pcall(L, 0, 0, 0);
#else
lua_dobuffer(L, B, size, name);
#endif
};

View File

@ -1,171 +0,0 @@
/* tolua: functions to push C values.
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include "tolua++.h"
#include "lauxlib.h"
#include <stdlib.h>
TOLUA_API void tolua_pushvalue (lua_State* L, int lo)
{
lua_pushvalue(L,lo);
}
TOLUA_API void tolua_pushboolean (lua_State* L, int value)
{
lua_pushboolean(L,value);
}
TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value)
{
lua_pushnumber(L,value);
}
TOLUA_API void tolua_pushstring (lua_State* L, const char* value)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushstring(L,value);
}
TOLUA_API void tolua_pushuserdata (lua_State* L, void* value)
{
if (value == NULL)
lua_pushnil(L);
else
lua_pushlightuserdata(L,value);
}
TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type)
{
if (value == NULL)
lua_pushnil(L);
else
{
luaL_getmetatable(L, type);
lua_pushstring(L,"tolua_ubox");
lua_rawget(L,-2); /* stack: mt ubox */
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_pushstring(L, "tolua_ubox");
lua_rawget(L, LUA_REGISTRYINDEX);
};
lua_pushlightuserdata(L,value);
lua_rawget(L,-2); /* stack: mt ubox ubox[u] */
if (lua_isnil(L,-1))
{
lua_pop(L,1); /* stack: mt ubox */
lua_pushlightuserdata(L,value);
*(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox u newud */
lua_pushvalue(L,-1); /* stack: mt ubox u newud newud */
lua_insert(L,-4); /* stack: mt newud ubox u newud */
lua_rawset(L,-3); /* stack: mt newud ubox */
lua_pop(L,1); /* stack: mt newud */
/*luaL_getmetatable(L,type);*/
lua_pushvalue(L, -2); /* stack: mt newud mt */
lua_setmetatable(L,-2); /* stack: mt newud */
#ifdef LUA_VERSION_NUM
lua_pushvalue(L, TOLUA_NOPEER);
lua_setfenv(L, -2);
#endif
}
else
{
/* check the need of updating the metatable to a more specialized class */
lua_insert(L,-2); /* stack: mt ubox[u] ubox */
lua_pop(L,1); /* stack: mt ubox[u] */
lua_pushstring(L,"tolua_super");
lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */
lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */
if (lua_istable(L,-1))
{
lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */
lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */
if (lua_toboolean(L,-1) == 1) /* if true */
{
lua_pop(L,3); /* mt ubox[u]*/
lua_remove(L, -2);
return;
}
}
/* type represents a more specilized type */
/*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */
lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */
lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */
lua_pop(L,3); /* stack: mt ubox[u] */
}
lua_remove(L, -2); /* stack: ubox[u]*/
}
}
TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type)
{
tolua_pushusertype(L,value,type);
tolua_register_gc(L,lua_gettop(L));
}
TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v)
{
lua_pushnumber(L,index);
lua_pushvalue(L,v);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v)
{
lua_pushnumber(L,index);
lua_pushboolean(L,v);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v)
{
lua_pushnumber(L,index);
tolua_pushnumber(L,v);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v)
{
lua_pushnumber(L,index);
tolua_pushstring(L,v);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v)
{
lua_pushnumber(L,index);
tolua_pushuserdata(L,v);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type)
{
lua_pushnumber(L,index);
tolua_pushusertype(L,v,type);
lua_settable(L,lo);
}
TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type)
{
lua_pushnumber(L,index);
tolua_pushusertype(L,v,type);
tolua_register_gc(L,lua_gettop(L));
lua_settable(L,lo);
}

View File

@ -1,133 +0,0 @@
/* tolua: funcitons to convert to C types
** Support code for Lua bindings.
** Written by Waldemar Celes
** TeCGraf/PUC-Rio
** Apr 2003
** $Id: $
*/
/* This code is free software; you can redistribute it and/or modify it.
** The software provided hereunder is on an "as is" basis, and
** the author has no obligation to provide maintenance, support, updates,
** enhancements, or modifications.
*/
#include "tolua++.h"
#include <string.h>
#include <stdlib.h>
TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def)
{
return lua_gettop(L)<abs(narg) ? def : lua_tonumber(L,narg);
}
TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def)
{
return lua_gettop(L)<abs(narg) ? def : lua_tostring(L,narg);
}
TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def)
{
/* return lua_gettop(L)<abs(narg) ? def : lua_touserdata(L,narg); */
if (lua_gettop(L)<abs(narg)) {
return def;
};
if (lua_islightuserdata(L, narg)) {
return lua_touserdata(L,narg);
};
return tolua_tousertype(L, narg, def);
}
extern int push_table_instance(lua_State* L, int lo);
TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def)
{
if (lua_gettop(L)<abs(narg))
return def;
else
{
void* u;
if (!lua_isuserdata(L, narg)) {
if (!push_table_instance(L, narg)) return NULL;
};
u = lua_touserdata(L,narg);
return (u==NULL) ? NULL : *((void**)u); /* nil represents NULL */
}
}
TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def)
{
return lua_gettop(L)<abs(narg) ? def : narg;
}
TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def)
{
return lua_gettop(L)<abs(narg) ? def : lua_toboolean(L,narg);
}
TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def)
{
double v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tonumber(L,-1);
lua_pop(L,1);
return v;
}
TOLUA_API const char* tolua_tofieldstring
(lua_State* L, int lo, int index, const char* def)
{
const char* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_tostring(L,-1);
lua_pop(L,1);
return v;
}
TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def)
{
void* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lua_touserdata(L,-1);
lua_pop(L,1);
return v;
}
TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def)
{
void* v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : (*(void **)(lua_touserdata(L, -1))); /* lua_unboxpointer(L,-1); */
lua_pop(L,1);
return v;
}
TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def)
{
int v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? def : lo;
lua_pop(L,1);
return v;
}
TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def)
{
int v;
lua_pushnumber(L,index);
lua_gettable(L,lo);
v = lua_isnil(L,-1) ? 0 : lua_toboolean(L,-1);
lua_pop(L,1);
return v;
}