Commit 02869e90 authored by Megax's avatar Megax
Browse files

* Frissites kesz de ugyan az a hiba mint a masiknal. Tuzedesen at kell...

* Frissites kesz de ugyan az a hiba mint a masiknal. Tuzedesen at kell vizsgalnom az okat mert lehet valahogy en okozom.
parent 19e9e632
using System;
using System.Collections.Generic;
using System.Reflection;
using Lua511;
using LuaWrap;
namespace LuaInterface
{
......@@ -64,9 +64,9 @@ namespace LuaInterface
return extractNetObject;
}
internal ExtractValue checkType(IntPtr luaState,int stackPos,Type paramType)
internal ExtractValue checkType(KopiLua.Lua.lua_State luaState,int stackPos,Type paramType)
{
LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);
LuaTypes luatype = KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes();
if(paramType.IsByRef) paramType=paramType.GetElementType();
......@@ -84,71 +84,71 @@ namespace LuaInterface
//CP: Added support for generic parameters
if (paramType.IsGenericParameter)
{
if (luatype == LuaTypes.LUA_TBOOLEAN)
if (luatype == LuaTypes.Boolean)
return extractValues[typeof(bool).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TSTRING)
else if (luatype == LuaTypes.String)
return extractValues[typeof(string).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TTABLE)
else if (luatype == LuaTypes.Table)
return extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TUSERDATA)
else if (luatype == LuaTypes.UserData)
return extractValues[typeof(object).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TFUNCTION)
else if (luatype == LuaTypes.Function)
return extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()];
else if (luatype == LuaTypes.LUA_TNUMBER)
else if (luatype == LuaTypes.Number)
return extractValues[typeof(double).TypeHandle.Value.ToInt64()];
else
;//an unsupported type was encountered
}
if (LuaDLL.lua_isnumber(luaState, stackPos))
if (KopiLua.Lua.lua_isnumber(luaState, stackPos).ToBoolean())
return extractValues[runtimeHandleValue];
if (paramType == typeof(bool))
{
if (LuaDLL.lua_isboolean(luaState, stackPos))
if (KopiLua.Lua.lua_isboolean(luaState, stackPos))
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(string))
{
if (LuaDLL.lua_isstring(luaState, stackPos))
if (KopiLua.Lua.lua_isstring(luaState, stackPos).ToBoolean())
return extractValues[runtimeHandleValue];
else if (luatype == LuaTypes.LUA_TNIL)
else if (luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer
}
else if (paramType == typeof(LuaTable))
{
if (luatype == LuaTypes.LUA_TTABLE)
if (luatype == LuaTypes.Table)
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(LuaUserData))
{
if (luatype == LuaTypes.LUA_TUSERDATA)
if (luatype == LuaTypes.UserData)
return extractValues[runtimeHandleValue];
}
else if (paramType == typeof(LuaFunction))
{
if (luatype == LuaTypes.LUA_TFUNCTION)
if (luatype == LuaTypes.Function)
return extractValues[runtimeHandleValue];
}
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.Function)
{
return new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated);
}
else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
else if (paramType.IsInterface && luatype == LuaTypes.Table)
{
return new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated);
}
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.Nil)
{
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject;
}
else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
else if (KopiLua.Lua.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table)
{
if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
if (KopiLua.Lua.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean())
{
object obj = translator.getNetObject(luaState, -1);
LuaDLL.lua_settop(luaState, -2);
KopiLua.Lua.lua_settop(luaState, -2);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject;
}
......@@ -170,136 +170,142 @@ namespace LuaInterface
* index stackPos as the desired type if it can, or null
* otherwise.
*/
private object getAsSbyte(IntPtr luaState,int stackPos)
private object getAsSbyte(KopiLua.Lua.lua_State luaState,int stackPos)
{
sbyte retVal=(sbyte)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
sbyte retVal=(sbyte)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean())
return null;
return retVal;
}
private object getAsByte(IntPtr luaState,int stackPos)
private object getAsByte(KopiLua.Lua.lua_State luaState,int stackPos)
{
byte retVal=(byte)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
byte retVal=(byte)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean())
return null;
return retVal;
}
private object getAsShort(IntPtr luaState,int stackPos)
private object getAsShort(KopiLua.Lua.lua_State luaState,int stackPos)
{
short retVal=(short)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
short retVal=(short)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean())
return null;
return retVal;
}
private object getAsUshort(IntPtr luaState,int stackPos)
private object getAsUshort(KopiLua.Lua.lua_State luaState,int stackPos)
{
ushort retVal=(ushort)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
ushort retVal=(ushort)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsInt(IntPtr luaState,int stackPos)
private object getAsInt(KopiLua.Lua.lua_State luaState,int stackPos)
{
int retVal=(int)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
int retVal=(int)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsUint(IntPtr luaState,int stackPos)
private object getAsUint(KopiLua.Lua.lua_State luaState,int stackPos)
{
uint retVal=(uint)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
uint retVal=(uint)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsLong(IntPtr luaState,int stackPos)
private object getAsLong(KopiLua.Lua.lua_State luaState,int stackPos)
{
long retVal=(long)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
long retVal=(long)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsUlong(IntPtr luaState,int stackPos)
private object getAsUlong(KopiLua.Lua.lua_State luaState,int stackPos)
{
ulong retVal=(ulong)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
ulong retVal=(ulong)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsDouble(IntPtr luaState,int stackPos)
private object getAsDouble(KopiLua.Lua.lua_State luaState,int stackPos)
{
double retVal=LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
double retVal=KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsChar(IntPtr luaState,int stackPos)
private object getAsChar(KopiLua.Lua.lua_State luaState,int stackPos)
{
char retVal=(char)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
char retVal=(char)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsFloat(IntPtr luaState,int stackPos)
private object getAsFloat(KopiLua.Lua.lua_State luaState,int stackPos)
{
float retVal=(float)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
float retVal=(float)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsDecimal(IntPtr luaState,int stackPos)
private object getAsDecimal(KopiLua.Lua.lua_State luaState,int stackPos)
{
decimal retVal=(decimal)LuaDLL.lua_tonumber(luaState,stackPos);
if(retVal==0 && !LuaDLL.lua_isnumber(luaState,stackPos)) return null;
decimal retVal=(decimal)KopiLua.Lua.lua_tonumber(luaState,stackPos);
if(retVal==0 && !KopiLua.Lua.lua_isnumber(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsBoolean(IntPtr luaState,int stackPos)
private object getAsBoolean(KopiLua.Lua.lua_State luaState,int stackPos)
{
return LuaDLL.lua_toboolean(luaState,stackPos);
return KopiLua.Lua.lua_toboolean(luaState,stackPos);
}
private object getAsString(IntPtr luaState,int stackPos)
private object getAsString(KopiLua.Lua.lua_State luaState,int stackPos)
{
string retVal=LuaDLL.lua_tostring(luaState,stackPos);
if(retVal=="" && !LuaDLL.lua_isstring(luaState,stackPos)) return null;
string retVal=KopiLua.Lua.lua_tostring(luaState,stackPos).ToString();
if(retVal=="" && !KopiLua.Lua.lua_isstring(luaState,stackPos).ToBoolean()) return null;
return retVal;
}
private object getAsTable(IntPtr luaState,int stackPos)
private object getAsTable(KopiLua.Lua.lua_State luaState,int stackPos)
{
return translator.getTable(luaState,stackPos);
}
private object getAsFunction(IntPtr luaState,int stackPos)
private object getAsFunction(KopiLua.Lua.lua_State luaState,int stackPos)
{
return translator.getFunction(luaState,stackPos);
}
private object getAsUserdata(IntPtr luaState,int stackPos)
private object getAsUserdata(KopiLua.Lua.lua_State luaState,int stackPos)
{
return translator.getUserData(luaState,stackPos);
}
public object getAsObject(IntPtr luaState,int stackPos)
public object getAsObject(KopiLua.Lua.lua_State luaState,int stackPos)
{
if(LuaDLL.lua_type(luaState,stackPos)==LuaTypes.LUA_TTABLE)
if(KopiLua.Lua.lua_type(luaState,stackPos).ToLuaTypes()==LuaTypes.Table)
{
if(LuaDLL.luaL_getmetafield(luaState,stackPos,"__index"))
if(KopiLua.Lua.luaL_getmetafield(luaState,stackPos,"__index").ToBoolean())
{
if(LuaDLL.luaL_checkmetatable(luaState,-1))
if(LuaLib.luaL_checkmetatable(luaState,-1))
{
LuaDLL.lua_insert(luaState,stackPos);
LuaDLL.lua_remove(luaState,stackPos+1);
KopiLua.Lua.lua_insert(luaState,stackPos);
KopiLua.Lua.lua_remove(luaState,stackPos+1);
}
else
{
LuaDLL.lua_settop(luaState,-2);
KopiLua.Lua.lua_settop(luaState,-2);
}
}
}
object obj=translator.getObject(luaState,stackPos);
return obj;
}
public object getAsNetObject(IntPtr luaState,int stackPos)
public object getAsNetObject(KopiLua.Lua.lua_State luaState,int stackPos)
{
object obj=translator.getNetObject(luaState,stackPos);
if(obj==null && LuaDLL.lua_type(luaState,stackPos)==LuaTypes.LUA_TTABLE)
if(obj==null && KopiLua.Lua.lua_type(luaState,stackPos).ToLuaTypes()==LuaTypes.Table)
{
if(LuaDLL.luaL_getmetafield(luaState,stackPos,"__index"))
if(KopiLua.Lua.luaL_getmetafield(luaState,stackPos,"__index").ToBoolean())
{
if(LuaDLL.luaL_checkmetatable(luaState,-1))
if(LuaLib.luaL_checkmetatable(luaState,-1))
{
LuaDLL.lua_insert(luaState,stackPos);
LuaDLL.lua_remove(luaState,stackPos+1);
KopiLua.Lua.lua_insert(luaState,stackPos);
KopiLua.Lua.lua_remove(luaState,stackPos+1);
obj=translator.getNetObject(luaState,stackPos);
}
else
{
LuaDLL.lua_settop(luaState,-2);
KopiLua.Lua.lua_settop(luaState,-2);
}
}
}
......
......@@ -44,7 +44,7 @@ namespace LuaInterface
this.translator=translator;
this.delegateType=delegateType;
}
public object extractGenerated(IntPtr luaState,int stackPos)
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType,translator.getFunction(luaState,stackPos));
}
......@@ -67,7 +67,7 @@ namespace LuaInterface
this.translator=translator;
this.klass=klass;
}
public object extractGenerated(IntPtr luaState,int stackPos)
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass,translator.getTable(luaState,stackPos));
}
......
......@@ -8,7 +8,7 @@ namespace LuaInterface
using System.Collections.Specialized;
using System.Reflection;
using System.Threading;
using Lua511;
using LuaWrap;
/*
* Main class of LuaInterface
......@@ -24,7 +24,6 @@ namespace LuaInterface
[CLSCompliant(true)]
public class Lua : IDisposable
{
static string init_luanet =
"local metatable = {} \n"+
"local import_type = luanet.import_type \n"+
......@@ -65,10 +64,10 @@ namespace LuaInterface
"-- Preload the mscorlib assembly \n"+
"luanet.load_assembly(\"mscorlib\") \n";
/*readonly */ IntPtr luaState;
/*readonly */ KopiLua.Lua.lua_State luaState;
ObjectTranslator translator;
LuaCSFunction panicCallback, lockCallback, unlockCallback;
KopiLua.Lua.lua_CFunction panicCallback/*, lockCallback, unlockCallback*/;
/// <summary>
/// Used to ensure multiple .net threads all get serialized by this single lock for access to the lua stack/objects
......@@ -77,30 +76,30 @@ namespace LuaInterface
public Lua()
{
luaState = LuaDLL.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone)
//LuaDLL.luaopen_base(luaState); // steffenj: luaopen_* no longer used
LuaDLL.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
LuaDLL.lua_pushstring(luaState, "LUAINTERFACE LOADED");
LuaDLL.lua_pushboolean(luaState, true);
LuaDLL.lua_settable(luaState, (int) LuaIndexes.LUA_REGISTRYINDEX);
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_setglobal(luaState, "luanet");
LuaDLL.lua_pushvalue(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
LuaDLL.lua_getglobal(luaState, "luanet");
LuaDLL.lua_pushstring(luaState, "getmetatable");
LuaDLL.lua_getglobal(luaState, "getmetatable");
LuaDLL.lua_settable(luaState, -3);
LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
luaState = KopiLua.Lua.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone)
//KopiLua.Lua.luaopen_base(luaState); // steffenj: luaopen_* no longer used
KopiLua.Lua.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
KopiLua.Lua.lua_pushstring(luaState, "LUAINTERFACE LOADED");
KopiLua.Lua.lua_pushboolean(luaState, 1);
KopiLua.Lua.lua_settable(luaState, (int)PseudoIndex.Registry);
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_setglobal(luaState, "luanet");
KopiLua.Lua.lua_pushvalue(luaState, (int)PseudoIndex.Globals);
KopiLua.Lua.lua_getglobal(luaState, "luanet");
KopiLua.Lua.lua_pushstring(luaState, "getmetatable");
KopiLua.Lua.lua_getglobal(luaState, "getmetatable");
KopiLua.Lua.lua_settable(luaState, -3);
KopiLua.Lua.lua_replace(luaState, (int)PseudoIndex.Globals);
translator=new ObjectTranslator(this,luaState);
LuaDLL.lua_replace(luaState, (int)LuaIndexes.LUA_GLOBALSINDEX);
LuaDLL.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring
KopiLua.Lua.lua_replace(luaState, (int)PseudoIndex.Globals);
LuaLib.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback = new LuaCSFunction(PanicCallback);
LuaDLL.lua_atpanic(luaState, panicCallback);
panicCallback = new KopiLua.Lua.lua_CFunction(PanicCallback);
KopiLua.Lua.lua_atpanic(luaState, panicCallback);
//LuaDLL.lua_atlock(luaState, lockCallback = new LuaCSFunction(LockCallback));
//LuaDLL.lua_atunlock(luaState, unlockCallback = new LuaCSFunction(UnlockCallback));
//KopiLua.Lua.lua_atlock(luaState, lockCallback = new KopiLua.Lua.lua_CFunction(LockCallback));
//KopiLua.Lua.lua_atunlock(luaState, unlockCallback = new KopiLua.Lua.lua_CFunction(UnlockCallback));
}
private bool _StatePassed;
......@@ -108,33 +107,33 @@ namespace LuaInterface
/*
* CAUTION: LuaInterface.Lua instances can't share the same lua state!
*/
public Lua(Int64 luaState)
public Lua(KopiLua.Lua.lua_State luaState)
{
IntPtr lState = new IntPtr(luaState);
LuaDLL.lua_pushstring(lState, "LUAINTERFACE LOADED");
LuaDLL.lua_gettable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX);
KopiLua.Lua.lua_State lState = luaState;
KopiLua.Lua.lua_pushstring(lState, "LUAINTERFACE LOADED");
KopiLua.Lua.lua_gettable(lState, (int)PseudoIndex.Registry);
if(LuaDLL.lua_toboolean(lState,-1))
if(KopiLua.Lua.lua_toboolean(lState,-1).ToBoolean())
{
LuaDLL.lua_settop(lState,-2);
KopiLua.Lua.lua_settop(lState,-2);
throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state");
}
else
{
LuaDLL.lua_settop(lState,-2);
LuaDLL.lua_pushstring(lState, "LUAINTERFACE LOADED");
LuaDLL.lua_pushboolean(lState, true);
LuaDLL.lua_settable(lState, (int)LuaIndexes.LUA_REGISTRYINDEX);
KopiLua.Lua.lua_settop(lState,-2);
KopiLua.Lua.lua_pushstring(lState, "LUAINTERFACE LOADED");
KopiLua.Lua.lua_pushboolean(lState, 1);
KopiLua.Lua.lua_settable(lState, (int)PseudoIndex.Registry);
this.luaState=lState;
LuaDLL.lua_pushvalue(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
LuaDLL.lua_getglobal(lState, "luanet");
LuaDLL.lua_pushstring(lState, "getmetatable");
LuaDLL.lua_getglobal(lState, "getmetatable");
LuaDLL.lua_settable(lState, -3);
LuaDLL.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
KopiLua.Lua.lua_pushvalue(lState, (int)PseudoIndex.Globals);
KopiLua.Lua.lua_getglobal(lState, "luanet");
KopiLua.Lua.lua_pushstring(lState, "getmetatable");
KopiLua.Lua.lua_getglobal(lState, "getmetatable");
KopiLua.Lua.lua_settable(lState, -3);
KopiLua.Lua.lua_replace(lState, (int)PseudoIndex.Globals);
translator=new ObjectTranslator(this, this.luaState);
LuaDLL.lua_replace(lState, (int)LuaIndexes.LUA_GLOBALSINDEX);
LuaDLL.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring
KopiLua.Lua.lua_replace(lState, (int)PseudoIndex.Globals);
LuaLib.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring
}
_StatePassed = true;
......@@ -145,7 +144,7 @@ namespace LuaInterface
/// </summary>
/// <param name="luaState"></param>
/// Not yet used
int LockCallback(IntPtr luaState)
int LockCallback(KopiLua.Lua.lua_State luaState)
{
// Monitor.Enter(luaLock);
......@@ -157,7 +156,7 @@ namespace LuaInterface
/// </summary>
/// <param name="luaState"></param>
/// Not yet used
int UnlockCallback(IntPtr luaState)
int UnlockCallback(KopiLua.Lua.lua_State luaState)
{
// Monitor.Exit(luaLock);
......@@ -169,15 +168,16 @@ namespace LuaInterface
if (_StatePassed)
return;
if (luaState != IntPtr.Zero)
LuaDLL.lua_close(luaState);
//luaState = IntPtr.Zero; <- suggested by Christopher Cebulski http://luaforge.net/forum/forum.php?thread_id=44593&forum_id=146
////// if (luaState != KopiLua.Lua.lua_State.Zero)
if (luaState != null)
KopiLua.Lua.lua_close(luaState);
//luaState = KopiLua.Lua.lua_State.Zero; <- suggested by Christopher Cebulski http://luaforge.net/forum/forum.php?thread_id=44593&forum_id=146
}
static int PanicCallback(IntPtr luaState)
static int PanicCallback(KopiLua.Lua.lua_State luaState)
{
// string desc = LuaDLL.lua_tostring(luaState, 1);
string reason = String.Format("unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(luaState, -1));
// string desc = KopiLua.Lua.lua_tostring(luaState, 1);
string reason = String.Format("unprotected error in call to Lua API ({0})", KopiLua.Lua.lua_tostring(luaState, -1));
// lua_tostring(L, -1);
......@@ -193,7 +193,7 @@ namespace LuaInterface
void ThrowExceptionFromError(int oldTop)
{
object err = translator.getObject(luaState, -1);
LuaDLL.lua_settop(luaState, oldTop);
KopiLua.Lua.lua_settop(luaState, oldTop);
// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
LuaScriptException luaEx = err as LuaScriptException;
......@@ -218,7 +218,7 @@ namespace LuaInterface
if (caughtExcept != null)
{
translator.throwError(luaState, caughtExcept);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
......@@ -241,12 +241,12 @@ namespace LuaInterface
/// <returns></returns>
public LuaFunction LoadString(string chunk, string name)
{
int oldTop = LuaDLL.lua_gettop(luaState);
int oldTop = KopiLua.Lua.lua_gettop(luaState);
executing = true;
try
{
if (LuaDLL.luaL_loadbuffer(luaState, chunk, name) != 0)
if (LuaLib.luaL_loadbuffer(luaState, chunk, name) != 0)
ThrowExceptionFromError(oldTop);
}
finally { executing = false; }
......@@ -264,8 +264,8 @@ namespace LuaInterface
/// <returns></returns>
public LuaFunction LoadFile(string fileName)
{
int oldTop = LuaDLL.lua_gettop(luaState);
if (LuaDLL.luaL_loadfile(luaState, fileName) != 0)
int oldTop = KopiLua.Lua.lua_gettop(luaState);
if (KopiLua.Lua.luaL_loadfile(luaState, fileName) != 0)
ThrowExceptionFromError(oldTop);
LuaFunction result = translator.getFunction(luaState, -1);
......@@ -281,13 +281,13 @@ namespace LuaInterface
*/
public object[] DoString(string chunk)
{
int oldTop=LuaDLL.lua_gettop(luaState);
if (LuaDLL.luaL_loadbuffer(luaState, chunk, "chunk") == 0)
int oldTop=KopiLua.Lua.lua_gettop(luaState);
if (LuaLib.luaL_loadbuffer(luaState, chunk, "chunk") == 0)
{
executing = true;
try
{
if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
if (KopiLua.Lua.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop);
else
ThrowExceptionFromError(oldTop);
......@@ -308,13 +308,13 @@ namespace LuaInterface
/// <returns></returns>
public object[] DoString(string chunk, string chunkName)
{
int oldTop = LuaDLL.lua_gettop(luaState);
int oldTop = KopiLua.Lua.lua_gettop(luaState);
executing = true;
if (LuaDLL.luaL_loadbuffer(luaState, chunk, chunkName) == 0)
if (LuaLib.luaL_loadbuffer(luaState, chunk, chunkName) == 0)
{
try
{
if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
if (KopiLua.Lua.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop);
else
ThrowExceptionFromError(oldTop);
......@@ -333,13 +333,13 @@ namespace LuaInterface
*/
public object[] DoFile(string fileName)
{
int oldTop=LuaDLL.lua_gettop(luaState);
if(LuaDLL.luaL_loadfile(luaState,fileName)==0)
int oldTop=KopiLua.Lua.lua_gettop(luaState);
if(KopiLua.Lua.luaL_loadfile(luaState,fileName)==0)
{
executing = true;
try
{
if (LuaDLL.lua_pcall(luaState, 0, -1, 0) == 0)
if (KopiLua.Lua.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop);
else
ThrowExceptionFromError(oldTop);
......@@ -362,9 +362,9 @@ namespace LuaInterface
get
{
object returnValue=null;
int oldTop=LuaDLL.lua_gettop(luaState);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
string[] path=fullPath.Split(new char[] { '.' });
LuaDLL.lua_getglobal(luaState,path[0]);
KopiLua.Lua.lua_getglobal(luaState,path[0]);
returnValue=translator.getObject(luaState,-1);
if(path.Length>1)
{
......@@ -372,26 +372,26 @@ namespace LuaInterface
Array.Copy(path,1,remainingPath,0,path.Length-1);
returnValue=getObject(remainingPath);
}
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return returnValue;
}
set
{
int oldTop=LuaDLL.lua_gettop(luaState);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
string[] path=fullPath.Split(new char[] { '.' });
if(path.Length==1)
{
translator.push(luaState,value);
LuaDLL.lua_setglobal(luaState,fullPath);
KopiLua.Lua.lua_setglobal(luaState,fullPath);
}
else
{
LuaDLL.lua_getglobal(luaState,path[0]);
KopiLua.Lua.lua_getglobal(luaState,path[0]);
string[] remainingPath=new string[path.Length-1];
Array.Copy(path,1,remainingPath,0,path.Length-1);
setObject(remainingPath,value);
}
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
// Globals auto-complete
if (value == null)
......@@ -440,7 +440,7 @@ namespace LuaInterface
private void registerGlobal(string path, Type type, int recursionCounter)
{
// If the type is a global method, list it directly
if (type == typeof(LuaCSFunction))
if (type == typeof(KopiLua.Lua.lua_CFunction))
{
// Format for easy method invocation
globals.Add(path + "(");
......@@ -519,8 +519,8 @@ namespace LuaInterface
object returnValue=null;
for(int i=0;i<remainingPath.Length;i++)
{
LuaDLL.lua_pushstring(luaState,remainingPath[i]);
LuaDLL.lua_gettable(luaState,-2);
KopiLua.Lua.lua_pushstring(luaState,remainingPath[i]);
KopiLua.Lua.lua_gettable(luaState,-2);
returnValue=translator.getObject(luaState,-1);
if(returnValue==null) break;
}
......@@ -561,7 +561,8 @@ namespace LuaInterface
public LuaFunction GetFunction(string fullPath)
{
object obj=this[fullPath];
return (obj is LuaCSFunction ? new LuaFunction((LuaCSFunction)obj,this) : (LuaFunction)obj);
//return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : (LuaFunction)obj);
return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this));
}
/*
* Gets a function global variable as a delegate of
......@@ -589,8 +590,8 @@ namespace LuaInterface
internal object[] callFunction(object function,object[] args,Type[] returnTypes)
{
int nArgs=0;
int oldTop=LuaDLL.lua_gettop(luaState);
if(!LuaDLL.lua_checkstack(luaState,args.Length+6))
int oldTop=KopiLua.Lua.lua_gettop(luaState);
if(!KopiLua.Lua.lua_checkstack(luaState,args.Length+6).ToBoolean())
throw new LuaException("Lua stack overflow");
translator.push(luaState,function);
if(args!=null)
......@@ -604,7 +605,7 @@ namespace LuaInterface
executing = true;
try
{
int error = LuaDLL.lua_pcall(luaState, nArgs, -1, 0);
int error = KopiLua.Lua.lua_pcall(luaState, nArgs, -1, 0);
if (error != 0)
ThrowExceptionFromError(oldTop);
}
......@@ -622,12 +623,12 @@ namespace LuaInterface
{
for(int i=0; i<remainingPath.Length-1;i++)
{
LuaDLL.lua_pushstring(luaState,remainingPath[i]);
LuaDLL.lua_gettable(luaState,-2);
KopiLua.Lua.lua_pushstring(luaState,remainingPath[i]);
KopiLua.Lua.lua_gettable(luaState,-2);
}
LuaDLL.lua_pushstring(luaState,remainingPath[remainingPath.Length-1]);
KopiLua.Lua.lua_pushstring(luaState,remainingPath[remainingPath.Length-1]);
translator.push(luaState,val);
LuaDLL.lua_settable(luaState,-3);
KopiLua.Lua.lua_settable(luaState,-3);
}
/*
* Creates a new table as a global variable or as a field
......@@ -636,40 +637,40 @@ namespace LuaInterface
public void NewTable(string fullPath)
{
string[] path=fullPath.Split(new char[] { '.' });
int oldTop=LuaDLL.lua_gettop(luaState);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
if(path.Length==1)
{
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_setglobal(luaState,fullPath);
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_setglobal(luaState,fullPath);
}
else
{
LuaDLL.lua_getglobal(luaState,path[0]);
KopiLua.Lua.lua_getglobal(luaState,path[0]);
for(int i=1; i<path.Length-1;i++)
{
LuaDLL.lua_pushstring(luaState,path[i]);
LuaDLL.lua_gettable(luaState,-2);
KopiLua.Lua.lua_pushstring(luaState,path[i]);
KopiLua.Lua.lua_gettable(luaState,-2);
}
LuaDLL.lua_pushstring(luaState,path[path.Length-1]);
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,path[path.Length-1]);
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_settable(luaState,-3);
}
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
}
public ListDictionary GetTableDict(LuaTable table)
{
ListDictionary dict = new ListDictionary();
int oldTop = LuaDLL.lua_gettop(luaState);
int oldTop = KopiLua.Lua.lua_gettop(luaState);
translator.push(luaState, table);
LuaDLL.lua_pushnil(luaState);
while (LuaDLL.lua_next(luaState, -2) != 0)
KopiLua.Lua.lua_pushnil(luaState);
while (KopiLua.Lua.lua_next(luaState, -2) != 0)
{
dict[translator.getObject(luaState, -2)] = translator.getObject(luaState, -1);
LuaDLL.lua_settop(luaState, -2);
KopiLua.Lua.lua_settop(luaState, -2);
}
LuaDLL.lua_settop(luaState, oldTop);
KopiLua.Lua.lua_settop(luaState, oldTop);
return dict;
}
......@@ -685,7 +686,7 @@ namespace LuaInterface
/// lua hook calback delegate
/// </summary>
/// <author>Reinhard Ostermeier</author>
private LuaHookFunction hookCallback = null;
private KopiLua.Lua.lua_Hook hookCallback = null;
/// <summary>
/// Activates the debug hook
......@@ -694,15 +695,15 @@ namespace LuaInterface
/// <param name="count">Count</param>
/// <returns>see lua docs. -1 if hook is already set</returns>
/// <author>Reinhard Ostermeier</author>
public int SetDebugHook(EventMasks mask, int count)
/*public int SetDebugHook(EventMasks mask, int count)
{
if (hookCallback == null)
{
hookCallback = new LuaHookFunction(DebugHookCallback);
return LuaDLL.lua_sethook(luaState, hookCallback, (int)mask, count);
hookCallback = new KopiLua.Lua.lua_Hook(DebugHookCallback);
return KopiLua.Lua.lua_sethook(luaState, hookCallback, (int)mask, count);
}
return -1;
}
}*/
/// <summary>
/// Removes the debug hook
......@@ -712,7 +713,7 @@ namespace LuaInterface
public int RemoveDebugHook()
{
hookCallback = null;
return LuaDLL.lua_sethook(luaState, null, 0, 0);
return KopiLua.Lua.lua_sethook(luaState, null, 0, 0);
}
/// <summary>
......@@ -722,7 +723,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author>
public EventMasks GetHookMask()
{
return (EventMasks)LuaDLL.lua_gethookmask(luaState);
return (EventMasks)KopiLua.Lua.lua_gethookmask(luaState);
}
/// <summary>
......@@ -732,7 +733,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author>
public int GetHookCount()
{
return LuaDLL.lua_gethookcount(luaState);
return KopiLua.Lua.lua_gethookcount(luaState);
}
/// <summary>
......@@ -742,21 +743,21 @@ namespace LuaInterface
/// <param name="luaDebug">lua debug structure</param>
/// <returns>Returns true if level was allowed, false if level was invalid.</returns>
/// <author>Reinhard Ostermeier</author>
public bool GetStack(int level, out LuaDebug luaDebug)
/*public bool GetStack(int level, out LuaDebug luaDebug)
{
luaDebug = new LuaDebug();
IntPtr ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
KopiLua.Lua.lua_State ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try
{
return LuaDLL.lua_getstack(luaState, level, ld) != 0;
return KopiLua.Lua.lua_getstack(luaState, level, ld) != 0;
}
finally
{
luaDebug = (LuaDebug)System.Runtime.InteropServices.Marshal.PtrToStructure(ld, typeof(LuaDebug));
System.Runtime.InteropServices.Marshal.FreeHGlobal(ld);
}
}
}*/
/// <summary>
/// Gets info (see lua docs)
......@@ -765,20 +766,20 @@ namespace LuaInterface
/// <param name="luaDebug">lua debug structure</param>
/// <returns>see lua docs</returns>
/// <author>Reinhard Ostermeier</author>
public int GetInfo(String what, ref LuaDebug luaDebug)
/*public int GetInfo(String what, ref LuaDebug luaDebug)
{
IntPtr ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
KopiLua.Lua.lua_State ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try
{
return LuaDLL.lua_getinfo(luaState, what, ld);
return KopiLua.Lua.lua_getinfo(luaState, what, ld);
}
finally
{
luaDebug = (LuaDebug)System.Runtime.InteropServices.Marshal.PtrToStructure(ld, typeof(LuaDebug));
System.Runtime.InteropServices.Marshal.FreeHGlobal(ld);
}
}
}*/
/// <summary>
/// Gets local (see lua docs)
......@@ -787,19 +788,19 @@ namespace LuaInterface
/// <param name="n">see lua docs</param>
/// <returns>see lua docs</returns>
/// <author>Reinhard Ostermeier</author>
public String GetLocal(LuaDebug luaDebug, int n)
/*public String GetLocal(LuaDebug luaDebug, int n)
{
IntPtr ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
KopiLua.Lua.lua_State ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try
{
return LuaDLL.lua_getlocal(luaState, ld, n);
return KopiLua.Lua.lua_getlocal(luaState, ld, n);
}
finally
{
System.Runtime.InteropServices.Marshal.FreeHGlobal(ld);
}
}
}*/
/// <summary>
/// Sets local (see lua docs)
......@@ -808,19 +809,19 @@ namespace LuaInterface
/// <param name="n">see lua docs</param>
/// <returns>see lua docs</returns>
/// <author>Reinhard Ostermeier</author>
public String SetLocal(LuaDebug luaDebug, int n)
/*public String SetLocal(LuaDebug luaDebug, int n)
{
IntPtr ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
KopiLua.Lua.lua_State ld = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(luaDebug));
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try
{
return LuaDLL.lua_setlocal(luaState, ld, n);
return KopiLua.Lua.lua_setlocal(luaState, ld, n);
}
finally
{
System.Runtime.InteropServices.Marshal.FreeHGlobal(ld);
}
}
}*/
/// <summary>
/// Gets up value (see lua docs)
......@@ -831,7 +832,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author>
public String GetUpValue(int funcindex, int n)
{
return LuaDLL.lua_getupvalue(luaState, funcindex, n);
return KopiLua.Lua.lua_getupvalue(luaState, funcindex, n).ToString();
}
/// <summary>
......@@ -843,7 +844,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author>
public String SetUpValue(int funcindex, int n)
{
return LuaDLL.lua_setupvalue(luaState, funcindex, n);
return KopiLua.Lua.lua_setupvalue(luaState, funcindex, n).ToString();
}
/// <summary>
......@@ -852,7 +853,7 @@ namespace LuaInterface
/// <param name="luaState">lua state</param>
/// <param name="luaDebug">Pointer to LuaDebug (lua_debug) structure</param>
/// <author>Reinhard Ostermeier</author>
private void DebugHookCallback(IntPtr luaState, IntPtr luaDebug)
/*private void DebugHookCallback(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_Debug luaDebug)
{
try
{
......@@ -867,7 +868,7 @@ namespace LuaInterface
{
OnHookException(new HookExceptionEventArgs(ex));
}
}
}*/
/// <summary>
/// Event that is raised when an exception occures during a hook call.
......@@ -899,7 +900,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author>
public object Pop()
{
int top = Lua511.LuaDLL.lua_gettop(luaState);
int top = KopiLua.Lua.lua_gettop(luaState);
return translator.popValues(luaState, top - 1)[0];
}
......@@ -917,8 +918,9 @@ namespace LuaInterface
internal void dispose(int reference)
{
if (luaState != IntPtr.Zero) //Fix submitted by Qingrui Li
LuaDLL.lua_unref(luaState,reference);
///////////// if (luaState != KopiLua.Lua.lua_State.Zero)
if (luaState != null) //Fix submitted by Qingrui Li
LuaLib.lua_unref(luaState,reference);
}
/*
* Gets a field of the table corresponding to the provided reference
......@@ -926,12 +928,12 @@ namespace LuaInterface
*/
internal object rawGetObject(int reference,string field)
{
int oldTop=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,reference);
LuaDLL.lua_pushstring(luaState,field);
LuaDLL.lua_rawget(luaState,-2);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,reference);
KopiLua.Lua.lua_pushstring(luaState,field);
KopiLua.Lua.lua_rawget(luaState,-2);
object obj=translator.getObject(luaState,-1);
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return obj;
}
/*
......@@ -939,10 +941,10 @@ namespace LuaInterface
*/
internal object getObject(int reference,string field)
{
int oldTop=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,reference);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,reference);
object returnValue=getObject(field.Split(new char[] {'.'}));
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return returnValue;
}
/*
......@@ -950,12 +952,12 @@ namespace LuaInterface
*/
internal object getObject(int reference,object field)
{
int oldTop=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,reference);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,reference);
translator.push(luaState,field);
LuaDLL.lua_gettable(luaState,-2);
KopiLua.Lua.lua_gettable(luaState,-2);
object returnValue=translator.getObject(luaState,-1);
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return returnValue;
}
/*
......@@ -964,10 +966,10 @@ namespace LuaInterface
*/
internal void setObject(int reference, string field, object val)
{
int oldTop=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,reference);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,reference);
setObject(field.Split(new char[] {'.'}),val);
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
}
/*
* Sets a numeric field of the table or userdata corresponding the the provided reference
......@@ -975,12 +977,12 @@ namespace LuaInterface
*/
internal void setObject(int reference, object field, object val)
{
int oldTop=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,reference);
int oldTop=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,reference);
translator.push(luaState,field);
translator.push(luaState,val);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,oldTop);
}
/*
......@@ -990,15 +992,15 @@ namespace LuaInterface
public LuaFunction RegisterFunction(string path, object target, MethodBase function /*MethodInfo function*/) //CP: Fix for struct constructor by Alexander Kappner (link: http://luaforge.net/forum/forum.php?thread_id=2859&forum_id=145)
{
// We leave nothing on the stack when we are done
int oldTop = LuaDLL.lua_gettop(luaState);
int oldTop = KopiLua.Lua.lua_gettop(luaState);
LuaMethodWrapper wrapper=new LuaMethodWrapper(translator,target,function.DeclaringType,function);
translator.push(luaState,new LuaCSFunction(wrapper.call));
translator.push(luaState,new KopiLua.Lua.lua_CFunction(wrapper.call));
this[path]=translator.getObject(luaState,-1);
LuaFunction f = GetFunction(path);
LuaDLL.lua_settop(luaState, oldTop);
KopiLua.Lua.lua_settop(luaState, oldTop);
return f;
}
......@@ -1009,15 +1011,15 @@ namespace LuaInterface
*/
internal bool compareRef(int ref1, int ref2)
{
int top=LuaDLL.lua_gettop(luaState);
LuaDLL.lua_getref(luaState,ref1);
LuaDLL.lua_getref(luaState,ref2);
int equal=LuaDLL.lua_equal(luaState,-1,-2);
LuaDLL.lua_settop(luaState,top);
int top=KopiLua.Lua.lua_gettop(luaState);
LuaLib.lua_getref(luaState,ref1);
LuaLib.lua_getref(luaState,ref2);
int equal=KopiLua.Lua.lua_equal(luaState,-1,-2);
KopiLua.Lua.lua_settop(luaState,top);
return (equal!=0);
}
internal void pushCSFunction(LuaCSFunction function)
internal void pushCSFunction(KopiLua.Lua.lua_CFunction function)
{
translator.pushFunction(luaState,function);
}
......
......@@ -3,22 +3,26 @@ using System.Runtime.Serialization;
namespace LuaInterface
{
/// <summary>
/// Exceptions thrown by the Lua runtime
/// </summary>
[Serializable]
public class LuaException : Exception
{
public LuaException()
{}
/// <summary>
/// Exceptions thrown by the Lua runtime
/// </summary>
[Serializable]
public class LuaException : Exception
{
public LuaException()
{
}
public LuaException(string message) : base(message)
{}
public LuaException(string message) : base(message)
{
}
public LuaException(string message, Exception innerException) : base(message, innerException)
{}
public LuaException(string message, Exception innerException) : base(message, innerException)
{
}
protected LuaException(SerializationInfo info, StreamingContext context) : base(info, context)
{}
}
protected LuaException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
using LuaWrap;
namespace LuaInterface
{
public class LuaFunction : LuaBase
{
//private Lua interpreter;
internal LuaCSFunction function;
internal KopiLua.Lua.lua_CFunction function;
//internal int reference;
public LuaFunction(int reference, Lua interpreter)
......@@ -18,7 +18,7 @@ namespace LuaInterface
_Interpreter = interpreter;
}
public LuaFunction(LuaCSFunction function, Lua interpreter)
public LuaFunction(KopiLua.Lua.lua_CFunction function, Lua interpreter)
{
_Reference = 0;
this.function = function;
......@@ -77,10 +77,10 @@ namespace LuaInterface
/*
* Pushes the function into the Lua stack
*/
internal void push(IntPtr luaState)
internal void push(KopiLua.Lua.lua_State luaState)
{
if (_Reference != 0)
LuaDLL.lua_getref(luaState, _Reference);
LuaLib.lua_getref(luaState, _Reference);
else
_Interpreter.pushCSFunction(function);
}
......
......@@ -20,4 +20,4 @@ namespace LuaInterface
/// </summary>
public string Description { get; set; }
}
}
}
\ No newline at end of file
......@@ -7,5 +7,6 @@ namespace LuaInterface
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property)]
public sealed class LuaHideAttribute : Attribute
{}
}
{
}
}
\ No newline at end of file
......@@ -82,7 +82,7 @@ namespace LuaWrap
NoRef = -2
}
public enum LuaType : int
public enum LuaTypes : int
{
None = -1,
Nil = 0,
......@@ -150,9 +150,9 @@ namespace LuaWrap
{
private static int tag = 0;
public static LuaType ToLuaType(this int type)
public static LuaTypes ToLuaTypes(this int type)
{
return (LuaType)type;
return (LuaTypes)type;
}
public static LuaEnum ToLuaEnum(this int lenum)
......@@ -324,7 +324,7 @@ namespace LuaWrap
{
int udata;
if(Lua.lua_type(luaState, index).ToLuaType() == LuaType.UserData)
if(Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData)
{
if(luaL_checkmetatable(luaState, index))
{
......
......@@ -87,4 +87,4 @@ namespace LuaInterface
}
#endregion
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
using System.Collections;
using LuaWrap;
namespace LuaInterface
{
......@@ -109,8 +109,8 @@ namespace LuaInterface
{
object obj = _Interpreter.rawGetObject(_Reference, field);
if (obj is LuaCSFunction)
return new LuaFunction((LuaCSFunction)obj, _Interpreter);
if (obj is KopiLua.Lua.lua_CFunction)
return new LuaFunction((KopiLua.Lua.lua_CFunction)obj, _Interpreter);
else
return obj;
}
......@@ -118,9 +118,9 @@ namespace LuaInterface
/*
* Pushes this table into the Lua stack
*/
internal void push(IntPtr luaState)
internal void push(KopiLua.Lua.lua_State luaState)
{
LuaDLL.lua_getref(luaState, _Reference);
LuaLib.lua_getref(luaState, _Reference);
}
public override string ToString()
{
......
using System;
using System.Collections.Generic;
using System.Text;
using Lua511;
using LuaWrap;
namespace LuaInterface
{
......@@ -58,9 +58,9 @@ namespace LuaInterface
/*
* Pushes the userdata into the Lua stack
*/
internal void push(IntPtr luaState)
internal void push(KopiLua.Lua.lua_State luaState)
{
LuaDLL.lua_getref(luaState, _Reference);
LuaLib.lua_getref(luaState, _Reference);
}
public override string ToString()
{
......
......@@ -6,8 +6,8 @@ namespace LuaInterface
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using Lua511;
using System.Runtime.InteropServices;
using LuaWrap;
/*
* Functions used in the metatables of userdata representing
......@@ -39,39 +39,39 @@ namespace LuaInterface
private ObjectTranslator translator;
private Hashtable memberCache = new Hashtable();
internal LuaCSFunction gcFunction, indexFunction, newindexFunction,
internal KopiLua.Lua.lua_CFunction gcFunction, indexFunction, newindexFunction,
baseIndexFunction, classIndexFunction, classNewindexFunction,
execDelegateFunction, callConstructorFunction, toStringFunction;
public MetaFunctions(ObjectTranslator translator)
{
this.translator = translator;
gcFunction = new LuaCSFunction(this.collectObject);
toStringFunction = new LuaCSFunction(this.toString);
indexFunction = new LuaCSFunction(this.getMethod);
newindexFunction = new LuaCSFunction(this.setFieldOrProperty);
baseIndexFunction = new LuaCSFunction(this.getBaseMethod);
callConstructorFunction = new LuaCSFunction(this.callConstructor);
classIndexFunction = new LuaCSFunction(this.getClassMethod);
classNewindexFunction = new LuaCSFunction(this.setClassFieldOrProperty);
execDelegateFunction = new LuaCSFunction(this.runFunctionDelegate);
gcFunction = new KopiLua.Lua.lua_CFunction(this.collectObject);
toStringFunction = new KopiLua.Lua.lua_CFunction(this.toString);
indexFunction = new KopiLua.Lua.lua_CFunction(this.getMethod);
newindexFunction = new KopiLua.Lua.lua_CFunction(this.setFieldOrProperty);
baseIndexFunction = new KopiLua.Lua.lua_CFunction(this.getBaseMethod);
callConstructorFunction = new KopiLua.Lua.lua_CFunction(this.callConstructor);
classIndexFunction = new KopiLua.Lua.lua_CFunction(this.getClassMethod);
classNewindexFunction = new KopiLua.Lua.lua_CFunction(this.setClassFieldOrProperty);
execDelegateFunction = new KopiLua.Lua.lua_CFunction(this.runFunctionDelegate);
}
/*
* __call metafunction of CLR delegates, retrieves and calls the delegate.
*/
private int runFunctionDelegate(IntPtr luaState)
private int runFunctionDelegate(KopiLua.Lua.lua_State luaState)
{
LuaCSFunction func = (LuaCSFunction)translator.getRawNetObject(luaState, 1);
LuaDLL.lua_remove(luaState, 1);
KopiLua.Lua.lua_CFunction func = (KopiLua.Lua.lua_CFunction)translator.getRawNetObject(luaState, 1);
KopiLua.Lua.lua_remove(luaState, 1);
return func(luaState);
}
/*
* __gc metafunction of CLR objects.
*/
private int collectObject(IntPtr luaState)
private int collectObject(KopiLua.Lua.lua_State luaState)
{
int udata = LuaDLL.luanet_rawnetobj(luaState, 1);
int udata = LuaLib.luanet_rawnetobj(luaState, 1);
if (udata != -1)
{
translator.collectObject(udata);
......@@ -85,14 +85,14 @@ namespace LuaInterface
/*
* __tostring metafunction of CLR objects.
*/
private int toString(IntPtr luaState)
private int toString(KopiLua.Lua.lua_State luaState)
{
object obj = translator.getRawNetObject(luaState, 1);
if (obj != null)
{
translator.push(luaState, obj.ToString() + ": " + obj.GetHashCode());
}
else LuaDLL.lua_pushnil(luaState);
else KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
......@@ -101,19 +101,19 @@ namespace LuaInterface
/// Debug tool to dump the lua stack
/// </summary>
/// FIXME, move somewhere else
public static void dumpStack(ObjectTranslator translator, IntPtr luaState)
public static void dumpStack(ObjectTranslator translator, KopiLua.Lua.lua_State luaState)
{
int depth = LuaDLL.lua_gettop(luaState);
int depth = KopiLua.Lua.lua_gettop(luaState);
Debug.WriteLine("lua stack depth: " + depth);
for (int i = 1; i <= depth; i++)
{
LuaTypes type = LuaDLL.lua_type(luaState, i);
LuaTypes type = KopiLua.Lua.lua_type(luaState, i).ToLuaTypes();
// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
string typestr = (type == LuaTypes.LUA_TTABLE) ? "table" : LuaDLL.lua_typename(luaState, type);
string typestr = (type == LuaTypes.Table) ? "table" : KopiLua.Lua.lua_typename(luaState, Convert.ToInt32(type)).ToString();
string strrep = LuaDLL.lua_tostring(luaState, i);
if (type == LuaTypes.LUA_TUSERDATA)
string strrep = KopiLua.Lua.lua_tostring(luaState, i).ToString();
if (type == LuaTypes.UserData)
{
object obj = translator.getRawNetObject(luaState, i);
strrep = obj.ToString();
......@@ -131,13 +131,13 @@ namespace LuaInterface
* either the value of the member or a delegate to call it.
* If the member does not exist returns nil.
*/
private int getMethod(IntPtr luaState)
private int getMethod(KopiLua.Lua.lua_State luaState)
{
object obj = translator.getRawNetObject(luaState, 1);
if (obj == null)
{
translator.throwError(luaState, "trying to index an invalid object reference");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
......@@ -204,7 +204,7 @@ namespace LuaInterface
{
translator.throwError(luaState, "method not found (or no indexer): " + index);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
else
{
......@@ -228,7 +228,7 @@ namespace LuaInterface
else
translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
}
}
......@@ -238,7 +238,7 @@ namespace LuaInterface
}
LuaDLL.lua_pushboolean(luaState, false);
KopiLua.Lua.lua_pushboolean(luaState, 0);
return 2;
}
......@@ -247,31 +247,31 @@ namespace LuaInterface
* __index metafunction of base classes (the base field of Lua tables).
* Adds a prefix to the method name to call the base version of the method.
*/
private int getBaseMethod(IntPtr luaState)
private int getBaseMethod(KopiLua.Lua.lua_State luaState)
{
object obj = translator.getRawNetObject(luaState, 1);
if (obj == null)
{
translator.throwError(luaState, "trying to index an invalid object reference");
LuaDLL.lua_pushnil(luaState);
LuaDLL.lua_pushboolean(luaState, false);
KopiLua.Lua.lua_pushnil(luaState);
KopiLua.Lua.lua_pushboolean(luaState, 0);
return 2;
}
string methodName = LuaDLL.lua_tostring(luaState, 2);
string methodName = KopiLua.Lua.lua_tostring(luaState, 2).ToString();
if (methodName == null)
{
LuaDLL.lua_pushnil(luaState);
LuaDLL.lua_pushboolean(luaState, false);
KopiLua.Lua.lua_pushnil(luaState);
KopiLua.Lua.lua_pushboolean(luaState, 0);
return 2;
}
getMember(luaState, obj.GetType(), obj, "__luaInterface_base_" + methodName, BindingFlags.Instance | BindingFlags.IgnoreCase);
LuaDLL.lua_settop(luaState, -2);
if (LuaDLL.lua_type(luaState, -1) == LuaTypes.LUA_TNIL)
KopiLua.Lua.lua_settop(luaState, -2);
if (KopiLua.Lua.lua_type(luaState, -1).ToLuaTypes() == LuaTypes.Nil)
{
LuaDLL.lua_settop(luaState, -2);
KopiLua.Lua.lua_settop(luaState, -2);
return getMember(luaState, obj.GetType(), obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase);
}
LuaDLL.lua_pushboolean(luaState, false);
KopiLua.Lua.lua_pushboolean(luaState, 0);
return 2;
}
......@@ -300,15 +300,15 @@ namespace LuaInterface
* Uses reflection to find members, and stores the reflected MemberInfo object in
* a cache (indexed by the type of the object and the name of the member).
*/
private int getMember(IntPtr luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType)
private int getMember(KopiLua.Lua.lua_State luaState, IReflect objType, object obj, string methodName, BindingFlags bindingType)
{
bool implicitStatic = false;
MemberInfo member = null;
object cachedMember = checkMemberCache(memberCache, objType, methodName);
//object cachedMember=null;
if (cachedMember is LuaCSFunction)
if (cachedMember is KopiLua.Lua.lua_CFunction)
{
translator.pushFunction(luaState, (LuaCSFunction)cachedMember);
translator.pushFunction(luaState, (KopiLua.Lua.lua_CFunction)cachedMember);
translator.push(luaState, true);
return 2;
}
......@@ -348,7 +348,7 @@ namespace LuaInterface
}
catch
{
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Property)
......@@ -369,12 +369,12 @@ namespace LuaInterface
if (objType is Type && !(((Type)objType) == typeof(object)))
return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType);
else
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
catch (TargetInvocationException e) // Convert this exception into a Lua error
{
ThrowError(luaState, e);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
}
else if (member.MemberType == MemberTypes.Event)
......@@ -405,7 +405,7 @@ namespace LuaInterface
else
{
// Member type must be 'method'
LuaCSFunction wrapper = new LuaCSFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);
KopiLua.Lua.lua_CFunction wrapper = new KopiLua.Lua.lua_CFunction((new LuaMethodWrapper(translator, objType, methodName, bindingType)).call);
if (cachedMember == null) setMemberCache(memberCache, objType, methodName, wrapper);
translator.pushFunction(luaState, wrapper);
......@@ -418,7 +418,7 @@ namespace LuaInterface
// If we reach this point we found a static method, but can't use it in this context because the user passed in an instance
translator.throwError(luaState, "can't pass instance to static method " + methodName);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
}
else
......@@ -429,7 +429,7 @@ namespace LuaInterface
translator.throwError(luaState, "unknown member name " + methodName);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
// push false because we are NOT returning a function (see luaIndexFunction)
......@@ -465,7 +465,7 @@ namespace LuaInterface
* the member name and the value to be stored as arguments. Throws
* and error if the assignment is invalid.
*/
private int setFieldOrProperty(IntPtr luaState)
private int setFieldOrProperty(KopiLua.Lua.lua_State luaState)
{
object target = translator.getRawNetObject(luaState, 1);
if (target == null)
......@@ -485,9 +485,9 @@ namespace LuaInterface
// We didn't find a property name, now see if we can use a [] style this accessor to set array contents
try
{
if (type.IsArray && LuaDLL.lua_isnumber(luaState, 2))
if (type.IsArray && KopiLua.Lua.lua_isnumber(luaState, 2).ToBoolean())
{
int index = (int)LuaDLL.lua_tonumber(luaState, 2);
int index = (int)KopiLua.Lua.lua_tonumber(luaState, 2);
Array arr = (Array)target;
object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType());
......@@ -542,7 +542,7 @@ namespace LuaInterface
/// <param name="target"></param>
/// <param name="bindingType"></param>
/// <returns>false if unable to find the named member, true for success</returns>
private bool trySetMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage)
private bool trySetMember(KopiLua.Lua.lua_State luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage)
{
detailMessage = null; // No error yet
......@@ -550,14 +550,14 @@ namespace LuaInterface
// changing the lua typecode to string
// Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to
// be true for isstring.
if (LuaDLL.lua_type(luaState, 2) != LuaTypes.LUA_TSTRING)
if (KopiLua.Lua.lua_type(luaState, 2).ToLuaTypes() != LuaTypes.String)
{
detailMessage = "property names must be strings";
return false;
}
// We only look up property names by string
string fieldName = LuaDLL.lua_tostring(luaState, 2);
string fieldName = KopiLua.Lua.lua_tostring(luaState, 2).ToString();
if (fieldName == null || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_'))
{
detailMessage = "invalid property name";
......@@ -622,7 +622,7 @@ namespace LuaInterface
* Writes to fields or properties, either static or instance. Throws an error
* if the operation is invalid.
*/
private int setMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType)
private int setMember(KopiLua.Lua.lua_State luaState, IReflect targetType, object target, BindingFlags bindingType)
{
string detail;
bool success = trySetMember(luaState, targetType, target, bindingType, out detail);
......@@ -638,7 +638,7 @@ namespace LuaInterface
/// </summary>
/// <param name="e"></param>
/// We try to look into the exception to give the most meaningful description
void ThrowError(IntPtr luaState, Exception e)
void ThrowError(KopiLua.Lua.lua_State luaState, Exception e)
{
// If we got inside a reflection show what really happened
TargetInvocationException te = e as TargetInvocationException;
......@@ -652,29 +652,29 @@ namespace LuaInterface
/*
* __index metafunction of type references, works on static members.
*/
private int getClassMethod(IntPtr luaState)
private int getClassMethod(KopiLua.Lua.lua_State luaState)
{
IReflect klass;
object obj = translator.getRawNetObject(luaState, 1);
if (obj == null || !(obj is IReflect))
{
translator.throwError(luaState, "trying to index an invalid type reference");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
else klass = (IReflect)obj;
if (LuaDLL.lua_isnumber(luaState, 2))
if (KopiLua.Lua.lua_isnumber(luaState, 2).ToBoolean())
{
int size = (int)LuaDLL.lua_tonumber(luaState, 2);
int size = (int)KopiLua.Lua.lua_tonumber(luaState, 2);
translator.push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size));
return 1;
}
else
{
string methodName = LuaDLL.lua_tostring(luaState, 2);
string methodName = KopiLua.Lua.lua_tostring(luaState, 2).ToString();
if (methodName == null)
{
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
} //CP: Ignore case
else return getMember(luaState, klass, null, methodName, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.IgnoreCase);
......@@ -683,7 +683,7 @@ namespace LuaInterface
/*
* __newindex function of type references, works on static members.
*/
private int setClassFieldOrProperty(IntPtr luaState)
private int setClassFieldOrProperty(KopiLua.Lua.lua_State luaState)
{
IReflect target;
object obj = translator.getRawNetObject(luaState, 1);
......@@ -701,7 +701,7 @@ namespace LuaInterface
* found or if the arguments are invalid. Throws an error if the constructor
* generates an exception.
*/
private int callConstructor(IntPtr luaState)
private int callConstructor(KopiLua.Lua.lua_State luaState)
{
MethodCache validConstructor = new MethodCache();
IReflect klass;
......@@ -709,11 +709,11 @@ namespace LuaInterface
if (obj == null || !(obj is IReflect))
{
translator.throwError(luaState, "trying to call constructor on an invalid type reference");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
else klass = (IReflect)obj;
LuaDLL.lua_remove(luaState, 1);
KopiLua.Lua.lua_remove(luaState, 1);
ConstructorInfo[] constructors = klass.UnderlyingSystemType.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
......@@ -727,11 +727,11 @@ namespace LuaInterface
catch (TargetInvocationException e)
{
ThrowError(luaState, e);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
catch
{
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
return 1;
}
......@@ -742,7 +742,7 @@ namespace LuaInterface
translator.throwError(luaState, String.Format("{0} does not contain constructor({1}) argument match",
klass.UnderlyingSystemType,
constructorName));
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
/*
......@@ -750,13 +750,13 @@ namespace LuaInterface
* if the match was succesful. It it was also returns the information
* necessary to invoke the method.
*/
internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
internal bool matchParameters(KopiLua.Lua.lua_State luaState, MethodBase method, ref MethodCache methodCache)
{
ExtractValue extractValue;
bool isMethod = true;
ParameterInfo[] paramInfo = method.GetParameters();
int currentLuaParam = 1;
int nLuaParams = LuaDLL.lua_gettop(luaState);
int nLuaParams = KopiLua.Lua.lua_gettop(luaState);
ArrayList paramList = new ArrayList();
List<int> outList = new List<int>();
List<MethodArgs> argTypes = new List<MethodArgs>();
......@@ -864,7 +864,7 @@ namespace LuaInterface
/// <param name="currentNetParam"></param>
/// <param name="extractValue"></param>
/// <returns></returns>
private bool _IsTypeCorrect(IntPtr luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
private bool _IsTypeCorrect(KopiLua.Lua.lua_State luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
{
try
{
......@@ -878,7 +878,7 @@ namespace LuaInterface
}
}
private bool _IsParamsArray(IntPtr luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
private bool _IsParamsArray(KopiLua.Lua.lua_State luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue)
{
extractValue = null;
......@@ -888,7 +888,7 @@ namespace LuaInterface
try
{
luaType = LuaDLL.lua_type(luaState, currentLuaParam);
luaType = KopiLua.Lua.lua_type(luaState, currentLuaParam).ToLuaTypes();
}
catch (Exception ex)
{
......@@ -898,13 +898,13 @@ namespace LuaInterface
return false;
}
if (luaType == LuaTypes.LUA_TTABLE)
if (luaType == LuaTypes.Table)
{
try
{
extractValue = translator.typeChecker.getExtractor(typeof(LuaTable));
}
catch (Exception ex)
catch (Exception/* ex*/)
{
Debug.WriteLine("An error occurred during an attempt to retrieve a LuaTable extractor while checking for params array status.");
}
......@@ -922,7 +922,7 @@ namespace LuaInterface
{
extractValue = translator.typeChecker.checkType(luaState, currentLuaParam, paramElementType);
}
catch (Exception ex)
catch (Exception/* ex*/)
{
Debug.WriteLine(string.Format("An error occurred during an attempt to retrieve an extractor ({0}) while checking for params array status.", paramElementType.FullName));
}
......
......@@ -6,7 +6,7 @@ namespace LuaInterface
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using Lua511;
using LuaWrap;
/*
* Cached method
......@@ -60,7 +60,7 @@ namespace LuaInterface
/*
* Argument extraction with type-conversion function
*/
delegate object ExtractValue(IntPtr luaState, int stackPos);
delegate object ExtractValue(KopiLua.Lua.lua_State luaState, int stackPos);
/*
* Wrapper class for methods/constructors accessed from Lua.
......@@ -132,14 +132,14 @@ namespace LuaInterface
* Calls the method. Receives the arguments from the Lua stack
* and returns values in it.
*/
public int call(IntPtr luaState)
public int call(KopiLua.Lua.lua_State luaState)
{
MethodBase methodToCall = _Method;
object targetObject = _Target;
bool failedCall = true;
int nReturnValues = 0;
if (!LuaDLL.lua_checkstack(luaState, 5))
if (!KopiLua.Lua.lua_checkstack(luaState, 5).ToBoolean())
throw new LuaException("Lua stack overflow");
bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
......@@ -153,15 +153,15 @@ namespace LuaInterface
else
targetObject = _ExtractTarget(luaState, 1);
//LuaDLL.lua_remove(luaState,1); // Pops the receiver
//KopiLua.Lua.lua_remove(luaState,1); // Pops the receiver
if (_LastCalledMethod.cachedMethod != null) // Cached?
{
int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
int numArgsPassed = LuaDLL.lua_gettop(luaState) - numStackToSkip;
int numArgsPassed = KopiLua.Lua.lua_gettop(luaState) - numStackToSkip;
if (numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match?
{
if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
if (!KopiLua.Lua.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
......@@ -202,7 +202,7 @@ namespace LuaInterface
}
if (_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
!LuaDLL.lua_isnil(luaState, i + 1 + numStackToSkip))
!KopiLua.Lua.lua_isnil(luaState, i + 1 + numStackToSkip))
{
throw new LuaException("argument number " + (i + 1) + " is invalid");
}
......@@ -245,11 +245,11 @@ namespace LuaInterface
if (targetObject == null)
{
_Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
LuaDLL.lua_remove(luaState, 1); // Pops the receiver
KopiLua.Lua.lua_remove(luaState, 1); // Pops the receiver
}
bool hasMatch = false;
......@@ -275,7 +275,7 @@ namespace LuaInterface
: ("invalid arguments to method: " + candidateName);
_Translator.throwError(luaState, msg);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
......@@ -302,7 +302,7 @@ namespace LuaInterface
else if (methodToCall.ContainsGenericParameters)
{
_Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
......@@ -311,13 +311,13 @@ namespace LuaInterface
if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{
targetObject = _ExtractTarget(luaState, 1);
LuaDLL.lua_remove(luaState, 1); // Pops the receiver
KopiLua.Lua.lua_remove(luaState, 1); // Pops the receiver
}
if (!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{
_Translator.throwError(luaState, "invalid arguments to method call");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
......@@ -325,7 +325,7 @@ namespace LuaInterface
if (failedCall)
{
if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
if (!KopiLua.Lua.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
{
......
......@@ -6,7 +6,7 @@ namespace LuaInterface
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using Lua511;
using LuaWrap;
/*
* Passes objects from the CLR to Lua and vice-versa
......@@ -25,24 +25,24 @@ namespace LuaInterface
internal Lua interpreter;
private MetaFunctions metaFunctions;
private List<Assembly> assemblies;
private LuaCSFunction registerTableFunction,unregisterTableFunction,getMethodSigFunction,
private KopiLua.Lua.lua_CFunction registerTableFunction,unregisterTableFunction,getMethodSigFunction,
getConstructorSigFunction,importTypeFunction,loadAssemblyFunction;
internal EventHandlerContainer pendingEvents = new EventHandlerContainer();
public ObjectTranslator(Lua interpreter,IntPtr luaState)
public ObjectTranslator(Lua interpreter,KopiLua.Lua.lua_State luaState)
{
this.interpreter=interpreter;
typeChecker=new CheckType(this);
metaFunctions=new MetaFunctions(this);
assemblies=new List<Assembly>();
importTypeFunction=new LuaCSFunction(this.importType);
loadAssemblyFunction=new LuaCSFunction(this.loadAssembly);
registerTableFunction=new LuaCSFunction(this.registerTable);
unregisterTableFunction=new LuaCSFunction(this.unregisterTable);
getMethodSigFunction=new LuaCSFunction(this.getMethodSignature);
getConstructorSigFunction=new LuaCSFunction(this.getConstructorSignature);
importTypeFunction=new KopiLua.Lua.lua_CFunction(this.importType);
loadAssemblyFunction=new KopiLua.Lua.lua_CFunction(this.loadAssembly);
registerTableFunction=new KopiLua.Lua.lua_CFunction(this.registerTable);
unregisterTableFunction=new KopiLua.Lua.lua_CFunction(this.unregisterTable);
getMethodSigFunction=new KopiLua.Lua.lua_CFunction(this.getMethodSignature);
getConstructorSigFunction=new KopiLua.Lua.lua_CFunction(this.getConstructorSignature);
createLuaObjectList(luaState);
createIndexingMetaFunction(luaState);
......@@ -55,117 +55,117 @@ namespace LuaInterface
/*
* Sets up the list of objects in the Lua side
*/
private void createLuaObjectList(IntPtr luaState)
private void createLuaObjectList(KopiLua.Lua.lua_State luaState)
{
LuaDLL.lua_pushstring(luaState,"luaNet_objects");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_pushstring(luaState,"__mode");
LuaDLL.lua_pushstring(luaState,"v");
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_setmetatable(luaState,-2);
LuaDLL.lua_settable(luaState, (int) LuaIndexes.LUA_REGISTRYINDEX);
KopiLua.Lua.lua_pushstring(luaState,"luaNet_objects");
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_pushstring(luaState,"__mode");
KopiLua.Lua.lua_pushstring(luaState,"v");
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_setmetatable(luaState,-2);
KopiLua.Lua.lua_settable(luaState, (int) PseudoIndex.Registry);
}
/*
* Registers the indexing function of CLR objects
* passed to Lua
*/
private void createIndexingMetaFunction(IntPtr luaState)
private void createIndexingMetaFunction(KopiLua.Lua.lua_State luaState)
{
LuaDLL.lua_pushstring(luaState,"luaNet_indexfunction");
LuaDLL.luaL_dostring(luaState,MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring
//LuaDLL.lua_pushstdcallcfunction(luaState,indexFunction);
LuaDLL.lua_rawset(luaState, (int) LuaIndexes.LUA_REGISTRYINDEX);
KopiLua.Lua.lua_pushstring(luaState,"luaNet_indexfunction");
LuaLib.luaL_dostring(luaState,MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring
//LuaLib.lua_pushstdcallcfunction(luaState,indexFunction);
KopiLua.Lua.lua_rawset(luaState, (int) PseudoIndex.Registry);
}
/*
* Creates the metatable for superclasses (the base
* field of registered tables)
*/
private void createBaseClassMetatable(IntPtr luaState)
private void createBaseClassMetatable(KopiLua.Lua.lua_State luaState)
{
LuaDLL.luaL_newmetatable(luaState,"luaNet_searchbase");
LuaDLL.lua_pushstring(luaState,"__gc");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__tostring");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__index");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.baseIndexFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__newindex");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_settop(luaState,-2);
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_searchbase");
KopiLua.Lua.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.baseIndexFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2);
}
/*
* Creates the metatable for type references
*/
private void createClassMetatable(IntPtr luaState)
private void createClassMetatable(KopiLua.Lua.lua_State luaState)
{
LuaDLL.luaL_newmetatable(luaState,"luaNet_class");
LuaDLL.lua_pushstring(luaState,"__gc");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__tostring");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__index");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.classIndexFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__newindex");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.classNewindexFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__call");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.callConstructorFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_settop(luaState,-2);
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_class");
KopiLua.Lua.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classIndexFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classNewindexFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.callConstructorFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2);
}
/*
* Registers the global functions used by LuaInterface
*/
private void setGlobalFunctions(IntPtr luaState)
private void setGlobalFunctions(KopiLua.Lua.lua_State luaState)
{
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.indexFunction);
LuaDLL.lua_setglobal(luaState,"get_object_member");
LuaDLL.lua_pushstdcallcfunction(luaState,importTypeFunction);
LuaDLL.lua_setglobal(luaState,"import_type");
LuaDLL.lua_pushstdcallcfunction(luaState,loadAssemblyFunction);
LuaDLL.lua_setglobal(luaState,"load_assembly");
LuaDLL.lua_pushstdcallcfunction(luaState,registerTableFunction);
LuaDLL.lua_setglobal(luaState,"make_object");
LuaDLL.lua_pushstdcallcfunction(luaState,unregisterTableFunction);
LuaDLL.lua_setglobal(luaState,"free_object");
LuaDLL.lua_pushstdcallcfunction(luaState,getMethodSigFunction);
LuaDLL.lua_setglobal(luaState,"get_method_bysig");
LuaDLL.lua_pushstdcallcfunction(luaState,getConstructorSigFunction);
LuaDLL.lua_setglobal(luaState,"get_constructor_bysig");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.indexFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_object_member");
LuaLib.lua_pushstdcallcfunction(luaState,importTypeFunction);
KopiLua.Lua.lua_setglobal(luaState,"import_type");
LuaLib.lua_pushstdcallcfunction(luaState,loadAssemblyFunction);
KopiLua.Lua.lua_setglobal(luaState,"load_assembly");
LuaLib.lua_pushstdcallcfunction(luaState,registerTableFunction);
KopiLua.Lua.lua_setglobal(luaState,"make_object");
LuaLib.lua_pushstdcallcfunction(luaState,unregisterTableFunction);
KopiLua.Lua.lua_setglobal(luaState,"free_object");
LuaLib.lua_pushstdcallcfunction(luaState,getMethodSigFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_method_bysig");
LuaLib.lua_pushstdcallcfunction(luaState,getConstructorSigFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_constructor_bysig");
}
/*
* Creates the metatable for delegates
*/
private void createFunctionMetatable(IntPtr luaState)
private void createFunctionMetatable(KopiLua.Lua.lua_State luaState)
{
LuaDLL.luaL_newmetatable(luaState,"luaNet_function");
LuaDLL.lua_pushstring(luaState,"__gc");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__call");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.execDelegateFunction);
LuaDLL.lua_settable(luaState,-3);
LuaDLL.lua_settop(luaState,-2);
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_function");
KopiLua.Lua.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.execDelegateFunction);
KopiLua.Lua.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2);
}
/*
* Passes errors (argument e) to the Lua interpreter
*/
internal void throwError(IntPtr luaState, object e)
internal void throwError(KopiLua.Lua.lua_State luaState, object e)
{
// We use this to remove anything pushed by luaL_where
int oldTop = LuaDLL.lua_gettop(luaState);
int oldTop = KopiLua.Lua.lua_gettop(luaState);
// Stack frame #1 is our C# wrapper, so not very interesting to the user
// Stack frame #2 must be the lua code that called us, so that's what we want to use
LuaDLL.luaL_where(luaState, 1);
KopiLua.Lua.luaL_where(luaState, 1);
object[] curlev = popValues(luaState, oldTop);
// Determine the position in the script where the exception was triggered
......@@ -190,17 +190,17 @@ namespace LuaInterface
}
push(luaState, e);
LuaDLL.lua_error(luaState);
KopiLua.Lua.lua_error(luaState);
}
/*
* Implementation of load_assembly. Throws an error
* if the assembly is not found.
*/
private int loadAssembly(IntPtr luaState)
private int loadAssembly(KopiLua.Lua.lua_State luaState)
{
try
{
string assemblyName=LuaDLL.lua_tostring(luaState,1);
string assemblyName=KopiLua.Lua.lua_tostring(luaState,1).ToString();
Assembly assembly = null;
......@@ -248,14 +248,14 @@ namespace LuaInterface
* Implementation of import_type. Returns nil if the
* type is not found.
*/
private int importType(IntPtr luaState)
private int importType(KopiLua.Lua.lua_State luaState)
{
string className=LuaDLL.lua_tostring(luaState,1);
string className=KopiLua.Lua.lua_tostring(luaState,1).ToString();
Type klass=FindType(className);
if(klass!=null)
pushType(luaState,klass);
else
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
/*
......@@ -263,12 +263,12 @@ namespace LuaInterface
* argument in the stack) as an object subclassing the
* type passed as second argument in the stack.
*/
private int registerTable(IntPtr luaState)
private int registerTable(KopiLua.Lua.lua_State luaState)
{
if(LuaDLL.lua_type(luaState,1)==LuaTypes.LUA_TTABLE)
if(KopiLua.Lua.lua_type(luaState,1).ToLuaTypes()==LuaTypes.Table)
{
LuaTable luaTable=getTable(luaState,1);
string superclassName = LuaDLL.lua_tostring(luaState, 2);
string superclassName = KopiLua.Lua.lua_tostring(luaState, 2).ToString();
if (superclassName != null)
{
Type klass = FindType(superclassName);
......@@ -278,20 +278,20 @@ namespace LuaInterface
// it as the metatable of the first argument
object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
pushObject(luaState, obj, "luaNet_metatable");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_pushstring(luaState, "__index");
LuaDLL.lua_pushvalue(luaState, -3);
LuaDLL.lua_settable(luaState, -3);
LuaDLL.lua_pushstring(luaState, "__newindex");
LuaDLL.lua_pushvalue(luaState, -3);
LuaDLL.lua_settable(luaState, -3);
LuaDLL.lua_setmetatable(luaState, 1);
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_pushstring(luaState, "__index");
KopiLua.Lua.lua_pushvalue(luaState, -3);
KopiLua.Lua.lua_settable(luaState, -3);
KopiLua.Lua.lua_pushstring(luaState, "__newindex");
KopiLua.Lua.lua_pushvalue(luaState, -3);
KopiLua.Lua.lua_settable(luaState, -3);
KopiLua.Lua.lua_setmetatable(luaState, 1);
// Pushes the object again, this time as the base field
// of the table and with the luaNet_searchbase metatable
LuaDLL.lua_pushstring(luaState, "base");
KopiLua.Lua.lua_pushstring(luaState, "base");
int index = addObject(obj);
pushNewObject(luaState, obj, index, "luaNet_searchbase");
LuaDLL.lua_rawset(luaState, 1);
KopiLua.Lua.lua_rawset(luaState, 1);
}
else
throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
......@@ -306,24 +306,24 @@ namespace LuaInterface
* Implementation of free_object. Clears the metatable and the
* base field, freeing the created object for garbage-collection
*/
private int unregisterTable(IntPtr luaState)
private int unregisterTable(KopiLua.Lua.lua_State luaState)
{
try
{
if(LuaDLL.lua_getmetatable(luaState,1)!=0)
if(KopiLua.Lua.lua_getmetatable(luaState,1)!=0)
{
LuaDLL.lua_pushstring(luaState,"__index");
LuaDLL.lua_gettable(luaState,-2);
KopiLua.Lua.lua_pushstring(luaState,"__index");
KopiLua.Lua.lua_gettable(luaState,-2);
object obj=getRawNetObject(luaState,-1);
if(obj==null) throwError(luaState,"unregister_table: arg is not valid table");
FieldInfo luaTableField=obj.GetType().GetField("__luaInterface_luaTable");
if(luaTableField==null) throwError(luaState,"unregister_table: arg is not valid table");
luaTableField.SetValue(obj,null);
LuaDLL.lua_pushnil(luaState);
LuaDLL.lua_setmetatable(luaState,1);
LuaDLL.lua_pushstring(luaState,"base");
LuaDLL.lua_pushnil(luaState);
LuaDLL.lua_settable(luaState,1);
KopiLua.Lua.lua_pushnil(luaState);
KopiLua.Lua.lua_setmetatable(luaState,1);
KopiLua.Lua.lua_pushstring(luaState,"base");
KopiLua.Lua.lua_pushnil(luaState);
KopiLua.Lua.lua_settable(luaState,1);
}
else throwError(luaState,"unregister_table: arg is not valid table");
}
......@@ -337,10 +337,10 @@ namespace LuaInterface
* Implementation of get_method_bysig. Returns nil
* if no matching method is not found.
*/
private int getMethodSignature(IntPtr luaState)
private int getMethodSignature(KopiLua.Lua.lua_State luaState)
{
IReflect klass; object target;
int udata=LuaDLL.luanet_checkudata(luaState,1,"luaNet_class");
int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class");
if(udata!=-1)
{
klass=(IReflect)objects[udata];
......@@ -352,26 +352,26 @@ namespace LuaInterface
if(target==null)
{
throwError(luaState,"get_method_bysig: first arg is not type or object reference");
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
klass=target.GetType();
}
string methodName=LuaDLL.lua_tostring(luaState,2);
Type[] signature=new Type[LuaDLL.lua_gettop(luaState)-2];
string methodName=KopiLua.Lua.lua_tostring(luaState,2).ToString();
Type[] signature=new Type[KopiLua.Lua.lua_gettop(luaState)-2];
for(int i=0;i<signature.Length;i++)
signature[i]=FindType(LuaDLL.lua_tostring(luaState,i+3));
signature[i]=FindType(KopiLua.Lua.lua_tostring(luaState,i+3).ToString());
try
{
//CP: Added ignore case
MethodInfo method=klass.GetMethod(methodName,BindingFlags.Public | BindingFlags.Static |
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null);
pushFunction(luaState,new LuaCSFunction((new LuaMethodWrapper(this,target,klass,method)).call));
pushFunction(luaState,new KopiLua.Lua.lua_CFunction((new LuaMethodWrapper(this,target,klass,method)).call));
}
catch(Exception e)
{
throwError(luaState,e);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
return 1;
}
......@@ -379,10 +379,10 @@ namespace LuaInterface
* Implementation of get_constructor_bysig. Returns nil
* if no matching constructor is found.
*/
private int getConstructorSignature(IntPtr luaState)
private int getConstructorSignature(KopiLua.Lua.lua_State luaState)
{
IReflect klass=null;
int udata=LuaDLL.luanet_checkudata(luaState,1,"luaNet_class");
int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class");
if(udata!=-1)
{
klass=(IReflect)objects[udata];
......@@ -391,32 +391,32 @@ namespace LuaInterface
{
throwError(luaState,"get_constructor_bysig: first arg is invalid type reference");
}
Type[] signature=new Type[LuaDLL.lua_gettop(luaState)-1];
Type[] signature=new Type[KopiLua.Lua.lua_gettop(luaState)-1];
for(int i=0;i<signature.Length;i++)
signature[i]=FindType(LuaDLL.lua_tostring(luaState,i+2));
signature[i]=FindType(KopiLua.Lua.lua_tostring(luaState,i+2).ToString());
try
{
ConstructorInfo constructor=klass.UnderlyingSystemType.GetConstructor(signature);
pushFunction(luaState,new LuaCSFunction((new LuaMethodWrapper(this,null,klass,constructor)).call));
pushFunction(luaState,new KopiLua.Lua.lua_CFunction((new LuaMethodWrapper(this,null,klass,constructor)).call));
}
catch(Exception e)
{
throwError(luaState,e);
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
return 1;
}
/*
* Pushes a type reference into the stack
*/
internal void pushType(IntPtr luaState, Type t)
internal void pushType(KopiLua.Lua.lua_State luaState, Type t)
{
pushObject(luaState,new ProxyType(t),"luaNet_class");
}
/*
* Pushes a delegate into the stack
*/
internal void pushFunction(IntPtr luaState, LuaCSFunction func)
internal void pushFunction(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_CFunction func)
{
pushObject(luaState,func,"luaNet_function");
}
......@@ -424,13 +424,13 @@ namespace LuaInterface
* Pushes a CLR object into the Lua stack as an userdata
* with the provided metatable
*/
internal void pushObject(IntPtr luaState, object o, string metatable)
internal void pushObject(KopiLua.Lua.lua_State luaState, object o, string metatable)
{
int index = -1;
// Pushes nil
if(o==null)
{
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
return;
}
......@@ -438,25 +438,25 @@ namespace LuaInterface
bool found = objectsBackMap.TryGetValue(o, out index);
if(found)
{
LuaDLL.luaL_getmetatable(luaState,"luaNet_objects");
LuaDLL.lua_rawgeti(luaState,-1,index);
KopiLua.Lua.luaL_getmetatable(luaState,"luaNet_objects");
KopiLua.Lua.lua_rawgeti(luaState,-1,index);
// Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep
// occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call
// this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect
// object here
// did we find a non nil object in our table? if not, we need to call collect object
LuaTypes type = LuaDLL.lua_type(luaState, -1);
if (type != LuaTypes.LUA_TNIL)
LuaTypes type = KopiLua.Lua.lua_type(luaState, -1).ToLuaTypes();
if (type != LuaTypes.Nil)
{
LuaDLL.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack
KopiLua.Lua.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack
return;
}
// MetaFunctions.dumpStack(this, luaState);
LuaDLL.lua_remove(luaState, -1); // remove the nil object value
LuaDLL.lua_remove(luaState, -1); // remove the metatable
KopiLua.Lua.lua_remove(luaState, -1); // remove the nil object value
KopiLua.Lua.lua_remove(luaState, -1); // remove the metatable
collectObject(o, index); // Remove from both our tables and fall out to get a new ID
}
......@@ -470,59 +470,59 @@ namespace LuaInterface
* Pushes a new object into the Lua stack with the provided
* metatable
*/
private void pushNewObject(IntPtr luaState,object o,int index,string metatable)
private void pushNewObject(KopiLua.Lua.lua_State luaState,object o,int index,string metatable)
{
if(metatable=="luaNet_metatable")
{
// Gets or creates the metatable for the object's type
LuaDLL.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName);
KopiLua.Lua.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName);
if(LuaDLL.lua_isnil(luaState,-1))
if(KopiLua.Lua.lua_isnil(luaState,-1))
{
LuaDLL.lua_settop(luaState,-2);
LuaDLL.luaL_newmetatable(luaState,o.GetType().AssemblyQualifiedName);
LuaDLL.lua_pushstring(luaState,"cache");
LuaDLL.lua_newtable(luaState);
LuaDLL.lua_rawset(luaState,-3);
LuaDLL.lua_pushlightuserdata(luaState,LuaDLL.luanet_gettag());
LuaDLL.lua_pushnumber(luaState,1);
LuaDLL.lua_rawset(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__index");
LuaDLL.lua_pushstring(luaState,"luaNet_indexfunction");
LuaDLL.lua_rawget(luaState, (int) LuaIndexes.LUA_REGISTRYINDEX);
LuaDLL.lua_rawset(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__gc");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
LuaDLL.lua_rawset(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__tostring");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
LuaDLL.lua_rawset(luaState,-3);
LuaDLL.lua_pushstring(luaState,"__newindex");
LuaDLL.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
LuaDLL.lua_rawset(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2);
KopiLua.Lua.luaL_newmetatable(luaState,o.GetType().AssemblyQualifiedName);
KopiLua.Lua.lua_pushstring(luaState,"cache");
KopiLua.Lua.lua_newtable(luaState);
KopiLua.Lua.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushlightuserdata(luaState,LuaLib.luanet_gettag());
KopiLua.Lua.lua_pushnumber(luaState,1);
KopiLua.Lua.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index");
KopiLua.Lua.lua_pushstring(luaState,"luaNet_indexfunction");
KopiLua.Lua.lua_rawget(luaState, (int) PseudoIndex.Registry);
KopiLua.Lua.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
KopiLua.Lua.lua_rawset(luaState,-3);
}
}
else
{
LuaDLL.luaL_getmetatable(luaState,metatable);
KopiLua.Lua.luaL_getmetatable(luaState,metatable);
}
// Stores the object index in the Lua list and pushes the
// index into the Lua stack
LuaDLL.luaL_getmetatable(luaState,"luaNet_objects");
LuaDLL.luanet_newudata(luaState,index);
LuaDLL.lua_pushvalue(luaState,-3);
LuaDLL.lua_remove(luaState,-4);
LuaDLL.lua_setmetatable(luaState,-2);
LuaDLL.lua_pushvalue(luaState,-1);
LuaDLL.lua_rawseti(luaState,-3,index);
LuaDLL.lua_remove(luaState,-2);
KopiLua.Lua.luaL_getmetatable(luaState,"luaNet_objects");
LuaLib.luanet_newudata(luaState,index);
KopiLua.Lua.lua_pushvalue(luaState,-3);
KopiLua.Lua.lua_remove(luaState,-4);
KopiLua.Lua.lua_setmetatable(luaState,-2);
KopiLua.Lua.lua_pushvalue(luaState,-1);
KopiLua.Lua.lua_rawseti(luaState,-3,index);
KopiLua.Lua.lua_remove(luaState,-2);
}
/*
* Gets an object from the Lua stack with the desired type, if it matches, otherwise
* returns null.
*/
internal object getAsType(IntPtr luaState,int stackPos,Type paramType)
internal object getAsType(KopiLua.Lua.lua_State luaState,int stackPos,Type paramType)
{
ExtractValue extractor=typeChecker.checkType(luaState,stackPos,paramType);
if(extractor!=null) return extractor(luaState,stackPos);
......@@ -586,34 +586,34 @@ namespace LuaInterface
/*
* Gets an object from the Lua stack according to its Lua type.
*/
internal object getObject(IntPtr luaState,int index)
internal object getObject(KopiLua.Lua.lua_State luaState,int index)
{
LuaTypes type=LuaDLL.lua_type(luaState,index);
LuaTypes type=KopiLua.Lua.lua_type(luaState,index).ToLuaTypes();
switch(type)
{
case LuaTypes.LUA_TNUMBER:
case LuaTypes.Number:
{
return LuaDLL.lua_tonumber(luaState,index);
return KopiLua.Lua.lua_tonumber(luaState,index);
}
case LuaTypes.LUA_TSTRING:
case LuaTypes.String:
{
return LuaDLL.lua_tostring(luaState,index);
return KopiLua.Lua.lua_tostring(luaState,index);
}
case LuaTypes.LUA_TBOOLEAN:
case LuaTypes.Boolean:
{
return LuaDLL.lua_toboolean(luaState,index);
return KopiLua.Lua.lua_toboolean(luaState,index);
}
case LuaTypes.LUA_TTABLE:
case LuaTypes.Table:
{
return getTable(luaState,index);
}
case LuaTypes.LUA_TFUNCTION:
case LuaTypes.Function:
{
return getFunction(luaState,index);
}
case LuaTypes.LUA_TUSERDATA:
case LuaTypes.UserData:
{
int udata=LuaDLL.luanet_tonetobject(luaState,index);
int udata=LuaLib.luanet_tonetobject(luaState,index);
if(udata!=-1)
return objects[udata];
else
......@@ -626,34 +626,34 @@ namespace LuaInterface
/*
* Gets the table in the index positon of the Lua stack.
*/
internal LuaTable getTable(IntPtr luaState,int index)
internal LuaTable getTable(KopiLua.Lua.lua_State luaState,int index)
{
LuaDLL.lua_pushvalue(luaState,index);
return new LuaTable(LuaDLL.lua_ref(luaState,1),interpreter);
KopiLua.Lua.lua_pushvalue(luaState,index);
return new LuaTable(LuaLib.lua_ref(luaState,1),interpreter);
}
/*
* Gets the userdata in the index positon of the Lua stack.
*/
internal LuaUserData getUserData(IntPtr luaState,int index)
internal LuaUserData getUserData(KopiLua.Lua.lua_State luaState,int index)
{
LuaDLL.lua_pushvalue(luaState,index);
return new LuaUserData(LuaDLL.lua_ref(luaState,1),interpreter);
KopiLua.Lua.lua_pushvalue(luaState,index);
return new LuaUserData(LuaLib.lua_ref(luaState,1),interpreter);
}
/*
* Gets the function in the index positon of the Lua stack.
*/
internal LuaFunction getFunction(IntPtr luaState,int index)
internal LuaFunction getFunction(KopiLua.Lua.lua_State luaState,int index)
{
LuaDLL.lua_pushvalue(luaState,index);
return new LuaFunction(LuaDLL.lua_ref(luaState,1),interpreter);
KopiLua.Lua.lua_pushvalue(luaState,index);
return new LuaFunction(LuaLib.lua_ref(luaState,1),interpreter);
}
/*
* Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as Lua functions.
*/
internal object getNetObject(IntPtr luaState,int index)
internal object getNetObject(KopiLua.Lua.lua_State luaState,int index)
{
int idx=LuaDLL.luanet_tonetobject(luaState,index);
int idx=LuaLib.luanet_tonetobject(luaState,index);
if(idx!=-1)
return objects[idx];
else
......@@ -663,9 +663,9 @@ namespace LuaInterface
* Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as is.
*/
internal object getRawNetObject(IntPtr luaState,int index)
internal object getRawNetObject(KopiLua.Lua.lua_State luaState,int index)
{
int udata=LuaDLL.luanet_rawnetobj(luaState,index);
int udata=LuaLib.luanet_rawnetobj(luaState,index);
if(udata!=-1)
{
return objects[udata];
......@@ -676,9 +676,9 @@ namespace LuaInterface
* Pushes the entire array into the Lua stack and returns the number
* of elements pushed.
*/
internal int returnValues(IntPtr luaState, object[] returnValues)
internal int returnValues(KopiLua.Lua.lua_State luaState, object[] returnValues)
{
if(LuaDLL.lua_checkstack(luaState,returnValues.Length+5))
if(KopiLua.Lua.lua_checkstack(luaState,returnValues.Length+5).ToBoolean())
{
for(int i=0;i<returnValues.Length;i++)
{
......@@ -692,9 +692,9 @@ namespace LuaInterface
* Gets the values from the provided index to
* the top of the stack and returns them in an array.
*/
internal object[] popValues(IntPtr luaState,int oldTop)
internal object[] popValues(KopiLua.Lua.lua_State luaState,int oldTop)
{
int newTop=LuaDLL.lua_gettop(luaState);
int newTop=KopiLua.Lua.lua_gettop(luaState);
if(oldTop==newTop)
{
return null;
......@@ -706,7 +706,7 @@ namespace LuaInterface
{
returnValues.Add(getObject(luaState,i));
}
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return returnValues.ToArray();
}
}
......@@ -715,9 +715,9 @@ namespace LuaInterface
* the top of the stack and returns them in an array, casting
* them to the provided types.
*/
internal object[] popValues(IntPtr luaState,int oldTop,Type[] popTypes)
internal object[] popValues(KopiLua.Lua.lua_State luaState,int oldTop,Type[] popTypes)
{
int newTop=LuaDLL.lua_gettop(luaState);
int newTop=KopiLua.Lua.lua_gettop(luaState);
if(oldTop==newTop)
{
return null;
......@@ -735,7 +735,7 @@ namespace LuaInterface
returnValues.Add(getAsType(luaState,i,popTypes[iTypes]));
iTypes++;
}
LuaDLL.lua_settop(luaState,oldTop);
KopiLua.Lua.lua_settop(luaState,oldTop);
return returnValues.ToArray();
}
}
......@@ -758,33 +758,33 @@ namespace LuaInterface
/*
* Pushes the object into the Lua stack according to its type.
*/
internal void push(IntPtr luaState, object o)
internal void push(KopiLua.Lua.lua_State luaState, object o)
{
if(o==null)
{
LuaDLL.lua_pushnil(luaState);
KopiLua.Lua.lua_pushnil(luaState);
}
else if(o is sbyte || o is byte || o is short || o is ushort ||
o is int || o is uint || o is long || o is float ||
o is ulong || o is decimal || o is double)
{
double d=Convert.ToDouble(o);
LuaDLL.lua_pushnumber(luaState,d);
KopiLua.Lua.lua_pushnumber(luaState,d);
}
else if(o is char)
{
double d = (char)o;
LuaDLL.lua_pushnumber(luaState,d);
KopiLua.Lua.lua_pushnumber(luaState,d);
}
else if(o is string)
{
string str=(string)o;
LuaDLL.lua_pushstring(luaState,str);
KopiLua.Lua.lua_pushstring(luaState,str);
}
else if(o is bool)
{
bool b=(bool)o;
LuaDLL.lua_pushboolean(luaState,b);
KopiLua.Lua.lua_pushboolean(luaState, (b == true ? 1 : 0));
}
else if(IsILua(o))
{
......@@ -794,9 +794,9 @@ namespace LuaInterface
{
((LuaTable)o).push(luaState);
}
else if(o is LuaCSFunction)
else if(o is KopiLua.Lua.lua_CFunction)
{
pushFunction(luaState,(LuaCSFunction)o);
pushFunction(luaState,(KopiLua.Lua.lua_CFunction)o);
}
else if(o is LuaFunction)
{
......@@ -811,7 +811,7 @@ namespace LuaInterface
* Checks if the method matches the arguments in the Lua stack, getting
* the arguments if it does.
*/
internal bool matchParameters(IntPtr luaState,MethodBase method,ref MethodCache methodCache)
internal bool matchParameters(KopiLua.Lua.lua_State luaState,MethodBase method,ref MethodCache methodCache)
{
return metaFunctions.matchParameters(luaState,method,ref methodCache);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment