Commit d7bb0c93 authored by Megax's avatar Megax
Browse files

* Mar csak egy falj nincs atalakitva. Ha az is meglesz (vagy kozben) akkor a...

* Mar csak egy falj nincs atalakitva. Ha az is meglesz (vagy kozben) akkor a fajlok kimeneti helyet is modositom + hozzadom azokat az exeket amik a luainterface-hez voltak.
parent bbf63a7e
/*
* 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;
namespace LuaInterface.Method
{
/*
* Wrapper class for events that does registration/deregistration
* of event handlers.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class RegisterEventHandler
{
private EventHandlerContainer pendingEvents;
private EventInfo eventInfo;
private object target;
public RegisterEventHandler(EventHandlerContainer pendingEvents, object target, EventInfo eventInfo)
{
this.target = target;
this.eventInfo = eventInfo;
this.pendingEvents = pendingEvents;
}
/*
* Adds a new event handler
*/
public Delegate Add(LuaFunction function)
{
//CP: Fix by Ben Bryant for event handling with one parameter
//link: http://luaforge.net/forum/message.php?msg_id=9266
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(eventInfo.EventHandlerType, function);
eventInfo.AddEventHandler(target, handlerDelegate);
pendingEvents.Add(handlerDelegate, this);
return handlerDelegate;
//MethodInfo mi = eventInfo.EventHandlerType.GetMethod("Invoke");
//ParameterInfo[] pi = mi.GetParameters();
//LuaEventHandler handler=CodeGeneration.Instance.GetEvent(pi[1].ParameterType,function);
//Delegate handlerDelegate=Delegate.CreateDelegate(eventInfo.EventHandlerType,handler,"HandleEvent");
//eventInfo.AddEventHandler(target,handlerDelegate);
//pendingEvents.Add(handlerDelegate, this);
//return handlerDelegate;
}
/*
* Removes an existing event handler
*/
public void Remove(Delegate handlerDelegate)
{
RemovePending(handlerDelegate);
pendingEvents.Remove(handlerDelegate);
}
/*
* Removes an existing event handler (without updating the pending handlers list)
*/
internal void RemovePending(Delegate handlerDelegate)
{
eventInfo.RemoveEventHandler(target, handlerDelegate);
}
}
}
\ 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.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using LuaInterface.Exceptions;
namespace LuaInterface
{
/*
* Cached method
*/
struct MethodCache
{
private MethodBase _cachedMethod;
public MethodBase cachedMethod
{
get
{
return _cachedMethod;
}
set
{
_cachedMethod = value;
MethodInfo mi = value as MethodInfo;
if (mi != null)
{
IsReturnVoid = string.Compare(mi.ReturnType.Name, "System.Void", true) == 0;
}
}
}
public bool IsReturnVoid;
// List or arguments
public object[] args;
// Positions of out parameters
public int[] outList;
// Types of parameters
public MethodArgs[] argTypes;
}
/*
* Parameter information
*/
struct MethodArgs
{
// Position of parameter
public int index;
// Type-conversion function
public ExtractValue extractValue;
public bool isParamsArray;
public Type paramsArrayType;
}
/*
* Argument extraction with type-conversion function
*/
delegate object ExtractValue(KopiLua.Lua.lua_State luaState, int stackPos);
/*
* Wrapper class for methods/constructors accessed from Lua.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class LuaMethodWrapper
{
private ObjectTranslator _Translator;
private MethodBase _Method;
private MethodCache _LastCalledMethod = new MethodCache();
private string _MethodName;
private MemberInfo[] _Members;
private IReflect _TargetType;
private ExtractValue _ExtractTarget;
private object _Target;
private BindingFlags _BindingType;
/*
* Constructs the wrapper for a known MethodBase instance
*/
public LuaMethodWrapper(ObjectTranslator translator, object target, IReflect targetType, MethodBase method)
{
_Translator = translator;
_Target = target;
_TargetType = targetType;
if (targetType != null)
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
_Method = method;
_MethodName = method.Name;
if (method.IsStatic)
{ _BindingType = BindingFlags.Static; }
else
{ _BindingType = BindingFlags.Instance; }
}
/*
* Constructs the wrapper for a known method name
*/
public LuaMethodWrapper(ObjectTranslator translator, IReflect targetType, string methodName, BindingFlags bindingType)
{
_Translator = translator;
_MethodName = methodName;
_TargetType = targetType;
if (targetType != null)
_ExtractTarget = translator.typeChecker.getExtractor(targetType);
_BindingType = bindingType;
//CP: Removed NonPublic binding search and added IgnoreCase
_Members = targetType.UnderlyingSystemType.GetMember(methodName, MemberTypes.Method, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase/*|BindingFlags.NonPublic*/);
}
/// <summary>
/// Convert C# exceptions into Lua errors
/// </summary>
/// <returns>num of things on stack</returns>
/// <param name="e">null for no pending exception</param>
int SetPendingException(Exception e)
{
return _Translator.interpreter.SetPendingException(e);
}
/*
* Calls the method. Receives the arguments from the Lua stack
* and returns values in it.
*/
public int call(KopiLua.Lua.lua_State luaState)
{
MethodBase methodToCall = _Method;
object targetObject = _Target;
bool failedCall = true;
int nReturnValues = 0;
if (!KopiLua.Lua.lua_checkstack(luaState, 5).ToBoolean())
throw new LuaException("Lua stack overflow");
bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
SetPendingException(null);
if (methodToCall == null) // Method from name
{
if (isStatic)
targetObject = null;
else
targetObject = _ExtractTarget(luaState, 1);
//KopiLua.Lua.lua_remove(luaState,1); // Pops the receiver
if (_LastCalledMethod.cachedMethod != null) // Cached?
{
int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
int numArgsPassed = KopiLua.Lua.lua_gettop(luaState) - numStackToSkip;
if (numArgsPassed == _LastCalledMethod.argTypes.Length) // No. of args match?
{
if (!KopiLua.Lua.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
{
for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
{
if (_LastCalledMethod.argTypes[i].isParamsArray)
{
object luaParamValue = _LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
Type paramArrayType = _LastCalledMethod.argTypes[i].paramsArrayType;
Array paramArray;
if (luaParamValue is LuaTable)
{
LuaTable table = (LuaTable)luaParamValue;
paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);
for (int x = 1; x <= table.Values.Count; x++)
{
paramArray.SetValue(Convert.ChangeType(table[x], paramArrayType), x - 1);
}
}
else
{
paramArray = Array.CreateInstance(paramArrayType, 1);
paramArray.SetValue(luaParamValue, 0);
}
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] = paramArray;
}
else
{
_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] =
_LastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);
}
if (_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
!KopiLua.Lua.lua_isnil(luaState, i + 1 + numStackToSkip))
{
throw new LuaException("argument number " + (i + 1) + " is invalid");
}
}
if ((_BindingType & BindingFlags.Static) == BindingFlags.Static)
{
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
}
else
{
if (_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
}
failedCall = false;
}
catch (TargetInvocationException e)
{
// Failure of method invocation
return SetPendingException(e.GetBaseException());
}
catch (Exception e)
{
if (_Members.Length == 1) // Is the method overloaded?
// No, throw error
return SetPendingException(e);
}
}
}
// Cache miss
if (failedCall)
{
// System.Diagnostics.Debug.WriteLine("cache miss on " + methodName);
// If we are running an instance variable, we can now pop the targetObject from the stack
if (!isStatic)
{
if (targetObject == null)
{
_Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
KopiLua.Lua.lua_remove(luaState, 1); // Pops the receiver
}
bool hasMatch = false;
string candidateName = null;
foreach (MemberInfo member in _Members)
{
candidateName = member.ReflectedType.Name + "." + member.Name;
MethodBase m = (MethodInfo)member;
bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod);
if (isMethod)
{
hasMatch = true;
break;
}
}
if (!hasMatch)
{
string msg = (candidateName == null)
? "invalid arguments to method call"
: ("invalid arguments to method: " + candidateName);
_Translator.throwError(luaState, msg);
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
}
else // Method from MethodBase instance
{
if (methodToCall.ContainsGenericParameters)
{
bool isMethod = _Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod);
if (methodToCall.IsGenericMethodDefinition)
{
//need to make a concrete type of the generic method definition
List<Type> typeArgs = new List<Type>();
foreach (object arg in _LastCalledMethod.args)
typeArgs.Add(arg.GetType());
MethodInfo concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod(typeArgs.ToArray());
_Translator.push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args));
failedCall = false;
}
else if (methodToCall.ContainsGenericParameters)
{
_Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
else
{
if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{
targetObject = _ExtractTarget(luaState, 1);
KopiLua.Lua.lua_remove(luaState, 1); // Pops the receiver
}
if (!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
{
_Translator.throwError(luaState, "invalid arguments to method call");
KopiLua.Lua.lua_pushnil(luaState);
return 1;
}
}
}
if (failedCall)
{
if (!KopiLua.Lua.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6).ToBoolean())
throw new LuaException("Lua stack overflow");
try
{
if (isStatic)
{
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
}
else
{
if (_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
else
_Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
}
}
catch (TargetInvocationException e)
{
return SetPendingException(e.GetBaseException());
}
catch (Exception e)
{
return SetPendingException(e);
}
}
// Pushes out and ref return values
for (int index = 0; index < _LastCalledMethod.outList.Length; index++)
{
nReturnValues++;
//for(int i=0;i<lastCalledMethod.outList.Length;i++)
_Translator.push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]);
}
//by isSingle 2010-09-10 11:26:31
//Desc:
// if not return void,we need add 1,
// or we will lost the function's return value
// when call dotnet function like "int foo(arg1,out arg2,out arg3)" in lua code
if (!_LastCalledMethod.IsReturnVoid && nReturnValues > 0)
{
nReturnValues++;
}
return nReturnValues < 1 ? 1 : nReturnValues;
}
}
/// <summary>
/// We keep track of what delegates we have auto attached to an event - to allow us to cleanly exit a LuaInterface session
/// </summary>
class EventHandlerContainer : IDisposable
{
Dictionary<Delegate, RegisterEventHandler> dict = new Dictionary<Delegate, RegisterEventHandler>();
public void Add(Delegate handler, RegisterEventHandler eventInfo)
{
dict.Add(handler, eventInfo);
}
public void Remove(Delegate handler)
{
bool found = dict.Remove(handler);
Debug.Assert(found);
}
/// <summary>
/// Remove any still registered handlers
/// </summary>
public void Dispose()
{
foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in dict)
{
pair.Value.RemovePending(pair.Key);
}
dict.Clear();
}
}
/*
* Wrapper class for events that does registration/deregistration
* of event handlers.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
class RegisterEventHandler
{
object target;
EventInfo eventInfo;
EventHandlerContainer pendingEvents;
public RegisterEventHandler(EventHandlerContainer pendingEvents, object target, EventInfo eventInfo)
{
this.target = target;
this.eventInfo = eventInfo;
this.pendingEvents = pendingEvents;
}
/*
* Adds a new event handler
*/
public Delegate Add(LuaFunction function)
{
//CP: Fix by Ben Bryant for event handling with one parameter
//link: http://luaforge.net/forum/message.php?msg_id=9266
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(eventInfo.EventHandlerType, function);
eventInfo.AddEventHandler(target, handlerDelegate);
pendingEvents.Add(handlerDelegate, this);
return handlerDelegate;
//MethodInfo mi = eventInfo.EventHandlerType.GetMethod("Invoke");
//ParameterInfo[] pi = mi.GetParameters();
//LuaEventHandler handler=CodeGeneration.Instance.GetEvent(pi[1].ParameterType,function);
//Delegate handlerDelegate=Delegate.CreateDelegate(eventInfo.EventHandlerType,handler,"HandleEvent");
//eventInfo.AddEventHandler(target,handlerDelegate);
//pendingEvents.Add(handlerDelegate, this);
//return handlerDelegate;
}
/*
* Removes an existing event handler
*/
public void Remove(Delegate handlerDelegate)
{
RemovePending(handlerDelegate);
pendingEvents.Remove(handlerDelegate);
}
/*
* Removes an existing event handler (without updating the pending handlers list)
*/
internal void RemovePending(Delegate handlerDelegate)
{
eventInfo.RemoveEventHandler(target, handlerDelegate);
}
}
/*
* Base wrapper class for Lua function event handlers.
* Subclasses that do actual event handling are created
* at runtime.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaEventHandler
{
public LuaFunction handler = null;
// CP: Fix provided by Ben Bryant for delegates with one param
// link: http://luaforge.net/forum/message.php?msg_id=9318
public void handleEvent(object[] args)
{
handler.Call(args);
}
//public void handleEvent(object sender,object data)
//{
// handler.call(new object[] { sender,data },new Type[0]);
//}
}
/*
* Wrapper class for Lua functions as delegates
* Subclasses with correct signatures are created
* at runtime.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaDelegate
{
public Type[] returnTypes;
public LuaFunction function;
public LuaDelegate()
{
function = null;
returnTypes = null;
}
public object callFunction(object[] args, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
if (returnTypes[0] == typeof(void))
{
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
iRefArgs = 1;
}
// Sets the value of out and ref parameters (from
// the values returned by the Lua function).
for (int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
iRefArgs++;
}
return returnValue;
}
}
/*
* Static helper methods for Lua tables acting as CLR objects.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaClassHelper
{
/*
* Gets the function called name from the provided table,
* returning null if it does not exist
*/
public static LuaFunction getTableFunction(LuaTable luaTable, string name)
{
object funcObj = luaTable.rawget(name);
if (funcObj is LuaFunction)
return (LuaFunction)funcObj;
else
return null;
}
/*
* Calls the provided function with the provided parameters
*/
public static object callFunction(LuaFunction function, object[] args, Type[] returnTypes, object[] inArgs, int[] outArgs)
{
// args is the return array of arguments, inArgs is the actual array
// of arguments passed to the function (with in parameters only), outArgs
// has the positions of out parameters
object returnValue;
int iRefArgs;
object[] returnValues = function.call(inArgs, returnTypes);
if (returnTypes[0] == typeof(void))
{
returnValue = null;
iRefArgs = 0;
}
else
{
returnValue = returnValues[0];
iRefArgs = 1;
}
for (int i = 0; i < outArgs.Length; i++)
{
args[outArgs[i]] = returnValues[iRefArgs];
iRefArgs++;
}
return returnValue;
}
}
}
...@@ -29,10 +29,13 @@ using System.Reflection; ...@@ -29,10 +29,13 @@ using System.Reflection;
using System.Diagnostics; using System.Diagnostics;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using LuaInterface.Method;
using LuaInterface.Exceptions; using LuaInterface.Exceptions;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
/* /*
* Passes objects from the CLR to Lua and vice-versa * Passes objects from the CLR to Lua and vice-versa
* *
...@@ -50,24 +53,24 @@ namespace LuaInterface ...@@ -50,24 +53,24 @@ namespace LuaInterface
internal Lua interpreter; internal Lua interpreter;
private MetaFunctions metaFunctions; private MetaFunctions metaFunctions;
private List<Assembly> assemblies; private List<Assembly> assemblies;
private KopiLua.Lua.lua_CFunction registerTableFunction,unregisterTableFunction,getMethodSigFunction, private LuaCore.lua_CFunction registerTableFunction,unregisterTableFunction,getMethodSigFunction,
getConstructorSigFunction,importTypeFunction,loadAssemblyFunction; getConstructorSigFunction,importTypeFunction,loadAssemblyFunction;
internal EventHandlerContainer pendingEvents = new EventHandlerContainer(); internal EventHandlerContainer pendingEvents = new EventHandlerContainer();
public ObjectTranslator(Lua interpreter,KopiLua.Lua.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 KopiLua.Lua.lua_CFunction(this.importType); importTypeFunction=new LuaCore.lua_CFunction(this.importType);
loadAssemblyFunction=new KopiLua.Lua.lua_CFunction(this.loadAssembly); loadAssemblyFunction=new LuaCore.lua_CFunction(this.loadAssembly);
registerTableFunction=new KopiLua.Lua.lua_CFunction(this.registerTable); registerTableFunction=new LuaCore.lua_CFunction(this.registerTable);
unregisterTableFunction=new KopiLua.Lua.lua_CFunction(this.unregisterTable); unregisterTableFunction=new LuaCore.lua_CFunction(this.unregisterTable);
getMethodSigFunction=new KopiLua.Lua.lua_CFunction(this.getMethodSignature); getMethodSigFunction=new LuaCore.lua_CFunction(this.getMethodSignature);
getConstructorSigFunction=new KopiLua.Lua.lua_CFunction(this.getConstructorSignature); getConstructorSigFunction=new LuaCore.lua_CFunction(this.getConstructorSignature);
createLuaObjectList(luaState); createLuaObjectList(luaState);
createIndexingMetaFunction(luaState); createIndexingMetaFunction(luaState);
...@@ -80,117 +83,117 @@ namespace LuaInterface ...@@ -80,117 +83,117 @@ namespace LuaInterface
/* /*
* Sets up the list of objects in the Lua side * Sets up the list of objects in the Lua side
*/ */
private void createLuaObjectList(KopiLua.Lua.lua_State luaState) private void createLuaObjectList(LuaCore.lua_State luaState)
{ {
KopiLua.Lua.lua_pushstring(luaState,"luaNet_objects"); LuaCore.lua_pushstring(luaState,"luaNet_objects");
KopiLua.Lua.lua_newtable(luaState); LuaCore.lua_newtable(luaState);
KopiLua.Lua.lua_newtable(luaState); LuaCore.lua_newtable(luaState);
KopiLua.Lua.lua_pushstring(luaState,"__mode"); LuaCore.lua_pushstring(luaState,"__mode");
KopiLua.Lua.lua_pushstring(luaState,"v"); LuaCore.lua_pushstring(luaState,"v");
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_setmetatable(luaState,-2); LuaCore.lua_setmetatable(luaState,-2);
KopiLua.Lua.lua_settable(luaState, (int) PseudoIndex.Registry); LuaCore.lua_settable(luaState, (int) PseudoIndex.Registry);
} }
/* /*
* Registers the indexing function of CLR objects * Registers the indexing function of CLR objects
* passed to Lua * passed to Lua
*/ */
private void createIndexingMetaFunction(KopiLua.Lua.lua_State luaState) private void createIndexingMetaFunction(LuaCore.lua_State luaState)
{ {
KopiLua.Lua.lua_pushstring(luaState,"luaNet_indexfunction"); LuaCore.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);
KopiLua.Lua.lua_rawset(luaState, (int) PseudoIndex.Registry); LuaCore.lua_rawset(luaState, (int) PseudoIndex.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(KopiLua.Lua.lua_State luaState) private void createBaseClassMetatable(LuaCore.lua_State luaState)
{ {
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_searchbase"); LuaCore.luaL_newmetatable(luaState,"luaNet_searchbase");
KopiLua.Lua.lua_pushstring(luaState,"__gc"); LuaCore.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring"); LuaCore.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index"); LuaCore.lua_pushstring(luaState,"__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.baseIndexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.baseIndexFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex"); LuaCore.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2); LuaCore.lua_settop(luaState,-2);
} }
/* /*
* Creates the metatable for type references * Creates the metatable for type references
*/ */
private void createClassMetatable(KopiLua.Lua.lua_State luaState) private void createClassMetatable(LuaCore.lua_State luaState)
{ {
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_class"); LuaCore.luaL_newmetatable(luaState,"luaNet_class");
KopiLua.Lua.lua_pushstring(luaState,"__gc"); LuaCore.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring"); LuaCore.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index"); LuaCore.lua_pushstring(luaState,"__index");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classIndexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classIndexFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex"); LuaCore.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classNewindexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.classNewindexFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__call"); LuaCore.lua_pushstring(luaState,"__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.callConstructorFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.callConstructorFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2); LuaCore.lua_settop(luaState,-2);
} }
/* /*
* Registers the global functions used by LuaInterface * Registers the global functions used by LuaInterface
*/ */
private void setGlobalFunctions(KopiLua.Lua.lua_State luaState) private void setGlobalFunctions(LuaCore.lua_State luaState)
{ {
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.indexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.indexFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_object_member"); LuaCore.lua_setglobal(luaState,"get_object_member");
LuaLib.lua_pushstdcallcfunction(luaState,importTypeFunction); LuaLib.lua_pushstdcallcfunction(luaState,importTypeFunction);
KopiLua.Lua.lua_setglobal(luaState,"import_type"); LuaCore.lua_setglobal(luaState,"import_type");
LuaLib.lua_pushstdcallcfunction(luaState,loadAssemblyFunction); LuaLib.lua_pushstdcallcfunction(luaState,loadAssemblyFunction);
KopiLua.Lua.lua_setglobal(luaState,"load_assembly"); LuaCore.lua_setglobal(luaState,"load_assembly");
LuaLib.lua_pushstdcallcfunction(luaState,registerTableFunction); LuaLib.lua_pushstdcallcfunction(luaState,registerTableFunction);
KopiLua.Lua.lua_setglobal(luaState,"make_object"); LuaCore.lua_setglobal(luaState,"make_object");
LuaLib.lua_pushstdcallcfunction(luaState,unregisterTableFunction); LuaLib.lua_pushstdcallcfunction(luaState,unregisterTableFunction);
KopiLua.Lua.lua_setglobal(luaState,"free_object"); LuaCore.lua_setglobal(luaState,"free_object");
LuaLib.lua_pushstdcallcfunction(luaState,getMethodSigFunction); LuaLib.lua_pushstdcallcfunction(luaState,getMethodSigFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_method_bysig"); LuaCore.lua_setglobal(luaState,"get_method_bysig");
LuaLib.lua_pushstdcallcfunction(luaState,getConstructorSigFunction); LuaLib.lua_pushstdcallcfunction(luaState,getConstructorSigFunction);
KopiLua.Lua.lua_setglobal(luaState,"get_constructor_bysig"); LuaCore.lua_setglobal(luaState,"get_constructor_bysig");
} }
/* /*
* Creates the metatable for delegates * Creates the metatable for delegates
*/ */
private void createFunctionMetatable(KopiLua.Lua.lua_State luaState) private void createFunctionMetatable(LuaCore.lua_State luaState)
{ {
KopiLua.Lua.luaL_newmetatable(luaState,"luaNet_function"); LuaCore.luaL_newmetatable(luaState,"luaNet_function");
KopiLua.Lua.lua_pushstring(luaState,"__gc"); LuaCore.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__call"); LuaCore.lua_pushstring(luaState,"__call");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.execDelegateFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.execDelegateFunction);
KopiLua.Lua.lua_settable(luaState,-3); LuaCore.lua_settable(luaState,-3);
KopiLua.Lua.lua_settop(luaState,-2); LuaCore.lua_settop(luaState,-2);
} }
/* /*
* Passes errors (argument e) to the Lua interpreter * Passes errors (argument e) to the Lua interpreter
*/ */
internal void throwError(KopiLua.Lua.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 = KopiLua.Lua.lua_gettop(luaState); int oldTop = LuaCore.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
KopiLua.Lua.luaL_where(luaState, 1); LuaCore.luaL_where(luaState, 1);
object[] curlev = popValues(luaState, oldTop); object[] 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
...@@ -215,17 +218,17 @@ namespace LuaInterface ...@@ -215,17 +218,17 @@ namespace LuaInterface
} }
push(luaState, e); push(luaState, e);
KopiLua.Lua.lua_error(luaState); LuaCore.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.
*/ */
private int loadAssembly(KopiLua.Lua.lua_State luaState) private int loadAssembly(LuaCore.lua_State luaState)
{ {
try try
{ {
string assemblyName=KopiLua.Lua.lua_tostring(luaState,1).ToString(); string assemblyName=LuaCore.lua_tostring(luaState,1).ToString();
Assembly assembly = null; Assembly assembly = null;
...@@ -273,14 +276,14 @@ namespace LuaInterface ...@@ -273,14 +276,14 @@ namespace LuaInterface
* Implementation of import_type. Returns nil if the * Implementation of import_type. Returns nil if the
* type is not found. * type is not found.
*/ */
private int importType(KopiLua.Lua.lua_State luaState) private int importType(LuaCore.lua_State luaState)
{ {
string className=KopiLua.Lua.lua_tostring(luaState,1).ToString(); string className=LuaCore.lua_tostring(luaState,1).ToString();
Type klass=FindType(className); Type klass=FindType(className);
if(klass!=null) if(klass!=null)
pushType(luaState,klass); pushType(luaState,klass);
else else
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
return 1; return 1;
} }
/* /*
...@@ -288,12 +291,12 @@ namespace LuaInterface ...@@ -288,12 +291,12 @@ namespace LuaInterface
* argument in the stack) as an object subclassing the * argument in the stack) as an object subclassing the
* type passed as second argument in the stack. * type passed as second argument in the stack.
*/ */
private int registerTable(KopiLua.Lua.lua_State luaState) private int registerTable(LuaCore.lua_State luaState)
{ {
if(KopiLua.Lua.lua_type(luaState,1).ToLuaTypes()==LuaTypes.Table) if(LuaCore.lua_type(luaState,1).ToLuaTypes()==LuaTypes.Table)
{ {
LuaTable luaTable=getTable(luaState,1); LuaTable luaTable=getTable(luaState,1);
string superclassName = KopiLua.Lua.lua_tostring(luaState, 2).ToString(); string superclassName = LuaCore.lua_tostring(luaState, 2).ToString();
if (superclassName != null) if (superclassName != null)
{ {
Type klass = FindType(superclassName); Type klass = FindType(superclassName);
...@@ -303,20 +306,20 @@ namespace LuaInterface ...@@ -303,20 +306,20 @@ namespace LuaInterface
// 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");
KopiLua.Lua.lua_newtable(luaState); LuaCore.lua_newtable(luaState);
KopiLua.Lua.lua_pushstring(luaState, "__index"); LuaCore.lua_pushstring(luaState, "__index");
KopiLua.Lua.lua_pushvalue(luaState, -3); LuaCore.lua_pushvalue(luaState, -3);
KopiLua.Lua.lua_settable(luaState, -3); LuaCore.lua_settable(luaState, -3);
KopiLua.Lua.lua_pushstring(luaState, "__newindex"); LuaCore.lua_pushstring(luaState, "__newindex");
KopiLua.Lua.lua_pushvalue(luaState, -3); LuaCore.lua_pushvalue(luaState, -3);
KopiLua.Lua.lua_settable(luaState, -3); LuaCore.lua_settable(luaState, -3);
KopiLua.Lua.lua_setmetatable(luaState, 1); LuaCore.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
KopiLua.Lua.lua_pushstring(luaState, "base"); LuaCore.lua_pushstring(luaState, "base");
int index = addObject(obj); int index = addObject(obj);
pushNewObject(luaState, obj, index, "luaNet_searchbase"); pushNewObject(luaState, obj, index, "luaNet_searchbase");
KopiLua.Lua.lua_rawset(luaState, 1); LuaCore.lua_rawset(luaState, 1);
} }
else else
throwError(luaState, "register_table: can not find superclass '" + superclassName + "'"); throwError(luaState, "register_table: can not find superclass '" + superclassName + "'");
...@@ -331,24 +334,24 @@ namespace LuaInterface ...@@ -331,24 +334,24 @@ namespace LuaInterface
* 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
*/ */
private int unregisterTable(KopiLua.Lua.lua_State luaState) private int unregisterTable(LuaCore.lua_State luaState)
{ {
try try
{ {
if(KopiLua.Lua.lua_getmetatable(luaState,1)!=0) if(LuaCore.lua_getmetatable(luaState,1)!=0)
{ {
KopiLua.Lua.lua_pushstring(luaState,"__index"); LuaCore.lua_pushstring(luaState,"__index");
KopiLua.Lua.lua_gettable(luaState,-2); LuaCore.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"); if(obj==null) throwError(luaState,"unregister_table: arg is not valid table");
FieldInfo luaTableField=obj.GetType().GetField("__luaInterface_luaTable"); FieldInfo luaTableField=obj.GetType().GetField("__luaInterface_luaTable");
if(luaTableField==null) throwError(luaState,"unregister_table: arg is not valid table"); if(luaTableField==null) throwError(luaState,"unregister_table: arg is not valid table");
luaTableField.SetValue(obj,null); luaTableField.SetValue(obj,null);
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
KopiLua.Lua.lua_setmetatable(luaState,1); LuaCore.lua_setmetatable(luaState,1);
KopiLua.Lua.lua_pushstring(luaState,"base"); LuaCore.lua_pushstring(luaState,"base");
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
KopiLua.Lua.lua_settable(luaState,1); LuaCore.lua_settable(luaState,1);
} }
else throwError(luaState,"unregister_table: arg is not valid table"); else throwError(luaState,"unregister_table: arg is not valid table");
} }
...@@ -362,7 +365,7 @@ namespace LuaInterface ...@@ -362,7 +365,7 @@ namespace LuaInterface
* 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(KopiLua.Lua.lua_State luaState) private int getMethodSignature(LuaCore.lua_State luaState)
{ {
IReflect klass; object target; IReflect klass; object target;
int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class"); int udata=LuaLib.luanet_checkudata(luaState,1,"luaNet_class");
...@@ -377,26 +380,26 @@ namespace LuaInterface ...@@ -377,26 +380,26 @@ namespace LuaInterface
if(target==null) if(target==null)
{ {
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");
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
return 1; return 1;
} }
klass=target.GetType(); klass=target.GetType();
} }
string methodName=KopiLua.Lua.lua_tostring(luaState,2).ToString(); string methodName=LuaCore.lua_tostring(luaState,2).ToString();
Type[] signature=new Type[KopiLua.Lua.lua_gettop(luaState)-2]; Type[] signature=new Type[LuaCore.lua_gettop(luaState)-2];
for(int i=0;i<signature.Length;i++) for(int i=0;i<signature.Length;i++)
signature[i]=FindType(KopiLua.Lua.lua_tostring(luaState,i+3).ToString()); signature[i]=FindType(LuaCore.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 | MethodInfo 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 KopiLua.Lua.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);
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
} }
return 1; return 1;
} }
...@@ -404,7 +407,7 @@ namespace LuaInterface ...@@ -404,7 +407,7 @@ namespace LuaInterface
* 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(KopiLua.Lua.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");
...@@ -416,32 +419,32 @@ namespace LuaInterface ...@@ -416,32 +419,32 @@ namespace LuaInterface
{ {
throwError(luaState,"get_constructor_bysig: first arg is invalid type reference"); throwError(luaState,"get_constructor_bysig: first arg is invalid type reference");
} }
Type[] signature=new Type[KopiLua.Lua.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(KopiLua.Lua.lua_tostring(luaState,i+2).ToString()); signature[i]=FindType(LuaCore.lua_tostring(luaState,i+2).ToString());
try try
{ {
ConstructorInfo constructor=klass.UnderlyingSystemType.GetConstructor(signature); ConstructorInfo constructor=klass.UnderlyingSystemType.GetConstructor(signature);
pushFunction(luaState,new KopiLua.Lua.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);
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
} }
return 1; return 1;
} }
/* /*
* Pushes a type reference into the stack * Pushes a type reference into the stack
*/ */
internal void pushType(KopiLua.Lua.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(KopiLua.Lua.lua_State luaState, KopiLua.Lua.lua_CFunction func) internal void pushFunction(LuaCore.lua_State luaState, LuaCore.lua_CFunction func)
{ {
pushObject(luaState,func,"luaNet_function"); pushObject(luaState,func,"luaNet_function");
} }
...@@ -449,13 +452,13 @@ namespace LuaInterface ...@@ -449,13 +452,13 @@ namespace LuaInterface
* 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
*/ */
internal void pushObject(KopiLua.Lua.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==null)
{ {
KopiLua.Lua.lua_pushnil(luaState); LuaCore.lua_pushnil(luaState);
return; return;
} }
...@@ -463,25 +466,25 @@ namespace LuaInterface ...@@ -463,25 +466,25 @@ namespace LuaInterface
bool found = objectsBackMap.TryGetValue(o, out index); bool found = objectsBackMap.TryGetValue(o, out index);
if(found) if(found)
{ {
KopiLua.Lua.luaL_getmetatable(luaState,"luaNet_objects"); LuaCore.luaL_getmetatable(luaState,"luaNet_objects");
KopiLua.Lua.lua_rawgeti(luaState,-1,index); LuaCore.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 = KopiLua.Lua.lua_type(luaState, -1).ToLuaTypes(); LuaTypes type = LuaCore.lua_type(luaState, -1).ToLuaTypes();
if (type != LuaTypes.Nil) if (type != LuaTypes.Nil)
{ {
KopiLua.Lua.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack LuaCore.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);
KopiLua.Lua.lua_remove(luaState, -1); // remove the nil object value LuaCore.lua_remove(luaState, -1); // remove the nil object value
KopiLua.Lua.lua_remove(luaState, -1); // remove the metatable LuaCore.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
} }
...@@ -495,59 +498,59 @@ namespace LuaInterface ...@@ -495,59 +498,59 @@ namespace LuaInterface
* 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(KopiLua.Lua.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
KopiLua.Lua.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName); LuaCore.luaL_getmetatable(luaState,o.GetType().AssemblyQualifiedName);
if(KopiLua.Lua.lua_isnil(luaState,-1)) if(LuaCore.lua_isnil(luaState,-1))
{ {
KopiLua.Lua.lua_settop(luaState,-2); LuaCore.lua_settop(luaState,-2);
KopiLua.Lua.luaL_newmetatable(luaState,o.GetType().AssemblyQualifiedName); LuaCore.luaL_newmetatable(luaState,o.GetType().AssemblyQualifiedName);
KopiLua.Lua.lua_pushstring(luaState,"cache"); LuaCore.lua_pushstring(luaState,"cache");
KopiLua.Lua.lua_newtable(luaState); LuaCore.lua_newtable(luaState);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushlightuserdata(luaState,LuaLib.luanet_gettag()); LuaCore.lua_pushlightuserdata(luaState,LuaLib.luanet_gettag());
KopiLua.Lua.lua_pushnumber(luaState,1); LuaCore.lua_pushnumber(luaState,1);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__index"); LuaCore.lua_pushstring(luaState,"__index");
KopiLua.Lua.lua_pushstring(luaState,"luaNet_indexfunction"); LuaCore.lua_pushstring(luaState,"luaNet_indexfunction");
KopiLua.Lua.lua_rawget(luaState, (int) PseudoIndex.Registry); LuaCore.lua_rawget(luaState, (int) PseudoIndex.Registry);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__gc"); LuaCore.lua_pushstring(luaState,"__gc");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.gcFunction);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__tostring"); LuaCore.lua_pushstring(luaState,"__tostring");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.toStringFunction);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
KopiLua.Lua.lua_pushstring(luaState,"__newindex"); LuaCore.lua_pushstring(luaState,"__newindex");
LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction); LuaLib.lua_pushstdcallcfunction(luaState,metaFunctions.newindexFunction);
KopiLua.Lua.lua_rawset(luaState,-3); LuaCore.lua_rawset(luaState,-3);
} }
} }
else else
{ {
KopiLua.Lua.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
KopiLua.Lua.luaL_getmetatable(luaState,"luaNet_objects"); LuaCore.luaL_getmetatable(luaState,"luaNet_objects");
LuaLib.luanet_newudata(luaState,index); LuaLib.luanet_newudata(luaState,index);
KopiLua.Lua.lua_pushvalue(luaState,-3); LuaCore.lua_pushvalue(luaState,-3);
KopiLua.Lua.lua_remove(luaState,-4); LuaCore.lua_remove(luaState,-4);
KopiLua.Lua.lua_setmetatable(luaState,-2); LuaCore.lua_setmetatable(luaState,-2);
KopiLua.Lua.lua_pushvalue(luaState,-1); LuaCore.lua_pushvalue(luaState,-1);
KopiLua.Lua.lua_rawseti(luaState,-3,index); LuaCore.lua_rawseti(luaState,-3,index);
KopiLua.Lua.lua_remove(luaState,-2); LuaCore.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(KopiLua.Lua.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); ExtractValue extractor=typeChecker.checkType(luaState,stackPos,paramType);
if(extractor!=null) return extractor(luaState,stackPos); if(extractor!=null) return extractor(luaState,stackPos);
...@@ -611,22 +614,22 @@ namespace LuaInterface ...@@ -611,22 +614,22 @@ namespace LuaInterface
/* /*
* 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(KopiLua.Lua.lua_State luaState,int index) internal object getObject(LuaCore.lua_State luaState,int index)
{ {
LuaTypes type=KopiLua.Lua.lua_type(luaState,index).ToLuaTypes(); LuaTypes type=LuaCore.lua_type(luaState,index).ToLuaTypes();
switch(type) switch(type)
{ {
case LuaTypes.Number: case LuaTypes.Number:
{ {
return KopiLua.Lua.lua_tonumber(luaState,index); return LuaCore.lua_tonumber(luaState,index);
} }
case LuaTypes.String: case LuaTypes.String:
{ {
return KopiLua.Lua.lua_tostring(luaState,index); return LuaCore.lua_tostring(luaState,index);
} }
case LuaTypes.Boolean: case LuaTypes.Boolean:
{ {
return KopiLua.Lua.lua_toboolean(luaState,index); return LuaCore.lua_toboolean(luaState,index);
} }
case LuaTypes.Table: case LuaTypes.Table:
{ {
...@@ -652,32 +655,32 @@ namespace LuaInterface ...@@ -652,32 +655,32 @@ namespace LuaInterface
/* /*
* 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(KopiLua.Lua.lua_State luaState,int index) internal LuaTable getTable(LuaCore.lua_State luaState,int index)
{ {
KopiLua.Lua.lua_pushvalue(luaState,index); LuaCore.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(KopiLua.Lua.lua_State luaState,int index) internal LuaUserData getUserData(LuaCore.lua_State luaState,int index)
{ {
KopiLua.Lua.lua_pushvalue(luaState,index); LuaCore.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(KopiLua.Lua.lua_State luaState,int index) internal LuaFunction getFunction(LuaCore.lua_State luaState,int index)
{ {
KopiLua.Lua.lua_pushvalue(luaState,index); LuaCore.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(KopiLua.Lua.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) if(idx!=-1)
...@@ -689,7 +692,7 @@ namespace LuaInterface ...@@ -689,7 +692,7 @@ namespace LuaInterface
* 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(KopiLua.Lua.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) if(udata!=-1)
...@@ -702,9 +705,9 @@ namespace LuaInterface ...@@ -702,9 +705,9 @@ namespace LuaInterface
* 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(KopiLua.Lua.lua_State luaState, object[] returnValues) internal int returnValues(LuaCore.lua_State luaState, object[] returnValues)
{ {
if(KopiLua.Lua.lua_checkstack(luaState,returnValues.Length+5).ToBoolean()) if(LuaCore.lua_checkstack(luaState,returnValues.Length+5).ToBoolean())
{ {
for(int i=0;i<returnValues.Length;i++) for(int i=0;i<returnValues.Length;i++)
{ {
...@@ -718,9 +721,9 @@ namespace LuaInterface ...@@ -718,9 +721,9 @@ namespace LuaInterface
* 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(KopiLua.Lua.lua_State luaState,int oldTop) internal object[] popValues(LuaCore.lua_State luaState,int oldTop)
{ {
int newTop=KopiLua.Lua.lua_gettop(luaState); int newTop=LuaCore.lua_gettop(luaState);
if(oldTop==newTop) if(oldTop==newTop)
{ {
return null; return null;
...@@ -732,7 +735,7 @@ namespace LuaInterface ...@@ -732,7 +735,7 @@ namespace LuaInterface
{ {
returnValues.Add(getObject(luaState,i)); returnValues.Add(getObject(luaState,i));
} }
KopiLua.Lua.lua_settop(luaState,oldTop); LuaCore.lua_settop(luaState,oldTop);
return returnValues.ToArray(); return returnValues.ToArray();
} }
} }
...@@ -741,9 +744,9 @@ namespace LuaInterface ...@@ -741,9 +744,9 @@ namespace LuaInterface
* 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(KopiLua.Lua.lua_State luaState,int oldTop,Type[] popTypes) internal object[] popValues(LuaCore.lua_State luaState,int oldTop,Type[] popTypes)
{ {
int newTop=KopiLua.Lua.lua_gettop(luaState); int newTop=LuaCore.lua_gettop(luaState);
if(oldTop==newTop) if(oldTop==newTop)
{ {
return null; return null;
...@@ -761,7 +764,7 @@ namespace LuaInterface ...@@ -761,7 +764,7 @@ namespace LuaInterface
returnValues.Add(getAsType(luaState,i,popTypes[iTypes])); returnValues.Add(getAsType(luaState,i,popTypes[iTypes]));
iTypes++; iTypes++;
} }
KopiLua.Lua.lua_settop(luaState,oldTop); LuaCore.lua_settop(luaState,oldTop);
return returnValues.ToArray(); return returnValues.ToArray();
} }
} }
...@@ -784,33 +787,33 @@ namespace LuaInterface ...@@ -784,33 +787,33 @@ namespace LuaInterface
/* /*
* Pushes the object into the Lua stack according to its type. * Pushes the object into the Lua stack according to its type.
*/ */
internal void push(KopiLua.Lua.lua_State luaState, object o) internal void push(LuaCore.lua_State luaState, object o)
{ {
if(o==null) if(o==null)
{ {
KopiLua.Lua.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);
KopiLua.Lua.lua_pushnumber(luaState,d); LuaCore.lua_pushnumber(luaState,d);
} }
else if(o is char) else if(o is char)
{ {
double d = (char)o; double d = (char)o;
KopiLua.Lua.lua_pushnumber(luaState,d); LuaCore.lua_pushnumber(luaState,d);
} }
else if(o is string) else if(o is string)
{ {
string str=(string)o; string str=(string)o;
KopiLua.Lua.lua_pushstring(luaState,str); LuaCore.lua_pushstring(luaState,str);
} }
else if(o is bool) else if(o is bool)
{ {
bool b=(bool)o; bool b=(bool)o;
KopiLua.Lua.lua_pushboolean(luaState, (b == true ? 1 : 0)); LuaCore.lua_pushboolean(luaState, (b == true ? 1 : 0));
} }
else if(IsILua(o)) else if(IsILua(o))
{ {
...@@ -820,9 +823,9 @@ namespace LuaInterface ...@@ -820,9 +823,9 @@ namespace LuaInterface
{ {
((LuaTable)o).push(luaState); ((LuaTable)o).push(luaState);
} }
else if(o is KopiLua.Lua.lua_CFunction) else if(o is LuaCore.lua_CFunction)
{ {
pushFunction(luaState,(KopiLua.Lua.lua_CFunction)o); pushFunction(luaState,(LuaCore.lua_CFunction)o);
} }
else if(o is LuaFunction) else if(o is LuaFunction)
{ {
...@@ -837,7 +840,7 @@ namespace LuaInterface ...@@ -837,7 +840,7 @@ namespace LuaInterface
* 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(KopiLua.Lua.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);
} }
......
...@@ -29,35 +29,32 @@ using System.Reflection; ...@@ -29,35 +29,32 @@ using System.Reflection;
namespace LuaInterface namespace LuaInterface
{ {
using LuaCore = KopiLua.Lua;
/// <summary> /// <summary>
/// Summary description for ProxyType. /// Summary description for ProxyType.
/// </summary> /// </summary>
public class ProxyType : IReflect public class ProxyType : IReflect
{ {
private Type proxy;
Type proxy;
public ProxyType(Type proxy) public ProxyType(Type proxy)
{ {
this.proxy = proxy; this.proxy = proxy;
} }
/// <summary> /// <summary>
/// Provide human readable short hand for this proxy object /// Provide human readable short hand for this proxy object
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public override string ToString() public override string ToString()
{ {
return "ProxyType(" + UnderlyingSystemType + ")"; return "ProxyType(" + UnderlyingSystemType + ")";
} }
public Type UnderlyingSystemType public Type UnderlyingSystemType
{ {
get get { return proxy; }
{
return proxy;
}
} }
public FieldInfo GetField(string name, BindingFlags bindingAttr) public FieldInfo GetField(string name, BindingFlags bindingAttr)
...@@ -114,6 +111,5 @@ namespace LuaInterface ...@@ -114,6 +111,5 @@ namespace LuaInterface
{ {
return proxy.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters); return proxy.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
} }
} }
} }
\ No newline at end of file
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