Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Oct 7, 2022
2 parents 98d7db4 + b8cb257 commit 3a6228d
Show file tree
Hide file tree
Showing 219 changed files with 4,322 additions and 1,325 deletions.
Binary file modified Dependencies/ILRuntime.Mono.Cecil.dll
Binary file not shown.
Binary file modified Dependencies/ILRuntime.Mono.Cecil.pdb
Binary file not shown.
Binary file modified Dependencies/netstandard2.0/ILRuntime.Mono.Cecil.dll
Binary file not shown.
Binary file modified Dependencies/netstandard2.0/ILRuntime.Mono.Cecil.pdb
Binary file not shown.
12 changes: 9 additions & 3 deletions ILRuntime/CLR/Method/CLRMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.CLR.Method
{
public class CLRMethod : IMethod
public sealed class CLRMethod : IMethod
{
MethodInfo def;
ConstructorInfo cDef;
Expand Down Expand Up @@ -270,7 +276,7 @@ void InitParameters()
return (StackObject*)((long)a - sizeof(StackObject) * b);
}

public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, IList<object> mStack, bool isNewObj = false)
public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, AutoList mStack, bool isNewObj = false)
{
if (parameters == null)
{
Expand Down Expand Up @@ -346,7 +352,7 @@ public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObj
}
}

