Unverified Commit 5e9a190a authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

WIP cleanup (#275)

* WIP cleanup

* Trying fixing iPhoneSimTests.

* More cleanups

* Minified initLua loadCLRpackage
parent 66947423
......@@ -22,31 +22,34 @@ namespace NLua.Method
*/
class LuaMethodWrapper
{
internal LuaNativeFunction invokeFunction;
ObjectTranslator _Translator;
MethodBase _Method;
MethodCache _LastCalledMethod = new MethodCache();
string _MethodName;
MemberInfo[] _Members;
ExtractValue _ExtractTarget;
object _Target;
bool _IsStatic;
internal LuaNativeFunction InvokeFunction;
readonly ObjectTranslator _translator;
readonly MethodBase _method;
readonly ExtractValue _extractTarget;
readonly object _target;
readonly bool _isStatic;
readonly string _methodName;
readonly MethodInfo[] _members;
MethodCache _lastCalledMethod;
/*
* Constructs the wrapper for a known MethodBase instance
*/
public LuaMethodWrapper(ObjectTranslator translator, object target, ProxyType targetType, MethodBase method)
{
invokeFunction = Call;
_Translator = translator;
_Target = target;
InvokeFunction = Call;
_translator = translator;
_target = target;
_extractTarget = translator.typeChecker.GetExtractor(targetType);
if (targetType != null)
_ExtractTarget = translator.typeChecker.GetExtractor(targetType);
_Method = method;
_MethodName = method.Name;
_IsStatic = method.IsStatic;
_method = method;
_methodName = method.Name;
_isStatic = method.IsStatic;
}
/*
......@@ -54,16 +57,16 @@ namespace NLua.Method
*/
public LuaMethodWrapper(ObjectTranslator translator, ProxyType targetType, string methodName, BindingFlags bindingType)
{
invokeFunction = Call;
_Translator = translator;
_MethodName = methodName;
InvokeFunction = Call;
if (targetType != null)
_ExtractTarget = translator.typeChecker.GetExtractor(targetType);
_translator = translator;
_methodName = methodName;
_extractTarget = translator.typeChecker.GetExtractor(targetType);
_IsStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static;
_Members = GetMethodsRecursively(targetType.UnderlyingSystemType, methodName, bindingType | BindingFlags.Public);
_isStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static;
_members = GetMethodsRecursively(targetType.UnderlyingSystemType,
methodName,
bindingType | BindingFlags.Public);
}
MethodInfo[] GetMethodsRecursively(Type type, string methodName, BindingFlags bindingType)
......@@ -84,7 +87,7 @@ namespace NLua.Method
/// <param name="e">null for no pending exception</param>
int SetPendingException(Exception e)
{
return _Translator.interpreter.SetPendingException(e);
return _translator.interpreter.SetPendingException(e);
}
/*
......@@ -94,15 +97,17 @@ namespace NLua.Method
int Call(IntPtr state)
{
var luaState = LuaState.FromIntPtr(state);
var methodToCall = _Method;
object targetObject = _Target;
MethodBase methodToCall = _method;
object targetObject = _target;
bool failedCall = true;
int nReturnValues = 0;
if (!luaState.CheckStack(5))
throw new LuaException("Lua stack overflow");
bool isStatic = _IsStatic;
bool isStatic = _isStatic;
SetPendingException(null);
if (methodToCall == null)
......@@ -110,57 +115,54 @@ namespace NLua.Method
if (isStatic)
targetObject = null;
else
targetObject = _ExtractTarget(luaState, 1);
targetObject = _extractTarget(luaState, 1);
if (_LastCalledMethod.cachedMethod != null)
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 = luaState.GetTop() - numStackToSkip;
MethodBase method = _LastCalledMethod.cachedMethod;
MethodBase method = _lastCalledMethod.cachedMethod;
if (numArgsPassed == _LastCalledMethod.argTypes.Length)
if (numArgsPassed == _lastCalledMethod.argTypes.Length)
{ // No. of args match?
if (!luaState.CheckStack(_LastCalledMethod.outList.Length + 6))
if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow");
object[] args = _LastCalledMethod.args;
object[] args = _lastCalledMethod.args;
try
{
for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
for (int i = 0; i < _lastCalledMethod.argTypes.Length; i++)
{
MethodArgs type = _LastCalledMethod.argTypes[i];
MethodArgs type = _lastCalledMethod.argTypes[i];
int index = i + 1 + numStackToSkip;
Func<int, object> valueExtractor = currentParam => {
return type.extractValue(luaState, currentParam);
};
if (_LastCalledMethod.argTypes[i].isParamsArray)
if (_lastCalledMethod.argTypes[i].IsParamsArray)
{
int count = _LastCalledMethod.argTypes.Length - i;
Array paramArray = _Translator.TableToArray(valueExtractor, type.paramsArrayType, index, count);
args[_LastCalledMethod.argTypes[i].index] = paramArray;
int count = _lastCalledMethod.argTypes.Length - i;
Array paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParamsArrayType, index, count);
args[_lastCalledMethod.argTypes[i].Index] = paramArray;
}
else
{
args[type.index] = valueExtractor(index);
args[type.Index] = type.ExtractValue(luaState, index);
}
if (_LastCalledMethod.args[_LastCalledMethod.argTypes[i].index] == null &&
if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
!luaState.IsNil(i + 1 + numStackToSkip))
throw new LuaException(string.Format("Argument number {0} is invalid", (i + 1)));
}
if (_IsStatic)
_Translator.Push(luaState, method.Invoke(null, _LastCalledMethod.args));
if (_isStatic)
_translator.Push(luaState, method.Invoke(null, _lastCalledMethod.args));
else
{
if (method.IsConstructor)
_Translator.Push(luaState, ((ConstructorInfo)method).Invoke(_LastCalledMethod.args));
_translator.Push(luaState, ((ConstructorInfo)method).Invoke(_lastCalledMethod.args));
else
_Translator.Push(luaState, method.Invoke(targetObject, _LastCalledMethod.args));
_translator.Push(luaState, method.Invoke(targetObject, _lastCalledMethod.args));
}
failedCall = false;
......@@ -168,12 +170,12 @@ namespace NLua.Method
catch (TargetInvocationException e)
{
// Failure of method invocation
if (_Translator.interpreter.UseTraceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback();
if (_translator.interpreter.UseTraceback) e.GetBaseException().Data["Traceback"] = _translator.interpreter.GetDebugTraceback();
return SetPendingException(e.GetBaseException());
}
catch (Exception e)
{
if (_Members.Length == 1) // Is the method overloaded?
if (_members.Length == 1) // Is the method overloaded?
// No, throw error
return SetPendingException(e);
}
......@@ -189,7 +191,7 @@ namespace NLua.Method
{
if (targetObject == null)
{
_Translator.ThrowError(luaState, string.Format("instance method '{0}' requires a non null target object", _MethodName));
_translator.ThrowError(luaState, string.Format("instance method '{0}' requires a non null target object", _methodName));
luaState.PushNil();
return 1;
}
......@@ -200,11 +202,11 @@ namespace NLua.Method
bool hasMatch = false;
string candidateName = null;
foreach (var member in _Members)
foreach (var member in _members)
{
candidateName = member.ReflectedType.Name + "." + member.Name;
var m = (MethodInfo)member;
bool isMethod = _Translator.MatchParameters(luaState, m, ref _LastCalledMethod);
bool isMethod = _translator.MatchParameters(luaState, m, ref _lastCalledMethod);
if (isMethod)
{
......@@ -216,7 +218,7 @@ namespace NLua.Method
if (!hasMatch)
{
string msg = (candidateName == null) ? "Invalid arguments to method call" : ("Invalid arguments to method: " + candidateName);
_Translator.ThrowError(luaState, msg);
_translator.ThrowError(luaState, msg);
luaState.PushNil();
return 1;
}
......@@ -226,23 +228,23 @@ namespace NLua.Method
{ // Method from MethodBase instance
if (methodToCall.ContainsGenericParameters)
{
_Translator.MatchParameters(luaState, methodToCall, ref _LastCalledMethod);
_translator.MatchParameters(luaState, methodToCall, ref _lastCalledMethod);
if (methodToCall.IsGenericMethodDefinition)
{
//need to make a concrete type of the generic method definition
var typeArgs = new List<Type>();
foreach (object arg in _LastCalledMethod.args)
foreach (object arg in _lastCalledMethod.args)
typeArgs.Add(arg.GetType());
var concreteMethod = ((MethodInfo)methodToCall).MakeGenericMethod(typeArgs.ToArray());
_Translator.Push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args));
_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");
_translator.ThrowError(luaState, "Unable to invoke method on generic class as the current method is an open generic method");
luaState.PushNil();
return 1;
}
......@@ -251,13 +253,13 @@ namespace NLua.Method
{
if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
{
targetObject = _ExtractTarget(luaState, 1);
targetObject = _extractTarget(luaState, 1);
luaState.Remove(1); // Pops the receiver
}
if (!_Translator.MatchParameters(luaState, methodToCall, ref _LastCalledMethod))
if (!_translator.MatchParameters(luaState, methodToCall, ref _lastCalledMethod))
{
_Translator.ThrowError(luaState, "Invalid arguments to method call");
_translator.ThrowError(luaState, "Invalid arguments to method call");
luaState.PushNil();
return 1;
}
......@@ -266,24 +268,24 @@ namespace NLua.Method
if (failedCall)
{
if (!luaState.CheckStack(_LastCalledMethod.outList.Length + 6))
if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
throw new LuaException("Lua stack overflow");
try
{
if (isStatic)
_Translator.Push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
_translator.Push(luaState, _lastCalledMethod.cachedMethod.Invoke(null, _lastCalledMethod.args));
else
{
if (_LastCalledMethod.cachedMethod.IsConstructor)
_Translator.Push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
if (_lastCalledMethod.cachedMethod.IsConstructor)
_translator.Push(luaState, ((ConstructorInfo)_lastCalledMethod.cachedMethod).Invoke(_lastCalledMethod.args));
else
_Translator.Push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
_translator.Push(luaState, _lastCalledMethod.cachedMethod.Invoke(targetObject, _lastCalledMethod.args));
}
}
catch (TargetInvocationException e)
{
if (_Translator.interpreter.UseTraceback) e.GetBaseException().Data["Traceback"] = _Translator.interpreter.GetDebugTraceback();
if (_translator.interpreter.UseTraceback) e.GetBaseException().Data["Traceback"] = _translator.interpreter.GetDebugTraceback();
return SetPendingException(e.GetBaseException());
}
catch (Exception e)
......@@ -293,17 +295,17 @@ namespace NLua.Method
}
// Pushes out and ref return values
for (int index = 0; index < _LastCalledMethod.outList.Length; index++)
for (int index = 0; index < _lastCalledMethod.outList.Length; index++)
{
nReturnValues++;
_Translator.Push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]);
_translator.Push(luaState, _lastCalledMethod.args[_lastCalledMethod.outList[index]]);
}
// 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)
if (!_lastCalledMethod.IsReturnVoid && nReturnValues > 0)
nReturnValues++;
return nReturnValues < 1 ? 1 : nReturnValues;
......
......@@ -8,10 +8,10 @@ namespace NLua.Method
struct MethodArgs
{
// Position of parameter
public int index;
public int Index;
// Type-conversion function
public ExtractValue extractValue;
public bool isParamsArray;
public Type paramsArrayType;
public ExtractValue ExtractValue;
public bool IsParamsArray;
public Type ParamsArrayType;
}
}
\ No newline at end of file
......@@ -5,15 +5,15 @@ namespace NLua.Method
{
class RegisterEventHandler
{
private EventHandlerContainer pendingEvents;
private EventInfo eventInfo;
private object target;
private readonly EventHandlerContainer _pendingEvents;
private readonly EventInfo _eventInfo;
private readonly object _target;
public RegisterEventHandler(EventHandlerContainer pendingEvents, object target, EventInfo eventInfo)
{
this.target = target;
this.eventInfo = eventInfo;
this.pendingEvents = pendingEvents;
_target = target;
_eventInfo = eventInfo;
_pendingEvents = pendingEvents;
}
/*
......@@ -23,9 +23,9 @@ namespace NLua.Method
{
//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);
Delegate handlerDelegate = CodeGeneration.Instance.GetDelegate(_eventInfo.EventHandlerType, function);
_eventInfo.AddEventHandler(_target, handlerDelegate);
_pendingEvents.Add(handlerDelegate, this);
return handlerDelegate;
}
......@@ -36,7 +36,7 @@ namespace NLua.Method
public void Remove(Delegate handlerDelegate)
{
RemovePending(handlerDelegate);
pendingEvents.Remove(handlerDelegate);
_pendingEvents.Remove(handlerDelegate);
}
/*
......@@ -44,7 +44,7 @@ namespace NLua.Method
*/
internal void RemovePending(Delegate handlerDelegate)
{
eventInfo.RemoveEventHandler(target, handlerDelegate);
_eventInfo.RemoveEventHandler(_target, handlerDelegate);
}
}
}
\ No newline at end of file
......@@ -14,8 +14,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Event\HookExceptionEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\LuaException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\LuaScriptException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\GeneralExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\StringExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\LuaExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\TypeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GenerateEventAssembly\ClassGenerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GenerateEventAssembly\CodeGeneration.cs" />
<Compile Include="$(MSBuildThisFileDirectory)GenerateEventAssembly\DelegateGenerator.cs" />
......
......@@ -36,20 +36,20 @@ namespace NLua
}
}
readonly LuaNativeFunction registerTableFunction;
readonly LuaNativeFunction unregisterTableFunction;
readonly LuaNativeFunction getMethodSigFunction;
readonly LuaNativeFunction getConstructorSigFunction;
readonly LuaNativeFunction importTypeFunction;
readonly LuaNativeFunction loadAssemblyFunction;
readonly LuaNativeFunction ctypeFunction;
readonly LuaNativeFunction enumFromIntFunction;
readonly LuaNativeFunction _registerTableFunction;
readonly LuaNativeFunction _unregisterTableFunction;
readonly LuaNativeFunction _getMethodSigFunction;
readonly LuaNativeFunction _getConstructorSigFunction;
readonly LuaNativeFunction _importTypeFunction;
readonly LuaNativeFunction _loadAssemblyFunction;
readonly LuaNativeFunction _ctypeFunction;
readonly LuaNativeFunction _enumFromIntFunction;
// object to object #
readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int>(new ReferenceComparer());
readonly Dictionary<object, int> _objectsBackMap = new Dictionary<object, int>(new ReferenceComparer());
// object # to object (FIXME - it should be possible to get object address as an object #)
readonly Dictionary<int, object> objects = new Dictionary<int, object>();
internal EventHandlerContainer pendingEvents = new EventHandlerContainer();
readonly Dictionary<int, object> _objects = new Dictionary<int, object>();
internal EventHandlerContainer PendingEvents = new EventHandlerContainer();
MetaFunctions metaFunctions;
List<Assembly> assemblies;
internal CheckType typeChecker;
......@@ -73,14 +73,14 @@ namespace NLua
metaFunctions = new MetaFunctions(this);
assemblies = new List<Assembly>();
importTypeFunction = ImportType;
loadAssemblyFunction = LoadAssembly;
registerTableFunction = RegisterTable;
unregisterTableFunction = UnregisterTable;
getMethodSigFunction = GetMethodSignature;
getConstructorSigFunction = GetConstructorSignature;
ctypeFunction = CType;
enumFromIntFunction = EnumFromInt;
_importTypeFunction = ImportType;
_loadAssemblyFunction = LoadAssembly;
_registerTableFunction = RegisterTable;
_unregisterTableFunction = UnregisterTable;
_getMethodSigFunction = GetMethodSignature;
_getConstructorSigFunction = GetConstructorSignature;
_ctypeFunction = CType;
_enumFromIntFunction = EnumFromInt;
CreateLuaObjectList(luaState);
CreateIndexingMetaFunction(luaState);
......@@ -114,7 +114,7 @@ namespace NLua
{
luaState.PushString("luaNet_indexfunction");
luaState.DoString(MetaFunctions.LuaIndexFunction);
luaState.RawSet((int)LuaRegistry.Index);
luaState.RawSet(LuaRegistry.Index);
}
/*
......@@ -170,21 +170,21 @@ namespace NLua
{
luaState.PushCFunction(metaFunctions.IndexFunction);
luaState.SetGlobal("get_object_member");
luaState.PushCFunction(importTypeFunction);
luaState.PushCFunction(_importTypeFunction);
luaState.SetGlobal("import_type");
luaState.PushCFunction(loadAssemblyFunction);
luaState.PushCFunction(_loadAssemblyFunction);
luaState.SetGlobal("load_assembly");
luaState.PushCFunction(registerTableFunction);
luaState.PushCFunction(_registerTableFunction);
luaState.SetGlobal("make_object");
luaState.PushCFunction(unregisterTableFunction);
luaState.PushCFunction(_unregisterTableFunction);
luaState.SetGlobal("free_object");
luaState.PushCFunction(getMethodSigFunction);
luaState.PushCFunction(_getMethodSigFunction);
luaState.SetGlobal("get_method_bysig");
luaState.PushCFunction(getConstructorSigFunction);
luaState.PushCFunction(_getConstructorSigFunction);
luaState.SetGlobal("get_constructor_bysig");
luaState.PushCFunction(ctypeFunction);
luaState.PushCFunction(_ctypeFunction);
luaState.SetGlobal("ctype");
luaState.PushCFunction(enumFromIntFunction);
luaState.PushCFunction(_enumFromIntFunction);
luaState.SetGlobal("enum");
}
......@@ -227,7 +227,8 @@ namespace NLua
if (message != null)
{
// Wrap Lua error (just a string) and store the error location
if (interpreter.UseTraceback) message += Environment.NewLine + interpreter.GetDebugTraceback();
if (interpreter.UseTraceback)
message += Environment.NewLine + interpreter.GetDebugTraceback();
e = new LuaScriptException(message, errLocation);
}
else
......@@ -384,17 +385,29 @@ namespace NLua
private int RegisterTableInternal(LuaState luaState)
{
if (luaState.Type(1) == LuaType.Table)
if (luaState.Type(1) != LuaType.Table)
{
var luaTable = GetTable(luaState, 1);
string superclassName = luaState.ToString(2).ToString();
ThrowError(luaState, "register_table: first arg is not a table");
return 0;
}
LuaTable luaTable = GetTable(luaState, 1);
string superclassName = luaState.ToString(2);
if (superclassName != null)
if (string.IsNullOrEmpty(superclassName))
{
ThrowError(luaState, "register_table: superclass name can not be null");
return 0;
}
var klass = FindType(superclassName);
if (klass != null)
if (klass == null)
{
ThrowError(luaState, "register_table: can not find superclass '" + superclassName + "'");
return 0;
}
// Creates and pushes the object in the stack, setting
// it as the metatable of the first argument
object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable);
......@@ -414,15 +427,6 @@ namespace NLua
int index = AddObject(obj);
PushNewObject(luaState, obj, index, "luaNet_searchbase");
luaState.RawSet(1);
}
else
ThrowError(luaState, "register_table: can not find superclass '" + superclassName + "'");
}
else
ThrowError(luaState, "register_table: superclass name can not be null");
}
else
ThrowError(luaState, "register_table: first arg is not a table");
return 0;
}
......@@ -443,10 +447,13 @@ namespace NLua
private int UnregisterTableInternal(LuaState luaState)
{
try
{
if (luaState.GetMetaTable(1))
if (!luaState.GetMetaTable(1))
{
ThrowError(luaState, "unregister_table: arg is not valid table");
return 0;
}
luaState.PushString("__index");
luaState.GetTable(-2);
object obj = GetRawNetObject(luaState, -1);
......@@ -459,20 +466,13 @@ namespace NLua
if (luaTableField == null)
ThrowError(luaState, "unregister_table: arg is not valid table");
// ReSharper disable once PossibleNullReferenceException
luaTableField.SetValue(obj, null);
luaState.PushNil();
luaState.SetMetaTable(1);
luaState.PushString("base");
luaState.PushNil();
luaState.SetTable(1);
}
else
ThrowError(luaState, "unregister_table: arg is not valid table");
}
catch (Exception e)
{
ThrowError(luaState, e.Message);
}
return 0;
}
......@@ -499,7 +499,7 @@ namespace NLua
if (udata != -1)
{
klass = (ProxyType)objects[udata];
klass = (ProxyType)_objects[udata];
target = null;
}
else
......@@ -527,7 +527,7 @@ namespace NLua
var method = klass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static |
BindingFlags.Instance, signature);
var wrapper = new LuaMethodWrapper(this, target, klass, method);
LuaNativeFunction invokeDelegate = wrapper.invokeFunction;
LuaNativeFunction invokeDelegate = wrapper.InvokeFunction;
PushFunction(luaState, invokeDelegate);
}
catch (Exception e)
......@@ -559,7 +559,7 @@ namespace NLua
int udata = luaState.CheckUObject(1, "luaNet_class");
if (udata != -1)
klass = (ProxyType)objects[udata];
klass = (ProxyType)_objects[udata];
if (klass == null)
ThrowError(luaState, "get_constructor_bysig: first arg is invalid type reference");
......@@ -573,7 +573,7 @@ namespace NLua
{
ConstructorInfo constructor = klass.UnderlyingSystemType.GetConstructor(signature);
var wrapper = new LuaMethodWrapper(this, null, klass, constructor);
var invokeDelegate = wrapper.invokeFunction;
var invokeDelegate = wrapper.InvokeFunction;
PushFunction(luaState, invokeDelegate);
}
catch (Exception e)
......@@ -617,7 +617,7 @@ namespace NLua
}
// Object already in the list of Lua objects? Push the stored reference.
bool found = (!o.GetType().IsValueType || o.GetType().IsEnum) && objectsBackMap.TryGetValue(o, out index);
bool found = (!o.GetType().IsValueType || o.GetType().IsEnum) && _objectsBackMap.TryGetValue(o, out index);
if (found)
{
......@@ -785,7 +785,7 @@ namespace NLua
internal void CollectObject(int udata)
{
object o;
bool found = objects.TryGetValue(udata, out o);
bool found = _objects.TryGetValue(udata, out o);
// The other variant of collectObject might have gotten here first, in that case we will silently ignore the missing entry
if (found)
......@@ -795,26 +795,23 @@ namespace NLua
/// <summary>
/// Given an object reference, remove it from our maps
/// </summary>
/// <param name = "o"></param>
/// <param name = "udata"></param>
private void CollectObject(object o, int udata)
{
objects.Remove(udata);
#if NETFX_CORE
if (!o.GetType ().GetTypeInfo ().IsValueType || o.GetType().GetTypeInfo().IsEnum)
#else
_objects.Remove(udata);
if (!o.GetType().IsValueType || o.GetType().IsEnum)
#endif
objectsBackMap.Remove(o);
_objectsBackMap.Remove(o);
}
private int AddObject(object obj)
{
// New object: inserts it in the list
int index = nextObj++;
objects[index] = obj;
_objects[index] = obj;
if (!obj.GetType().IsValueType || obj.GetType().IsEnum)
objectsBackMap[obj] = index;
_objectsBackMap[obj] = index;
return index;
}
......@@ -829,29 +826,19 @@ namespace NLua
switch (type)
{
case LuaType.Number:
{
return luaState.ToNumber(index);
}
case LuaType.String:
{
return luaState.ToString(index);
}
case LuaType.Boolean:
{
return luaState.ToBoolean(index);
}
case LuaType.Table:
{
return GetTable(luaState, index);
}
case LuaType.Function:
{
return GetFunction(luaState, index);
}
case LuaType.UserData:
{
int udata = luaState.ToNetObject(index, Tag);
return udata != -1 ? objects[udata] : GetUserData(luaState, index);
return udata != -1 ? _objects[udata] : GetUserData(luaState, index);
}
default:
return null;
......@@ -888,7 +875,6 @@ namespace NLua
internal LuaFunction GetFunction(LuaState luaState, int index)
{
luaState.PushCopy(index);
var x = luaState.Type(1);
int reference = luaState.Ref(LuaRegistry.Index);
if (reference == -1)
return null;
......@@ -902,7 +888,7 @@ namespace NLua
internal object GetNetObject(LuaState luaState, int index)
{
int idx = luaState.ToNetObject(index, Tag);
return idx != -1 ? objects[idx] : null;
return idx != -1 ? _objects[idx] : null;
}
/*
......@@ -912,7 +898,7 @@ namespace NLua
internal object GetRawNetObject(LuaState luaState, int index)
{
int udata = luaState.RawNetObj(index);
return udata != -1 ? objects[udata] : null;
return udata != -1 ? _objects[udata] : null;
}
......@@ -926,8 +912,7 @@ namespace NLua
if (oldTop == newTop)
return null;
else
{
var returnValues = new List<object>();
for (int i = oldTop + 1; i <= newTop; i++)
returnValues.Add(GetObject(luaState, i));
......@@ -935,7 +920,6 @@ namespace NLua
luaState.SetTop(oldTop);
return returnValues.ToArray();
}
}
/*
* Gets the values from the provided index to
......@@ -1030,9 +1014,9 @@ namespace NLua
return metaFunctions.MatchParameters(luaState, method, ref methodCache);
}
internal Array TableToArray(Func<int, object> luaParamValue, Type paramArrayType, int startIndex, int count)
internal Array TableToArray(LuaState luaState, ExtractValue extractValue, Type paramArrayType, int startIndex, int count)
{
return metaFunctions.TableToArray(luaParamValue, paramArrayType, startIndex, count);
return metaFunctions.TableToArray(luaState, extractValue, paramArrayType, ref startIndex, count);
}
private Type TypeOf(LuaState luaState, int idx)
......@@ -1041,7 +1025,7 @@ namespace NLua
if (udata == -1)
return null;
var pt = (ProxyType)objects[udata];
var pt = (ProxyType)_objects[udata];
return pt.UnderlyingSystemType;
}
......
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using LuaState = KeraLua.Lua;
namespace NLua
{
internal class ObjectTranslatorPool
{
private static volatile ObjectTranslatorPool instance = new ObjectTranslatorPool();
private static volatile ObjectTranslatorPool _instance = new ObjectTranslatorPool();
private ConcurrentDictionary<LuaState, ObjectTranslator> translators = new ConcurrentDictionary<LuaState, ObjectTranslator>();
public static ObjectTranslatorPool Instance {
get
{
return instance;
}
}
public static ObjectTranslatorPool Instance => _instance;
public ObjectTranslatorPool()
{
}
public void Add(LuaState luaState, ObjectTranslator translator)
{
......
......@@ -8,11 +8,11 @@ namespace NLua
/// </summary>
public class ProxyType
{
private Type proxy;
private readonly Type _proxy;
public ProxyType(Type proxy)
{
this.proxy = proxy;
_proxy = proxy;
}
/// <summary>
......@@ -24,36 +24,30 @@ namespace NLua
return "ProxyType(" + UnderlyingSystemType + ")";
}
public Type UnderlyingSystemType {
get { return proxy; }
}
public Type UnderlyingSystemType => _proxy;
public override bool Equals(object obj)
{
if (obj is Type)
return proxy.Equals((Type)obj);
return _proxy == (Type)obj;
if (obj is ProxyType)
return proxy.Equals(((ProxyType)obj).UnderlyingSystemType);
return proxy.Equals(obj);
return _proxy == ((ProxyType)obj).UnderlyingSystemType;
return _proxy.Equals(obj);
}
public override int GetHashCode()
{
return proxy.GetHashCode();
return _proxy.GetHashCode();
}
public MemberInfo[] GetMember(string name, BindingFlags bindingAttr)
{
return proxy.GetMember(name, bindingAttr);
return _proxy.GetMember(name, bindingAttr);
}
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Type[] signature)
{
#if NETFX_CORE
return proxy.GetMethod (name, bindingAttr, signature);
#else
return proxy.GetMethod(name, bindingAttr, null, signature, null);
#endif
return _proxy.GetMethod(name, bindingAttr, null, signature, null);
}
}
}
\ 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