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