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
......@@ -9,33 +9,33 @@ namespace NLua
using LuaState = KeraLua.Lua;
sealed class CheckType
{
Dictionary<Type, ExtractValue> extractValues = new Dictionary<Type, ExtractValue>();
ExtractValue extractNetObject;
ObjectTranslator translator;
readonly Dictionary<Type, ExtractValue> _extractValues = new Dictionary<Type, ExtractValue>();
readonly ExtractValue _extractNetObject;
readonly ObjectTranslator _translator;
public CheckType(ObjectTranslator translator)
{
this.translator = translator;
extractValues.Add(GetExtractDictionaryKey(typeof(object)), new ExtractValue(GetAsObject));
extractValues.Add(GetExtractDictionaryKey(typeof(sbyte)), new ExtractValue(GetAsSbyte));
extractValues.Add(GetExtractDictionaryKey(typeof(byte)), new ExtractValue(GetAsByte));
extractValues.Add(GetExtractDictionaryKey(typeof(short)), new ExtractValue(GetAsShort));
extractValues.Add(GetExtractDictionaryKey(typeof(ushort)), new ExtractValue(GetAsUshort));
extractValues.Add(GetExtractDictionaryKey(typeof(int)), new ExtractValue(GetAsInt));
extractValues.Add(GetExtractDictionaryKey(typeof(uint)), new ExtractValue(GetAsUint));
extractValues.Add(GetExtractDictionaryKey(typeof(long)), new ExtractValue(GetAsLong));
extractValues.Add(GetExtractDictionaryKey(typeof(ulong)), new ExtractValue(GetAsUlong));
extractValues.Add(GetExtractDictionaryKey(typeof(double)), new ExtractValue(GetAsDouble));
extractValues.Add(GetExtractDictionaryKey(typeof(char)), new ExtractValue(GetAsChar));
extractValues.Add(GetExtractDictionaryKey(typeof(float)), new ExtractValue(GetAsFloat));
extractValues.Add(GetExtractDictionaryKey(typeof(decimal)), new ExtractValue(GetAsDecimal));
extractValues.Add(GetExtractDictionaryKey(typeof(bool)), new ExtractValue(GetAsBoolean));
extractValues.Add(GetExtractDictionaryKey(typeof(string)), new ExtractValue(GetAsString));
extractValues.Add(GetExtractDictionaryKey(typeof(char[])), new ExtractValue(GetAsCharArray));
extractValues.Add(GetExtractDictionaryKey(typeof(LuaFunction)), new ExtractValue(GetAsFunction));
extractValues.Add(GetExtractDictionaryKey(typeof(LuaTable)), new ExtractValue(GetAsTable));
extractValues.Add(GetExtractDictionaryKey(typeof(LuaUserData)), new ExtractValue(GetAsUserdata));
extractNetObject = new ExtractValue(GetAsNetObject);
_translator = translator;
_extractValues.Add(GetExtractDictionaryKey(typeof(object)), GetAsObject);
_extractValues.Add(GetExtractDictionaryKey(typeof(sbyte)), GetAsSbyte);
_extractValues.Add(GetExtractDictionaryKey(typeof(byte)), GetAsByte);
_extractValues.Add(GetExtractDictionaryKey(typeof(short)), GetAsShort);
_extractValues.Add(GetExtractDictionaryKey(typeof(ushort)), GetAsUshort);
_extractValues.Add(GetExtractDictionaryKey(typeof(int)), GetAsInt);
_extractValues.Add(GetExtractDictionaryKey(typeof(uint)), GetAsUint);
_extractValues.Add(GetExtractDictionaryKey(typeof(long)), GetAsLong);
_extractValues.Add(GetExtractDictionaryKey(typeof(ulong)), GetAsUlong);
_extractValues.Add(GetExtractDictionaryKey(typeof(double)), GetAsDouble);
_extractValues.Add(GetExtractDictionaryKey(typeof(char)), GetAsChar);
_extractValues.Add(GetExtractDictionaryKey(typeof(float)), GetAsFloat);
_extractValues.Add(GetExtractDictionaryKey(typeof(decimal)), GetAsDecimal);
_extractValues.Add(GetExtractDictionaryKey(typeof(bool)), GetAsBoolean);
_extractValues.Add(GetExtractDictionaryKey(typeof(string)), GetAsString);
_extractValues.Add(GetExtractDictionaryKey(typeof(char[])), GetAsCharArray);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaFunction)), GetAsFunction);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaTable)), GetAsTable);
_extractValues.Add(GetExtractDictionaryKey(typeof(LuaUserData)), GetAsUserdata);
_extractNetObject = GetAsNetObject;
}
/*
......@@ -53,7 +53,7 @@ namespace NLua
paramType = paramType.GetElementType();
var extractKey = GetExtractDictionaryKey(paramType);
return extractValues.ContainsKey(extractKey) ? extractValues[extractKey] : extractNetObject;
return _extractValues.ContainsKey(extractKey) ? _extractValues[extractKey] : _extractNetObject;
}
internal ExtractValue CheckLuaType(LuaState luaState, int stackPos, Type paramType)
......@@ -91,90 +91,90 @@ namespace NLua
{
// Return the correct extractor anyways
if (netParamIsNumeric || paramType == typeof(bool))
return extractValues[extractKey];
return extractNetObject;
return _extractValues[extractKey];
return _extractNetObject;
}
}
if (paramType.Equals(typeof(object)))
return extractValues[extractKey];
return _extractValues[extractKey];
//CP: Added support for generic parameters
if (paramType.IsGenericParameter)
{
if (luatype == LuaType.Boolean)
return extractValues[GetExtractDictionaryKey(typeof(bool))];
else if (luatype == LuaType.String)
return extractValues[GetExtractDictionaryKey(typeof(string))];
else if (luatype == LuaType.Table)
return extractValues[GetExtractDictionaryKey(typeof(LuaTable))];
else if (luatype == LuaType.UserData)
return extractValues[GetExtractDictionaryKey(typeof(object))];
else if (luatype == LuaType.Function)
return extractValues[GetExtractDictionaryKey(typeof(LuaFunction))];
else if (luatype == LuaType.Number)
return extractValues[GetExtractDictionaryKey(typeof(double))];
return _extractValues[GetExtractDictionaryKey(typeof(bool))];
if (luatype == LuaType.String)
return _extractValues[GetExtractDictionaryKey(typeof(string))];
if (luatype == LuaType.Table)
return _extractValues[GetExtractDictionaryKey(typeof(LuaTable))];
if (luatype == LuaType.UserData)
return _extractValues[GetExtractDictionaryKey(typeof(object))];
if (luatype == LuaType.Function)
return _extractValues[GetExtractDictionaryKey(typeof(LuaFunction))];
if (luatype == LuaType.Number)
return _extractValues[GetExtractDictionaryKey(typeof(double))];
}
bool netParamIsString = paramType == typeof(string) || paramType == typeof(char[]);
if (netParamIsNumeric)
{
if (luaState.IsNumber(stackPos) && !netParamIsString)
return extractValues[extractKey];
return _extractValues[extractKey];
}
else if (paramType == typeof(bool))
{
if (luaState.IsBoolean(stackPos))
return extractValues[extractKey];
return _extractValues[extractKey];
}
else if (netParamIsString)
{
if (luaState.IsString(stackPos))
return extractValues[extractKey];
else if (luatype == LuaType.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer
return _extractValues[extractKey];
if (luatype == LuaType.Nil)
return _extractNetObject; // kevinh - silently convert nil to a null string pointer
}
else if (paramType == typeof(LuaTable))
{
if (luatype == LuaType.Table || luatype == LuaType.Nil)
return extractValues[extractKey];
return _extractValues[extractKey];
}
else if (paramType == typeof(LuaUserData))
{
if (luatype == LuaType.UserData || luatype == LuaType.Nil)
return extractValues[extractKey];
return _extractValues[extractKey];
}
else if (paramType == typeof(LuaFunction))
{
if (luatype == LuaType.Function || luatype == LuaType.Nil)
return extractValues[extractKey];
return _extractValues[extractKey];
}
else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaType.Function)
return new ExtractValue(new DelegateGenerator(translator, paramType).ExtractGenerated);
return new DelegateGenerator(_translator, paramType).ExtractGenerated;
else if (paramType.IsInterface && luatype == LuaType.Table)
return new ExtractValue(new ClassGenerator(translator, paramType).ExtractGenerated);
return new ClassGenerator(_translator, paramType).ExtractGenerated;
else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaType.Nil)
{
// kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
return extractNetObject;
return _extractNetObject;
}
else if (luaState.Type(stackPos) == LuaType.Table)
{
if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
{
object obj = translator.GetNetObject(luaState, -1);
object obj = _translator.GetNetObject(luaState, -1);
luaState.SetTop(-2);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject;
return _extractNetObject;
}
else
return null;
}
else
{
object obj = translator.GetNetObject(luaState, stackPos);
object obj = _translator.GetNetObject(luaState, stackPos);
if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
return extractNetObject;
return _extractNetObject;
}
return null;
......@@ -323,17 +323,17 @@ namespace NLua
private object GetAsTable(LuaState luaState, int stackPos)
{
return translator.GetTable(luaState, stackPos);
return _translator.GetTable(luaState, stackPos);
}
private object GetAsFunction(LuaState luaState, int stackPos)
{
return translator.GetFunction(luaState, stackPos);
return _translator.GetFunction(luaState, stackPos);
}
private object GetAsUserdata(LuaState luaState, int stackPos)
{
return translator.GetUserData(luaState, stackPos);
return _translator.GetUserData(luaState, stackPos);
}
public object GetAsObject(LuaState luaState, int stackPos)
......@@ -342,7 +342,7 @@ namespace NLua
{
if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
{
if (luaState.CheckMetaTable(-1, translator.Tag))
if (luaState.CheckMetaTable(-1, _translator.Tag))
{
luaState.Insert(stackPos);
luaState.Remove(stackPos + 1);
......@@ -352,23 +352,23 @@ namespace NLua
}
}
object obj = translator.GetObject(luaState, stackPos);
object obj = _translator.GetObject(luaState, stackPos);
return obj;
}
public object GetAsNetObject(LuaState luaState, int stackPos)
{
object obj = translator.GetNetObject(luaState, stackPos);
object obj = _translator.GetNetObject(luaState, stackPos);
if (obj == null && luaState.Type(stackPos) == LuaType.Table)
{
if (luaState.GetMetaField(stackPos, "__index") != LuaType.Nil)
{
if (luaState.CheckMetaTable(-1, translator.Tag))
if (luaState.CheckMetaTable(-1, _translator.Tag))
{
luaState.Insert(stackPos);
luaState.Remove(stackPos + 1);
obj = translator.GetNetObject(luaState, stackPos);
obj = _translator.GetNetObject(luaState, stackPos);
}
else
luaState.SetTop(-2);
......
......@@ -6,18 +6,16 @@ namespace NLua.Event
/// <summary>
/// Event args for hook callback event
/// </summary>
/// <author>Reinhard Ostermeier</author>
public class DebugHookEventArgs : EventArgs
{
readonly LuaDebug luaDebug;
public DebugHookEventArgs(LuaDebug luaDebug)
{
this.luaDebug = luaDebug;
LuaDebug = luaDebug;
}
public LuaDebug LuaDebug {
get { return luaDebug; }
}
/// <summary>
/// Lua Debug Information
/// </summary>
public LuaDebug LuaDebug { get; }
}
}
\ No newline at end of file
......@@ -4,15 +4,11 @@ namespace NLua.Event
{
public class HookExceptionEventArgs : EventArgs
{
private readonly Exception m_Exception;
public Exception Exception {
get { return m_Exception; }
}
public Exception Exception { get; }
public HookExceptionEventArgs(Exception ex)
{
m_Exception = ex;
Exception = ex;
}
}
}
\ No newline at end of file
......@@ -8,10 +8,6 @@ namespace NLua.Exceptions
[Serializable]
public class LuaException : Exception
{
public LuaException ()
{
}
public LuaException (string message) : base(message)
{
}
......
......@@ -12,18 +12,14 @@ namespace NLua.Exceptions
/// <summary>
/// Returns true if the exception has occured as the result of a .NET exception in user code
/// </summary>
public bool IsNetException { get; private set; }
public bool IsNetException { get; }
private readonly string source;
private readonly string _source;
/// <summary>
/// The position in the script where the exception was triggered.
/// </summary>
#if SILVERLIGHT && !WINDOWS_PHONE
public string Source { get { return source; } }
#else
public override string Source { get { return source; } }
#endif
public override string Source => _source;
/// <summary>
/// Creates a new Lua-only exception.
......@@ -32,7 +28,7 @@ namespace NLua.Exceptions
/// <param name="source">The position in the script where the exception was triggered.</param>
public LuaScriptException(string message, string source) : base(message)
{
this.source = source;
_source = source;
}
/// <summary>
......@@ -43,14 +39,14 @@ namespace NLua.Exceptions
public LuaScriptException(Exception innerException, string source)
: base("A .NET exception occured in user-code", innerException)
{
this.source = source;
this.IsNetException = true;
_source = source;
IsNetException = true;
}
public override string ToString()
{
// Prepend the error source
return GetType().FullName + ": " + source + Message;
return GetType().FullName + ": " + _source + Message;
}
}
}
\ No newline at end of file
using System.Collections.Generic;
namespace NLua.Extensions
{
static class StringExtensions
{
public static IEnumerable<string> SplitWithEscape(this string input, char separator, char escapeCharacter)
{
int start = 0;
int index = 0;
while (index < input.Length)
{
index = input.IndexOf(separator, index);
if (index == -1)
break;
if (input[index - 1] == escapeCharacter)
{
input = input.Remove(index - 1, 1);
continue;
}
yield return input.Substring(start, index - start);
index++;
start = index;
}
yield return input.Substring(start);
}
}
}
\ No newline at end of file
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
......@@ -92,7 +92,7 @@ namespace NLua.Extensions
public static MethodInfo[] GetExtensionMethods(this Type type, IEnumerable<Assembly> assemblies = null)
{
List<Type> types = new List<Type>();
var types = new List<Type>();
types.AddRange(type.Assembly.GetTypes().Where(t => t.IsPublic));
......@@ -106,58 +106,34 @@ namespace NLua.Extensions
}
}
var query = from extensionType in types
where extensionType.IsSealed && !extensionType.IsGenericType && !extensionType.IsNested
from method in extensionType.GetMethods(BindingFlags.Static | BindingFlags.Public)
where method.IsDefined(typeof(ExtensionAttribute), false)
where (method.GetParameters()[0].ParameterType == type
|| type.IsSubclassOf(method.GetParameters()[0].ParameterType)
|| type.GetInterfaces().Contains(method.GetParameters()[0].ParameterType))
select method;
return query.ToArray<MethodInfo>();
var query = types
.Where(extensionType =>
extensionType.IsSealed && !extensionType.IsGenericType && !extensionType.IsNested)
.SelectMany(extensionType => extensionType.GetMethods(BindingFlags.Static | BindingFlags.Public),
(extensionType, method) => new {extensionType, method})
.Where(t => t.method.IsDefined(typeof(ExtensionAttribute), false))
.Where(t =>
t.method.GetParameters()[0].ParameterType == type ||
type.IsSubclassOf(t.method.GetParameters()[0].ParameterType) ||
type.GetInterfaces().Contains(t.method.GetParameters()[0].ParameterType))
.Select(t => t.method);
return query.ToArray();
}
/// <summary>
/// Extends the System.Type-type to search for a given extended MethodeName.
/// </summary>
/// <param name="MethodeName">Name of the Methode</param>
/// <returns>the found Method or null</returns>
/// <param name="t"></param>
/// <param name="name"></param>
/// <param name="assemblies"></param>
/// <returns></returns>
public static MethodInfo GetExtensionMethod(this Type t, string name, IEnumerable<Assembly> assemblies = null)
{
var mi = from methode in t.GetExtensionMethods(assemblies)
where methode.Name == name
select methode;
if (!mi.Any<MethodInfo>())
var mi = t.GetExtensionMethods(assemblies).Where(method => method.Name == name).ToArray();
if (mi.Length == 0)
return null;
else
return mi.First<MethodInfo>();
}
}
static class StringExtensions
{
public static IEnumerable<string> SplitWithEscape(this string input, char separator, char escapeCharacter)
{
int start = 0;
int index = 0;
while (index < input.Length)
{
index = input.IndexOf(separator, index);
if (index == -1)
break;
if (input[index - 1] == escapeCharacter)
{
input = input.Remove(index - 1, 1);
continue;
}
yield return input.Substring(start, index - start);
index++;
start = index;
}
yield return input.Substring(start);
return mi[0];
}
}
}
\ No newline at end of file
......@@ -5,18 +5,18 @@ namespace NLua
{
class ClassGenerator
{
private ObjectTranslator translator;
private Type klass;
private readonly ObjectTranslator _translator;
private readonly Type _klass;
public ClassGenerator(ObjectTranslator objTranslator, Type typeClass)
{
translator = objTranslator;
klass = typeClass;
_translator = objTranslator;
_klass = typeClass;
}
public object ExtractGenerated(LuaState luaState, int stackPos)
{
return CodeGeneration.Instance.GetClassInstance(klass, translator.GetTable(luaState, stackPos));
return CodeGeneration.Instance.GetClassInstance(_klass, _translator.GetTable(luaState, stackPos));
}
}
}
\ No newline at end of file
......@@ -2,9 +2,7 @@ using System;
using System.Threading;
using System.Reflection;
using NLua.Extensions;
using System.Reflection.Emit;
using System.Collections;
using System.Collections.Generic;
using NLua.Method;
......@@ -12,12 +10,10 @@ namespace NLua
{
class CodeGeneration
{
private Dictionary<Type, LuaClassType> classCollection = new Dictionary<Type, LuaClassType>();
private Dictionary<Type, Type> delegateCollection = new Dictionary<Type, Type>();
private static readonly CodeGeneration instance = new CodeGeneration();
private AssemblyName assemblyName;
private readonly Dictionary<Type, LuaClassType> _classCollection = new Dictionary<Type, LuaClassType>();
private readonly Dictionary<Type, Type> _delegateCollection = new Dictionary<Type, Type>();
#if !(__IOS__ || __TVOS__ || __WATCHOS__) && !SILVERLIGHT && !NETSTANDARD
#if !(__IOS__ || __TVOS__ || __WATCHOS__) && !NETSTANDARD
private Dictionary<Type, Type> eventHandlerCollection = new Dictionary<Type, Type>();
private Type eventHandlerParent = typeof(LuaEventHandler);
private Type delegateParent = typeof(LuaDelegate);
......@@ -34,7 +30,7 @@ namespace NLua
private CodeGeneration()
{
// Create an assembly name
assemblyName = new AssemblyName();
var assemblyName = new AssemblyName();
assemblyName.Name = "NLua_generatedcode";
// Create a new assembly with one module.
#if NETCOREAPP
......@@ -49,9 +45,7 @@ namespace NLua
/*
* Singleton instance of the class
*/
public static CodeGeneration Instance {
get { return instance; }
}
public static CodeGeneration Instance { get; } = new CodeGeneration();
/*
* Generates an event handler that calls a Lua function
......@@ -60,15 +54,13 @@ namespace NLua
{
#if __IOS__ || __TVOS__ || __WATCHOS__
throw new NotImplementedException (" Emit not available on Xamarin.iOS ");
#elif SILVERLIGHT
throw new NotImplementedException(" Emit not available on Silverlight ");
#elif NETSTANDARD
throw new NotImplementedException(" Emit not available on .NET Standard ");
#else
string typeName;
lock (this)
{
typeName = "LuaGeneratedClass" + luaClassNumber.ToString();
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
......@@ -105,15 +97,13 @@ namespace NLua
{
#if __IOS__ || __TVOS__ || __WATCHOS__
throw new NotImplementedException ("GenerateDelegate is not available on iOS, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
#elif SILVERLIGHT
throw new NotImplementedException("GenerateDelegate is not available on Silverlight, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
#elif NETSTANDARD
throw new NotImplementedException("GenerateDelegate is not available on Windows Store, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
#else
string typeName;
lock (this)
{
typeName = "LuaGeneratedClass" + luaClassNumber.ToString();
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
......@@ -297,13 +287,10 @@ namespace NLua
GetReturnTypesFromMethod(method, out returnTypes[i]);
i++;
}
else
else if (!method.IsPrivate && !method.IsFinal && method.IsVirtual)
{
if (!method.IsPrivate && !method.IsFinal && method.IsVirtual)
{
GetReturnTypesFromMethod(method, out returnTypes[i]);
i++;
}
GetReturnTypesFromMethod(method, out returnTypes[i]);
i++;
}
}
}
......@@ -316,15 +303,13 @@ namespace NLua
{
#if __IOS__ || __TVOS__ || __WATCHOS__
throw new NotImplementedException (" Emit not available on Xamarin.iOS ");
#elif SILVERLIGHT
throw new NotImplementedException (" Emit not available on Silverlight ");
#elif NETSTANDARD
throw new NotImplementedException (" Emit not available on .NET Standard ");
#else
string typeName;
lock (this)
{
typeName = "LuaGeneratedClass" + luaClassNumber.ToString();
typeName = "LuaGeneratedClass" + luaClassNumber;
luaClassNumber++;
}
......@@ -414,12 +399,9 @@ namespace NLua
for (int i = 0; i < paramTypes.Length; i++)
{
paramTypes[i] = paramInfo[i].ParameterType;
#if SILVERLIGHT
if (paramInfo[i].IsOut) {
#else
if ((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
if (!paramInfo[i].IsIn && paramInfo[i].IsOut)
{
#endif
nOutParams++;
}
......@@ -433,7 +415,7 @@ namespace NLua
returnTypes = returnTypesList.ToArray();
}
#if !(__IOS__ || __TVOS__ || __WATCHOS__) && !SILVERLIGHT && !NETSTANDARD
#if !(__IOS__ || __TVOS__ || __WATCHOS__) && !NETSTANDARD
/*
* Generates an overriden implementation of method inside myType that delegates
......@@ -458,7 +440,7 @@ namespace NLua
for (int i = 0; i < paramTypes.Length; i++)
{
paramTypes[i] = paramInfo[i].ParameterType;
if ((!paramInfo[i].IsIn) && paramInfo[i].IsOut)
if (!paramInfo[i].IsIn && paramInfo[i].IsOut)
nOutParams++;
if (paramTypes[i].IsByRef)
......@@ -684,8 +666,6 @@ namespace NLua
{
#if __IOS__ || __TVOS__ || __WATCHOS__
throw new NotImplementedException (" Emit not available on Xamarin.iOS ");
#elif SILVERLIGHT
throw new NotImplementedException (" Emit not available on Silverlight ");
#elif NETSTANDARD
throw new NotImplementedException (" Emit not available on .NET Standard ");
#else
......@@ -700,22 +680,22 @@ namespace NLua
}
var luaEventHandler = (LuaEventHandler)Activator.CreateInstance(eventConsumerType);
luaEventHandler.handler = eventHandler;
luaEventHandler.Handler = eventHandler;
return luaEventHandler;
#endif
}
public void RegisterLuaDelegateType(Type delegateType, Type luaDelegateType)
{
delegateCollection[delegateType] = luaDelegateType;
_delegateCollection[delegateType] = luaDelegateType;
}
public void RegisterLuaClassType(Type klass, Type luaClass)
{
LuaClassType luaClassType = new LuaClassType();
var luaClassType = new LuaClassType();
luaClassType.klass = luaClass;
GetReturnTypesFromClass(klass, out luaClassType.returnTypes);
classCollection[klass] = luaClassType;
_classCollection[klass] = luaClassType;
}
/*
* Gets a delegate with delegateType that calls the luaFunc Lua function
......@@ -726,12 +706,14 @@ namespace NLua
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);
delegateCollection[delegateType] = luaDelegateType;
_delegateCollection[delegateType] = luaDelegateType;
}
var methodInfo = delegateType.GetMethod("Invoke");
......@@ -746,12 +728,7 @@ namespace NLua
var luaDelegate = (LuaDelegate)Activator.CreateInstance(luaDelegateType);
luaDelegate.function = luaFunc;
luaDelegate.returnTypes = returnTypes.ToArray();
#if NETFX_CORE
var mi = luaDelegate.GetType ().GetTypeInfo ().GetDeclaredMethod ("CallFunction");
return mi.CreateDelegate (delegateType, luaDelegate);
#else
return Delegate.CreateDelegate(delegateType, luaDelegate, "CallFunction");
#endif
}
/*
......@@ -764,13 +741,13 @@ namespace NLua
{
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);
classCollection[klass] = luaClassType;
_classCollection[klass] = luaClassType;
}
return Activator.CreateInstance(luaClassType.klass, new object[] {
......
......@@ -5,19 +5,18 @@ namespace NLua
{
class DelegateGenerator
{
private ObjectTranslator translator;
private Type delegateType;
private readonly ObjectTranslator _translator;
private readonly Type _delegateType;
public DelegateGenerator(ObjectTranslator objectTranslator, Type type)
{
translator = objectTranslator;
delegateType = type;
_translator = objectTranslator;
_delegateType = type;
}
public object ExtractGenerated(LuaState luaState, int stackPos)
{
return CodeGeneration.Instance.GetDelegate(delegateType, translator.GetFunction(luaState, stackPos));
return CodeGeneration.Instance.GetDelegate(_delegateType, _translator.GetFunction(luaState, stackPos));
}
}
}
\ No newline at end of file
This diff is collapsed.
......@@ -7,11 +7,14 @@ namespace NLua
/// </summary>
public abstract class LuaBase : IDisposable
{
private bool _Disposed;
protected int
_Reference;
protected Lua
_Interpreter;
private bool _disposed;
protected readonly int _Reference;
protected Lua _Interpreter;
protected LuaBase(int reference)
{
_Reference = reference;
}
~LuaBase()
{
......@@ -26,28 +29,23 @@ namespace NLua
public virtual void Dispose(bool disposeManagedResources)
{
if (!_Disposed)
{
if (disposeManagedResources)
{
if (_Reference != 0)
_Interpreter.DisposeInternal(_Reference);
}
_Interpreter = null;
_Disposed = true;
}
if (_disposed)
return;
if (_Reference != 0 && disposeManagedResources)
_Interpreter.DisposeInternal(_Reference);
_Interpreter = null;
_disposed = true;
}
public override bool Equals(object o)
{
if (o is LuaBase)
{
var l = (LuaBase)o;
return _Interpreter.CompareRef(l._Reference, _Reference);
}
else
var reference = o as LuaBase;
if (reference == null)
return false;
return _Interpreter.CompareRef(reference._Reference, _Reference);
}
public override int GetHashCode()
......
......@@ -8,18 +8,16 @@ namespace NLua
{
public class LuaFunction : LuaBase
{
internal LuaNativeFunction function;
internal readonly LuaNativeFunction function;
public LuaFunction(int reference, Lua interpreter)
public LuaFunction(int reference, Lua interpreter):base(reference)
{
_Reference = reference;
function = null;
_Interpreter = interpreter;
}
public LuaFunction(LuaNativeFunction nativeFunction, Lua interpreter)
public LuaFunction(LuaNativeFunction nativeFunction, Lua interpreter):base (0)
{
_Reference = 0;
function = nativeFunction;
_Interpreter = interpreter;
}
......@@ -60,17 +58,15 @@ namespace NLua
public override bool Equals(object o)
{
if (o is LuaFunction)
{
var l = (LuaFunction)o;
var l = o as LuaFunction;
if (this._Reference != 0 && l._Reference != 0)
return _Interpreter.CompareRef(l._Reference, this._Reference);
else
return this.function == l.function;
}
else
if (l == null)
return false;
if (_Reference != 0 && l._Reference != 0)
return _Interpreter.CompareRef(l._Reference, _Reference);
return function == l.function;
}
public override int GetHashCode()
......
using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using NLua.Extensions;
namespace NLua
{
......@@ -18,10 +16,10 @@ namespace NLua
{
#region Sanity checks
if (lua == null)
throw new ArgumentNullException("lua");
throw new ArgumentNullException(nameof(lua));
if (o == null)
throw new ArgumentNullException("o");
throw new ArgumentNullException(nameof(o));
#endregion
foreach (var method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
......@@ -47,13 +45,13 @@ namespace NLua
{
#region Sanity checks
if (lua == null)
throw new ArgumentNullException("lua");
throw new ArgumentNullException(nameof(lua));
if (type == null)
throw new ArgumentNullException("type");
throw new ArgumentNullException(nameof(type));
if (!type.IsClass)
throw new ArgumentException("The type must be a class!", "type");
throw new ArgumentException("The type must be a class!", nameof(type));
#endregion
foreach (var method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
......@@ -78,7 +76,7 @@ namespace NLua
public static void Enumeration<T>(Lua lua)
{
if (lua == null)
throw new ArgumentNullException("lua");
throw new ArgumentNullException(nameof(lua));
var type = typeof(T);
......
......@@ -9,9 +9,8 @@ namespace NLua
{
public class LuaTable : LuaBase
{
public LuaTable(int reference, Lua interpreter)
public LuaTable(int reference, Lua interpreter):base(reference)
{
_Reference = reference;
_Interpreter = interpreter;
}
......@@ -48,13 +47,11 @@ namespace NLua
return _Interpreter.GetTableDict(this).GetEnumerator();
}
public ICollection Keys {
get { return _Interpreter.GetTableDict(this).Keys; }
}
public ICollection Keys => _Interpreter.GetTableDict(this).Keys;
public ICollection Values => _Interpreter.GetTableDict(this).Values;
public ICollection Values {
get { return _Interpreter.GetTableDict(this).Values; }
}
/*
* Gets an string fields of a table ignoring its metatable,
......
......@@ -3,9 +3,8 @@ namespace NLua
{
public class LuaUserData : LuaBase
{
public LuaUserData(int reference, Lua interpreter)
public LuaUserData(int reference, Lua interpreter):base(reference)
{
_Reference = reference;
_Interpreter = interpreter;
}
......
This diff is collapsed.
......@@ -9,16 +9,16 @@ namespace NLua.Method
/// </summary>
class EventHandlerContainer : IDisposable
{
private Dictionary<Delegate, RegisterEventHandler> dict = new Dictionary<Delegate, RegisterEventHandler>();
private readonly Dictionary<Delegate, RegisterEventHandler> _dict = new Dictionary<Delegate, RegisterEventHandler>();
public void Add(Delegate handler, RegisterEventHandler eventInfo)
{
dict.Add(handler, eventInfo);
_dict.Add(handler, eventInfo);
}
public void Remove(Delegate handler)
{
bool found = dict.Remove(handler);
bool found = _dict.Remove(handler);
Debug.Assert(found);
}
......@@ -27,10 +27,10 @@ namespace NLua.Method
/// </summary>
public void Dispose()
{
foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in dict)
foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in _dict)
pair.Value.RemovePending(pair.Key);
dict.Clear();
_dict.Clear();
}
}
}
\ No newline at end of file
......@@ -2,12 +2,6 @@ using System;
namespace NLua.Method
{
/*
* Static helper methods for Lua tables acting as CLR objects.
*
* Author: Fabio Mascarenhas
* Version: 1.0
*/
public class LuaClassHelper
{
/*
......@@ -19,12 +13,11 @@ namespace NLua.Method
if (luaTable == null)
return null;
object funcObj = luaTable.RawGet(name);
var funcObj = luaTable.RawGet(name) as LuaFunction;
if (funcObj is LuaFunction)
return (LuaFunction)funcObj;
else
return null;
if (funcObj != null)
return funcObj;
return null;
}
/*
......
......@@ -2,13 +2,13 @@ namespace NLua.Method
{
public class LuaEventHandler
{
public LuaFunction handler = null;
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);
Handler.Call(args);
}
}
}
\ 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