Commit 8a76b646 authored by Megax's avatar Megax
Browse files

* LuaInterface szerkezeti felepitese lasacskan at lesz formalva. Amikor lesz...

* LuaInterface szerkezeti felepitese lasacskan at lesz formalva. Amikor lesz idom es kedvem folytatom az atalakitast.
parent 02869e90
/*
* 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;
namespace LuaInterface
{
/*
* Class used for generating delegates that get a table from the Lua
* stack as a an object of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class ClassGenerator
{
private ObjectTranslator translator;
private Type klass;
public ClassGenerator(ObjectTranslator translator, Type klass)
{
this.translator = translator;
this.klass = klass;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState, int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass, translator.getTable(luaState, stackPos));
}
}
}
\ No newline at end of file
/*
* 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.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
namespace LuaInterface
{
/*
* Structure to store a type and the return types of
* its methods (the type of the returned value and out/ref
* parameters).
*/
struct LuaClassType
{
public Type klass;
public Type[][] returnTypes;
}
/*
* Common interface for types generated from tables. The method
* returns the table that overrides some or all of the type's methods.
*/
public interface ILuaGeneratedType
{
LuaTable __luaInterface_getLuaTable();
}
/*
* Class used for generating delegates that get a function from the Lua
* stack as a delegate of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class DelegateGenerator
{
private ObjectTranslator translator;
private Type delegateType;
public DelegateGenerator(ObjectTranslator translator,Type delegateType)
{
this.translator=translator;
this.delegateType=delegateType;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType,translator.getFunction(luaState,stackPos));
}
}
/*
* Class used for generating delegates that get a table from the Lua
* stack as a an object of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class ClassGenerator
{
private ObjectTranslator translator;
private Type klass;
public ClassGenerator(ObjectTranslator translator,Type klass)
{
this.translator=translator;
this.klass=klass;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState,int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass,translator.getTable(luaState,stackPos));
}
}
/*
* Dynamically generates new types from existing types and
* Lua function and table values. Generated types are event handlers,
......@@ -83,20 +42,17 @@ namespace LuaInterface
*/
class CodeGeneration
{
private Type eventHandlerParent=typeof(LuaEventHandler);
private Dictionary<Type, Type> eventHandlerCollection=new Dictionary<Type, Type>();
private Type delegateParent=typeof(LuaDelegate);
private Dictionary<Type, Type> delegateCollection=new Dictionary<Type, Type>();
private Type classHelper=typeof(LuaClassHelper);
private Dictionary<Type, LuaClassType> classCollection=new Dictionary<Type, LuaClassType>();
private Dictionary<Type, LuaClassType> classCollection = new Dictionary<Type, LuaClassType>();
private Dictionary<Type, Type> eventHandlerCollection = new Dictionary<Type, Type>();
private Dictionary<Type, Type> delegateCollection = new Dictionary<Type, Type>();
private static readonly CodeGeneration instance = new CodeGeneration();
private Type eventHandlerParent = typeof(LuaEventHandler);
private Type delegateParent = typeof(LuaDelegate);
private Type classHelper = typeof(LuaClassHelper);
private AssemblyName assemblyName;
private AssemblyBuilder newAssembly;
private ModuleBuilder newModule;
private int luaClassNumber=1;
private static readonly CodeGeneration instance = new CodeGeneration();
private int luaClassNumber = 1;
static CodeGeneration()
{
......@@ -105,12 +61,11 @@ namespace LuaInterface
private CodeGeneration()
{
// Create an assembly name
assemblyName=new AssemblyName( );
assemblyName.Name="LuaInterface_generatedcode";
assemblyName = new AssemblyName();
assemblyName.Name = "LuaInterface_generatedcode";
// Create a new assembly with one module.
newAssembly=Thread.GetDomain().DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
newModule=newAssembly.DefineDynamicModule("LuaInterface_generatedcode");
newAssembly = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
newModule = newAssembly.DefineDynamicModule("LuaInterface_generatedcode");
}
/*
......@@ -118,10 +73,7 @@ namespace LuaInterface
*/
public static CodeGeneration Instance
{
get
{
return instance;
}
get { return instance; }
}
/*
......@@ -135,30 +87,27 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
// Define a public class in the assembly, called typeName
TypeBuilder myType=newModule.DefineType(typeName,TypeAttributes.Public,eventHandlerParent);
var myType = newModule.DefineType(typeName, TypeAttributes.Public, eventHandlerParent);
// Defines the handler method. Its signature is void(object,<subclassofEventArgs>)
Type[] paramTypes = new Type[2];
paramTypes[0]=typeof(object);
paramTypes[1]=eventHandlerType;
Type returnType=typeof(void);
MethodBuilder handleMethod=myType.DefineMethod("HandleEvent",
MethodAttributes.Public|MethodAttributes.HideBySig,
returnType,paramTypes);
// Defines the handler method. Its signature is void(object, <subclassofEventArgs>)
var paramTypes = new Type[2];
paramTypes[0] = typeof(object);
paramTypes[1] = eventHandlerType;
var returnType = typeof(void);
var handleMethod = myType.DefineMethod("HandleEvent", MethodAttributes.Public | MethodAttributes.HideBySig, returnType, paramTypes);
// Emits the IL for the method. It loads the arguments
// and calls the handleEvent method of the base class
ILGenerator generator=handleMethod.GetILGenerator( );
ILGenerator generator = handleMethod.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Ldarg_2);
MethodInfo miGenericEventHandler;
miGenericEventHandler=eventHandlerParent.GetMethod("handleEvent");
generator.Emit(OpCodes.Call,miGenericEventHandler);
var miGenericEventHandler = eventHandlerParent.GetMethod("handleEvent");
generator.Emit(OpCodes.Call, miGenericEventHandler);
// returns
generator.Emit(OpCodes.Ret);
// creates the new type
return myType.CreateType();
}
......@@ -175,112 +124,126 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
// Define a public class in the assembly, called typeName
TypeBuilder myType=newModule.DefineType(typeName,TypeAttributes.Public,delegateParent);
var myType = newModule.DefineType(typeName, TypeAttributes.Public, delegateParent);
// Defines the delegate method with the same signature as the
// Invoke method of delegateType
MethodInfo invokeMethod=delegateType.GetMethod("Invoke");
ParameterInfo[] paramInfo=invokeMethod.GetParameters();
Type[] paramTypes=new Type[paramInfo.Length];
Type returnType=invokeMethod.ReturnType;
var invokeMethod = delegateType.GetMethod("Invoke");
var paramInfo = invokeMethod.GetParameters();
var paramTypes = new Type[paramInfo.Length];
var returnType = invokeMethod.ReturnType;
// Counts out and ref params, for use later
int nOutParams=0; int nOutAndRefParams=0;
for(int i=0;i<paramTypes.Length;i++)
int nOutParams = 0; int nOutAndRefParams = 0;
for(int i = 0; i < paramTypes.Length; i++)
{
paramTypes[i]=paramInfo[i].ParameterType;
paramTypes[i] = paramInfo[i].ParameterType;
if((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
nOutParams++;
if(paramTypes[i].IsByRef)
nOutAndRefParams++;
}
int[] refArgs=new int[nOutAndRefParams];
MethodBuilder delegateMethod=myType.DefineMethod("CallFunction",
invokeMethod.Attributes,
returnType,paramTypes);
int[] refArgs = new int[nOutAndRefParams];
var delegateMethod = myType.DefineMethod("CallFunction", invokeMethod.Attributes, returnType, paramTypes);
// Generates the IL for the method
ILGenerator generator=delegateMethod.GetILGenerator( );
ILGenerator generator = delegateMethod.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments
if(!(returnType == typeof(void))) // return value
generator.DeclareLocal(returnType);
else
generator.DeclareLocal(typeof(object));
// Initializes local variables
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length);
generator.Emit(OpCodes.Newarr,typeof(object));
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length);
generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length-nOutParams);
generator.Emit(OpCodes.Newarr,typeof(object));
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length-nOutParams);
generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldc_I4,nOutAndRefParams);
generator.Emit(OpCodes.Newarr,typeof(int));
generator.Emit(OpCodes.Ldc_I4, nOutAndRefParams);
generator.Emit(OpCodes.Newarr, typeof(int));
generator.Emit(OpCodes.Stloc_2);
// Stores the arguments in the local variables
for(int iArgs=0,iInArgs=0,iOutArgs=0;iArgs<paramTypes.Length;iArgs++)
for(int iArgs = 0, iInArgs = 0, iOutArgs = 0; iArgs < paramTypes.Length; iArgs++)
{
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,iArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1);
generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef)
{
if(paramTypes[iArgs].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType());
} else generator.Emit(OpCodes.Ldind_Ref);
generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
}
else
generator.Emit(OpCodes.Ldind_Ref);
}
else
{
if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]);
generator.Emit(OpCodes.Box, paramTypes[iArgs]);
}
generator.Emit(OpCodes.Stelem_Ref);
if(paramTypes[iArgs].IsByRef)
{
generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Ldc_I4,iOutArgs);
generator.Emit(OpCodes.Ldc_I4,iArgs);
generator.Emit(OpCodes.Ldc_I4, iOutArgs);
generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Stelem_I4);
refArgs[iOutArgs]=iArgs;
refArgs[iOutArgs] = iArgs;
iOutArgs++;
}
if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut))
{
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4,iInArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1);
generator.Emit(OpCodes.Ldc_I4, iInArgs);
generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef)
{
if(paramTypes[iArgs].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
}
else generator.Emit(OpCodes.Ldind_Ref);
else
generator.Emit(OpCodes.Ldind_Ref);
}
else
{
if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]);
generator.Emit(OpCodes.Box, paramTypes[iArgs]);
}
generator.Emit(OpCodes.Stelem_Ref);
iInArgs++;
}
}
// Calls the callFunction method of the base class
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldloc_2);
MethodInfo miGenericEventHandler;
miGenericEventHandler=delegateParent.GetMethod("callFunction");
generator.Emit(OpCodes.Call,miGenericEventHandler);
var miGenericEventHandler = delegateParent.GetMethod("callFunction");
generator.Emit(OpCodes.Call, miGenericEventHandler);
// Stores return value
if(returnType == typeof(void))
{
......@@ -289,43 +252,48 @@ namespace LuaInterface
}
else if(returnType.IsValueType)
{
generator.Emit(OpCodes.Unbox,returnType);
generator.Emit(OpCodes.Ldobj,returnType);
} else generator.Emit(OpCodes.Castclass,returnType);
generator.Emit(OpCodes.Unbox, returnType);
generator.Emit(OpCodes.Ldobj, returnType);
}
else
generator.Emit(OpCodes.Castclass, returnType);
generator.Emit(OpCodes.Stloc_3);
// Stores new value of out and ref params
for(int i=0;i<refArgs.Length;i++)
for(int i = 0; i < refArgs.Length; i++)
{
generator.Emit(OpCodes.Ldarg,refArgs[i]+1);
generator.Emit(OpCodes.Ldarg, refArgs[i]+1);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,refArgs[i]);
generator.Emit(OpCodes.Ldc_I4, refArgs[i]);
generator.Emit(OpCodes.Ldelem_Ref);
if(paramTypes[refArgs[i]].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Unbox,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Unbox, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj, paramTypes[refArgs[i]].GetElementType());
}
else
{
generator.Emit(OpCodes.Castclass,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Castclass, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stind_Ref);
}
}
// Returns
if(!(returnType == typeof(void)))
generator.Emit(OpCodes.Ldloc_3);
generator.Emit(OpCodes.Ret);
// creates the new type
return myType.CreateType();
generator.Emit(OpCodes.Ret);
return myType.CreateType(); // creates the new type
}
/*
* Generates an implementation of klass, if it is an interface, or
* a subclass of klass that delegates its virtual methods to a Lua table.
*/
public void GenerateClass(Type klass,out Type newType,out Type[][] returnTypes)
public void GenerateClass(Type klass, out Type newType, out Type[][] returnTypes)
{
string typeName;
lock(this)
......@@ -333,66 +301,68 @@ namespace LuaInterface
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
TypeBuilder myType;
// Define a public class in the assembly, called typeName
if(klass.IsInterface)
myType=newModule.DefineType(typeName,TypeAttributes.Public,typeof(object),new Type[] { klass,typeof(ILuaGeneratedType) });
myType = newModule.DefineType(typeName, TypeAttributes.Public, typeof(object), new Type[] { klass, typeof(ILuaGeneratedType) });
else
myType=newModule.DefineType(typeName,TypeAttributes.Public,klass,new Type[] { typeof(ILuaGeneratedType) });
myType = newModule.DefineType(typeName, TypeAttributes.Public, klass, new Type[] { typeof(ILuaGeneratedType) });
// Field that stores the Lua table
FieldBuilder luaTableField=myType.DefineField("__luaInterface_luaTable",typeof(LuaTable),FieldAttributes.Public);
var luaTableField = myType.DefineField("__luaInterface_luaTable", typeof(LuaTable), FieldAttributes.Public);
// Field that stores the return types array
FieldBuilder returnTypesField=myType.DefineField("__luaInterface_returnTypes",typeof(Type[][]),FieldAttributes.Public);
var returnTypesField = myType.DefineField("__luaInterface_returnTypes", typeof(Type[][]), FieldAttributes.Public);
// Generates the constructor for the new type, it takes a Lua table and an array
// of return types and stores them in the respective fields
ConstructorBuilder constructor=
myType.DefineConstructor(MethodAttributes.Public,CallingConventions.Standard,new Type[] { typeof(LuaTable),typeof(Type[][]) });
ILGenerator generator=constructor.GetILGenerator();
var constructor = myType.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(LuaTable), typeof(Type[][]) });
ILGenerator generator = constructor.GetILGenerator();
generator.Emit(OpCodes.Ldarg_0);
if(klass.IsInterface)
generator.Emit(OpCodes.Call,typeof(object).GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Call, typeof(object).GetConstructor(Type.EmptyTypes));
else
generator.Emit(OpCodes.Call,klass.GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Call, klass.GetConstructor(Type.EmptyTypes));
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Stfld,luaTableField);
generator.Emit(OpCodes.Stfld, luaTableField);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldarg_2);
generator.Emit(OpCodes.Stfld,returnTypesField);
generator.Emit(OpCodes.Stfld, returnTypesField);
generator.Emit(OpCodes.Ret);
// Generates overriden versions of the klass' public virtual methods
MethodInfo[] classMethods=klass.GetMethods();
returnTypes=new Type[classMethods.Length][];
int i=0;
foreach(MethodInfo method in classMethods)
var classMethods = klass.GetMethods();
returnTypes = new Type[classMethods.Length][];
int i = 0;
foreach(var method in classMethods)
{
if(klass.IsInterface)
{
GenerateMethod(myType,method,
MethodAttributes.HideBySig|MethodAttributes.Virtual|MethodAttributes.NewSlot,
i,luaTableField,returnTypesField,false,out returnTypes[i]);
GenerateMethod(myType, method, MethodAttributes.HideBySig|MethodAttributes.Virtual|MethodAttributes.NewSlot,
i, luaTableField, returnTypesField, false, out returnTypes[i]);
i++;
}
else
{
if(!method.IsPrivate && !method.IsFinal && method.IsVirtual)
{
GenerateMethod(myType,method,(method.Attributes|MethodAttributes.NewSlot)^MethodAttributes.NewSlot,i,
luaTableField,returnTypesField,true,out returnTypes[i]);
GenerateMethod(myType, method, (method.Attributes|MethodAttributes.NewSlot)^MethodAttributes.NewSlot, i,
luaTableField, returnTypesField, true, out returnTypes[i]);
i++;
}
}
}
// Generates an implementation of the __luaInterface_getLuaTable method
MethodBuilder returnTableMethod=myType.DefineMethod("__luaInterface_getLuaTable",
MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.Virtual,
typeof(LuaTable),new Type[0]);
myType.DefineMethodOverride(returnTableMethod,typeof(ILuaGeneratedType).GetMethod("__luaInterface_getLuaTable"));
generator=returnTableMethod.GetILGenerator();
generator.Emit(OpCodes.Ldfld,luaTableField);
var returnTableMethod = myType.DefineMethod("__luaInterface_getLuaTable",
MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual, typeof(LuaTable), new Type[0]);
myType.DefineMethodOverride(returnTableMethod, typeof(ILuaGeneratedType).GetMethod("__luaInterface_getLuaTable"));
generator = returnTableMethod.GetILGenerator();
generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Ret);
// Creates the type
newType=myType.CreateType();
newType = myType.CreateType(); // Creates the type
}
/*
......@@ -401,170 +371,197 @@ namespace LuaInterface
* doesn't the method calls the base method (or does nothing, in case of interface
* implementations).
*/
private void GenerateMethod(TypeBuilder myType,MethodInfo method,MethodAttributes attributes,
int methodIndex,FieldInfo luaTableField,FieldInfo returnTypesField,bool generateBase,out Type[] returnTypes)
private void GenerateMethod(TypeBuilder myType, MethodInfo method, MethodAttributes attributes, int methodIndex,
FieldInfo luaTableField, FieldInfo returnTypesField, bool generateBase, out Type[] returnTypes)
{
ParameterInfo[] paramInfo=method.GetParameters();
Type[] paramTypes=new Type[paramInfo.Length];
List<Type> returnTypesList=new List<Type>();
var paramInfo = method.GetParameters();
var paramTypes = new Type[paramInfo.Length];
var returnTypesList = new List<Type>();
// Counts out and ref parameters, for later use,
// and creates the list of return types
int nOutParams=0; int nOutAndRefParams=0;
Type returnType=method.ReturnType;
int nOutParams = 0;
int nOutAndRefParams = 0;
var returnType = method.ReturnType;
returnTypesList.Add(returnType);
for(int i=0;i<paramTypes.Length;i++)
for(int i = 0; i < paramTypes.Length; i++)
{
paramTypes[i]=paramInfo[i].ParameterType;
paramTypes[i] = paramInfo[i].ParameterType;
if((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
nOutParams++;
if(paramTypes[i].IsByRef)
{
returnTypesList.Add(paramTypes[i].GetElementType());
nOutAndRefParams++;
}
}
int[] refArgs=new int[nOutAndRefParams];
returnTypes=returnTypesList.ToArray();
int[] refArgs = new int[nOutAndRefParams];
returnTypes = returnTypesList.ToArray();
// Generates a version of the method that calls the base implementation
// directly, for use by the base field of the table
if(generateBase)
{
MethodBuilder baseMethod=myType.DefineMethod("__luaInterface_base_"+method.Name,
var baseMethod = myType.DefineMethod("__luaInterface_base_"+method.Name,
MethodAttributes.Private|MethodAttributes.NewSlot|MethodAttributes.HideBySig,
returnType,paramTypes);
ILGenerator generatorBase=baseMethod.GetILGenerator();
returnType, paramTypes);
ILGenerator generatorBase = baseMethod.GetILGenerator();
generatorBase.Emit(OpCodes.Ldarg_0);
for(int i=0;i<paramTypes.Length;i++)
generatorBase.Emit(OpCodes.Ldarg,i+1);
generatorBase.Emit(OpCodes.Call,method);
for(int i = 0; i < paramTypes.Length; i++)
generatorBase.Emit(OpCodes.Ldarg, i+1);
generatorBase.Emit(OpCodes.Call, method);
if(returnType == typeof(void))
generatorBase.Emit(OpCodes.Pop);
generatorBase.Emit(OpCodes.Ret);
}
// Defines the method
MethodBuilder methodImpl=myType.DefineMethod(method.Name,attributes,
returnType,paramTypes);
var methodImpl = myType.DefineMethod(method.Name, attributes, returnType, paramTypes);
// If it's an implementation of an interface tells what method it
// is overriding
if(myType.BaseType.Equals(typeof(object)))
myType.DefineMethodOverride(methodImpl,method);
ILGenerator generator=methodImpl.GetILGenerator( );
myType.DefineMethodOverride(methodImpl, method);
ILGenerator generator = methodImpl.GetILGenerator( );
generator.DeclareLocal(typeof(object[])); // original arguments
generator.DeclareLocal(typeof(object[])); // with out-only arguments removed
generator.DeclareLocal(typeof(int[])); // indexes of out and ref arguments
if(!(returnType == typeof(void))) // return value
generator.DeclareLocal(returnType);
else
generator.DeclareLocal(typeof(object));
// Initializes local variables
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length);
generator.Emit(OpCodes.Newarr,typeof(object));
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length);
generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldc_I4,paramTypes.Length-nOutParams+1);
generator.Emit(OpCodes.Newarr,typeof(object));
generator.Emit(OpCodes.Ldc_I4, paramTypes.Length-nOutParams+1);
generator.Emit(OpCodes.Newarr, typeof(object));
generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldc_I4,nOutAndRefParams);
generator.Emit(OpCodes.Newarr,typeof(int));
generator.Emit(OpCodes.Ldc_I4, nOutAndRefParams);
generator.Emit(OpCodes.Newarr, typeof(int));
generator.Emit(OpCodes.Stloc_2);
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4_0);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,luaTableField);
generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Stelem_Ref);
// Stores the arguments into the local variables, as needed
for(int iArgs=0,iInArgs=1,iOutArgs=0;iArgs<paramTypes.Length;iArgs++)
for(int iArgs = 0, iInArgs = 1, iOutArgs = 0; iArgs < paramTypes.Length; iArgs++)
{
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,iArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1);
generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef)
{
if(paramTypes[iArgs].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
}
else generator.Emit(OpCodes.Ldind_Ref);
else
generator.Emit(OpCodes.Ldind_Ref);
}
else
{
if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]);
generator.Emit(OpCodes.Box, paramTypes[iArgs]);
}
generator.Emit(OpCodes.Stelem_Ref);
if(paramTypes[iArgs].IsByRef)
{
generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Ldc_I4,iOutArgs);
generator.Emit(OpCodes.Ldc_I4,iArgs);
generator.Emit(OpCodes.Ldc_I4, iOutArgs);
generator.Emit(OpCodes.Ldc_I4, iArgs);
generator.Emit(OpCodes.Stelem_I4);
refArgs[iOutArgs]=iArgs;
refArgs[iOutArgs] = iArgs;
iOutArgs++;
}
if(paramInfo[iArgs].IsIn || (!paramInfo[iArgs].IsOut))
{
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldc_I4,iInArgs);
generator.Emit(OpCodes.Ldarg,iArgs+1);
generator.Emit(OpCodes.Ldc_I4, iInArgs);
generator.Emit(OpCodes.Ldarg, iArgs+1);
if(paramTypes[iArgs].IsByRef)
{
if(paramTypes[iArgs].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Ldobj,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box,paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Ldobj, paramTypes[iArgs].GetElementType());
generator.Emit(OpCodes.Box, paramTypes[iArgs].GetElementType());
}
else generator.Emit(OpCodes.Ldind_Ref);
else
generator.Emit(OpCodes.Ldind_Ref);
}
else
{
if(paramTypes[iArgs].IsValueType)
generator.Emit(OpCodes.Box,paramTypes[iArgs]);
generator.Emit(OpCodes.Box, paramTypes[iArgs]);
}
generator.Emit(OpCodes.Stelem_Ref);
iInArgs++;
}
}
// Gets the function the method will delegate to by calling
// the getTableFunction method of class LuaClassHelper
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,luaTableField);
generator.Emit(OpCodes.Ldstr,method.Name);
generator.Emit(OpCodes.Call,classHelper.GetMethod("getTableFunction"));
Label lab1=generator.DefineLabel();
generator.Emit(OpCodes.Ldfld, luaTableField);
generator.Emit(OpCodes.Ldstr, method.Name);
generator.Emit(OpCodes.Call, classHelper.GetMethod("getTableFunction"));
var lab1 = generator.DefineLabel();
generator.Emit(OpCodes.Dup);
generator.Emit(OpCodes.Brtrue_S,lab1);
generator.Emit(OpCodes.Brtrue_S, lab1);
// Function does not exist, call base method
generator.Emit(OpCodes.Pop);
if(!method.IsAbstract)
{
generator.Emit(OpCodes.Ldarg_0);
for(int i=0;i<paramTypes.Length;i++)
generator.Emit(OpCodes.Ldarg,i+1);
generator.Emit(OpCodes.Call,method);
for(int i = 0; i < paramTypes.Length; i++)
generator.Emit(OpCodes.Ldarg, i+1);
generator.Emit(OpCodes.Call, method);
if(returnType == typeof(void))
generator.Emit(OpCodes.Pop);
generator.Emit(OpCodes.Ret);
generator.Emit(OpCodes.Ldnull);
} else
}
else
generator.Emit(OpCodes.Ldnull);
Label lab2=generator.DefineLabel();
generator.Emit(OpCodes.Br_S,lab2);
var lab2 = generator.DefineLabel();
generator.Emit(OpCodes.Br_S, lab2);
generator.MarkLabel(lab1);
// Function exists, call using method callFunction of LuaClassHelper
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld,returnTypesField);
generator.Emit(OpCodes.Ldc_I4,methodIndex);
generator.Emit(OpCodes.Ldfld, returnTypesField);
generator.Emit(OpCodes.Ldc_I4, methodIndex);
generator.Emit(OpCodes.Ldelem_Ref);
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Call,classHelper.GetMethod("callFunction"));
generator.Emit(OpCodes.Call, classHelper.GetMethod("callFunction"));
generator.MarkLabel(lab2);
// Stores the function return value
if(returnType == typeof(void))
{
......@@ -573,27 +570,31 @@ namespace LuaInterface
}
else if(returnType.IsValueType)
{
generator.Emit(OpCodes.Unbox,returnType);
generator.Emit(OpCodes.Ldobj,returnType);
generator.Emit(OpCodes.Unbox, returnType);
generator.Emit(OpCodes.Ldobj, returnType);
}
else generator.Emit(OpCodes.Castclass,returnType);
else
generator.Emit(OpCodes.Castclass, returnType);
generator.Emit(OpCodes.Stloc_3);
// Sets return values of out and ref parameters
for(int i=0;i<refArgs.Length;i++)
for(int i = 0; i < refArgs.Length; i++)
{
generator.Emit(OpCodes.Ldarg,refArgs[i]+1);
generator.Emit(OpCodes.Ldarg, refArgs[i]+1);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldc_I4,refArgs[i]);
generator.Emit(OpCodes.Ldc_I4, refArgs[i]);
generator.Emit(OpCodes.Ldelem_Ref);
if(paramTypes[refArgs[i]].GetElementType().IsValueType)
{
generator.Emit(OpCodes.Unbox,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Unbox, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Ldobj, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stobj, paramTypes[refArgs[i]].GetElementType());
}
else
{
generator.Emit(OpCodes.Castclass,paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Castclass, paramTypes[refArgs[i]].GetElementType());
generator.Emit(OpCodes.Stind_Ref);
}
}
......@@ -601,6 +602,7 @@ namespace LuaInterface
// Returns
if(!(returnType == typeof(void)))
generator.Emit(OpCodes.Ldloc_3);
generator.Emit(OpCodes.Ret);
}
......@@ -611,17 +613,17 @@ namespace LuaInterface
public LuaEventHandler GetEvent(Type eventHandlerType, LuaFunction eventHandler)
{
Type eventConsumerType;
if (eventHandlerCollection.ContainsKey(eventHandlerType))
{
eventConsumerType=eventHandlerCollection[eventHandlerType];
}
if(eventHandlerCollection.ContainsKey(eventHandlerType))
eventConsumerType = eventHandlerCollection[eventHandlerType];
else
{
eventConsumerType=GenerateEvent(eventHandlerType);
eventConsumerType = GenerateEvent(eventHandlerType);
eventHandlerCollection[eventHandlerType] = eventConsumerType;
}
LuaEventHandler luaEventHandler=(LuaEventHandler)Activator.CreateInstance(eventConsumerType);
luaEventHandler.handler=eventHandler;
var luaEventHandler = (LuaEventHandler)Activator.CreateInstance(eventConsumerType);
luaEventHandler.handler = eventHandler;
return luaEventHandler;
}
......@@ -631,26 +633,30 @@ namespace LuaInterface
*/
public Delegate GetDelegate(Type delegateType, LuaFunction luaFunc)
{
List<Type> returnTypes=new List<Type>();
var returnTypes = new List<Type>();
Type luaDelegateType;
if (delegateCollection.ContainsKey(delegateType))
{
luaDelegateType=delegateCollection[delegateType];
}
if(delegateCollection.ContainsKey(delegateType))
luaDelegateType = delegateCollection[delegateType];
else
{
luaDelegateType=GenerateDelegate(delegateType);
luaDelegateType = GenerateDelegate(delegateType);
delegateCollection[delegateType] = luaDelegateType;
}
MethodInfo methodInfo=delegateType.GetMethod("Invoke");
var methodInfo = delegateType.GetMethod("Invoke");
returnTypes.Add(methodInfo.ReturnType);
foreach(ParameterInfo paramInfo in methodInfo.GetParameters())
{
if(paramInfo.ParameterType.IsByRef)
returnTypes.Add(paramInfo.ParameterType);
LuaDelegate luaDelegate=(LuaDelegate)Activator.CreateInstance(luaDelegateType);
luaDelegate.function=luaFunc;
luaDelegate.returnTypes=returnTypes.ToArray();
return Delegate.CreateDelegate(delegateType,luaDelegate,"CallFunction");
}
var luaDelegate = (LuaDelegate)Activator.CreateInstance(luaDelegateType);
luaDelegate.function = luaFunc;
luaDelegate.returnTypes = returnTypes.ToArray();
return Delegate.CreateDelegate(delegateType, luaDelegate, "CallFunction");
}
/*
......@@ -662,17 +668,17 @@ namespace LuaInterface
public object GetClassInstance(Type klass, LuaTable luaTable)
{
LuaClassType luaClassType;
if (classCollection.ContainsKey(klass))
{
luaClassType=classCollection[klass];
}
if(classCollection.ContainsKey(klass))
luaClassType = classCollection[klass];
else
{
luaClassType=new LuaClassType();
GenerateClass(klass,out luaClassType.klass,out luaClassType.returnTypes);
luaClassType = new LuaClassType();
GenerateClass(klass, out luaClassType.klass, out luaClassType.returnTypes);
classCollection[klass] = luaClassType;
}
return Activator.CreateInstance(luaClassType.klass,new object[] {luaTable,luaClassType.returnTypes});
return Activator.CreateInstance(luaClassType.klass, new object[] {luaTable, luaClassType.returnTypes});
}
}
}
\ No newline at end of file
/*
* 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;
namespace LuaInterface
{
/*
* Class used for generating delegates that get a function from the Lua
* stack as a delegate of a specific type.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class DelegateGenerator
{
private ObjectTranslator translator;
private Type delegateType;
public DelegateGenerator(ObjectTranslator translator, Type delegateType)
{
this.translator = translator;
this.delegateType = delegateType;
}
public object extractGenerated(KopiLua.Lua.lua_State luaState, int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType, translator.getFunction(luaState, stackPos));
}
}
}
\ No newline at end of file
/*
* 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;
namespace LuaInterface
{
/*
* Common interface for types generated from tables. The method
* returns the table that overrides some or all of the type's methods.
*/
public interface ILuaGeneratedType
{
LuaTable __luaInterface_getLuaTable();
}
}
\ No newline at end of file
/*
* 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;
namespace LuaInterface
{
/*
* Structure to store a type and the return types of
* its methods (the type of the returned value and out/ref
* parameters).
*/
struct LuaClassType
{
public Type klass;
public Type[][] returnTypes;
}
}
\ No newline at end of file
/*
* 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.IO;
using System.Threading;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using LuaInterface.Event;
namespace LuaInterface
{
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Reflection;
using System.Threading;
using LuaWrap;
/*
* Main class of LuaInterface
* Object-oriented wrapper to Lua API
......@@ -64,15 +88,15 @@ namespace LuaInterface
"-- Preload the mscorlib assembly \n"+
"luanet.load_assembly(\"mscorlib\") \n";
/*readonly */ KopiLua.Lua.lua_State luaState;
ObjectTranslator translator;
private /*readonly */ KopiLua.Lua.lua_State luaState;
private ObjectTranslator translator;
KopiLua.Lua.lua_CFunction panicCallback/*, lockCallback, unlockCallback*/;
private KopiLua.Lua.lua_CFunction panicCallback;
/// <summary>
/// Used to ensure multiple .net threads all get serialized by this single lock for access to the lua stack/objects
/// </summary>
object luaLock = new object();
private object luaLock = new object();
public Lua()
{
......@@ -561,8 +585,8 @@ namespace LuaInterface
public LuaFunction GetFunction(string fullPath)
{
object obj=this[fullPath];
//return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : (LuaFunction)obj);
return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this));
return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : (LuaFunction)obj);
//return (obj is KopiLua.Lua.lua_CFunction ? new LuaFunction((KopiLua.Lua.lua_CFunction)obj,this) : /*(LuaFunction)*/new LuaFunction(obj.GetHashCode(), this));
}
/*
* Gets a function global variable as a delegate of
......@@ -1041,98 +1065,4 @@ namespace LuaInterface
#endregion
}
/// <summary>
/// Event codes for lua hook function
/// </summary>
/// <remarks>
/// Do not change any of the values because they must match the lua values
/// </remarks>
/// <author>Reinhard Ostermeier</author>
public enum EventCodes
{
LUA_HOOKCALL = 0,
LUA_HOOKRET = 1,
LUA_HOOKLINE = 2,
LUA_HOOKCOUNT = 3,
LUA_HOOKTAILRET = 4,
}
/// <summary>
/// Event masks for lua hook callback
/// </summary>
/// <remarks>
/// Do not change any of the values because they must match the lua values
/// </remarks>
/// <author>Reinhard Ostermeier</author>
[Flags]
public enum EventMasks
{
LUA_MASKCALL = (1 << EventCodes.LUA_HOOKCALL),
LUA_MASKRET = (1 << EventCodes.LUA_HOOKRET),
LUA_MASKLINE = (1 << EventCodes.LUA_HOOKLINE),
LUA_MASKCOUNT = (1 << EventCodes.LUA_HOOKCOUNT),
LUA_MASKALL = Int32.MaxValue,
}
/// <summary>
/// Structure for lua debug information
/// </summary>
/// <remarks>
/// Do not change this struct because it must match the lua structure lua_debug
/// </remarks>
/// <author>Reinhard Ostermeier</author>
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct LuaDebug
{
public EventCodes eventCode;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String name;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String namewhat;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String what;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPStr)]
public String source;
public int currentline;
public int nups;
public int linedefined;
public int lastlinedefined;
[System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 60/*LUA_IDSIZE*/)]
public String shortsrc;
public int i_ci;
}
/// <summary>
/// Event args for hook callback event
/// </summary>
/// <author>Reinhard Ostermeier</author>
public class DebugHookEventArgs : EventArgs
{
private readonly LuaDebug luaDebug;
public DebugHookEventArgs(LuaDebug luaDebug)
{
this.luaDebug = luaDebug;
}
public LuaDebug LuaDebug
{
get { return luaDebug; }
}
}
public class HookExceptionEventArgs : EventArgs
{
private readonly Exception m_Exception;
public Exception Exception
{
get { return m_Exception; }
}
public HookExceptionEventArgs(Exception ex)
{
m_Exception = ex;
}
}
}
\ No newline at end of file
using System;
/*
* 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.Collections.Generic;
using System.Text;
......
/*
* 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.Runtime.Serialization;
......
using System;
using System.Collections.Generic;
/*
* 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.Text;
using LuaWrap;
using System.Collections.Generic;
namespace LuaInterface
{
public class LuaFunction : LuaBase
{
//private Lua interpreter;
internal KopiLua.Lua.lua_CFunction function;
//internal int reference;
public LuaFunction(int reference, Lua interpreter)
{
......@@ -25,39 +47,6 @@ namespace LuaInterface
_Interpreter = interpreter;
}
//~LuaFunction()
//{
// if (reference != 0)
// interpreter.dispose(reference);
//}
//bool disposed = false;
//~LuaFunction()
//{
// Dispose(false);
//}
//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}
//public virtual void Dispose(bool disposeManagedResources)
//{
// if (!this.disposed)
// {
// if (disposeManagedResources)
// {
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
// }
// disposed = true;
// }
//}
/*
* Calls the function casting return values to the types
* in returnTypes
......
/*
* 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;
namespace LuaInterface
......
/*
* 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;
namespace LuaInterface
......
......@@ -36,7 +36,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CheckType.cs" />
<Compile Include="GenerateEventAssembly.cs" />
<Compile Include="Lua.cs" />
<Compile Include="LuaException.cs" />
<Compile Include="Metatables.cs" />
......@@ -53,6 +52,17 @@
<Compile Include="LuaScriptException.cs" />
<Compile Include="LuaTable.cs" />
<Compile Include="LuaUserData.cs" />
<Compile Include="Extensions\GeneralExtensions.cs" />
<Compile Include="GenerateEventAssembly\LuaClassType.cs" />
<Compile Include="GenerateEventAssembly\ILuaGeneratedType.cs" />
<Compile Include="GenerateEventAssembly\DelegateGenerator.cs" />
<Compile Include="GenerateEventAssembly\ClassGenerator.cs" />
<Compile Include="GenerateEventAssembly\CodeGeneration.cs" />
<Compile Include="Event\EventCodes.cs" />
<Compile Include="Event\EventMasks.cs" />
<Compile Include="Event\LuaDebug.cs" />
<Compile Include="Event\DebugHookEventArgs.cs" />
<Compile Include="Event\HookExceptionEventArgs.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
......@@ -68,4 +78,9 @@
<Name>KopiLua</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Extensions\" />
<Folder Include="GenerateEventAssembly\" />
<Folder Include="Event\" />
</ItemGroup>
</Project>
//
// LuaDLL.cs
//
// Author:
// Joshua Simmons <simmons.44@gmail.com>
//
// Copyright (c) 2009 Joshua Simmons
//
// 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.
/*
* This file is part of LuaInterface.
*
* Copyright (C) 2003-2005 Fabio Mascarenhas de Queiroz.
* Copyright (C) 2009 Joshua Simmons <simmons.44@gmail.com>
* 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.IO;
using System.Runtime.InteropServices;
using KopiLua;
using System.Runtime.Serialization.Formatters.Binary;
using LuaInterface.Extensions;
namespace LuaWrap
namespace LuaInterface
{
/// <summary>
/// Enumeration of basic lua globals.
......@@ -114,7 +115,7 @@ namespace LuaWrap
Collect = 2,
/// <summary>
/// Returns the current amount of memory (in Kbytes) in use by Lua.
/// Returns the current amount of memory (in Kbytes) in use by KopiLua.Lua.
/// </summary>
Count = 3,
......@@ -150,6 +151,22 @@ namespace LuaWrap
{
private static int tag = 0;
/// <summary>
/// Function to get byte array from a object
/// </summary>
/// <param name="_Object">object to get byte array</param>
/// <returns>Byte Array</returns>
public static byte[] ObjectToByteArray(object obj)
{
if(obj.IsNull())
return null;
var bf = new BinaryFormatter();
var ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
public static LuaTypes ToLuaTypes(this int type)
{
return (LuaTypes)type;
......@@ -175,9 +192,9 @@ namespace LuaWrap
/// <param name="fn">
/// A <see cref="CallbackFunction"/>
/// </param>
public static void lua_pushcfunction(Lua.lua_State state, Lua.lua_CFunction fn)
public static void lua_pushcfunction(KopiLua.Lua.lua_State state, KopiLua.Lua.lua_CFunction fn)
{
Lua.lua_pushcclosure(state, fn, 0);
KopiLua.Lua.lua_pushcclosure(state, fn, 0);
}
#endregion
......@@ -194,9 +211,9 @@ namespace LuaWrap
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public static bool luaL_dofile(Lua.lua_State state, string filename)
public static bool luaL_dofile(KopiLua.Lua.lua_State state, string filename)
{
return (Lua.luaL_loadfile(state, filename).ToLuaEnum() == LuaEnum.Ok) && (Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
return (KopiLua.Lua.luaL_loadfile(state, filename).ToLuaEnum() == LuaEnum.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
}
/// <summary>
......@@ -211,14 +228,14 @@ namespace LuaWrap
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public static bool luaL_dostring(Lua.lua_State state, string chunk)
public static bool luaL_dostring(KopiLua.Lua.lua_State state, string chunk)
{
return (Lua.luaL_loadstring(state, chunk).ToLuaEnum() == LuaEnum.Ok) && (Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
return (KopiLua.Lua.luaL_loadstring(state, chunk).ToLuaEnum() == LuaEnum.Ok) && (KopiLua.Lua.lua_pcall(state, 0, (int)LuaEnum.MultiRet, 0).ToLuaEnum() == LuaEnum.Ok);
}
public static LuaEnum luaL_loadbuffer(Lua.lua_State luaState, string buff, string name)
public static LuaEnum luaL_loadbuffer(KopiLua.Lua.lua_State luaState, string buff, string name)
{
LuaEnum result = Lua.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnum();
var result = KopiLua.Lua.luaL_loadbuffer(luaState, buff, (uint)buff.Length, name).ToLuaEnum();
return result;
}
......@@ -234,22 +251,24 @@ namespace LuaWrap
/// <param name="r">
/// A <see cref="System.Int32"/>
/// </param>
public static void luaL_getref(Lua.lua_State state, int t, int r)
public static void luaL_getref(KopiLua.Lua.lua_State state, int t, int r)
{
Lua.lua_rawgeti(state, t, r);
KopiLua.Lua.lua_rawgeti(state, t, r);
}
public static bool luaL_checkmetatable(Lua.lua_State luaState,int index)
public static bool luaL_checkmetatable(KopiLua.Lua.lua_State luaState,int index)
{
bool retVal = false;
Console.WriteLine("v: " + luaState.tt.ToString());
if(Lua.lua_getmetatable(luaState,index)!=0)
if(KopiLua.Lua.lua_getmetatable(luaState,index)!=0)
{
Lua.lua_pushlightuserdata(luaState, tag);
Lua.lua_rawget(luaState, -2);
retVal = !Lua.lua_isnil(luaState, -1);
Lua.lua_settop(luaState, -3);
KopiLua.Lua.lua_pushlightuserdata(luaState, tag);
KopiLua.Lua.lua_rawget(luaState, -2);
retVal = !KopiLua.Lua.lua_isnil(luaState, -1);
KopiLua.Lua.lua_settop(luaState, -3);
}
return retVal;
}
......@@ -258,44 +277,43 @@ namespace LuaWrap
return tag;
}
public static void lua_getref(Lua.lua_State luaState, int reference)
public static void lua_getref(KopiLua.Lua.lua_State luaState, int reference)
{
Lua.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference);
KopiLua.Lua.lua_rawgeti(luaState, (int)PseudoIndex.Registry, reference);
}
public static void lua_unref(Lua.lua_State luaState, int reference)
public static void lua_unref(KopiLua.Lua.lua_State luaState, int reference)
{
Lua.luaL_unref(luaState, (int)PseudoIndex.Registry, reference);
KopiLua.Lua.luaL_unref(luaState, (int)PseudoIndex.Registry, reference);
}
public static int luanet_rawnetobj(Lua.lua_State luaState,int obj)
public static int luanet_rawnetobj(KopiLua.Lua.lua_State luaState,int obj)
{
int udata= (int)Lua.lua_touserdata2(luaState, obj);
if(udata!=0)
return udata;
return -1;
int udata = (int)KopiLua.Lua.lua_touserdata2(luaState, obj);
return udata != 0 ? udata : -1;
}
public static void lua_pushstdcallcfunction(Lua.lua_State luaState, KopiLua.Lua.lua_CFunction function)
public static void lua_pushstdcallcfunction(KopiLua.Lua.lua_State luaState,KopiLua.Lua.lua_CFunction function)
{
lua_pushcfunction(luaState, function);
}
public static int checkudata_raw(Lua.lua_State luaState, int ud, string tname)
public static int checkudata_raw(KopiLua.Lua.lua_State luaState, int ud, string tname)
{
int p = (int)Lua.lua_touserdata2(luaState, ud);
int p = (int)KopiLua.Lua.lua_touserdata2(luaState, ud);
//Console.WriteLine(BitConverter.ToInt32(ObjectToByteArray(KopiLua.Lua.lua_touserdata(luaState, ud)), 0));
if(p != 0)
{
/* value is a userdata? */
if(Lua.lua_getmetatable(luaState, ud)!=0)
if(KopiLua.Lua.lua_getmetatable(luaState, ud)!=0)
{
/* does it have a metatable? */
Lua.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */
bool isEqual = Lua.lua_rawequal(luaState, -1, -2).ToBoolean();
KopiLua.Lua.lua_getfield(luaState, (int)PseudoIndex.Registry, tname); /* get correct metatable */
bool isEqual = KopiLua.Lua.lua_rawequal(luaState, -1, -2).ToBoolean();
// NASTY - we need our own version of the lua_pop macro
// lua_pop(L, 2); /* remove both metatables */
Lua.lua_settop(luaState, -(2) - 1);
KopiLua.Lua.lua_settop(luaState, -(2) - 1);
if(isEqual) /* does it have the correct mt? */
return p;
......@@ -305,33 +323,31 @@ namespace LuaWrap
return 0;
}
public static int luanet_checkudata(Lua.lua_State luaState, int ud, string tname)
public static int luanet_checkudata(KopiLua.Lua.lua_State luaState, int ud, string tname)
{
int udata = checkudata_raw(luaState, ud, tname);
if(udata != 0)
return udata;
return -1;
return udata != 0 ? udata : -1;
}
public static void luanet_newudata(Lua.lua_State luaState, int val)
public static void luanet_newudata(KopiLua.Lua.lua_State luaState, int val)
{
Lua.lua_newuserdata(luaState, (uint)val);
KopiLua.Lua.lua_newuserdata(luaState, (uint)val);
}
public static int luanet_tonetobject(Lua.lua_State luaState, int index)
public static int luanet_tonetobject(KopiLua.Lua.lua_State luaState, int index)
{
int udata;
Console.WriteLine("x" + KopiLua.Lua.lua_type(luaState, index).ToString());
if(Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData)
if(KopiLua.Lua.lua_type(luaState, index).ToLuaTypes() == LuaTypes.UserData)
{
if(luaL_checkmetatable(luaState, index))
{
udata = (int)Lua.lua_touserdata2(luaState, index);
udata = (int)KopiLua.Lua.lua_touserdata2(luaState, index);
if(udata != 0)
return udata;
}
udata = checkudata_raw(luaState, index, "luaNet_class");
if(udata != 0)
return udata;
......@@ -344,17 +360,14 @@ namespace LuaWrap
if(udata != 0)
return udata;
}
return -1;
}
public static int lua_ref(Lua.lua_State luaState, int lockRef)
public static int lua_ref(KopiLua.Lua.lua_State luaState, int lockRef)
{
if(lockRef != 0)
return Lua.luaL_ref(luaState, (int)PseudoIndex.Registry);
else
return 0;
return lockRef != 0 ? KopiLua.Lua.luaL_ref(luaState, (int)PseudoIndex.Registry) : 0;
}
#endregion
}
}
\ No newline at end of file
using System;
/*
* 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.Diagnostics.CodeAnalysis;
using System.Reflection;
......
using System;
/*
* 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;
namespace LuaInterface
{
......
using System;
using System.Collections.Generic;
/*
* 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.Text;
using System.Collections;
using LuaWrap;
using System.Collections.Generic;
namespace LuaInterface
{
......@@ -14,43 +38,12 @@ namespace LuaInterface
*/
public class LuaTable : LuaBase
{
//internal int _Reference;
//private Lua _Interpreter;
public LuaTable(int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
}
//bool disposed = false;
//~LuaTable()
//{
// Dispose(false);
//}
//public void Dispose()
//{
// Dispose(true);
// GC.SuppressFinalize(this);
//}
//public virtual void Dispose(bool disposeManagedResources)
//{
// if (!this.disposed)
// {
// if (disposeManagedResources)
// {
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
// }
// disposed = true;
// }
//}
//~LuaTable()
//{
// _Interpreter.dispose(_Reference);
//}
/*
* Indexer for string fields of the table
*/
......@@ -126,18 +119,5 @@ namespace LuaInterface
{
return "table";
}
//public override bool Equals(object o)
//{
// if (o is LuaTable)
// {
// LuaTable l = (LuaTable)o;
// return _Interpreter.compareRef(l._Reference, _Reference);
// }
// else return false;
//}
//public override int GetHashCode()
//{
// return _Reference;
//}
}
}
using System;
using System.Collections.Generic;
/*
* 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.Text;
using LuaWrap;
using System.Collections.Generic;
namespace LuaInterface
{
public class LuaUserData : LuaBase
{
//internal int _Reference;
//private Lua _Interpreter;
public LuaUserData(int reference, Lua interpreter)
{
_Reference = reference;
_Interpreter = interpreter;
}
//~LuaUserData()
//{
// if (_Reference != 0)
// _Interpreter.dispose(_Reference);
//}
/*
* Indexer for string fields of the userdata
*/
......@@ -66,18 +84,5 @@ namespace LuaInterface
{
return "userdata";
}
//public override bool Equals(object o)
//{
// if (o is LuaUserData)
// {
// LuaUserData l = (LuaUserData)o;
// return _Interpreter.compareRef(l._Reference, _Reference);
// }
// else return false;
//}
//public override int GetHashCode()
//{
// return _Reference;
//}
}
}
\ No newline at end of file
/*
* 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.IO;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace LuaInterface
{
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using LuaWrap;
/*
* Functions used in the metatables of userdata representing
* CLR objects
......@@ -110,7 +134,7 @@ namespace LuaInterface
{
LuaTypes type = KopiLua.Lua.lua_type(luaState, i).ToLuaTypes();
// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
string typestr = (type == LuaTypes.Table) ? "table" : KopiLua.Lua.lua_typename(luaState, Convert.ToInt32(type)).ToString();
string typestr = (type == LuaTypes.Table) ? "table" : KopiLua.Lua.lua_typename(luaState, (int)type).ToString();
string strrep = KopiLua.Lua.lua_tostring(luaState, i).ToString();
if (type == LuaTypes.UserData)
......
/*
* 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.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
namespace LuaInterface
{
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using LuaWrap;
/*
* Cached method
*/
......
/*
* 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.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
namespace LuaInterface
{
using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using LuaWrap;
/*
* Passes objects from the CLR to Lua and vice-versa
*
......@@ -206,7 +230,7 @@ namespace LuaInterface
try
{
assembly = Assembly.LoadWithPartialName(assemblyName);
assembly = Assembly.Load(assemblyName);
}
catch (BadImageFormatException)
{
......@@ -614,6 +638,7 @@ namespace LuaInterface
case LuaTypes.UserData:
{
int udata=LuaLib.luanet_tonetobject(luaState,index);
Console.WriteLine(udata);
if(udata!=-1)
return objects[udata];
else
......
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