Commit 75df6585 authored by Megax's avatar Megax
Browse files

* Tobb dolog is vissza lett alakitva LuaLIb-ben s gfoot-tol lettek atveve...

* Tobb dolog is vissza lett alakitva LuaLIb-ben s gfoot-tol lettek atveve otletek. Most jol mukodik a LuaInterface a kopiluaval.
parent 05ba53d3
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System; using System;
using System.Threading; using System.Threading;
using LuaInterface; using LuaInterface;
...@@ -25,17 +50,17 @@ namespace LuaRunner ...@@ -25,17 +50,17 @@ namespace LuaRunner
// For attaching from the debugger // For attaching from the debugger
// Thread.Sleep(20000); // Thread.Sleep(20000);
using (Lua lua = new Lua()) using(Lua lua = new Lua())
{ {
//lua.OpenLibs(); // steffenj: Lua 5.1.1 API change (all libs already opened in Lua constructor!) //lua.OpenLibs(); // steffenj: Lua 5.1.1 API change (all libs already opened in Lua constructor!)
lua.NewTable("arg"); lua.NewTable("arg");
LuaTable argc = (LuaTable)lua["arg"]; LuaTable argc = (LuaTable)lua["arg"];
argc[-1] = "LuaRunner"; argc[-1] = "LuaRunner";
argc[0] = args[0]; argc[0] = args[0];
for (int i = 1; i < args.Length; i++)
{ for(int i = 1; i < args.Length; i++)
argc[i] = args[i]; argc[i] = args[i];
}
argc["n"] = args.Length - 1; argc["n"] = args.Length - 1;
try try
...@@ -43,12 +68,13 @@ namespace LuaRunner ...@@ -43,12 +68,13 @@ namespace LuaRunner
//Console.WriteLine("DoFile(" + args[0] + ");"); //Console.WriteLine("DoFile(" + args[0] + ");");
lua.DoFile(args[0]); lua.DoFile(args[0]);
} }
catch (Exception e) catch(Exception e)
{ {
// steffenj: BEGIN error message improved, output is now in decending order of importance (message, where, stacktrace) // steffenj: BEGIN error message improved, output is now in decending order of importance (message, where, stacktrace)
// limit size of strack traceback message to roughly 1 console screen height // limit size of strack traceback message to roughly 1 console screen height
string trace = e.StackTrace; string trace = e.StackTrace;
if (e.StackTrace.Length > 1300)
if(e.StackTrace.Length > 1300)
trace = e.StackTrace.Substring(0, 1300) + " [...] (traceback cut short)"; trace = e.StackTrace.Substring(0, 1300) + " [...] (traceback cut short)";
Console.WriteLine(); Console.WriteLine();
......
using System.Reflection; /*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using LuaInterface.Config;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("LuaRunner")] [assembly: AssemblyTitle("LuaRunner")]
[assembly: AssemblyDescription("")] [assembly: AssemblyDescription(Consts.LuaInterfaceDescription)]
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration(Consts.LuaInterfaceConfiguration)]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany(Consts.LuaInterfaceCompany)]
[assembly: AssemblyProduct("LuaRunner")] [assembly: AssemblyProduct(Consts.LuaInterfaceProduct)]
[assembly: AssemblyCopyright("Copyright © 2006")] [assembly: AssemblyCopyright(Consts.LuaInterfaceCopyright)]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark(Consts.LuaInterfaceTrademark)]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible // Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from // to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. // COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0db73438-fb76-4991-b61d-3c072d340154")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
// //
// Major Version // Major Version
...@@ -29,5 +52,8 @@ using System.Runtime.InteropServices; ...@@ -29,5 +52,8 @@ using System.Runtime.InteropServices;
// Build Number // Build Number
// Revision // Revision
// //
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.2")] [assembly: AssemblyVersion("1.5.2")]
[assembly: AssemblyFileVersion("1.5.2")] [assembly: AssemblyFileVersion("1.5.2")]
\ No newline at end of file
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget> <PlatformTarget>x86</PlatformTarget>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<NoWarn>3001;</NoWarn>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType> <DebugType>none</DebugType>
......
...@@ -514,7 +514,6 @@ namespace KopiLua ...@@ -514,7 +514,6 @@ namespace KopiLua
public static void lua_pushlightuserdata (lua_State L, object p) { public static void lua_pushlightuserdata (lua_State L, object p) {
lua_lock(L); lua_lock(L);
setpvalue(L.top, p); setpvalue(L.top, p);
Console.WriteLine(L.top.value.p.GetType());
api_incr_top(L); api_incr_top(L);
lua_unlock(L); lua_unlock(L);
} }
......
...@@ -169,7 +169,6 @@ namespace KopiLua ...@@ -169,7 +169,6 @@ namespace KopiLua
#endif #endif
{ {
f(L, ud); f(L, ud);
Console.WriteLine(lj.status);
} }
#if CATCH_EXCEPTIONS #if CATCH_EXCEPTIONS
catch catch
...@@ -308,10 +307,7 @@ namespace KopiLua ...@@ -308,10 +307,7 @@ namespace KopiLua
StkId p; StkId p;
ptrdiff_t funcr = savestack(L, func); ptrdiff_t funcr = savestack(L, func);
if (!ttisfunction(tm)) if (!ttisfunction(tm))
{
Console.WriteLine(false);
luaG_typeerror(L, func, "call"); luaG_typeerror(L, func, "call");
}
/* Open a hole inside the stack at `func' */ /* Open a hole inside the stack at `func' */
for (p = L.top; p > func; StkId.dec(ref p)) setobjs2s(L, p, p - 1); for (p = L.top; p > func; StkId.dec(ref p)) setobjs2s(L, p, p - 1);
incr_top(L); incr_top(L);
...@@ -334,16 +330,13 @@ namespace KopiLua ...@@ -334,16 +330,13 @@ namespace KopiLua
public static int luaD_precall (lua_State L, StkId func, int nresults) { public static int luaD_precall (lua_State L, StkId func, int nresults) {
LClosure cl; LClosure cl;
ptrdiff_t funcr; ptrdiff_t funcr;
Console.WriteLine(15);
if (!ttisfunction(func)) /* `func' is not a function? */ if (!ttisfunction(func)) /* `func' is not a function? */
func = tryfuncTM(L, func); /* check the `function' tag method */ func = tryfuncTM(L, func); /* check the `function' tag method */
Console.WriteLine(16);
funcr = savestack(L, func); funcr = savestack(L, func);
Console.WriteLine(20);
cl = clvalue(func).l; cl = clvalue(func).l;
Console.WriteLine(30);
L.ci.savedpc = InstructionPtr.Assign(L.savedpc); L.ci.savedpc = InstructionPtr.Assign(L.savedpc);
Console.WriteLine(300);
if (cl.isC==0) { /* Lua function? prepare its call */ if (cl.isC==0) { /* Lua function? prepare its call */
CallInfo ci; CallInfo ci;
StkId st, base_; StkId st, base_;
...@@ -379,7 +372,6 @@ namespace KopiLua ...@@ -379,7 +372,6 @@ namespace KopiLua
return PCRLUA; return PCRLUA;
} }
else { /* if is a C function, call it */ else { /* if is a C function, call it */
Console.WriteLine(301);
CallInfo ci; CallInfo ci;
int n; int n;
luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
...@@ -447,7 +439,6 @@ namespace KopiLua ...@@ -447,7 +439,6 @@ namespace KopiLua
** function position. ** function position.
*/ */
private static void luaD_call (lua_State L, StkId func, int nResults) { private static void luaD_call (lua_State L, StkId func, int nResults) {
Console.WriteLine("asd");
if (++L.nCcalls >= LUAI_MAXCCALLS) { if (++L.nCcalls >= LUAI_MAXCCALLS) {
if (L.nCcalls == LUAI_MAXCCALLS) if (L.nCcalls == LUAI_MAXCCALLS)
luaG_runerror(L, "C stack overflow"); luaG_runerror(L, "C stack overflow");
...@@ -456,15 +447,10 @@ namespace KopiLua ...@@ -456,15 +447,10 @@ namespace KopiLua
} }
if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */
{
Console.WriteLine("asd30");
luaV_execute(L, 1); /* call it */ luaV_execute(L, 1); /* call it */
Console.WriteLine("asd3");
}
L.nCcalls--; L.nCcalls--;
Console.WriteLine("asd4");
luaC_checkGC(L); luaC_checkGC(L);
Console.WriteLine("asd5");
} }
...@@ -549,7 +535,6 @@ namespace KopiLua ...@@ -549,7 +535,6 @@ namespace KopiLua
ptrdiff_t old_errfunc = L.errfunc; ptrdiff_t old_errfunc = L.errfunc;
L.errfunc = ef; L.errfunc = ef;
status = luaD_rawrunprotected(L, func, u); status = luaD_rawrunprotected(L, func, u);
Console.WriteLine("status: " + status.ToString());
if (status != 0) { /* an error occurred? */ if (status != 0) { /* an error occurred? */
StkId oldtop = restorestack(L, old_top); StkId oldtop = restorestack(L, old_top);
luaF_close(L, oldtop); /* close eventual pending closures */ luaF_close(L, oldtop); /* close eventual pending closures */
......
...@@ -786,24 +786,18 @@ namespace KopiLua ...@@ -786,24 +786,18 @@ namespace KopiLua
public static int luaO_rawequalObj (TValue t1, TValue t2) { public static int luaO_rawequalObj (TValue t1, TValue t2) {
Console.WriteLine("luaO_rawequalObj:" + (ttype(t1) != ttype(t2)).ToString());
if (ttype(t1) != ttype(t2)) return 0; if (ttype(t1) != ttype(t2)) return 0;
else switch (ttype(t1)) { else switch (ttype(t1)) {
case LUA_TNIL: case LUA_TNIL:
Console.WriteLine("luaO_rawequalObj1:" + (1).ToString());
return 1; return 1;
case LUA_TNUMBER: case LUA_TNUMBER:
Console.WriteLine("luaO_rawequalObj2:" + (luai_numeq(nvalue(t1), nvalue(t2))).ToString());
return luai_numeq(nvalue(t1), nvalue(t2)) ? 1 : 0; return luai_numeq(nvalue(t1), nvalue(t2)) ? 1 : 0;
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
Console.WriteLine("luaO_rawequalObj3:" + (bvalue(t1) == bvalue(t2)).ToString());
return bvalue(t1) == bvalue(t2) ? 1 : 0; /* boolean true must be 1....but not in C# !! */ return bvalue(t1) == bvalue(t2) ? 1 : 0; /* boolean true must be 1....but not in C# !! */
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA:
Console.WriteLine("luaO_rawequalObj4:" + (pvalue(t1) == pvalue(t2)).ToString());
return pvalue(t1) == pvalue(t2) ? 1 : 0; return pvalue(t1) == pvalue(t2) ? 1 : 0;
default: default:
lua_assert(iscollectable(t1)); lua_assert(iscollectable(t1));
Console.WriteLine("luaO_rawequalObj5:" + (gcvalue(t1) == gcvalue(t2)).ToString());
return gcvalue(t1) == gcvalue(t2) ? 1 : 0; return gcvalue(t1) == gcvalue(t2) ? 1 : 0;
} }
} }
......
...@@ -99,9 +99,8 @@ namespace KopiLua ...@@ -99,9 +99,8 @@ namespace KopiLua
mt = G(L).mt[ttype(o)]; mt = G(L).mt[ttype(o)];
break; break;
} }
Console.WriteLine("mt: " + (mt!=null).ToString());
return ((mt!=null) ? luaH_getstr(mt, G(L).tmname[(int)event_]) : luaO_nilobject); return ((mt!=null) ? luaH_getstr(mt, G(L).tmname[(int)event_]) : luaO_nilobject);
} }
} }
} }
...@@ -89,7 +89,7 @@ namespace LuaInterface ...@@ -89,7 +89,7 @@ namespace LuaInterface
internal ExtractValue checkType(LuaCore.lua_State luaState, int stackPos, Type paramType) internal ExtractValue checkType(LuaCore.lua_State luaState, int stackPos, Type paramType)
{ {
var luatype = LuaCore.lua_type(luaState, stackPos).ToLuaTypes(); var luatype = LuaLib.lua_type(luaState, stackPos);
if(paramType.IsByRef) if(paramType.IsByRef)
paramType = paramType.GetElementType(); paramType = paramType.GetElementType();
...@@ -123,17 +123,17 @@ namespace LuaInterface ...@@ -123,17 +123,17 @@ namespace LuaInterface
//;//an unsupported type was encountered //;//an unsupported type was encountered
} }
if(LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(LuaLib.lua_isnumber(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
if(paramType == typeof(bool)) if(paramType == typeof(bool))
{ {
if(LuaCore.lua_isboolean(luaState, stackPos)) if(LuaLib.lua_isboolean(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
} }
else if(paramType == typeof(string)) else if(paramType == typeof(string))
{ {
if(LuaCore.lua_isstring(luaState, stackPos).ToBoolean()) if(LuaLib.lua_isstring(luaState, stackPos))
return extractValues[runtimeHandleValue]; return extractValues[runtimeHandleValue];
else if(luatype == LuaTypes.Nil) else if(luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer return extractNetObject; // kevinh - silently convert nil to a null string pointer
...@@ -162,12 +162,12 @@ namespace LuaInterface ...@@ -162,12 +162,12 @@ namespace LuaInterface
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject; return extractNetObject;
} }
else if(LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) else if(LuaLib.lua_type(luaState, stackPos) == LuaTypes.Table)
{ {
if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaLib.luaL_getmetafield(luaState, stackPos, "__index"))
{ {
object obj = translator.getNetObject(luaState, -1); object obj = translator.getNetObject(luaState, -1);
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
if(!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType())) if(!obj.IsNull() && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject; return extractNetObject;
} }
...@@ -191,8 +191,8 @@ namespace LuaInterface ...@@ -191,8 +191,8 @@ namespace LuaInterface
*/ */
private object getAsSbyte(LuaCore.lua_State luaState, int stackPos) private object getAsSbyte(LuaCore.lua_State luaState, int stackPos)
{ {
sbyte retVal = (sbyte)LuaCore.lua_tonumber(luaState, stackPos); sbyte retVal = (sbyte)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -200,8 +200,8 @@ namespace LuaInterface ...@@ -200,8 +200,8 @@ namespace LuaInterface
private object getAsByte(LuaCore.lua_State luaState, int stackPos) private object getAsByte(LuaCore.lua_State luaState, int stackPos)
{ {
byte retVal = (byte)LuaCore.lua_tonumber(luaState, stackPos); byte retVal = (byte)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -209,8 +209,8 @@ namespace LuaInterface ...@@ -209,8 +209,8 @@ namespace LuaInterface
private object getAsShort(LuaCore.lua_State luaState, int stackPos) private object getAsShort(LuaCore.lua_State luaState, int stackPos)
{ {
short retVal = (short)LuaCore.lua_tonumber(luaState, stackPos); short retVal = (short)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -218,8 +218,8 @@ namespace LuaInterface ...@@ -218,8 +218,8 @@ namespace LuaInterface
private object getAsUshort(LuaCore.lua_State luaState, int stackPos) private object getAsUshort(LuaCore.lua_State luaState, int stackPos)
{ {
ushort retVal = (ushort)LuaCore.lua_tonumber(luaState, stackPos); ushort retVal = (ushort)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -227,8 +227,8 @@ namespace LuaInterface ...@@ -227,8 +227,8 @@ namespace LuaInterface
private object getAsInt(LuaCore.lua_State luaState, int stackPos) private object getAsInt(LuaCore.lua_State luaState, int stackPos)
{ {
int retVal = (int)LuaCore.lua_tonumber(luaState, stackPos); int retVal = (int)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -236,8 +236,8 @@ namespace LuaInterface ...@@ -236,8 +236,8 @@ namespace LuaInterface
private object getAsUint(LuaCore.lua_State luaState, int stackPos) private object getAsUint(LuaCore.lua_State luaState, int stackPos)
{ {
uint retVal = (uint)LuaCore.lua_tonumber(luaState, stackPos); uint retVal = (uint)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -245,8 +245,8 @@ namespace LuaInterface ...@@ -245,8 +245,8 @@ namespace LuaInterface
private object getAsLong(LuaCore.lua_State luaState, int stackPos) private object getAsLong(LuaCore.lua_State luaState, int stackPos)
{ {
long retVal = (long)LuaCore.lua_tonumber(luaState, stackPos); long retVal = (long)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -254,8 +254,8 @@ namespace LuaInterface ...@@ -254,8 +254,8 @@ namespace LuaInterface
private object getAsUlong(LuaCore.lua_State luaState, int stackPos) private object getAsUlong(LuaCore.lua_State luaState, int stackPos)
{ {
ulong retVal = (ulong)LuaCore.lua_tonumber(luaState, stackPos); ulong retVal = (ulong)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -263,8 +263,8 @@ namespace LuaInterface ...@@ -263,8 +263,8 @@ namespace LuaInterface
private object getAsDouble(LuaCore.lua_State luaState, int stackPos) private object getAsDouble(LuaCore.lua_State luaState, int stackPos)
{ {
double retVal = LuaCore.lua_tonumber(luaState, stackPos); double retVal = LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -272,8 +272,8 @@ namespace LuaInterface ...@@ -272,8 +272,8 @@ namespace LuaInterface
private object getAsChar(LuaCore.lua_State luaState, int stackPos) private object getAsChar(LuaCore.lua_State luaState, int stackPos)
{ {
char retVal = (char)LuaCore.lua_tonumber(luaState, stackPos); char retVal = (char)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -281,8 +281,8 @@ namespace LuaInterface ...@@ -281,8 +281,8 @@ namespace LuaInterface
private object getAsFloat(LuaCore.lua_State luaState, int stackPos) private object getAsFloat(LuaCore.lua_State luaState, int stackPos)
{ {
float retVal = (float)LuaCore.lua_tonumber(luaState, stackPos); float retVal = (float)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -290,8 +290,8 @@ namespace LuaInterface ...@@ -290,8 +290,8 @@ namespace LuaInterface
private object getAsDecimal(LuaCore.lua_State luaState, int stackPos) private object getAsDecimal(LuaCore.lua_State luaState, int stackPos)
{ {
decimal retVal = (decimal)LuaCore.lua_tonumber(luaState, stackPos); decimal retVal = (decimal)LuaLib.lua_tonumber(luaState, stackPos);
if(retVal == 0 && !LuaCore.lua_isnumber(luaState, stackPos).ToBoolean()) if(retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -299,13 +299,13 @@ namespace LuaInterface ...@@ -299,13 +299,13 @@ namespace LuaInterface
private object getAsBoolean(LuaCore.lua_State luaState, int stackPos) private object getAsBoolean(LuaCore.lua_State luaState, int stackPos)
{ {
return LuaCore.lua_toboolean(luaState, stackPos); return LuaLib.lua_toboolean(luaState, stackPos);
} }
private object getAsString(LuaCore.lua_State luaState, int stackPos) private object getAsString(LuaCore.lua_State luaState, int stackPos)
{ {
string retVal = LuaCore.lua_tostring(luaState, stackPos).ToString(); string retVal = LuaLib.lua_tostring(luaState, stackPos).ToString();
if(retVal == string.Empty && !LuaCore.lua_isstring(luaState, stackPos).ToBoolean()) if(retVal == string.Empty && !LuaLib.lua_isstring(luaState, stackPos))
return null; return null;
return retVal; return retVal;
...@@ -328,17 +328,17 @@ namespace LuaInterface ...@@ -328,17 +328,17 @@ namespace LuaInterface
public object getAsObject(LuaCore.lua_State luaState, int stackPos) public object getAsObject(LuaCore.lua_State luaState, int stackPos)
{ {
if(LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) if(LuaLib.lua_type(luaState, stackPos) == LuaTypes.Table)
{ {
if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaLib.luaL_getmetafield(luaState, stackPos, "__index"))
{ {
if(LuaLib.luaL_checkmetatable(luaState, -1)) if(LuaLib.luaL_checkmetatable(luaState, -1))
{ {
LuaCore.lua_insert(luaState, stackPos); LuaLib.lua_insert(luaState, stackPos);
LuaCore.lua_remove(luaState, stackPos+1); LuaLib.lua_remove(luaState, stackPos+1);
} }
else else
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
} }
} }
...@@ -350,18 +350,18 @@ namespace LuaInterface ...@@ -350,18 +350,18 @@ namespace LuaInterface
{ {
object obj = translator.getNetObject(luaState, stackPos); object obj = translator.getNetObject(luaState, stackPos);
if(obj.IsNull() && LuaCore.lua_type(luaState, stackPos).ToLuaTypes() == LuaTypes.Table) if(obj.IsNull() && LuaLib.lua_type(luaState, stackPos) == LuaTypes.Table)
{ {
if(LuaCore.luaL_getmetafield(luaState, stackPos, "__index").ToBoolean()) if(LuaLib.luaL_getmetafield(luaState, stackPos, "__index"))
{ {
if(LuaLib.luaL_checkmetatable(luaState, -1)) if(LuaLib.luaL_checkmetatable(luaState, -1))
{ {
LuaCore.lua_insert(luaState, stackPos); LuaLib.lua_insert(luaState, stackPos);
LuaCore.lua_remove(luaState, stackPos+1); LuaLib.lua_remove(luaState, stackPos+1);
obj = translator.getNetObject(luaState, stackPos); obj = translator.getNetObject(luaState, stackPos);
} }
else else
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
} }
} }
......
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2012 Megax <http://megax.yeahunter.hu/>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
namespace LuaInterface.Config
{
public static class Consts
{
public const string LuaInterfaceDescription = "Bridge between the Lua runtime and the CLR";
#if DEBUG
public const string LuaInterfaceConfiguration = "Debug";
#else
public const string LuaInterfaceConfiguration = "Release";
#endif
public const string LuaInterfaceCompany = "LuaInterface Productions";
public const string LuaInterfaceProduct = "LuaInterface";
public const string LuaInterfaceCopyright = "Copyright 2003-2008 Fabio Mascarenhas, Kevin Hesterm and 2012 Megax";
public const string LuaInterfaceTrademark = "MIT license";
public const string LuaInterfaceVersion = "2.0.4";
public const string LuaInterfaceFileVersion = "2.0.4.0";
}
}
\ No newline at end of file
...@@ -154,30 +154,30 @@ namespace LuaInterface ...@@ -154,30 +154,30 @@ namespace LuaInterface
public Lua() public Lua()
{ {
luaState = LuaCore.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone) luaState = LuaLib.luaL_newstate(); // steffenj: Lua 5.1.1 API change (lua_open is gone)
//LuaCore.luaopen_base(luaState); // steffenj: luaopen_* no longer used //LuaLib.luaopen_base(luaState); // steffenj: luaopen_* no longer used
LuaCore.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here) LuaLib.luaL_openlibs(luaState); // steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
LuaCore.lua_pushstring(luaState, "LUAINTERFACE LOADED"); LuaLib.lua_pushstring(luaState, "LUAINTERFACE LOADED");
LuaCore.lua_pushboolean(luaState, 1); LuaLib.lua_pushboolean(luaState, true);
LuaCore.lua_settable(luaState, (int)PseudoIndex.Registry); LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_setglobal(luaState, "luanet"); LuaLib.lua_setglobal(luaState, "luanet");
LuaCore.lua_pushvalue(luaState, (int)PseudoIndex.Globals); LuaLib.lua_pushvalue(luaState, (int)LuaIndexes.Globals);
LuaCore.lua_getglobal(luaState, "luanet"); LuaLib.lua_getglobal(luaState, "luanet");
LuaCore.lua_pushstring(luaState, "getmetatable"); LuaLib.lua_pushstring(luaState, "getmetatable");
LuaCore.lua_getglobal(luaState, "getmetatable"); LuaLib.lua_getglobal(luaState, "getmetatable");
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_replace(luaState, (int)PseudoIndex.Globals); LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
translator = new ObjectTranslator(this, luaState); translator = new ObjectTranslator(this, luaState);
LuaCore.lua_replace(luaState, (int)PseudoIndex.Globals); LuaLib.lua_replace(luaState, (int)LuaIndexes.Globals);
LuaLib.luaL_dostring(luaState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring 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 // We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback = new LuaCore.lua_CFunction(PanicCallback); panicCallback = new LuaCore.lua_CFunction(PanicCallback);
LuaCore.lua_atpanic(luaState, panicCallback); LuaLib.lua_atpanic(luaState, panicCallback);
//LuaCore.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback)); //LuaLib.lua_atlock(luaState, lockCallback = new LuaCore.lua_CFunction(LockCallback));
//LuaCore.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback)); //LuaLib.lua_atunlock(luaState, unlockCallback = new LuaCore.lua_CFunction(UnlockCallback));
} }
/* /*
...@@ -185,29 +185,29 @@ namespace LuaInterface ...@@ -185,29 +185,29 @@ namespace LuaInterface
*/ */
public Lua(LuaCore.lua_State lState) public Lua(LuaCore.lua_State lState)
{ {
LuaCore.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED");
LuaCore.lua_gettable(lState, (int)PseudoIndex.Registry); LuaLib.lua_gettable(lState, (int)LuaIndexes.Registry);
if(LuaCore.lua_toboolean(lState, -1).ToBoolean()) if(LuaLib.lua_toboolean(lState, -1))
{ {
LuaCore.lua_settop(lState, -2); LuaLib.lua_settop(lState, -2);
throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state"); throw new LuaException("There is already a LuaInterface.Lua instance associated with this Lua state");
} }
else else
{ {
LuaCore.lua_settop(lState, -2); LuaLib.lua_settop(lState, -2);
LuaCore.lua_pushstring(lState, "LUAINTERFACE LOADED"); LuaLib.lua_pushstring(lState, "LUAINTERFACE LOADED");
LuaCore.lua_pushboolean(lState, 1); LuaLib.lua_pushboolean(lState, true);
LuaCore.lua_settable(lState, (int)PseudoIndex.Registry); LuaLib.lua_settable(lState, (int)LuaIndexes.Registry);
luaState = lState; luaState = lState;
LuaCore.lua_pushvalue(lState, (int)PseudoIndex.Globals); LuaLib.lua_pushvalue(lState, (int)LuaIndexes.Globals);
LuaCore.lua_getglobal(lState, "luanet"); LuaLib.lua_getglobal(lState, "luanet");
LuaCore.lua_pushstring(lState, "getmetatable"); LuaLib.lua_pushstring(lState, "getmetatable");
LuaCore.lua_getglobal(lState, "getmetatable"); LuaLib.lua_getglobal(lState, "getmetatable");
LuaCore.lua_settable(lState, -3); LuaLib.lua_settable(lState, -3);
LuaCore.lua_replace(lState, (int)PseudoIndex.Globals); LuaLib.lua_replace(lState, (int)LuaIndexes.Globals);
translator = new ObjectTranslator(this, luaState); translator = new ObjectTranslator(this, luaState);
LuaCore.lua_replace(lState, (int)PseudoIndex.Globals); LuaLib.lua_replace(lState, (int)LuaIndexes.Globals);
LuaLib.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring LuaLib.luaL_dostring(lState, Lua.init_luanet); // steffenj: lua_dostring renamed to luaL_dostring
} }
...@@ -249,8 +249,8 @@ namespace LuaInterface ...@@ -249,8 +249,8 @@ namespace LuaInterface
static int PanicCallback(LuaCore.lua_State luaState) static int PanicCallback(LuaCore.lua_State luaState)
{ {
// string desc = LuaCore.lua_tostring(luaState, 1); // string desc = LuaLib.lua_tostring(luaState, 1);
string reason = string.Format("unprotected error in call to Lua API ({0})", LuaCore.lua_tostring(luaState, -1)); string reason = string.Format("unprotected error in call to Lua API ({0})", LuaLib.lua_tostring(luaState, -1));
// lua_tostring(L, -1); // lua_tostring(L, -1);
throw new LuaException(reason); throw new LuaException(reason);
} }
...@@ -262,7 +262,7 @@ namespace LuaInterface ...@@ -262,7 +262,7 @@ namespace LuaInterface
private void ThrowExceptionFromError(int oldTop) private void ThrowExceptionFromError(int oldTop)
{ {
object err = translator.getObject(luaState, -1); object err = translator.getObject(luaState, -1);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
// A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved) // A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
var luaEx = err as LuaScriptException; var luaEx = err as LuaScriptException;
...@@ -289,7 +289,7 @@ namespace LuaInterface ...@@ -289,7 +289,7 @@ namespace LuaInterface
if(!caughtExcept.IsNull()) if(!caughtExcept.IsNull())
{ {
translator.throwError(luaState, caughtExcept); translator.throwError(luaState, caughtExcept);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
else else
...@@ -304,7 +304,7 @@ namespace LuaInterface ...@@ -304,7 +304,7 @@ namespace LuaInterface
/// <returns></returns> /// <returns></returns>
public LuaFunction LoadString(string chunk, string name) public LuaFunction LoadString(string chunk, string name)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
executing = true; executing = true;
try try
...@@ -329,9 +329,9 @@ namespace LuaInterface ...@@ -329,9 +329,9 @@ namespace LuaInterface
/// <returns></returns> /// <returns></returns>
public LuaFunction LoadFile(string fileName) public LuaFunction LoadFile(string fileName)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
if(LuaCore.luaL_loadfile(luaState, fileName) != 0) if(LuaLib.luaL_loadfile(luaState, fileName) != 0)
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
var result = translator.getFunction(luaState, -1); var result = translator.getFunction(luaState, -1);
...@@ -345,7 +345,7 @@ namespace LuaInterface ...@@ -345,7 +345,7 @@ namespace LuaInterface
*/ */
public object[] DoString(string chunk) public object[] DoString(string chunk)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
if(LuaLib.luaL_loadbuffer(luaState, chunk, "chunk") == 0) if(LuaLib.luaL_loadbuffer(luaState, chunk, "chunk") == 0)
{ {
...@@ -353,7 +353,7 @@ namespace LuaInterface ...@@ -353,7 +353,7 @@ namespace LuaInterface
try try
{ {
if(LuaCore.lua_pcall(luaState, 0, -1, 0) == 0) if(LuaLib.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop); return translator.popValues(luaState, oldTop);
else else
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
...@@ -377,14 +377,14 @@ namespace LuaInterface ...@@ -377,14 +377,14 @@ namespace LuaInterface
/// <returns></returns> /// <returns></returns>
public object[] DoString(string chunk, string chunkName) public object[] DoString(string chunk, string chunkName)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
executing = true; executing = true;
if(LuaLib.luaL_loadbuffer(luaState, chunk, chunkName) == 0) if(LuaLib.luaL_loadbuffer(luaState, chunk, chunkName) == 0)
{ {
try try
{ {
if(LuaCore.lua_pcall(luaState, 0, -1, 0) == 0) if(LuaLib.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop); return translator.popValues(luaState, oldTop);
else else
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
...@@ -406,15 +406,15 @@ namespace LuaInterface ...@@ -406,15 +406,15 @@ namespace LuaInterface
*/ */
public object[] DoFile(string fileName) public object[] DoFile(string fileName)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
if(LuaCore.luaL_loadfile(luaState, fileName) == 0) if(LuaLib.luaL_loadfile(luaState, fileName) == 0)
{ {
executing = true; executing = true;
try try
{ {
if(LuaCore.lua_pcall(luaState, 0, -1, 0) == 0) if(LuaLib.lua_pcall(luaState, 0, -1, 0) == 0)
return translator.popValues(luaState, oldTop); return translator.popValues(luaState, oldTop);
else else
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
...@@ -440,9 +440,9 @@ namespace LuaInterface ...@@ -440,9 +440,9 @@ namespace LuaInterface
get get
{ {
object returnValue = null; object returnValue = null;
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
string[] path = fullPath.Split(new char[] { '.' }); string[] path = fullPath.Split(new char[] { '.' });
LuaCore.lua_getglobal(luaState, path[0]); LuaLib.lua_getglobal(luaState, path[0]);
returnValue = translator.getObject(luaState, -1); returnValue = translator.getObject(luaState, -1);
if(path.Length>1) if(path.Length>1)
...@@ -452,28 +452,28 @@ namespace LuaInterface ...@@ -452,28 +452,28 @@ namespace LuaInterface
returnValue = getObject(remainingPath); returnValue = getObject(remainingPath);
} }
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return returnValue; return returnValue;
} }
set set
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
string[] path = fullPath.Split(new char[] { '.' }); string[] path = fullPath.Split(new char[] { '.' });
if(path.Length == 1) if(path.Length == 1)
{ {
translator.push(luaState, value); translator.push(luaState, value);
LuaCore.lua_setglobal(luaState, fullPath); LuaLib.lua_setglobal(luaState, fullPath);
} }
else else
{ {
LuaCore.lua_getglobal(luaState, path[0]); LuaLib.lua_getglobal(luaState, path[0]);
string[] remainingPath = new string[path.Length-1]; string[] remainingPath = new string[path.Length-1];
Array.Copy(path, 1, remainingPath, 0, path.Length-1); Array.Copy(path, 1, remainingPath, 0, path.Length-1);
setObject(remainingPath, value); setObject(remainingPath, value);
} }
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
// Globals auto-complete // Globals auto-complete
if(value.IsNull()) if(value.IsNull())
...@@ -581,8 +581,8 @@ namespace LuaInterface ...@@ -581,8 +581,8 @@ namespace LuaInterface
for(int i = 0; i < remainingPath.Length; i++) for(int i = 0; i < remainingPath.Length; i++)
{ {
LuaCore.lua_pushstring(luaState, remainingPath[i]); LuaLib.lua_pushstring(luaState, remainingPath[i]);
LuaCore.lua_gettable(luaState, -2); LuaLib.lua_gettable(luaState, -2);
returnValue = translator.getObject(luaState, -1); returnValue = translator.getObject(luaState, -1);
if(returnValue.IsNull()) if(returnValue.IsNull())
...@@ -632,7 +632,6 @@ namespace LuaInterface ...@@ -632,7 +632,6 @@ namespace LuaInterface
{ {
object obj = this[fullPath]; object obj = this[fullPath];
return (obj is LuaCore.lua_CFunction ? new LuaFunction((LuaCore.lua_CFunction)obj, this) : (LuaFunction)obj); return (obj is LuaCore.lua_CFunction ? new LuaFunction((LuaCore.lua_CFunction)obj, this) : (LuaFunction)obj);
//return (obj is LuaCore.lua_CFunction ? new LuaFunction((LuaCore.lua_CFunction)obj, this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this));
} }
/* /*
...@@ -661,9 +660,9 @@ namespace LuaInterface ...@@ -661,9 +660,9 @@ namespace LuaInterface
internal object[] callFunction(object function, object[] args, Type[] returnTypes) internal object[] callFunction(object function, object[] args, Type[] returnTypes)
{ {
int nArgs = 0; int nArgs = 0;
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
if(!LuaCore.lua_checkstack(luaState, args.Length+6).ToBoolean()) if(!LuaLib.lua_checkstack(luaState, args.Length+6))
throw new LuaException("Lua stack overflow"); throw new LuaException("Lua stack overflow");
translator.push(luaState, function); translator.push(luaState, function);
...@@ -680,7 +679,7 @@ namespace LuaInterface ...@@ -680,7 +679,7 @@ namespace LuaInterface
try try
{ {
int error = LuaCore.lua_pcall(luaState, nArgs, -1, 0); int error = LuaLib.lua_pcall(luaState, nArgs, -1, 0);
if(error != 0) if(error != 0)
ThrowExceptionFromError(oldTop); ThrowExceptionFromError(oldTop);
} }
...@@ -699,13 +698,13 @@ namespace LuaInterface ...@@ -699,13 +698,13 @@ namespace LuaInterface
{ {
for(int i = 0; i < remainingPath.Length-1; i++) for(int i = 0; i < remainingPath.Length-1; i++)
{ {
LuaCore.lua_pushstring(luaState, remainingPath[i]); LuaLib.lua_pushstring(luaState, remainingPath[i]);
LuaCore.lua_gettable(luaState, -2); LuaLib.lua_gettable(luaState, -2);
} }
LuaCore.lua_pushstring(luaState, remainingPath[remainingPath.Length-1]); LuaLib.lua_pushstring(luaState, remainingPath[remainingPath.Length-1]);
translator.push(luaState, val); translator.push(luaState, val);
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
} }
/* /*
...@@ -715,45 +714,45 @@ namespace LuaInterface ...@@ -715,45 +714,45 @@ namespace LuaInterface
public void NewTable(string fullPath) public void NewTable(string fullPath)
{ {
string[] path = fullPath.Split(new char[] { '.' }); string[] path = fullPath.Split(new char[] { '.' });
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
if(path.Length == 1) if(path.Length == 1)
{ {
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_setglobal(luaState, fullPath); LuaLib.lua_setglobal(luaState, fullPath);
} }
else else
{ {
LuaCore.lua_getglobal(luaState, path[0]); LuaLib.lua_getglobal(luaState, path[0]);
for(int i = 1; i < path.Length-1; i++) for(int i = 1; i < path.Length-1; i++)
{ {
LuaCore.lua_pushstring(luaState, path[i]); LuaLib.lua_pushstring(luaState, path[i]);
LuaCore.lua_gettable(luaState, -2); LuaLib.lua_gettable(luaState, -2);
} }
LuaCore.lua_pushstring(luaState, path[path.Length-1]); LuaLib.lua_pushstring(luaState, path[path.Length-1]);
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
} }
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
} }
public ListDictionary GetTableDict(LuaTable table) public ListDictionary GetTableDict(LuaTable table)
{ {
var dict = new ListDictionary(); var dict = new ListDictionary();
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
translator.push(luaState, table); translator.push(luaState, table);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
while(LuaCore.lua_next(luaState, -2) != 0) while(LuaLib.lua_next(luaState, -2) != 0)
{ {
dict[translator.getObject(luaState, -2)] = translator.getObject(luaState, -1); dict[translator.getObject(luaState, -2)] = translator.getObject(luaState, -1);
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
} }
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return dict; return dict;
} }
...@@ -825,7 +824,7 @@ namespace LuaInterface ...@@ -825,7 +824,7 @@ namespace LuaInterface
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false); System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try try
{ {
return LuaCore.lua_getstack(luaState, level, luaDebug) != 0; return LuaLib.lua_getstack(luaState, level, luaDebug) != 0;
} }
finally finally
{ {
...@@ -847,7 +846,7 @@ namespace LuaInterface ...@@ -847,7 +846,7 @@ namespace LuaInterface
System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false); System.Runtime.InteropServices.Marshal.StructureToPtr(luaDebug, ld, false);
try try
{ {
return LuaCore.lua_getinfo(luaState, what, ld); return LuaLib.lua_getinfo(luaState, what, ld);
} }
finally finally
{ {
...@@ -953,7 +952,7 @@ namespace LuaInterface ...@@ -953,7 +952,7 @@ namespace LuaInterface
/// <author>Reinhard Ostermeier</author> /// <author>Reinhard Ostermeier</author>
public object Pop() public object Pop()
{ {
int top = LuaCore.lua_gettop(luaState); int top = LuaLib.lua_gettop(luaState);
return translator.popValues(luaState, top - 1)[0]; return translator.popValues(luaState, top - 1)[0];
} }
...@@ -970,7 +969,6 @@ namespace LuaInterface ...@@ -970,7 +969,6 @@ namespace LuaInterface
internal void dispose(int reference) internal void dispose(int reference)
{ {
///////////// if(luaState != LuaCore.lua_State.Zero)
if(!luaState.IsNull()) //Fix submitted by Qingrui Li if(!luaState.IsNull()) //Fix submitted by Qingrui Li
LuaLib.lua_unref(luaState, reference); LuaLib.lua_unref(luaState, reference);
} }
...@@ -981,12 +979,12 @@ namespace LuaInterface ...@@ -981,12 +979,12 @@ namespace LuaInterface
*/ */
internal object rawGetObject(int reference, string field) internal object rawGetObject(int reference, string field)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, reference); LuaLib.lua_getref(luaState, reference);
LuaCore.lua_pushstring(luaState, field); LuaLib.lua_pushstring(luaState, field);
LuaCore.lua_rawget(luaState, -2); LuaLib.lua_rawget(luaState, -2);
object obj = translator.getObject(luaState, -1); object obj = translator.getObject(luaState, -1);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return obj; return obj;
} }
...@@ -995,10 +993,10 @@ namespace LuaInterface ...@@ -995,10 +993,10 @@ namespace LuaInterface
*/ */
internal object getObject(int reference, string field) internal object getObject(int reference, string field)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, reference); LuaLib.lua_getref(luaState, reference);
object returnValue = getObject(field.Split(new char[] {'.'})); object returnValue = getObject(field.Split(new char[] {'.'}));
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return returnValue; return returnValue;
} }
...@@ -1008,12 +1006,12 @@ namespace LuaInterface ...@@ -1008,12 +1006,12 @@ namespace LuaInterface
internal object getObject(int reference, object field) internal object getObject(int reference, object field)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, reference); LuaLib.lua_getref(luaState, reference);
translator.push(luaState, field); translator.push(luaState, field);
LuaCore.lua_gettable(luaState, -2); LuaLib.lua_gettable(luaState, -2);
object returnValue = translator.getObject(luaState, -1); object returnValue = translator.getObject(luaState, -1);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return returnValue; return returnValue;
} }
...@@ -1023,10 +1021,10 @@ namespace LuaInterface ...@@ -1023,10 +1021,10 @@ namespace LuaInterface
*/ */
internal void setObject(int reference, string field, object val) internal void setObject(int reference, string field, object val)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, reference); LuaLib.lua_getref(luaState, reference);
setObject(field.Split(new char[] {'.'}), val); setObject(field.Split(new char[] {'.'}), val);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
} }
/* /*
...@@ -1035,12 +1033,12 @@ namespace LuaInterface ...@@ -1035,12 +1033,12 @@ namespace LuaInterface
*/ */
internal void setObject(int reference, object field, object val) internal void setObject(int reference, object field, object val)
{ {
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, reference); LuaLib.lua_getref(luaState, reference);
translator.push(luaState, field); translator.push(luaState, field);
translator.push(luaState, val); translator.push(luaState, val);
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
} }
/* /*
...@@ -1050,12 +1048,12 @@ namespace LuaInterface ...@@ -1050,12 +1048,12 @@ 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) 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 // We leave nothing on the stack when we are done
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
var wrapper = new LuaMethodWrapper(translator, target, function.DeclaringType, function); var wrapper = new LuaMethodWrapper(translator, target, function.DeclaringType, function);
translator.push(luaState, new LuaCore.lua_CFunction(wrapper.call)); translator.push(luaState, new LuaCore.lua_CFunction(wrapper.call));
this[path] = translator.getObject(luaState, -1); this[path] = translator.getObject(luaState, -1);
var f = GetFunction(path); var f = GetFunction(path);
LuaCore.lua_settop(luaState, oldTop); LuaLib.lua_settop(luaState, oldTop);
return f; return f;
} }
...@@ -1064,11 +1062,11 @@ namespace LuaInterface ...@@ -1064,11 +1062,11 @@ namespace LuaInterface
*/ */
internal bool compareRef(int ref1, int ref2) internal bool compareRef(int ref1, int ref2)
{ {
int top = LuaCore.lua_gettop(luaState); int top = LuaLib.lua_gettop(luaState);
LuaLib.lua_getref(luaState, ref1); LuaLib.lua_getref(luaState, ref1);
LuaLib.lua_getref(luaState, ref2); LuaLib.lua_getref(luaState, ref2);
int equal = LuaCore.lua_equal(luaState, -1, -2); int equal = LuaLib.lua_equal(luaState, -1, -2);
LuaCore.lua_settop(luaState, top); LuaLib.lua_settop(luaState, top);
return (equal != 0); return (equal != 0);
} }
......
...@@ -61,7 +61,6 @@ ...@@ -61,7 +61,6 @@
<Compile Include="Metatables.cs" /> <Compile Include="Metatables.cs" />
<Compile Include="ObjectTranslator.cs" /> <Compile Include="ObjectTranslator.cs" />
<Compile Include="ProxyType.cs" /> <Compile Include="ProxyType.cs" />
<Compile Include="LuaLib\LuaLib.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LuaBase.cs" /> <Compile Include="LuaBase.cs" />
<Compile Include="LuaFunction.cs" /> <Compile Include="LuaFunction.cs" />
...@@ -85,8 +84,6 @@ ...@@ -85,8 +84,6 @@
<Compile Include="LuaLib\LuaEnums.cs" /> <Compile Include="LuaLib\LuaEnums.cs" />
<Compile Include="LuaLib\References.cs" /> <Compile Include="LuaLib\References.cs" />
<Compile Include="LuaLib\LuaTypes.cs" /> <Compile Include="LuaLib\LuaTypes.cs" />
<Compile Include="LuaLib\GCOption.cs" />
<Compile Include="LuaLib\PseudoIndex.cs" />
<Compile Include="Method\MethodCache.cs" /> <Compile Include="Method\MethodCache.cs" />
<Compile Include="Method\MethodArgs.cs" /> <Compile Include="Method\MethodArgs.cs" />
<Compile Include="Method\LuaMethodWrapper.cs" /> <Compile Include="Method\LuaMethodWrapper.cs" />
...@@ -95,6 +92,10 @@ ...@@ -95,6 +92,10 @@
<Compile Include="Method\LuaEventHandler.cs" /> <Compile Include="Method\LuaEventHandler.cs" />
<Compile Include="Method\LuaDelegate.cs" /> <Compile Include="Method\LuaDelegate.cs" />
<Compile Include="Method\LuaClassHelper.cs" /> <Compile Include="Method\LuaClassHelper.cs" />
<Compile Include="LuaLib\LuaIndexes.cs" />
<Compile Include="LuaLib\GCOptions.cs" />
<Compile Include="LuaLib\LuaLib.cs" />
<Compile Include="Config\LuaInterfaceConfig.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
...@@ -116,5 +117,6 @@ ...@@ -116,5 +117,6 @@
<Folder Include="Event\" /> <Folder Include="Event\" />
<Folder Include="Exceptions\" /> <Folder Include="Exceptions\" />
<Folder Include="Method\" /> <Folder Include="Method\" />
<Folder Include="Config\" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -28,7 +28,7 @@ using System; ...@@ -28,7 +28,7 @@ using System;
namespace LuaInterface namespace LuaInterface
{ {
public enum GCOption : int public enum GCOptions : int
{ {
/// <summary> /// <summary>
/// Stops the garbage collector. /// Stops the garbage collector.
......
...@@ -28,7 +28,7 @@ using System; ...@@ -28,7 +28,7 @@ using System;
namespace LuaInterface namespace LuaInterface
{ {
public enum PseudoIndex : int public enum LuaIndexes : int
{ {
Registry = (-10000), Registry = (-10000),
Environment = (-10001), Environment = (-10001),
......
...@@ -26,217 +26,502 @@ ...@@ -26,217 +26,502 @@
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using LuaInterface.Extensions; using LuaInterface.Extensions;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua; using LuaCore = KopiLua.Lua;
public static class LuaLib public class LuaLib
{ {
private static int tag = 0; // Not sure of the purpose of this, but I'm keeping it -kevinh
private static object tag = 0;
public static LuaTypes ToLuaTypes(this int type) // steffenj: BEGIN additional Lua API functions new in Lua 5.1
public static int lua_gc(LuaCore.lua_State luaState, GCOptions what, int data)
{ {
return (LuaTypes)type; return LuaCore.lua_gc(luaState, (int)what, data);
} }
public static LuaEnums ToLuaEnums(this int lenum) public static string lua_typename(LuaCore.lua_State luaState, LuaTypes type)
{ {
return (LuaEnums)lenum; return LuaCore.lua_typename(luaState, (int)type).ToString();
} }
public static bool ToBoolean(this int number) public static string luaL_typename(LuaCore.lua_State luaState, int stackPos)
{ {
return number == 1; return lua_typename(luaState, lua_type(luaState, stackPos));
} }
#region Core Library public static void luaL_error(LuaCore.lua_State luaState, string message)
/// <summary>
/// Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function.
/// </summary>
/// <param name="state">
/// A <see cref="IntPtr"/>
/// </param>
/// <param name="fn">
/// A <see cref="CallbackFunction"/>
/// </param>
public static void lua_pushcfunction(LuaCore.lua_State state, LuaCore.lua_CFunction fn)
{ {
LuaCore.lua_pushcclosure(state, fn, 0); LuaCore.luaL_error(luaState, message);
} }
#endregion
#region Auxiliary Library public static void luaL_where(LuaCore.lua_State luaState, int level)
/// <summary>
/// Loads and runs the given file.
/// </summary>
/// <param name="state">
/// A <see cref="IntPtr"/>
/// </param>
/// <param name="filename">
/// A <see cref="System.String"/>
/// </param>
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public static bool luaL_dofile(LuaCore.lua_State state, string filename)
{ {
return (LuaCore.luaL_loadfile(state, filename).ToLuaEnums() == LuaEnums.Ok) && (LuaCore.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok); LuaCore.luaL_where(luaState, level);
} }
/// <summary> // steffenj: BEGIN Lua 5.1.1 API change (lua_open replaced by luaL_newstate)
/// Loads and runs the given string. public static LuaCore.lua_State luaL_newstate()
/// </summary>
/// <param name="state">
/// A <see cref="IntPtr"/>
/// </param>
/// <param name="chunk">
/// A <see cref="System.String"/>
/// </param>
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public static bool luaL_dostring(LuaCore.lua_State state, string chunk)
{ {
return (LuaCore.luaL_loadstring(state, chunk).ToLuaEnums() == LuaEnums.Ok) && (LuaCore.lua_pcall(state, 0, (int)LuaEnums.MultiRet, 0).ToLuaEnums() == LuaEnums.Ok); return LuaCore.luaL_newstate();
} }
public static LuaEnums luaL_loadbuffer(LuaCore.lua_State luaState, string buff, string name) // steffenj: BEGIN Lua 5.1.1 API change (new function luaL_openlibs)
public static void luaL_openlibs(LuaCore.lua_State luaState)
{ {
var result = LuaCore.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnums(); LuaCore.luaL_openlibs(luaState);
}
// steffenj: END Lua 5.1.1 API change (lua_strlen is now lua_objlen)
// steffenj: BEGIN Lua 5.1.1 API change (lua_dostring is now a macro luaL_dostring)
public static int luaL_loadstring(LuaCore.lua_State luaState, string chunk)
{
return LuaCore.luaL_loadstring(luaState, chunk);
}
public static int luaL_dostring(LuaCore.lua_State luaState, string chunk)
{
int result = luaL_loadstring(luaState, chunk);
if(result != 0)
return result; return result;
return lua_pcall(luaState, 0, -1, 0);
} }
public static bool luaL_checkmetatable(LuaCore.lua_State luaState,int index) /// <summary>DEPRECATED - use luaL_dostring(LuaCore.lua_State luaState, string chunk) instead!</summary>
public static int lua_dostring(LuaCore.lua_State luaState, string chunk)
{ {
bool retVal = false; return luaL_dostring(luaState, chunk);
Console.WriteLine("v: " + luaState.tt.ToString()); }
if(LuaCore.lua_getmetatable(luaState,index) != 0) // steffenj: END Lua 5.1.1 API change (lua_dostring is now a macro luaL_dostring)
// steffenj: BEGIN Lua 5.1.1 API change (lua_newtable is gone, lua_createtable is new)
public static void lua_createtable(LuaCore.lua_State luaState, int narr, int nrec)
{ {
LuaCore.lua_pushlightuserdata(luaState, tag); LuaCore.lua_createtable(luaState, narr, nrec);
LuaCore.lua_rawget(luaState, -2);
retVal = !LuaCore.lua_isnil(luaState, -1);
LuaCore.lua_settop(luaState, -3);
} }
return retVal; public static void lua_newtable(LuaCore.lua_State luaState)
{
lua_createtable(luaState, 0, 0);
} }
public static int luanet_gettag() // steffenj: END Lua 5.1.1 API change (lua_newtable is gone, lua_createtable is new)
// steffenj: BEGIN Lua 5.1.1 API change (lua_dofile now in LuaLib as luaL_dofile macro)
public static int luaL_dofile(LuaCore.lua_State luaState, string fileName)
{ {
return tag; int result = LuaCore.luaL_loadfile(luaState, fileName);
if(result != 0)
return result;
return LuaCore.lua_pcall(luaState, 0, -1, 0);
}
// steffenj: END Lua 5.1.1 API change (lua_dofile now in LuaLib as luaL_dofile)
public static void lua_getglobal(LuaCore.lua_State luaState, string name)
{
lua_pushstring(luaState, name);
LuaCore.lua_gettable(luaState, (int)LuaIndexes.Globals);
}
public static void lua_setglobal(LuaCore.lua_State luaState, string name)
{
lua_pushstring(luaState,name);
lua_insert(luaState,-2);
lua_settable(luaState, (int)LuaIndexes.Globals);
}
public static void lua_settop(LuaCore.lua_State luaState, int newTop)
{
LuaCore.lua_settop(luaState, newTop);
}
public static void lua_pop(LuaCore.lua_State luaState, int amount)
{
lua_settop(luaState, -(amount) - 1);
}
public static void lua_insert(LuaCore.lua_State luaState, int newTop)
{
LuaCore.lua_insert(luaState, newTop);
}
public static void lua_remove(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_remove(luaState, index);
}
public static void lua_gettable(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_gettable(luaState, index);
}
public static void lua_rawget(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_rawget(luaState, index);
}
public static void lua_settable(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_settable(luaState, index);
}
public static void lua_rawset(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_rawset(luaState, index);
}
public static void lua_setmetatable(LuaCore.lua_State luaState, int objIndex)
{
LuaCore.lua_setmetatable(luaState, objIndex);
}
public static int lua_getmetatable(LuaCore.lua_State luaState, int objIndex)
{
return LuaCore.lua_getmetatable(luaState, objIndex);
}
public static int lua_equal(LuaCore.lua_State luaState, int index1, int index2)
{
return LuaCore.lua_equal(luaState, index1, index2);
}
public static void lua_pushvalue(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_pushvalue(luaState, index);
}
public static void lua_replace(LuaCore.lua_State luaState, int index)
{
LuaCore.lua_replace(luaState, index);
}
public static int lua_gettop(LuaCore.lua_State luaState)
{
return LuaCore.lua_gettop(luaState);
}
public static LuaTypes lua_type(LuaCore.lua_State luaState, int index)
{
return (LuaTypes) LuaCore.lua_type(luaState, index);
}
public static bool lua_isnil(LuaCore.lua_State luaState, int index)
{
return lua_type(luaState, index) == LuaTypes.Nil;
}
public static bool lua_isnumber(LuaCore.lua_State luaState, int index)
{
return lua_type(luaState, index) == LuaTypes.Number;
}
public static bool lua_isboolean(LuaCore.lua_State luaState, int index)
{
return lua_type(luaState, index) == LuaTypes.Boolean;
}
public static int luaL_ref(LuaCore.lua_State luaState, int registryIndex)
{
return LuaCore.luaL_ref(luaState, registryIndex);
}
public static int lua_ref(LuaCore.lua_State luaState, int lockRef)
{
return lockRef != 0 ? luaL_ref(luaState, (int)LuaIndexes.Registry) : 0;
}
public static void lua_rawgeti(LuaCore.lua_State luaState, int tableIndex, int index)
{
LuaCore.lua_rawgeti(luaState, tableIndex, index);
}
public static void lua_rawseti(LuaCore.lua_State luaState, int tableIndex, int index)
{
LuaCore.lua_rawseti(luaState, tableIndex, index);
}
public static object lua_newuserdata(LuaCore.lua_State luaState, int size)
{
return LuaCore.lua_newuserdata(luaState, (uint)size);
}
public static object lua_touserdata(LuaCore.lua_State luaState, int index)
{
return LuaCore.lua_touserdata(luaState, index);
} }
public static void lua_getref(LuaCore.lua_State luaState, int reference) public static void lua_getref(LuaCore.lua_State luaState, int reference)
{ {
LuaCore.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference); lua_rawgeti(luaState, (int)LuaIndexes.Registry, reference);
} }
public static void lua_unref(LuaCore.lua_State luaState, int reference) public static void lua_unref(LuaCore.lua_State luaState, int reference)
{ {
LuaCore.luaL_unref(luaState, (int)PseudoIndex.Registry, reference); LuaCore.luaL_unref(luaState, (int)LuaIndexes.Registry, reference);
} }
public static int luanet_rawnetobj(LuaCore.lua_State luaState, int obj) public static bool lua_isstring(LuaCore.lua_State luaState, int index)
{ {
//int udata = (int)LuaCore.lua_touserdata2(luaState, obj); return LuaCore.lua_isstring(luaState, index) != 0;
int udata = fourBytesToInt(LuaCore.lua_touserdata(luaState, obj) as byte[]); }
return udata != 0 ? udata : -1;
public static bool lua_iscfunction(LuaCore.lua_State luaState, int index)
{
return LuaCore.lua_iscfunction(luaState, index);
}
public static void lua_pushnil(LuaCore.lua_State luaState)
{
LuaCore.lua_pushnil(luaState);
}
public static void lua_call(LuaCore.lua_State luaState, int nArgs, int nResults)
{
LuaCore.lua_call(luaState, nArgs, nResults);
}
public static int lua_pcall(LuaCore.lua_State luaState, int nArgs, int nResults, int errfunc)
{
return LuaCore.lua_pcall(luaState, nArgs, nResults, errfunc);
}
public static LuaCore.lua_CFunction lua_tocfunction(LuaCore.lua_State luaState, int index)
{
return LuaCore.lua_tocfunction(luaState, index);
}
public static double lua_tonumber(LuaCore.lua_State luaState, int index)
{
return LuaCore.lua_tonumber(luaState, index);
}
public static bool lua_toboolean(LuaCore.lua_State luaState, int index)
{
return LuaCore.lua_toboolean(luaState, index) != 0;
}
public static string lua_tostring(LuaCore.lua_State luaState, int index)
{
#if true
// FIXME use the same format string as lua i.e. LUA_NUMBER_FMT
var t = lua_type(luaState, index);
if(t == LuaTypes.Number)
return string.Format("{0}", lua_tonumber(luaState, index));
else if(t == LuaTypes.String)
{
uint strlen;
return LuaCore.lua_tolstring(luaState, index, out strlen).ToString();
}
else if(t == LuaTypes.Nil)
return null; // treat lua nulls to as C# nulls
else
return "0"; // Because luaV_tostring does this
#else
size_t strlen;
// Note! This method will _change_ the representation of the object on the stack to a string.
// We do not want this behavior so we do the conversion ourselves
const char *str = LuaCore.lua_tolstring(luaState, index, &strlen);
if (str)
return Marshal::PtrToStringAnsi(IntPtr((char *) str), strlen);
else
return nullptr; // treat lua nulls to as C# nulls
#endif
}
public static void lua_atpanic(LuaCore.lua_State luaState, LuaCore.lua_CFunction panicf)
{
LuaCore.lua_atpanic(luaState, (LuaCore.lua_CFunction)panicf);
} }
public static void lua_pushstdcallcfunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction function) public static void lua_pushstdcallcfunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction function)
{ {
lua_pushcfunction(luaState, function); LuaCore.lua_pushcfunction(luaState, function);
}
public static void lua_pushnumber(LuaCore.lua_State luaState, double number)
{
LuaCore.lua_pushnumber(luaState, number);
}
public static void lua_pushboolean(LuaCore.lua_State luaState, bool value)
{
LuaCore.lua_pushboolean(luaState, value ? 1 : 0);
}
public static void lua_pushstring(LuaCore.lua_State luaState, string str)
{
LuaCore.lua_pushstring(luaState, str);
}
public static int luaL_newmetatable(LuaCore.lua_State luaState, string meta)
{
return LuaCore.luaL_newmetatable(luaState, meta);
}
// steffenj: BEGIN Lua 5.1.1 API change (luaL_getmetatable is now a macro using lua_getfield)
public static void lua_getfield(LuaCore.lua_State luaState, int stackPos, string meta)
{
LuaCore.lua_getfield(luaState, stackPos, meta);
}
public static void luaL_getmetatable(LuaCore.lua_State luaState, string meta)
{
lua_getfield(luaState, (int)LuaIndexes.Registry, meta);
} }
public static int checkudata_raw(LuaCore.lua_State luaState, int ud, string tname) public static object luaL_checkudata(LuaCore.lua_State luaState, int stackPos, string meta)
{ {
//int p = (int)LuaCore.lua_touserdata2(luaState, ud); return LuaCore.luaL_checkudata(luaState, stackPos, meta);
int p = fourBytesToInt(LuaCore.lua_touserdata(luaState, ud) as byte[]); }
Console.WriteLine("asddddd:" + p.ToString());
if(p != 0) public static bool luaL_getmetafield(LuaCore.lua_State luaState, int stackPos, string field)
{
return LuaCore.luaL_getmetafield(luaState, stackPos, field) != 0;
}
public static int luaL_loadbuffer(LuaCore.lua_State luaState, string buff, string name)
{
return LuaCore.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name);
}
public static int luaL_loadfile(LuaCore.lua_State luaState, string filename)
{
return LuaCore.luaL_loadfile(luaState, filename);
}
public static void lua_error(LuaCore.lua_State luaState)
{
LuaCore.lua_error(luaState);
}
public static bool lua_checkstack(LuaCore.lua_State luaState,int extra)
{
return LuaCore.lua_checkstack(luaState, extra) != 0;
}
public static int lua_next(LuaCore.lua_State luaState,int index)
{
return LuaCore.lua_next(luaState, index);
}
public static void lua_pushlightuserdata(LuaCore.lua_State luaState, object udata)
{
LuaCore.lua_pushlightuserdata(luaState, udata);
}
public static int luanet_rawnetobj(LuaCore.lua_State luaState,int obj)
{
byte[] bytes = lua_touserdata(luaState, obj) as byte[];
return fourBytesToInt(bytes);
}
// Starting with 5.1 the auxlib version of checkudata throws an exception if the type isn't right
// Instead, we want to run our own version that checks the type and just returns null for failure
private static object checkudata_raw(LuaCore.lua_State L, int ud, string tname)
{
object p = LuaCore.lua_touserdata(L, ud);
if(p != null)
{ {
/* value is a userdata? */ /* value is a userdata? */
if(LuaCore.lua_getmetatable(luaState, ud) != 0) if(LuaCore.lua_getmetatable(L, ud) != 0)
{ {
bool isEqual;
/* does it have a metatable? */ /* does it have a metatable? */
LuaCore.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */ LuaCore.lua_getfield(L, (int)LuaIndexes.Registry, tname); /* get correct metatable */
Console.Write("valami:");
Console.Write(LuaCore.lua_rawequal(luaState, -1, -2) != 0); isEqual = LuaCore.lua_rawequal(L, -1, -2) != 0;
Console.Write("\n");
Console.Write("valami2:");
Console.Write(LuaCore.lua_rawequal(luaState, -1, -2).ToBoolean());
Console.Write("\n");
bool isEqual = LuaCore.lua_rawequal(luaState, -1, -2).ToBoolean();
// NASTY - we need our own version of the lua_pop macro // NASTY - we need our own version of the lua_pop macro
// lua_pop(L, 2); /* remove both metatables */ // lua_pop(L, 2); /* remove both metatables */
LuaCore.lua_settop(luaState, -(2) - 1); LuaCore.lua_settop(L, -(2) - 1);
if(isEqual) /* does it have the correct mt? */ if(isEqual) /* does it have the correct mt? */
return p; return p;
} }
} }
return 0; return null;
} }
public static int luanet_checkudata(LuaCore.lua_State luaState, int ud, string tname) public static int luanet_checkudata(LuaCore.lua_State luaState, int ud, string tname)
{ {
int udata = checkudata_raw(luaState, ud, tname); object udata = checkudata_raw(luaState, ud, tname);
return udata != 0 ? udata : -1; return !udata.IsNull() ? fourBytesToInt(udata as byte[]) : -1;
}
public static bool luaL_checkmetatable(LuaCore.lua_State luaState,int index)
{
bool retVal = false;
if(lua_getmetatable(luaState, index) != 0)
{
lua_pushlightuserdata(luaState, tag);
lua_rawget(luaState, -2);
retVal = !lua_isnil(luaState, -1);
lua_settop(luaState, -3);
}
return retVal;
}
public static object luanet_gettag()
{
return tag;
} }
public static void luanet_newudata(LuaCore.lua_State luaState, int val) public static void luanet_newudata(LuaCore.lua_State luaState,int val)
{ {
//LuaCore.lua_newuserdata(luaState, (uint)val); var userdata = lua_newuserdata(luaState, sizeof(int)) as byte[];
byte[] userdata = LuaCore.lua_newuserdata(luaState, sizeof(int)) as byte[];
intToFourBytes(val, userdata); intToFourBytes(val, userdata);
} }
public static int luanet_tonetobject(LuaCore.lua_State luaState, int index) public static int luanet_tonetobject(LuaCore.lua_State luaState,int index)
{ {
int udata; byte[] udata;
Console.WriteLine("x" + LuaCore.lua_type(luaState, index).ToString());
if(LuaCore.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData) if(lua_type(luaState, index) == LuaTypes.UserData)
{ {
if(luaL_checkmetatable(luaState, index)) if(luaL_checkmetatable(luaState, index))
{ {
udata = fourBytesToInt(LuaCore.lua_touserdata(luaState, index) as byte[]); udata=lua_touserdata(luaState, index) as byte[];
if(udata != 0) if(!udata.IsNull())
return udata; return fourBytesToInt(udata);
} }
udata = checkudata_raw(luaState, index, "luaNet_class"); udata = checkudata_raw(luaState, index, "luaNet_class") as byte[];
if(udata != 0) if(!udata.IsNull())
return udata; return fourBytesToInt(udata);
udata = checkudata_raw(luaState, index, "luaNet_searchbase"); udata = checkudata_raw(luaState, index, "luaNet_searchbase") as byte[];
if(udata != 0) if(!udata.IsNull())
return udata; return fourBytesToInt(udata);
udata = checkudata_raw(luaState, index, "luaNet_function"); udata = checkudata_raw(luaState, index, "luaNet_function") as byte[];
if(udata != 0) if(!udata.IsNull())
return udata; return fourBytesToInt(udata);
} }
return -1; return -1;
} }
public static int lua_ref(LuaCore.lua_State luaState, int lockRef)
{
return lockRef != 0 ? LuaCore.luaL_ref(luaState, (int)PseudoIndex.Registry) : 0;
}
private static int fourBytesToInt(byte[] bytes) private static int fourBytesToInt(byte[] bytes)
{ {
Console.WriteLine("tömb:" + bytes.Length.ToString());
return bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24); return bytes[0] + (bytes[1] << 8) + (bytes[2] << 16) + (bytes[3] << 24);
} }
...@@ -248,6 +533,5 @@ namespace LuaInterface ...@@ -248,6 +533,5 @@ namespace LuaInterface
bytes[2] = (byte)(val >> 16); bytes[2] = (byte)(val >> 16);
bytes[3] = (byte)(val >> 24); bytes[3] = (byte)(val >> 24);
} }
#endregion
} }
} }
\ No newline at end of file
...@@ -90,7 +90,7 @@ namespace LuaInterface ...@@ -90,7 +90,7 @@ namespace LuaInterface
private int runFunctionDelegate(LuaCore.lua_State luaState) private int runFunctionDelegate(LuaCore.lua_State luaState)
{ {
LuaCore.lua_CFunction func = (LuaCore.lua_CFunction)translator.getRawNetObject(luaState, 1); LuaCore.lua_CFunction func = (LuaCore.lua_CFunction)translator.getRawNetObject(luaState, 1);
LuaCore.lua_remove(luaState, 1); LuaLib.lua_remove(luaState, 1);
return func(luaState); return func(luaState);
} }
...@@ -121,7 +121,7 @@ namespace LuaInterface ...@@ -121,7 +121,7 @@ namespace LuaInterface
if(!obj.IsNull()) if(!obj.IsNull())
translator.push(luaState, obj.ToString() + ": " + obj.GetHashCode()); translator.push(luaState, obj.ToString() + ": " + obj.GetHashCode());
else else
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
...@@ -133,15 +133,15 @@ namespace LuaInterface ...@@ -133,15 +133,15 @@ namespace LuaInterface
/// FIXME, move somewhere else /// FIXME, move somewhere else
public static void dumpStack(ObjectTranslator translator, LuaCore.lua_State luaState) public static void dumpStack(ObjectTranslator translator, LuaCore.lua_State luaState)
{ {
int depth = LuaCore.lua_gettop(luaState); int depth = LuaLib.lua_gettop(luaState);
Debug.WriteLine("lua stack depth: " + depth); Debug.WriteLine("lua stack depth: " + depth);
for(int i = 1; i <= depth; i++) for(int i = 1; i <= depth; i++)
{ {
var type = LuaCore.lua_type(luaState, i).ToLuaTypes(); var type = LuaLib.lua_type(luaState, i);
// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types // 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.Table) ? "table" : LuaCore.lua_typename(luaState, (int)type).ToString(); string typestr = (type == LuaTypes.Table) ? "table" : LuaLib.lua_typename(luaState, type);
string strrep = LuaCore.lua_tostring(luaState, i).ToString(); string strrep = LuaLib.lua_tostring(luaState, i).ToString();
if(type == LuaTypes.UserData) if(type == LuaTypes.UserData)
{ {
...@@ -167,7 +167,7 @@ namespace LuaInterface ...@@ -167,7 +167,7 @@ namespace LuaInterface
if(obj.IsNull()) if(obj.IsNull())
{ {
translator.throwError(luaState, "trying to index an invalid object reference"); translator.throwError(luaState, "trying to index an invalid object reference");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
...@@ -234,7 +234,7 @@ namespace LuaInterface ...@@ -234,7 +234,7 @@ namespace LuaInterface
if(actualParms.IsNull() || actualParms.Length != 1) if(actualParms.IsNull() || actualParms.Length != 1)
{ {
translator.throwError(luaState, "method not found (or no indexer): " + index); translator.throwError(luaState, "method not found (or no indexer): " + index);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
else else
{ {
...@@ -258,7 +258,7 @@ namespace LuaInterface ...@@ -258,7 +258,7 @@ namespace LuaInterface
else else
translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
} }
} }
...@@ -266,7 +266,7 @@ namespace LuaInterface ...@@ -266,7 +266,7 @@ namespace LuaInterface
} }
} }
LuaCore.lua_pushboolean(luaState, 0); LuaLib.lua_pushboolean(luaState, false);
return 2; return 2;
} }
...@@ -281,30 +281,30 @@ namespace LuaInterface ...@@ -281,30 +281,30 @@ namespace LuaInterface
if(obj.IsNull()) if(obj.IsNull())
{ {
translator.throwError(luaState, "trying to index an invalid object reference"); translator.throwError(luaState, "trying to index an invalid object reference");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
LuaCore.lua_pushboolean(luaState, 0); LuaLib.lua_pushboolean(luaState, false);
return 2; return 2;
} }
string methodName = LuaCore.lua_tostring(luaState, 2).ToString(); string methodName = LuaLib.lua_tostring(luaState, 2).ToString();
if(methodName.IsNull()) if(methodName.IsNull())
{ {
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
LuaCore.lua_pushboolean(luaState, 0); LuaLib.lua_pushboolean(luaState, false);
return 2; return 2;
} }
getMember(luaState, obj.GetType(), obj, "__luaInterface_base_" + methodName, BindingFlags.Instance | BindingFlags.IgnoreCase); getMember(luaState, obj.GetType(), obj, "__luaInterface_base_" + methodName, BindingFlags.Instance | BindingFlags.IgnoreCase);
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
if(LuaCore.lua_type(luaState, -1).ToLuaTypes() == LuaTypes.Nil) if(LuaLib.lua_type(luaState, -1) == LuaTypes.Nil)
{ {
LuaCore.lua_settop(luaState, -2); LuaLib.lua_settop(luaState, -2);
return getMember(luaState, obj.GetType(), obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase); return getMember(luaState, obj.GetType(), obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase);
} }
LuaCore.lua_pushboolean(luaState, 0); LuaLib.lua_pushboolean(luaState, false);
return 2; return 2;
} }
...@@ -384,7 +384,7 @@ namespace LuaInterface ...@@ -384,7 +384,7 @@ namespace LuaInterface
} }
catch catch
{ {
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
} }
else if(member.MemberType == MemberTypes.Property) else if(member.MemberType == MemberTypes.Property)
...@@ -405,12 +405,12 @@ namespace LuaInterface ...@@ -405,12 +405,12 @@ namespace LuaInterface
if(objType is Type && !(((Type)objType) == typeof(object))) if(objType is Type && !(((Type)objType) == typeof(object)))
return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType); return getMember(luaState, ((Type)objType).BaseType, obj, methodName, bindingType);
else else
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
catch(TargetInvocationException e) // Convert this exception into a Lua error catch(TargetInvocationException e) // Convert this exception into a Lua error
{ {
ThrowError(luaState, e); ThrowError(luaState, e);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
} }
else if(member.MemberType == MemberTypes.Event) else if(member.MemberType == MemberTypes.Event)
...@@ -456,7 +456,7 @@ namespace LuaInterface ...@@ -456,7 +456,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 // 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); translator.throwError(luaState, "can't pass instance to static method " + methodName);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
} }
else else
...@@ -465,7 +465,7 @@ namespace LuaInterface ...@@ -465,7 +465,7 @@ namespace LuaInterface
// is not sufficient. valid data members may return nil and therefore there must be some // is not sufficient. valid data members may return nil and therefore there must be some
// way to know the member just doesn't exist. // way to know the member just doesn't exist.
translator.throwError(luaState, "unknown member name " + methodName); translator.throwError(luaState, "unknown member name " + methodName);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
// push false because we are NOT returning a function (see luaIndexFunction) // push false because we are NOT returning a function (see luaIndexFunction)
...@@ -525,9 +525,9 @@ namespace LuaInterface ...@@ -525,9 +525,9 @@ namespace LuaInterface
// We didn't find a property name, now see if we can use a [] style this accessor to set array contents // We didn't find a property name, now see if we can use a [] style this accessor to set array contents
try try
{ {
if(type.IsArray && LuaCore.lua_isnumber(luaState, 2).ToBoolean()) if(type.IsArray && LuaLib.lua_isnumber(luaState, 2))
{ {
int index = (int)LuaCore.lua_tonumber(luaState, 2); int index = (int)LuaLib.lua_tonumber(luaState, 2);
var arr = (Array)target; var arr = (Array)target;
object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType()); object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType());
arr.SetValue(val, index); arr.SetValue(val, index);
...@@ -586,14 +586,14 @@ namespace LuaInterface ...@@ -586,14 +586,14 @@ namespace LuaInterface
// changing the lua typecode to string // changing the lua typecode to string
// Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to // Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to
// be true for isstring. // be true for isstring.
if(LuaCore.lua_type(luaState, 2).ToLuaTypes() != LuaTypes.String) if(LuaLib.lua_type(luaState, 2) != LuaTypes.String)
{ {
detailMessage = "property names must be strings"; detailMessage = "property names must be strings";
return false; return false;
} }
// We only look up property names by string // We only look up property names by string
string fieldName = LuaCore.lua_tostring(luaState, 2).ToString(); string fieldName = LuaLib.lua_tostring(luaState, 2).ToString();
if(fieldName.IsNull() || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_')) if(fieldName.IsNull() || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_'))
{ {
detailMessage = "invalid property name"; detailMessage = "invalid property name";
...@@ -700,25 +700,25 @@ namespace LuaInterface ...@@ -700,25 +700,25 @@ namespace LuaInterface
if(obj.IsNull() || !(obj is IReflect)) if(obj.IsNull() || !(obj is IReflect))
{ {
translator.throwError(luaState, "trying to index an invalid type reference"); translator.throwError(luaState, "trying to index an invalid type reference");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
else else
klass = (IReflect)obj; klass = (IReflect)obj;
if(LuaCore.lua_isnumber(luaState, 2).ToBoolean()) if(LuaLib.lua_isnumber(luaState, 2))
{ {
int size = (int)LuaCore.lua_tonumber(luaState, 2); int size = (int)LuaLib.lua_tonumber(luaState, 2);
translator.push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size)); translator.push(luaState, Array.CreateInstance(klass.UnderlyingSystemType, size));
return 1; return 1;
} }
else else
{ {
string methodName = LuaCore.lua_tostring(luaState, 2).ToString(); string methodName = LuaLib.lua_tostring(luaState, 2).ToString();
if(methodName.IsNull()) if(methodName.IsNull())
{ {
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} //CP: Ignore case } //CP: Ignore case
else else
...@@ -760,13 +760,13 @@ namespace LuaInterface ...@@ -760,13 +760,13 @@ namespace LuaInterface
if(obj.IsNull() || !(obj is IReflect)) if(obj.IsNull() || !(obj is IReflect))
{ {
translator.throwError(luaState, "trying to call constructor on an invalid type reference"); translator.throwError(luaState, "trying to call constructor on an invalid type reference");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
else else
klass = (IReflect)obj; klass = (IReflect)obj;
LuaCore.lua_remove(luaState, 1); LuaLib.lua_remove(luaState, 1);
var constructors = klass.UnderlyingSystemType.GetConstructors(); var constructors = klass.UnderlyingSystemType.GetConstructors();
foreach(var constructor in constructors) foreach(var constructor in constructors)
...@@ -782,11 +782,11 @@ namespace LuaInterface ...@@ -782,11 +782,11 @@ namespace LuaInterface
catch(TargetInvocationException e) catch(TargetInvocationException e)
{ {
ThrowError(luaState, e); ThrowError(luaState, e);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
catch catch
{ {
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
return 1; return 1;
...@@ -796,7 +796,7 @@ namespace LuaInterface ...@@ -796,7 +796,7 @@ namespace LuaInterface
string constructorName = (constructors.Length == 0) ? "unknown" : constructors[0].Name; string constructorName = (constructors.Length == 0) ? "unknown" : constructors[0].Name;
translator.throwError(luaState, String.Format("{0} does not contain constructor({1}) argument match", translator.throwError(luaState, String.Format("{0} does not contain constructor({1}) argument match",
klass.UnderlyingSystemType, constructorName)); klass.UnderlyingSystemType, constructorName));
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
...@@ -811,7 +811,7 @@ namespace LuaInterface ...@@ -811,7 +811,7 @@ namespace LuaInterface
bool isMethod = true; bool isMethod = true;
var paramInfo = method.GetParameters(); var paramInfo = method.GetParameters();
int currentLuaParam = 1; int currentLuaParam = 1;
int nLuaParams = LuaCore.lua_gettop(luaState); int nLuaParams = LuaLib.lua_gettop(luaState);
var paramList = new ArrayList(); var paramList = new ArrayList();
var outList = new List<int>(); var outList = new List<int>();
var argTypes = new List<MethodArgs>(); var argTypes = new List<MethodArgs>();
...@@ -933,7 +933,7 @@ namespace LuaInterface ...@@ -933,7 +933,7 @@ namespace LuaInterface
try try
{ {
luaType = LuaCore.lua_type(luaState, currentLuaParam).ToLuaTypes(); luaType = LuaLib.lua_type(luaState, currentLuaParam);
} }
catch(Exception ex) catch(Exception ex)
{ {
......
...@@ -115,7 +115,7 @@ namespace LuaInterface.Method ...@@ -115,7 +115,7 @@ namespace LuaInterface.Method
bool failedCall = true; bool failedCall = true;
int nReturnValues = 0; int nReturnValues = 0;
if(!LuaCore.lua_checkstack(luaState, 5).ToBoolean()) if(!LuaLib.lua_checkstack(luaState, 5))
throw new LuaException("Lua stack overflow"); throw new LuaException("Lua stack overflow");
bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static; bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
...@@ -128,15 +128,15 @@ namespace LuaInterface.Method ...@@ -128,15 +128,15 @@ namespace LuaInterface.Method
else else
targetObject = _ExtractTarget(luaState, 1); targetObject = _ExtractTarget(luaState, 1);
//LuaCore.lua_remove(luaState,1); // Pops the receiver //LuaLib.lua_remove(luaState,1); // Pops the receiver
if(!_LastCalledMethod.cachedMethod.IsNull()) // Cached? if(!_LastCalledMethod.cachedMethod.IsNull()) // 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 numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
int numArgsPassed = LuaCore.lua_gettop(luaState) - numStackToSkip; int numArgsPassed = LuaLib.lua_gettop(luaState) - numStackToSkip;
if(numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match? if(numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match?
{ {
if(!LuaCore.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean()) if(!LuaLib.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow"); throw new LuaException("Lua stack overflow");
try try
...@@ -172,7 +172,7 @@ namespace LuaInterface.Method ...@@ -172,7 +172,7 @@ namespace LuaInterface.Method
} }
if(_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null && if(_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
!LuaCore.lua_isnil(luaState, i + 1 + numStackToSkip)) !LuaLib.lua_isnil(luaState, i + 1 + numStackToSkip))
throw new LuaException("argument number " + (i + 1) + " is invalid"); throw new LuaException("argument number " + (i + 1) + " is invalid");
} }
...@@ -212,11 +212,11 @@ namespace LuaInterface.Method ...@@ -212,11 +212,11 @@ namespace LuaInterface.Method
if(targetObject.IsNull()) if(targetObject.IsNull())
{ {
_Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName)); _Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
LuaCore.lua_remove(luaState, 1); // Pops the receiver LuaLib.lua_remove(luaState, 1); // Pops the receiver
} }
bool hasMatch = false; bool hasMatch = false;
...@@ -239,7 +239,7 @@ namespace LuaInterface.Method ...@@ -239,7 +239,7 @@ namespace LuaInterface.Method
{ {
string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName); string msg = (candidateName == null) ? "invalid arguments to method call" : ("invalid arguments to method: " + candidateName);
_Translator.throwError(luaState, msg); _Translator.throwError(luaState, msg);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
} }
...@@ -265,7 +265,7 @@ namespace LuaInterface.Method ...@@ -265,7 +265,7 @@ namespace LuaInterface.Method
else if(methodToCall.ContainsGenericParameters) else if(methodToCall.ContainsGenericParameters)
{ {
_Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method"); _Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
} }
...@@ -274,13 +274,13 @@ namespace LuaInterface.Method ...@@ -274,13 +274,13 @@ namespace LuaInterface.Method
if(!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null) if(!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{ {
targetObject = _ExtractTarget(luaState, 1); targetObject = _ExtractTarget(luaState, 1);
LuaCore.lua_remove(luaState, 1); // Pops the receiver LuaLib.lua_remove(luaState, 1); // Pops the receiver
} }
if(!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod)) if(!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{ {
_Translator.throwError(luaState, "invalid arguments to method call"); _Translator.throwError(luaState, "invalid arguments to method call");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
} }
...@@ -288,7 +288,7 @@ namespace LuaInterface.Method ...@@ -288,7 +288,7 @@ namespace LuaInterface.Method
if(failedCall) if(failedCall)
{ {
if(!LuaCore.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean()) if(!LuaLib.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow"); throw new LuaException("Lua stack overflow");
try try
......
...@@ -31,7 +31,8 @@ using System.Collections; ...@@ -31,7 +31,8 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using LuaInterface.Method; using LuaInterface.Method;
using LuaInterface.Exceptions; using LuaInterface.Exceptions;
// TODO: Kódot átt kell alakítani a többihez hasonlóan. using LuaInterface.Extensions;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua; using LuaCore = KopiLua.Lua;
...@@ -44,33 +45,35 @@ namespace LuaInterface ...@@ -44,33 +45,35 @@ namespace LuaInterface
*/ */
public class ObjectTranslator public class ObjectTranslator
{ {
internal CheckType typeChecker; private LuaCore.lua_CFunction registerTableFunction, unregisterTableFunction, getMethodSigFunction,
getConstructorSigFunction, importTypeFunction, loadAssemblyFunction;
// object # to object (FIXME - it should be possible to get object address as an object #)
public readonly Dictionary<int, object> objects = new Dictionary<int, object>();
// object to object # // object to object #
public readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>(); public readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>();
internal Lua interpreter; // object # to object (FIXME - it should be possible to get object address as an object #)
public readonly Dictionary<int, object> objects = new Dictionary<int, object>();
internal EventHandlerContainer pendingEvents = new EventHandlerContainer();
private MetaFunctions metaFunctions; private MetaFunctions metaFunctions;
private List<Assembly> assemblies; private List<Assembly> assemblies;
private LuaCore.lua_CFunction registerTableFunction,unregisterTableFunction,getMethodSigFunction, internal CheckType typeChecker;
getConstructorSigFunction,importTypeFunction,loadAssemblyFunction; internal Lua interpreter;
/// <summary>
internal EventHandlerContainer pendingEvents = new EventHandlerContainer(); /// We want to ensure that objects always have a unique ID
/// </summary>
private int nextObj = 0;
public ObjectTranslator(Lua interpreter,LuaCore.lua_State luaState) public ObjectTranslator(Lua interpreter, LuaCore.lua_State luaState)
{ {
this.interpreter=interpreter; this.interpreter = interpreter;
typeChecker=new CheckType(this); typeChecker = new CheckType(this);
metaFunctions=new MetaFunctions(this); metaFunctions = new MetaFunctions(this);
assemblies=new List<Assembly>(); assemblies = new List<Assembly>();
importTypeFunction=new LuaCore.lua_CFunction(this.importType); importTypeFunction = new LuaCore.lua_CFunction(this.importType);
loadAssemblyFunction=new LuaCore.lua_CFunction(this.loadAssembly); loadAssemblyFunction = new LuaCore.lua_CFunction(this.loadAssembly);
registerTableFunction=new LuaCore.lua_CFunction(this.registerTable); registerTableFunction = new LuaCore.lua_CFunction(this.registerTable);
unregisterTableFunction=new LuaCore.lua_CFunction(this.unregisterTable); unregisterTableFunction = new LuaCore.lua_CFunction(this.unregisterTable);
getMethodSigFunction=new LuaCore.lua_CFunction(this.getMethodSignature); getMethodSigFunction = new LuaCore.lua_CFunction(this.getMethodSignature);
getConstructorSigFunction=new LuaCore.lua_CFunction(this.getConstructorSignature); getConstructorSigFunction = new LuaCore.lua_CFunction(this.getConstructorSignature);
createLuaObjectList(luaState); createLuaObjectList(luaState);
createIndexingMetaFunction(luaState); createIndexingMetaFunction(luaState);
...@@ -85,132 +88,141 @@ namespace LuaInterface ...@@ -85,132 +88,141 @@ namespace LuaInterface
*/ */
private void createLuaObjectList(LuaCore.lua_State luaState) private void createLuaObjectList(LuaCore.lua_State luaState)
{ {
LuaCore.lua_pushstring(luaState,"luaNet_objects"); LuaLib.lua_pushstring(luaState, "luaNet_objects");
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_pushstring(luaState,"__mode"); LuaLib.lua_pushstring(luaState, "__mode");
LuaCore.lua_pushstring(luaState,"v"); LuaLib.lua_pushstring(luaState, "v");
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_setmetatable(luaState,-2); LuaLib.lua_setmetatable(luaState, -2);
LuaCore.lua_settable(luaState, (int) PseudoIndex.Registry); LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry);
} }
/* /*
* Registers the indexing function of CLR objects * Registers the indexing function of CLR objects
* passed to Lua * passed to Lua
*/ */
private void createIndexingMetaFunction(LuaCore.lua_State luaState) private void createIndexingMetaFunction(LuaCore.lua_State luaState)
{ {
LuaCore.lua_pushstring(luaState,"luaNet_indexfunction"); LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
LuaLib.luaL_dostring(luaState,MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring LuaLib.luaL_dostring(luaState, MetaFunctions.luaIndexFunction); // steffenj: lua_dostring renamed to luaL_dostring
//LuaLib.lua_pushstdcallcfunction(luaState,indexFunction); //LuaLib.lua_pushstdcallcfunction(luaState, indexFunction);
LuaCore.lua_rawset(luaState, (int) PseudoIndex.Registry); LuaLib.lua_rawset(luaState, (int)LuaIndexes.Registry);
} }
/* /*
* Creates the metatable for superclasses (the base * Creates the metatable for superclasses (the base
* field of registered tables) * field of registered tables)
*/ */
private void createBaseClassMetatable(LuaCore.lua_State luaState) private void createBaseClassMetatable(LuaCore.lua_State luaState)
{ {
LuaCore.luaL_newmetatable(luaState,"luaNet_searchbase"); LuaLib.luaL_newmetatable(luaState, "luaNet_searchbase");
LuaCore.lua_pushstring(luaState,"__gc"); LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__tostring"); LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__index"); LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.baseIndexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.baseIndexFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__newindex"); LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_settop(luaState,-2); LuaLib.lua_settop(luaState, -2);
} }
/* /*
* Creates the metatable for type references * Creates the metatable for type references
*/ */
private void createClassMetatable(LuaCore.lua_State luaState) private void createClassMetatable(LuaCore.lua_State luaState)
{ {
LuaCore.luaL_newmetatable(luaState,"luaNet_class"); LuaLib.luaL_newmetatable(luaState, "luaNet_class");
LuaCore.lua_pushstring(luaState,"__gc"); LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__tostring"); LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__index"); LuaLib.lua_pushstring(luaState, "__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classIndexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.classIndexFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__newindex"); LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classNewindexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.classNewindexFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__call"); LuaLib.lua_pushstring(luaState, "__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.callConstructorFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.callConstructorFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_settop(luaState,-2); LuaLib.lua_settop(luaState, -2);
} }
/* /*
* Registers the global functions used by LuaInterface * Registers the global functions used by LuaInterface
*/ */
private void setGlobalFunctions(LuaCore.lua_State luaState) private void setGlobalFunctions(LuaCore.lua_State luaState)
{ {
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.indexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.indexFunction);
LuaCore.lua_setglobal(luaState,"get_object_member"); LuaLib.lua_setglobal(luaState, "get_object_member");
LuaLib.lua_pushstdcallcfunction(luaState,importTypeFunction); LuaLib.lua_pushstdcallcfunction(luaState, importTypeFunction);
LuaCore.lua_setglobal(luaState,"import_type"); LuaLib.lua_setglobal(luaState, "import_type");
LuaLib.lua_pushstdcallcfunction(luaState,loadAssemblyFunction); LuaLib.lua_pushstdcallcfunction(luaState, loadAssemblyFunction);
LuaCore.lua_setglobal(luaState,"load_assembly"); LuaLib.lua_setglobal(luaState, "load_assembly");
LuaLib.lua_pushstdcallcfunction(luaState,registerTableFunction); LuaLib.lua_pushstdcallcfunction(luaState, registerTableFunction);
LuaCore.lua_setglobal(luaState,"make_object"); LuaLib.lua_setglobal(luaState, "make_object");
LuaLib.lua_pushstdcallcfunction(luaState,unregisterTableFunction); LuaLib.lua_pushstdcallcfunction(luaState, unregisterTableFunction);
LuaCore.lua_setglobal(luaState,"free_object"); LuaLib.lua_setglobal(luaState, "free_object");
LuaLib.lua_pushstdcallcfunction(luaState,getMethodSigFunction); LuaLib.lua_pushstdcallcfunction(luaState, getMethodSigFunction);
LuaCore.lua_setglobal(luaState,"get_method_bysig"); LuaLib.lua_setglobal(luaState, "get_method_bysig");
LuaLib.lua_pushstdcallcfunction(luaState,getConstructorSigFunction); LuaLib.lua_pushstdcallcfunction(luaState, getConstructorSigFunction);
LuaCore.lua_setglobal(luaState,"get_constructor_bysig"); LuaLib.lua_setglobal(luaState, "get_constructor_bysig");
} }
/* /*
* Creates the metatable for delegates * Creates the metatable for delegates
*/ */
private void createFunctionMetatable(LuaCore.lua_State luaState) private void createFunctionMetatable(LuaCore.lua_State luaState)
{ {
LuaCore.luaL_newmetatable(luaState,"luaNet_function"); LuaLib.luaL_newmetatable(luaState, "luaNet_function");
LuaCore.lua_pushstring(luaState,"__gc"); LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState,"__call"); LuaLib.lua_pushstring(luaState, "__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.execDelegateFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.execDelegateFunction);
LuaCore.lua_settable(luaState,-3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_settop(luaState,-2); LuaLib.lua_settop(luaState, -2);
} }
/* /*
* Passes errors (argument e) to the Lua interpreter * Passes errors (argument e) to the Lua interpreter
*/ */
internal void throwError(LuaCore.lua_State luaState, object e) internal void throwError(LuaCore.lua_State luaState, object e)
{ {
// We use this to remove anything pushed by luaL_where // We use this to remove anything pushed by luaL_where
int oldTop = LuaCore.lua_gettop(luaState); int oldTop = LuaLib.lua_gettop(luaState);
// Stack frame #1 is our C# wrapper, so not very interesting to the user // 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 // Stack frame #2 must be the lua code that called us, so that's what we want to use
LuaCore.luaL_where(luaState, 1); LuaLib.luaL_where(luaState, 1);
object[] curlev = popValues(luaState, oldTop); var curlev = popValues(luaState, oldTop);
// Determine the position in the script where the exception was triggered // Determine the position in the script where the exception was triggered
string errLocation = ""; string errLocation = string.Empty;
if (curlev.Length > 0)
if(curlev.Length > 0)
errLocation = curlev[0].ToString(); errLocation = curlev[0].ToString();
string message = e as string; string message = e as string;
if (message != null)
if(!message.IsNull())
{ {
// Wrap Lua error (just a string) and store the error location // Wrap Lua error (just a string) and store the error location
e = new LuaScriptException(message, errLocation); e = new LuaScriptException(message, errLocation);
} }
else else
{ {
Exception ex = e as Exception; var ex = e as Exception;
if (ex != null)
if(!ex.IsNull())
{ {
// Wrap generic .NET exception as an InnerException and store the error location // Wrap generic .NET exception as an InnerException and store the error location
e = new LuaScriptException(ex, errLocation); e = new LuaScriptException(ex, errLocation);
...@@ -218,8 +230,9 @@ namespace LuaInterface ...@@ -218,8 +230,9 @@ namespace LuaInterface
} }
push(luaState, e); push(luaState, e);
LuaCore.lua_error(luaState); LuaLib.lua_error(luaState);
} }
/* /*
* Implementation of load_assembly. Throws an error * Implementation of load_assembly. Throws an error
* if the assembly is not found. * if the assembly is not found.
...@@ -228,32 +241,27 @@ namespace LuaInterface ...@@ -228,32 +241,27 @@ namespace LuaInterface
{ {
try try
{ {
string assemblyName=LuaCore.lua_tostring(luaState,1).ToString(); string assemblyName = LuaLib.lua_tostring(luaState, 1).ToString();
Assembly assembly = null; Assembly assembly = null;
try try
{ {
assembly = Assembly.Load(assemblyName); assembly = Assembly.Load(assemblyName);
} }
catch (BadImageFormatException) catch(BadImageFormatException)
{ {
// The assemblyName was invalid. It is most likely a path. // The assemblyName was invalid. It is most likely a path.
} }
if (assembly == null) if(assembly.IsNull())
{
assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName)); assembly = Assembly.Load(AssemblyName.GetAssemblyName(assemblyName));
}
if (assembly != null && !assemblies.Contains(assembly)) if(!assembly.IsNull() && !assemblies.Contains(assembly))
{
assemblies.Add(assembly); assemblies.Add(assembly);
}
} }
catch(Exception e) catch(Exception e)
{ {
throwError(luaState,e); throwError(luaState, e);
} }
return 0; return 0;
...@@ -261,14 +269,13 @@ namespace LuaInterface ...@@ -261,14 +269,13 @@ namespace LuaInterface
internal Type FindType(string className) internal Type FindType(string className)
{ {
foreach(Assembly assembly in assemblies) foreach(var assembly in assemblies)
{
Type klass=assembly.GetType(className);
if(klass!=null)
{ {
var klass = assembly.GetType(className);
if(!klass.IsNull())
return klass; return klass;
} }
}
return null; return null;
} }
...@@ -278,14 +285,17 @@ namespace LuaInterface ...@@ -278,14 +285,17 @@ namespace LuaInterface
*/ */
private int importType(LuaCore.lua_State luaState) private int importType(LuaCore.lua_State luaState)
{ {
string className=LuaCore.lua_tostring(luaState,1).ToString(); string className = LuaLib.lua_tostring(luaState, 1).ToString();
Type klass=FindType(className); var klass = FindType(className);
if(klass!=null)
pushType(luaState,klass); if(!klass.IsNull())
pushType(luaState, klass);
else else
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
/* /*
* Implementation of make_object. Registers a table (first * Implementation of make_object. Registers a table (first
* argument in the stack) as an object subclassing the * argument in the stack) as an object subclassing the
...@@ -293,33 +303,35 @@ namespace LuaInterface ...@@ -293,33 +303,35 @@ namespace LuaInterface
*/ */
private int registerTable(LuaCore.lua_State luaState) private int registerTable(LuaCore.lua_State luaState)
{ {
if(LuaCore.lua_type(luaState,1).ToLuaTypes()==LuaTypes.Table) if(LuaLib.lua_type(luaState, 1) == LuaTypes.Table)
{ {
LuaTable luaTable=getTable(luaState,1); var luaTable = getTable(luaState, 1);
string superclassName = LuaCore.lua_tostring(luaState, 2).ToString(); string superclassName = LuaLib.lua_tostring(luaState, 2).ToString();
if (superclassName != null)
if(!superclassName.IsNull())
{ {
Type klass = FindType(superclassName); var klass = FindType(superclassName);
if (klass != null)
if(!klass.IsNull())
{ {
// Creates and pushes the object in the stack, setting // Creates and pushes the object in the stack, setting
// it as the metatable of the first argument // it as the metatable of the first argument
object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable); object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
pushObject(luaState, obj, "luaNet_metatable"); pushObject(luaState, obj, "luaNet_metatable");
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_pushstring(luaState, "__index"); LuaLib.lua_pushstring(luaState, "__index");
LuaCore.lua_pushvalue(luaState, -3); LuaLib.lua_pushvalue(luaState, -3);
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_pushstring(luaState, "__newindex"); LuaLib.lua_pushstring(luaState, "__newindex");
LuaCore.lua_pushvalue(luaState, -3); LuaLib.lua_pushvalue(luaState, -3);
LuaCore.lua_settable(luaState, -3); LuaLib.lua_settable(luaState, -3);
LuaCore.lua_setmetatable(luaState, 1); LuaLib.lua_setmetatable(luaState, 1);
// Pushes the object again, this time as the base field // Pushes the object again, this time as the base field
// of the table and with the luaNet_searchbase metatable // of the table and with the luaNet_searchbase metatable
LuaCore.lua_pushstring(luaState, "base"); LuaLib.lua_pushstring(luaState, "base");
int index = addObject(obj); int index = addObject(obj);
pushNewObject(luaState, obj, index, "luaNet_searchbase"); pushNewObject(luaState, obj, index, "luaNet_searchbase");
LuaCore.lua_rawset(luaState, 1); LuaLib.lua_rawset(luaState, 1);
} }
else else
throwError(luaState, "register_table: can not find superclass '" + superclassName + "'"); throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
...@@ -327,9 +339,12 @@ namespace LuaInterface ...@@ -327,9 +339,12 @@ namespace LuaInterface
else else
throwError(luaState, "register_table: superclass name can not be null"); throwError(luaState, "register_table: superclass name can not be null");
} }
else throwError(luaState,"register_table: first arg is not a table"); else
throwError(luaState, "register_table: first arg is not a table");
return 0; return 0;
} }
/* /*
* Implementation of free_object. Clears the metatable and the * Implementation of free_object. Clears the metatable and the
* base field, freeing the created object for garbage-collection * base field, freeing the created object for garbage-collection
...@@ -338,116 +353,139 @@ namespace LuaInterface ...@@ -338,116 +353,139 @@ namespace LuaInterface
{ {
try try
{ {
if(LuaCore.lua_getmetatable(luaState,1)!=0) if(LuaLib.lua_getmetatable(luaState, 1) != 0)
{ {
LuaCore.lua_pushstring(luaState,"__index"); LuaLib.lua_pushstring(luaState, "__index");
LuaCore.lua_gettable(luaState,-2); LuaLib.lua_gettable(luaState, -2);
object obj=getRawNetObject(luaState,-1); 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(obj.IsNull())
if(luaTableField==null) throwError(luaState,"unregister_table: arg is not valid table"); throwError(luaState, "unregister_table: arg is not valid table");
luaTableField.SetValue(obj,null);
LuaCore.lua_pushnil(luaState); var luaTableField = obj.GetType().GetField("__luaInterface_luaTable");
LuaCore.lua_setmetatable(luaState,1);
LuaCore.lua_pushstring(luaState,"base"); if(luaTableField.IsNull())
LuaCore.lua_pushnil(luaState); throwError(luaState, "unregister_table: arg is not valid table");
LuaCore.lua_settable(luaState,1);
luaTableField.SetValue(obj, null);
LuaLib.lua_pushnil(luaState);
LuaLib.lua_setmetatable(luaState, 1);
LuaLib.lua_pushstring(luaState, "base");
LuaLib.lua_pushnil(luaState);
LuaLib.lua_settable(luaState, 1);
} }
else throwError(luaState,"unregister_table: arg is not valid table"); else
throwError(luaState, "unregister_table: arg is not valid table");
} }
catch(Exception e) catch(Exception e)
{ {
throwError(luaState,e.Message); throwError(luaState, e.Message);
} }
return 0; return 0;
} }
/* /*
* Implementation of get_method_bysig. Returns nil * Implementation of get_method_bysig. Returns nil
* if no matching method is not found. * if no matching method is not found.
*/ */
private int getMethodSignature(LuaCore.lua_State luaState) private int getMethodSignature(LuaCore.lua_State luaState)
{ {
IReflect klass; object target; IReflect klass;
int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class"); object target;
if(udata!=-1) int udata = LuaLib.luanet_checkudata(luaState, 1, "luaNet_class");
if(udata != -1)
{ {
klass=(IReflect)objects[udata]; klass = (IReflect)objects[udata];
target=null; target = null;
} }
else else
{ {
target=getRawNetObject(luaState,1); target = getRawNetObject(luaState, 1);
if(target==null)
if(target.IsNull())
{ {
throwError(luaState,"get_method_bysig: first arg is not type or object reference"); throwError(luaState, "get_method_bysig: first arg is not type or object reference");
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return 1; return 1;
} }
klass=target.GetType();
klass = target.GetType();
} }
string methodName=LuaCore.lua_tostring(luaState,2).ToString();
Type[] signature=new Type[LuaCore.lua_gettop(luaState)-2]; string methodName = LuaLib.lua_tostring(luaState, 2).ToString();
for(int i=0;i<signature.Length;i++) var signature = new Type[LuaLib.lua_gettop(luaState)-2];
signature[i]=FindType(LuaCore.lua_tostring(luaState,i+3).ToString());
for(int i = 0; i < signature.Length; i++)
signature[i] = FindType(LuaLib.lua_tostring(luaState, i+3).ToString());
try try
{ {
//CP: Added ignore case //CP: Added ignore case
MethodInfo method=klass.GetMethod(methodName,BindingFlags.Public | BindingFlags.Static | var method = klass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static |
BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null); BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null);
pushFunction(luaState,new LuaCore.lua_CFunction((new LuaMethodWrapper(this,target,klass,method)).call)); pushFunction(luaState, new LuaCore.lua_CFunction((new LuaMethodWrapper(this, target, klass, method)).call));
} }
catch(Exception e) catch(Exception e)
{ {
throwError(luaState,e); throwError(luaState, e);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
return 1; return 1;
} }
/* /*
* Implementation of get_constructor_bysig. Returns nil * Implementation of get_constructor_bysig. Returns nil
* if no matching constructor is found. * if no matching constructor is found.
*/ */
private int getConstructorSignature(LuaCore.lua_State luaState) private int getConstructorSignature(LuaCore.lua_State luaState)
{ {
IReflect klass=null; IReflect klass = null;
int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class"); int udata = LuaLib.luanet_checkudata(luaState, 1, "luaNet_class");
if(udata!=-1)
{ if(udata != -1)
klass=(IReflect)objects[udata]; klass = (IReflect)objects[udata];
}
if(klass==null) if(klass.IsNull())
{ throwError(luaState, "get_constructor_bysig: first arg is invalid type reference");
throwError(luaState,"get_constructor_bysig: first arg is invalid type reference");
} var signature = new Type[LuaLib.lua_gettop(luaState)-1];
Type[] signature=new Type[LuaCore.lua_gettop(luaState)-1];
for(int i=0;i<signature.Length;i++) for(int i = 0; i < signature.Length; i++)
signature[i]=FindType(LuaCore.lua_tostring(luaState,i+2).ToString()); signature[i] = FindType(LuaLib.lua_tostring(luaState, i+2).ToString());
try try
{ {
ConstructorInfo constructor=klass.UnderlyingSystemType.GetConstructor(signature); ConstructorInfo constructor = klass.UnderlyingSystemType.GetConstructor(signature);
pushFunction(luaState,new LuaCore.lua_CFunction((new LuaMethodWrapper(this,null,klass,constructor)).call)); pushFunction(luaState, new LuaCore.lua_CFunction((new LuaMethodWrapper(this, null, klass, constructor)).call));
} }
catch(Exception e) catch(Exception e)
{ {
throwError(luaState,e); throwError(luaState, e);
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
} }
return 1; return 1;
} }
/* /*
* Pushes a type reference into the stack * Pushes a type reference into the stack
*/ */
internal void pushType(LuaCore.lua_State luaState, Type t) internal void pushType(LuaCore.lua_State luaState, Type t)
{ {
pushObject(luaState,new ProxyType(t),"luaNet_class"); pushObject(luaState, new ProxyType(t), "luaNet_class");
} }
/* /*
* Pushes a delegate into the stack * Pushes a delegate into the stack
*/ */
internal void pushFunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction func) internal void pushFunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction func)
{ {
pushObject(luaState,func,"luaNet_function"); pushObject(luaState, func, "luaNet_function");
} }
/* /*
* Pushes a CLR object into the Lua stack as an userdata * Pushes a CLR object into the Lua stack as an userdata
* with the provided metatable * with the provided metatable
...@@ -455,330 +493,309 @@ namespace LuaInterface ...@@ -455,330 +493,309 @@ namespace LuaInterface
internal void pushObject(LuaCore.lua_State luaState, object o, string metatable) internal void pushObject(LuaCore.lua_State luaState, object o, string metatable)
{ {
int index = -1; int index = -1;
// Pushes nil // Pushes nil
if(o==null) if(o.IsNull())
{ {
LuaCore.lua_pushnil(luaState); LuaLib.lua_pushnil(luaState);
return; return;
} }
// Object already in the list of Lua objects? Push the stored reference. // Object already in the list of Lua objects? Push the stored reference.
bool found = objectsBackMap.TryGetValue(o, out index); bool found = objectsBackMap.TryGetValue(o, out index);
if(found) if(found)
{ {
LuaCore.luaL_getmetatable(luaState,"luaNet_objects"); LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
LuaCore.lua_rawgeti(luaState,-1,index); LuaLib.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 // 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 // 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 // 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 // object here
// did we find a non nil object in our table? if not, we need to call collect object // did we find a non nil object in our table? if not, we need to call collect object
LuaTypes type = LuaCore.lua_type(luaState, -1).ToLuaTypes(); var type = LuaLib.lua_type(luaState, -1);
if (type != LuaTypes.Nil) if(type != LuaTypes.Nil)
{ {
LuaCore.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack LuaLib.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack
return; return;
} }
// MetaFunctions.dumpStack(this, luaState); // MetaFunctions.dumpStack(this, luaState);
LuaCore.lua_remove(luaState, -1); // remove the nil object value LuaLib.lua_remove(luaState, -1); // remove the nil object value
LuaCore.lua_remove(luaState, -1); // remove the metatable LuaLib.lua_remove(luaState, -1); // remove the metatable
collectObject(o, index); // Remove from both our tables and fall out to get a new ID collectObject(o, index); // Remove from both our tables and fall out to get a new ID
} }
index = addObject(o);
pushNewObject(luaState,o,index,metatable); index = addObject(o);
pushNewObject(luaState, o, index, metatable);
} }
/* /*
* Pushes a new object into the Lua stack with the provided * Pushes a new object into the Lua stack with the provided
* metatable * metatable
*/ */
private void pushNewObject(LuaCore.lua_State luaState,object o,int index,string metatable) private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable)
{ {
if(metatable=="luaNet_metatable") if(metatable == "luaNet_metatable")
{ {
// Gets or creates the metatable for the object's type // Gets or creates the metatable for the object's type
LuaCore.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName); LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName);
if(LuaCore.lua_isnil(luaState,-1)) if(LuaLib.lua_isnil(luaState, -1))
{ {
LuaCore.lua_settop(luaState,-2); LuaLib.lua_settop(luaState, -2);
LuaCore.luaL_newmetatable(luaState,o.GetType().AssemblyQualifiedName); LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName);
LuaCore.lua_pushstring(luaState,"cache"); LuaLib.lua_pushstring(luaState, "cache");
LuaCore.lua_newtable(luaState); LuaLib.lua_newtable(luaState);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
LuaCore.lua_pushlightuserdata(luaState,LuaLib.luanet_gettag()); LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag());
LuaCore.lua_pushnumber(luaState,1); LuaLib.lua_pushnumber(luaState, 1);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
LuaCore.lua_pushstring(luaState,"__index"); LuaLib.lua_pushstring(luaState, "__index");
LuaCore.lua_pushstring(luaState,"luaNet_indexfunction"); LuaLib.lua_pushstring(luaState, "luaNet_indexfunction");
LuaCore.lua_rawget(luaState, (int) PseudoIndex.Registry); LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
LuaCore.lua_pushstring(luaState,"__gc"); LuaLib.lua_pushstring(luaState, "__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
LuaCore.lua_pushstring(luaState,"__tostring"); LuaLib.lua_pushstring(luaState, "__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
LuaCore.lua_pushstring(luaState,"__newindex"); LuaLib.lua_pushstring(luaState, "__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction);
LuaCore.lua_rawset(luaState,-3); LuaLib.lua_rawset(luaState, -3);
} }
} }
else else
{ LuaLib.luaL_getmetatable(luaState, metatable);
LuaCore.luaL_getmetatable(luaState,metatable);
}
// Stores the object index in the Lua list and pushes the // Stores the object index in the Lua list and pushes the
// index into the Lua stack // index into the Lua stack
LuaCore.luaL_getmetatable(luaState,"luaNet_objects"); LuaLib.luaL_getmetatable(luaState, "luaNet_objects");
LuaLib.luanet_newudata(luaState,index); LuaLib.luanet_newudata(luaState, index);
LuaCore.lua_pushvalue(luaState,-3); LuaLib.lua_pushvalue(luaState, -3);
LuaCore.lua_remove(luaState,-4); LuaLib.lua_remove(luaState, -4);
LuaCore.lua_setmetatable(luaState,-2); LuaLib.lua_setmetatable(luaState, -2);
LuaCore.lua_pushvalue(luaState,-1); LuaLib.lua_pushvalue(luaState, -1);
LuaCore.lua_rawseti(luaState,-3,index); LuaLib.lua_rawseti(luaState, -3, index);
LuaCore.lua_remove(luaState,-2); LuaLib.lua_remove(luaState, -2);
} }
/* /*
* Gets an object from the Lua stack with the desired type, if it matches, otherwise * Gets an object from the Lua stack with the desired type, if it matches, otherwise
* returns null. * returns null.
*/ */
internal object getAsType(LuaCore.lua_State luaState,int stackPos,Type paramType) internal object getAsType(LuaCore.lua_State luaState, int stackPos, Type paramType)
{ {
ExtractValue extractor=typeChecker.checkType(luaState,stackPos,paramType); var extractor = typeChecker.checkType(luaState, stackPos, paramType);
if(extractor!=null) return extractor(luaState,stackPos); return !extractor.IsNull() ? extractor(luaState, stackPos) : null;
return null;
} }
/// <summary> /// <summary>
/// Given the Lua int ID for an object remove it from our maps /// Given the Lua int ID for an object remove it from our maps
/// </summary> /// </summary>
/// <param name="udata"></param> /// <param name = "udata"></param>
internal void collectObject(int udata) internal void collectObject(int udata)
{ {
object o; object o;
bool found = objects.TryGetValue(udata, out o); bool found = objects.TryGetValue(udata, out o);
// The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry // The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry
if (found) if(found)
{ {
// Debug.WriteLine("Removing " + o.ToString() + " @ " + udata); // Debug.WriteLine("Removing " + o.ToString() + " @ " + udata);
objects.Remove(udata); objects.Remove(udata);
objectsBackMap.Remove(o); objectsBackMap.Remove(o);
} }
} }
/// <summary> /// <summary>
/// Given an object reference, remove it from our maps /// Given an object reference, remove it from our maps
/// </summary> /// </summary>
/// <param name="udata"></param> /// <param name = "udata"></param>
void collectObject(object o, int udata) private void collectObject(object o, int udata)
{ {
// Debug.WriteLine("Removing " + o.ToString() + " @ " + udata); // Debug.WriteLine("Removing " + o.ToString() + " @ " + udata);
objects.Remove(udata); objects.Remove(udata);
objectsBackMap.Remove(o); objectsBackMap.Remove(o);
} }
private int addObject(object obj)
/// <summary>
/// We want to ensure that objects always have a unique ID
/// </summary>
int nextObj = 0;
int addObject(object obj)
{ {
// New object: inserts it in the list // New object: inserts it in the list
int index = nextObj++; int index = nextObj++;
// Debug.WriteLine("Adding " + obj.ToString() + " @ " + index); // Debug.WriteLine("Adding " + obj.ToString() + " @ " + index);
objects[index] = obj; objects[index] = obj;
objectsBackMap[obj] = index; objectsBackMap[obj] = index;
return index; return index;
} }
/* /*
* Gets an object from the Lua stack according to its Lua type. * Gets an object from the Lua stack according to its Lua type.
*/ */
internal object getObject(LuaCore.lua_State luaState,int index) internal object getObject(LuaCore.lua_State luaState, int index)
{ {
LuaTypes type=LuaCore.lua_type(luaState,index).ToLuaTypes(); var type = LuaLib.lua_type(luaState, index);
switch(type) switch(type)
{ {
case LuaTypes.Number: case LuaTypes.Number:
{ {
return LuaCore.lua_tonumber(luaState,index); return LuaLib.lua_tonumber(luaState, index);
} }
case LuaTypes.String: case LuaTypes.String:
{ {
return LuaCore.lua_tostring(luaState,index); return LuaLib.lua_tostring(luaState, index);
} }
case LuaTypes.Boolean: case LuaTypes.Boolean:
{ {
return LuaCore.lua_toboolean(luaState,index); return LuaLib.lua_toboolean(luaState, index);
} }
case LuaTypes.Table: case LuaTypes.Table:
{ {
return getTable(luaState,index); return getTable(luaState, index);
} }
case LuaTypes.Function: case LuaTypes.Function:
{ {
return getFunction(luaState,index); return getFunction(luaState, index);
} }
case LuaTypes.UserData: case LuaTypes.UserData:
{ {
int udata=LuaLib.luanet_tonetobject(luaState,index); int udata = LuaLib.luanet_tonetobject(luaState, index);
Console.WriteLine(udata); return udata != -1 ? objects[udata] : getUserData(luaState, index);
if(udata!=-1)
return objects[udata];
else
return getUserData(luaState,index);
} }
default: default:
return null; return null;
} }
} }
/* /*
* Gets the table in the index positon of the Lua stack. * Gets the table in the index positon of the Lua stack.
*/ */
internal LuaTable getTable(LuaCore.lua_State luaState,int index) internal LuaTable getTable(LuaCore.lua_State luaState, int index)
{ {
LuaCore.lua_pushvalue(luaState,index); LuaLib.lua_pushvalue(luaState, index);
return new LuaTable(LuaLib.lua_ref(luaState,1),interpreter); return new LuaTable(LuaLib.lua_ref(luaState, 1), interpreter);
} }
/* /*
* Gets the userdata in the index positon of the Lua stack. * Gets the userdata in the index positon of the Lua stack.
*/ */
internal LuaUserData getUserData(LuaCore.lua_State luaState,int index) internal LuaUserData getUserData(LuaCore.lua_State luaState, int index)
{ {
LuaCore.lua_pushvalue(luaState,index); LuaLib.lua_pushvalue(luaState, index);
return new LuaUserData(LuaLib.lua_ref(luaState,1),interpreter); return new LuaUserData(LuaLib.lua_ref(luaState, 1), interpreter);
} }
/* /*
* Gets the function in the index positon of the Lua stack. * Gets the function in the index positon of the Lua stack.
*/ */
internal LuaFunction getFunction(LuaCore.lua_State luaState,int index) internal LuaFunction getFunction(LuaCore.lua_State luaState, int index)
{ {
LuaCore.lua_pushvalue(luaState,index); LuaLib.lua_pushvalue(luaState, index);
return new LuaFunction(LuaLib.lua_ref(luaState,1),interpreter); return new LuaFunction(LuaLib.lua_ref(luaState, 1), interpreter);
} }
/* /*
* Gets the CLR object in the index positon of the Lua stack. Returns * Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as Lua functions. * delegates as Lua functions.
*/ */
internal object getNetObject(LuaCore.lua_State luaState,int index) internal object getNetObject(LuaCore.lua_State luaState, int index)
{ {
int idx=LuaLib.luanet_tonetobject(luaState,index); int idx = LuaLib.luanet_tonetobject(luaState, index);
if(idx!=-1) return idx != -1 ? objects[idx] : null;
return objects[idx];
else
return null;
} }
/* /*
* Gets the CLR object in the index positon of the Lua stack. Returns * Gets the CLR object in the index positon of the Lua stack. Returns
* delegates as is. * delegates as is.
*/ */
internal object getRawNetObject(LuaCore.lua_State luaState,int index) internal object getRawNetObject(LuaCore.lua_State luaState, int index)
{ {
int udata=LuaLib.luanet_rawnetobj(luaState,index); int udata = LuaLib.luanet_rawnetobj(luaState, index);
if(udata!=-1) return udata != -1 ? objects[udata] : null;
{
return objects[udata];
}
return null;
} }
/* /*
* Pushes the entire array into the Lua stack and returns the number * Pushes the entire array into the Lua stack and returns the number
* of elements pushed. * of elements pushed.
*/ */
internal int returnValues(LuaCore.lua_State luaState, object[] returnValues) internal int returnValues(LuaCore.lua_State luaState, object[] returnValues)
{ {
if(LuaCore.lua_checkstack(luaState,returnValues.Length+5).ToBoolean()) if(LuaLib.lua_checkstack(luaState, returnValues.Length+5))
{
for(int i=0;i<returnValues.Length;i++)
{ {
push(luaState,returnValues[i]); for(int i = 0; i < returnValues.Length; i++)
} push(luaState, returnValues[i]);
return returnValues.Length; return returnValues.Length;
} else }
else
return 0; return 0;
} }
/* /*
* Gets the values from the provided index to * Gets the values from the provided index to
* the top of the stack and returns them in an array. * the top of the stack and returns them in an array.
*/ */
internal object[] popValues(LuaCore.lua_State luaState,int oldTop) internal object[] popValues(LuaCore.lua_State luaState, int oldTop)
{
int newTop=LuaCore.lua_gettop(luaState);
if(oldTop==newTop)
{ {
int newTop = LuaLib.lua_gettop(luaState);
if(oldTop == newTop)
return null; return null;
}
else else
{ {
ArrayList returnValues=new ArrayList(); var returnValues = new ArrayList();
for(int i=oldTop+1;i<=newTop;i++) for(int i = oldTop+1; i <= newTop; i++)
{ returnValues.Add(getObject(luaState, i));
returnValues.Add(getObject(luaState,i));
} LuaLib.lua_settop(luaState, oldTop);
LuaCore.lua_settop(luaState,oldTop);
return returnValues.ToArray(); return returnValues.ToArray();
} }
} }
/* /*
* Gets the values from the provided index to * Gets the values from the provided index to
* the top of the stack and returns them in an array, casting * the top of the stack and returns them in an array, casting
* them to the provided types. * them to the provided types.
*/ */
internal object[] popValues(LuaCore.lua_State luaState,int oldTop,Type[] popTypes) internal object[] popValues(LuaCore.lua_State luaState, int oldTop, Type[] popTypes)
{
int newTop=LuaCore.lua_gettop(luaState);
if(oldTop==newTop)
{ {
int newTop = LuaLib.lua_gettop(luaState);
if(oldTop == newTop)
return null; return null;
}
else else
{ {
int iTypes; int iTypes;
ArrayList returnValues=new ArrayList(); var returnValues = new ArrayList();
if(popTypes[0] == typeof(void)) if(popTypes[0] == typeof(void))
iTypes=1; iTypes = 1;
else else
iTypes=0; iTypes = 0;
for(int i=oldTop+1;i<=newTop;i++)
for(int i = oldTop+1; i <= newTop; i++)
{ {
returnValues.Add(getAsType(luaState,i,popTypes[iTypes])); returnValues.Add(getAsType(luaState, i, popTypes[iTypes]));
iTypes++; iTypes++;
} }
LuaCore.lua_settop(luaState,oldTop);
LuaLib.lua_settop(luaState, oldTop);
return returnValues.ToArray(); return returnValues.ToArray();
} }
} }
// kevinh - the following line doesn't work for remoting proxies - they always return a match for 'is' // kevinh - the following line doesn't work for remoting proxies - they always return a match for 'is'
// else if(o is ILuaGeneratedType) // else if(o is ILuaGeneratedType)
static bool IsILua(object o) private static bool IsILua(object o)
{ {
if(o is ILuaGeneratedType) if(o is ILuaGeneratedType)
{ {
// Make sure we are _really_ ILuaGenerated // Make sure we are _really_ ILuaGenerated
Type typ = o.GetType(); var typ = o.GetType();
return (!typ.GetInterface("ILuaGeneratedType").IsNull ());
return (typ.GetInterface("ILuaGeneratedType") != null);
} }
else else
return false; return false;
...@@ -789,60 +806,49 @@ namespace LuaInterface ...@@ -789,60 +806,49 @@ namespace LuaInterface
*/ */
internal void push(LuaCore.lua_State luaState, object o) internal void push(LuaCore.lua_State luaState, object o)
{ {
if(o==null) if(o.IsNull())
{ LuaLib.lua_pushnil(luaState);
LuaCore.lua_pushnil(luaState);
}
else if(o is sbyte || o is byte || o is short || o is ushort || 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 int || o is uint || o is long || o is float ||
o is ulong || o is decimal || o is double) o is ulong || o is decimal || o is double)
{ {
double d=Convert.ToDouble(o); double d = Convert.ToDouble(o);
LuaCore.lua_pushnumber(luaState,d); LuaLib.lua_pushnumber(luaState, d);
} }
else if(o is char) else if(o is char)
{ {
double d = (char)o; double d = (char)o;
LuaCore.lua_pushnumber(luaState,d); LuaLib.lua_pushnumber(luaState, d);
} }
else if(o is string) else if(o is string)
{ {
string str=(string)o; string str = (string)o;
LuaCore.lua_pushstring(luaState,str); LuaLib.lua_pushstring(luaState, str);
} }
else if(o is bool) else if(o is bool)
{ {
bool b=(bool)o; bool b = (bool)o;
LuaCore.lua_pushboolean(luaState, (b == true ? 1 : 0)); LuaLib.lua_pushboolean(luaState, b);
} }
else if(IsILua(o)) else if(IsILua(o))
{
(((ILuaGeneratedType)o).__luaInterface_getLuaTable()).push(luaState); (((ILuaGeneratedType)o).__luaInterface_getLuaTable()).push(luaState);
}
else if(o is LuaTable) else if(o is LuaTable)
{
((LuaTable)o).push(luaState); ((LuaTable)o).push(luaState);
}
else if(o is LuaCore.lua_CFunction) else if(o is LuaCore.lua_CFunction)
{ pushFunction(luaState, (LuaCore.lua_CFunction)o);
pushFunction(luaState,(LuaCore.lua_CFunction)o);
}
else if(o is LuaFunction) else if(o is LuaFunction)
{
((LuaFunction)o).push(luaState); ((LuaFunction)o).push(luaState);
}
else else
{ pushObject(luaState, o, "luaNet_metatable");
pushObject(luaState,o,"luaNet_metatable");
}
} }
/* /*
* Checks if the method matches the arguments in the Lua stack, getting * Checks if the method matches the arguments in the Lua stack, getting
* the arguments if it does. * the arguments if it does.
*/ */
internal bool matchParameters(LuaCore.lua_State luaState,MethodBase method,ref MethodCache methodCache) internal bool matchParameters(LuaCore.lua_State luaState, MethodBase method, ref MethodCache methodCache)
{ {
return metaFunctions.matchParameters(luaState,method,ref methodCache); return metaFunctions.matchParameters(luaState, method, ref methodCache);
} }
} }
} }
\ No newline at end of file
...@@ -27,21 +27,18 @@ using System; ...@@ -27,21 +27,18 @@ using System;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using LuaInterface.Config;
// Information about this assembly is defined by the following attributes. // Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project. // Change them to the values specific to your project.
[assembly: AssemblyTitle("LuaInterface")] [assembly: AssemblyTitle("LuaInterface")]
[assembly: AssemblyDescription("Bridge between the Lua runtime and the CLR")] [assembly: AssemblyDescription(Consts.LuaInterfaceDescription)]
#if DEBUG [assembly: AssemblyConfiguration(Consts.LuaInterfaceConfiguration)]
[assembly: AssemblyConfiguration("Debug")] [assembly: AssemblyCompany(Consts.LuaInterfaceCompany)]
#else [assembly: AssemblyProduct(Consts.LuaInterfaceProduct)]
[assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright(Consts.LuaInterfaceCopyright)]
#endif [assembly: AssemblyTrademark(Consts.LuaInterfaceTrademark)]
//[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LuaInterface")]
[assembly: AssemblyCopyright("Copyright 2003-2008 Fabio Mascarenhas, Kevin Hesterm and 2012 Megax")]
//[assembly: AssemblyTrademark("")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
...@@ -60,5 +57,5 @@ using System.Runtime.InteropServices; ...@@ -60,5 +57,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.4")] [assembly: AssemblyVersion(Consts.LuaInterfaceVersion)]
[assembly: AssemblyFileVersion("2.0.4.0")] [assembly: AssemblyFileVersion(Consts.LuaInterfaceFileVersion)]
\ No newline at end of file \ No newline at end of file
...@@ -5,8 +5,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{0E28CF40-4 ...@@ -5,8 +5,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{0E28CF40-4
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLuaInterface", "Test\TestLuaInterface\TestLuaInterface.csproj", "{AEAB974E-4F69-4840-A2C4-7BC55F7C7C3E}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLuaInterface", "Test\TestLuaInterface\TestLuaInterface.csproj", "{AEAB974E-4F69-4840-A2C4-7BC55F7C7C3E}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LuaInterface.Test", "Test\LuaInterface.Test\LuaInterface.Test.csproj", "{F97A7B76-AD92-49F9-B733-F347B7C8439F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Applications", "Applications", "{B13128D8-A4F3-4C53-A4C6-F2EA34F527BD}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Applications", "Applications", "{B13128D8-A4F3-4C53-A4C6-F2EA34F527BD}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LuaRunner", "Applications\LuaRunner\LuaRunner.csproj", "{3CE4CCB6-3465-43E3-B5ED-5FB9B70D20E5}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LuaRunner", "Applications\LuaRunner\LuaRunner.csproj", "{3CE4CCB6-3465-43E3-B5ED-5FB9B70D20E5}"
...@@ -57,18 +55,9 @@ Global ...@@ -57,18 +55,9 @@ Global
{F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x64.Build.0 = Release|x64 {F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x64.Build.0 = Release|x64
{F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x86.ActiveCfg = Release|x86 {F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x86.ActiveCfg = Release|x86
{F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x86.Build.0 = Release|x86 {F55CABBB-4108-4A39-94E1-581FD46DC021}.Release|x86.Build.0 = Release|x86
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Debug|x64.ActiveCfg = Debug|x64
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Debug|x64.Build.0 = Debug|x64
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Debug|x86.ActiveCfg = Debug|x86
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Debug|x86.Build.0 = Debug|x86
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Release|x64.ActiveCfg = Release|x64
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Release|x64.Build.0 = Release|x64
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Release|x86.ActiveCfg = Release|x86
{F97A7B76-AD92-49F9-B733-F347B7C8439F}.Release|x86.Build.0 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{AEAB974E-4F69-4840-A2C4-7BC55F7C7C3E} = {0E28CF40-4DFA-46FE-95BB-E90648DFE6F5} {AEAB974E-4F69-4840-A2C4-7BC55F7C7C3E} = {0E28CF40-4DFA-46FE-95BB-E90648DFE6F5}
{F97A7B76-AD92-49F9-B733-F347B7C8439F} = {0E28CF40-4DFA-46FE-95BB-E90648DFE6F5}
{3CE4CCB6-3465-43E3-B5ED-5FB9B70D20E5} = {B13128D8-A4F3-4C53-A4C6-F2EA34F527BD} {3CE4CCB6-3465-43E3-B5ED-5FB9B70D20E5} = {B13128D8-A4F3-4C53-A4C6-F2EA34F527BD}
{E8DDBC21-EF74-4ABA-9C49-BFC702BE25D8} = {B8664957-CB71-4F11-A4DB-59E7514BC5F3} {E8DDBC21-EF74-4ABA-9C49-BFC702BE25D8} = {B8664957-CB71-4F11-A4DB-59E7514BC5F3}
{F55CABBB-4108-4A39-94E1-581FD46DC021} = {B8664957-CB71-4F11-A4DB-59E7514BC5F3} {F55CABBB-4108-4A39-94E1-581FD46DC021} = {B8664957-CB71-4F11-A4DB-59E7514BC5F3}
......
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