unsafe void FixReference(int paramCount, StackObject* esp, object[] param, IList<object> mStack, object instance, bool hasThis)
unsafe void FixReference(int paramCount, StackObject* esp, object[] param, AutoList mStack, object instance, bool hasThis)
{
var cnt = hasThis ? paramCount + 1 : paramCount;
for (int i = cnt; i >= 1; i--)
Expand Down
35 changes: 34 additions & 1 deletion ILRuntime/CLR/Method/ILMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using ILRuntime.Reflection;
namespace ILRuntime.CLR.Method
{
public class ILMethod : IMethod
public sealed class ILMethod : IMethod
{
OpCode[] body;
OpCodeR[] bodyRegister;
Expand All @@ -27,8 +27,11 @@ public class ILMethod : IMethod
ExceptionHandler[] exceptionHandler, exceptionHandlerR;
KeyValuePair<string, IType>[] genericParameters;
IType[] genericArguments;
ILMethod genericDefinition;
Dictionary<int, int[]> jumptables, jumptablesR;
bool isDelegateInvoke;
bool isEventAdd, isEventRemove;
int eventFieldIndex;
bool jitPending;
ILRuntimeMethodInfo refletionMethodInfo;
ILRuntimeConstructorInfo reflectionCtorInfo;
Expand Down Expand Up @@ -157,6 +160,7 @@ public Mono.Collections.Generic.Collection<Mono.Cecil.Cil.VariableDefinition> Va

public IType[] GenericArugmentsArray { get { return genericArguments; } }

public ILMethod GenericDefinition { get { return genericDefinition; } }
public bool ShouldUseRegisterVM
{
get
Expand Down Expand Up @@ -347,6 +351,27 @@ public bool IsDelegateInvoke
}
}

public bool IsEventAdd
{
get
{
return isEventAdd;
}
}

public bool IsEventRemove
{
get
{
return isEventRemove;
}
}

public int EventFieldIndex
{
get { return eventFieldIndex; }
}

public bool IsStatic
{
get { return def.IsStatic; }
Expand Down Expand Up @@ -797,6 +822,13 @@ unsafe void InitToken(ref OpCode code, object token, Dictionary<Mono.Cecil.Cil.I
}
}

public void SetEventAddOrRemove(bool isEventAdd, bool isEventRemove, int fieldIdx)
{
this.isEventRemove = isEventRemove;
this.isEventAdd = isEventAdd;
eventFieldIndex = fieldIdx;
}

internal int GetTypeTokenHashCode(object token)
{
var t = appdomain.GetType(token, declaringType, this);
Expand Down Expand Up @@ -928,6 +960,7 @@ public IMethod MakeGenericMethod(IType[] genericArguments)
ILMethod m = new ILMethod(def, declaringType, appdomain, jitFlags);
m.genericParameters = genericParameters;
m.genericArguments = genericArguments;
m.genericDefinition = this;
if (m.def.ReturnType.IsGenericParameter)
{
m.ReturnType = m.FindGenericArgument(m.def.ReturnType.Name);
Expand Down
11 changes: 8 additions & 3 deletions ILRuntime/CLR/TypeSystem/CLRType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Stack;

#if DEBUG && !DISABLE_ILRUNTIME_DEBUG
using AutoList = System.Collections.Generic.List<object>;
#else
using AutoList = ILRuntime.Other.UncheckedList<object>;
#endif
namespace ILRuntime.CLR.TypeSystem
{
public unsafe class CLRType : IType
public sealed unsafe class CLRType : IType
{
Type clrType;
bool isPrimitive, isValueType, isEnum;
Expand Down Expand Up @@ -404,7 +409,7 @@ public object GetFieldValue(int hash, object target)
return null;
}

public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntepreter intp, ref StackObject* esp, IList<object> mStack)
public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntepreter intp, ref StackObject* esp, AutoList mStack)
{
if (fieldMapping == null)
InitializeFields();
Expand All @@ -420,7 +425,7 @@ public bool CopyFieldToStack(int hash, object target, Runtime.Intepreter.ILIntep
return false;
}

public bool AssignFieldFromStack(int hash, ref object target, Runtime.Intepreter.ILIntepreter intp, StackObject* esp, IList<object> mStack)
public bool AssignFieldFromStack(int hash, ref object target, Runtime.Intepreter.ILIntepreter intp, StackObject* esp, AutoList mStack)
{
if (fieldMapping == null)
InitializeFields();
Expand Down
60 changes: 39 additions & 21 deletions ILRuntime/CLR/TypeSystem/ILType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ILRuntime.CLR.TypeSystem
{
public class ILType : IType
public sealed class ILType : IType
{
Dictionary<string, List<ILMethod>> methods;
TypeReference typeRef;
Expand Down Expand Up @@ -716,52 +716,70 @@ public IMethod GetMethod ( string name, int paramCount, bool declaredOnly = fals
}
}

void InitializeMethods ()
void InitializeMethods()
{
methods = new Dictionary<string, List<ILMethod>> ();
constructors = new List<ILMethod> ();
if ( definition == null )
methods = new Dictionary<string, List<ILMethod>>();
constructors = new List<ILMethod>();
if (definition == null)
return;
if ( definition.HasCustomAttributes )
if (definition.HasCustomAttributes)
{
for ( int i = 0; i < definition.CustomAttributes.Count; i++ )
for (int i = 0; i < definition.CustomAttributes.Count; i++)
{
int f;
if ( definition.CustomAttributes [ i ].GetJITFlags ( AppDomain, out f ) )
if (definition.CustomAttributes[i].GetJITFlags(AppDomain, out f))
{
this.jitFlags = f;
break;
}
}
}
foreach ( var i in definition.Methods )
foreach (var i in definition.Methods)
{
if ( i.IsConstructor )
if (i.IsConstructor)
{
if ( i.IsStatic )
staticConstructor = new ILMethod ( i, this, appdomain, jitFlags );
if (i.IsStatic)
staticConstructor = new ILMethod(i, this, appdomain, jitFlags);
else
constructors.Add ( new ILMethod ( i, this, appdomain, jitFlags ) );
constructors.Add(new ILMethod(i, this, appdomain, jitFlags));
}
else
{
List<ILMethod> lst;
if ( !methods.TryGetValue ( i.Name, out lst ) )
if (!methods.TryGetValue(i.Name, out lst))
{
lst = new List<ILMethod> ();
methods [ i.Name ] = lst;
lst = new List<ILMethod>();
methods[i.Name] = lst;
}
var m = new ILMethod ( i, this, appdomain, jitFlags );
lst.Add ( m );
var m = new ILMethod(i, this, appdomain, jitFlags);
lst.Add(m);
}
}

foreach (var i in definition.Events)
{
int fieldIdx = -1;
InitializeFields();
if(i.AddMethod.IsStatic)
staticFieldMapping.TryGetValue(i.Name,out fieldIdx);
else
fieldMapping.TryGetValue(i.Name, out fieldIdx);
if (methods.TryGetValue(i.AddMethod.Name, out var lst))
{
lst[0].SetEventAddOrRemove(true, false, fieldIdx);
}
if (methods.TryGetValue(i.RemoveMethod.Name, out lst))
{
lst[0].SetEventAddOrRemove(false, true, fieldIdx);
}
}

if ( !appdomain.SuppressStaticConstructor && !staticConstructorCalled )
if (!appdomain.SuppressStaticConstructor && !staticConstructorCalled)
{
staticConstructorCalled = true;
if ( staticConstructor != null && ( !TypeReference.HasGenericParameters || IsGenericInstance ) )
if (staticConstructor != null && (!TypeReference.HasGenericParameters || IsGenericInstance))
{
appdomain.Invoke ( staticConstructor, null, null );
appdomain.Invoke(staticConstructor, null, null);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Other/UncheckedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ILRuntime.Other
/// </summary>
/// <typeparam name="T"></typeparam>
[Serializable]
public class UncheckedList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection
public sealed class UncheckedList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection
{

private const int _defaultCapacity = 4;
Expand Down
5 changes: 5 additions & 0 deletions ILRuntime/Reflection/ILRuntimeFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit)
return res.ToArray();
}

public override object GetRawConstantValue()
{
return definition.Constant;
}

public override object GetValue(object obj)
{
unsafe
Expand Down
26 changes: 26 additions & 0 deletions ILRuntime/Reflection/ILRuntimeMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ public override ParameterInfo[] GetParameters()
return parameters;
}

public override bool IsGenericMethod
{
get
{
return method.IsGenericInstance || method.GenericParameterCount > 0;
}
}

public override bool IsGenericMethodDefinition
{
get
{
return method.GenericParameterCount > 0;
}
}

public override MethodInfo MakeGenericMethod(params Type[] typeArguments)
{
IType[] arg = new IType[typeArguments.Length];
for (int i = 0; i < arg.Length; i++)
{
arg[i] = appdomain.GetType(typeArguments[i]);
}
return ((ILMethod)method.MakeGenericMethod(arg)).ReflectionMethodInfo;
}

public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
{
if (method.HasThis)
Expand Down
2 changes: 1 addition & 1 deletion ILRuntime/Reflection/ILRuntimeParameterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ILRuntimeParameterInfo(Mono.Cecil.ParameterDefinition definition, IType t
this.AppDomain = appdomain;

AttrsImpl = (ParameterAttributes)definition.Attributes;
ClassImpl = type.ReflectionType;
ClassImpl = type is ILGenericParameterType ? typeof(ILGenericParameterType) : type.ReflectionType;
DefaultValueImpl = definition.Constant;
MemberImpl = member;
NameImpl = definition.Name;
Expand Down
34 changes: 25 additions & 9 deletions ILRuntime/Reflection/ILRuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ void InitializeProperties()
properties[i] = pi;
if (pd.GetMethod != null)
{
pi.Getter = type.GetMethod(pd.GetMethod.Name, pd.GetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, null, null)).ToList(), null) as ILMethod;
pi.Getter = type.GetMethod(pd.GetMethod.Name, pd.GetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, type, null)).ToList(), null) as ILMethod;
}
if (pd.SetMethod != null)
{
pi.Setter = type.GetMethod(pd.SetMethod.Name, pd.SetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, null, null)).ToList(), null) as ILMethod;
pi.Setter = type.GetMethod(pd.SetMethod.Name, pd.SetMethod.Parameters.Select(p => type.AppDomain.GetType(p.ParameterType, type, null)).ToList(), null) as ILMethod;
}
}
}
Expand Down Expand Up @@ -303,10 +303,22 @@ public override FieldInfo GetField(string name, BindingFlags bindingAttr)
{
if (fields == null)
InitializeFields();
foreach(var i in fields)
bool isPublic = (bindingAttr & BindingFlags.Public) == BindingFlags.Public;
bool isPrivate = (bindingAttr & BindingFlags.NonPublic) == BindingFlags.NonPublic;
bool isStatic = (bindingAttr & BindingFlags.Static) == BindingFlags.Static;
bool isInstance = (bindingAttr & BindingFlags.Instance) == BindingFlags.Instance;
bool isDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
for (int i = 0; i < fields.Length; i++)
{
if (i.Name == name)
return i;
FieldInfo fi = fields[i];
if (isPublic != fi.IsPublic && isPrivate != !fi.IsPublic)
continue;
if ((isStatic != fi.IsStatic) && (isInstance != !fi.IsStatic))
continue;
if (isDeclaredOnly && i < type.FieldStartIndex)
continue;
if (fi.Name == name)
return fi;
}
if (BaseType != null && BaseType is ILRuntimeWrapperType)
{
Expand All @@ -323,14 +335,18 @@ public override FieldInfo[] GetFields(BindingFlags bindingAttr)
bool isPrivate = (bindingAttr & BindingFlags.NonPublic) == BindingFlags.NonPublic;
bool isStatic = (bindingAttr & BindingFlags.Static) == BindingFlags.Static;
bool isInstance = (bindingAttr & BindingFlags.Instance) == BindingFlags.Instance;
bool isDeclaredOnly = (bindingAttr & BindingFlags.DeclaredOnly) == BindingFlags.DeclaredOnly;
List<FieldInfo> res = new List<FieldInfo>();
foreach(var i in fields)
for (int i = 0; i < fields.Length; i++)
{
if (isPublic != i.IsPublic && isPrivate != !i.IsPublic)
FieldInfo fi = fields[i];
if (isPublic != fi.IsPublic && isPrivate != !fi.IsPublic)
continue;
if ((isStatic != i.IsStatic) && (isInstance != !i.IsStatic))
if ((isStatic != fi.IsStatic) && (isInstance != !fi.IsStatic))
continue;
res.Add(i);
if (isDeclaredOnly && i < type.FieldStartIndex)
continue;
res.Add(fi);
}
if ((bindingAttr & BindingFlags.DeclaredOnly) != BindingFlags.DeclaredOnly)
{
Expand Down
Loading

0 comments on commit 3a6228d

Please sign in to comment.