tanghai 4 роки тому
батько
коміт
b0283cb15a
30 змінених файлів з 626 додано та 171 видалено
  1. 0 8
      Unity/Assets/Mono/Core/Async.meta
  2. 15 1
      Unity/Assets/ThirdParty/ILRuntime/CLR/Method/CLRMethod.cs
  3. 24 5
      Unity/Assets/ThirdParty/ILRuntime/CLR/Method/ILMethod.cs
  4. 3 0
      Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethod.cs
  5. 26 0
      Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethodExtensions.cs
  6. 11 0
      Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethodExtensions.cs.meta
  7. 2 2
      Unity/Assets/ThirdParty/ILRuntime/CLR/TypeSystem/CLRType.cs
  8. 4 1
      Unity/Assets/ThirdParty/ILRuntime/Other/UncheckedStack.cs
  9. 9 2
      Unity/Assets/ThirdParty/ILRuntime/Reflection/Extensions.cs
  10. 13 1
      Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeMethodInfo.cs
  11. 7 1
      Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimePropertyInfo.cs
  12. 2 0
      Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeType.cs
  13. 1 1
      Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeWrapperType.cs
  14. 0 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs
  15. 4 2
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Debugger/DebuggerServer/DebuggerServer.cs
  16. 21 7
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/AppDomain.cs
  17. 208 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/CLRRedirections.cs
  18. 4 4
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs
  19. 31 0
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/DelegateManager.cs
  20. 22 5
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/InvocationContext.cs
  21. 0 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/ILRuntimeJITAttribute.cs
  22. 127 18
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs
  23. 43 37
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/ILIntepreter.cs
  24. 34 14
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs
  25. 0 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs
  26. 1 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/CodeBasicBlock.cs
  27. 10 26
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/ILIntepreter.Register.cs
  28. 3 30
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Stack/StackObject.cs
  29. 0 1
      Unity/Assets/ThirdParty/ILRuntime/Runtime/Stack/StackObjectAllocator.cs
  30. 1 0
      Unity/Unity.ThirdParty.csproj

+ 0 - 8
Unity/Assets/Mono/Core/Async.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: 02e318d4d8ed14c418284ee797a6d599
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 15 - 1
Unity/Assets/ThirdParty/ILRuntime/CLR/Method/CLRMethod.cs

@@ -417,7 +417,7 @@ namespace ILRuntime.CLR.Method
                 }
 
                 argString = argString.Substring(0, argString.Length - 2);
-                throw new Exception($"MakeGenericMethod failed : {def.DeclaringType.FullName}.{def.Name}<{argString}>");
+                throw new Exception(string.Format("MakeGenericMethod failed : {0}.{1}<{2}>", def.DeclaringType.FullName, def.Name, argString));
             }
 #endif
             var res = new CLRMethod(t, declaringType, appdomain);
@@ -439,5 +439,19 @@ namespace ILRuntime.CLR.Method
                 hashCode = System.Threading.Interlocked.Add(ref instance_id, 1);
             return hashCode;
         }
+
+
+        bool? isExtend;
+        public bool IsExtend
+        {
+            get
+            {
+                if (isExtend == null)
+                {
+                    isExtend = this.IsExtendMethod();
+                }
+                return isExtend.Value;
+            }
+        }
     }
 }

+ 24 - 5
Unity/Assets/ThirdParty/ILRuntime/CLR/Method/ILMethod.cs

@@ -405,8 +405,9 @@ namespace ILRuntime.CLR.Method
                     case OpCodeEnum.Callvirt:
                         {
                             var m = appdomain.GetMethod(ins.TokenInteger);
-                            if (m is ILMethod ilm)
+                            if (m is ILMethod)
                             {
+                                ILMethod ilm = (ILMethod)m;
                                 //如果参数alreadyPrewarmed不为空,则不仅prewarm当前方法,还会递归prewarm所有子调用
                                 //如果参数alreadyPrewarmed为空,则只prewarm当前方法
                                 if (alreadyPrewarmed != null)
@@ -414,8 +415,9 @@ namespace ILRuntime.CLR.Method
                                     ilm.Prewarm(alreadyPrewarmed);
                                 }
                             }
-                            else if (m is CLRMethod clrm)
+                            else if (m is CLRMethod)
                             {
+                                CLRMethod clrm = (CLRMethod)m;
                                 ILRuntime.CLR.Utils.Extensions.GetTypeFlags(clrm.DeclearingType.TypeForCLR);
                             }
                         }
@@ -456,8 +458,9 @@ namespace ILRuntime.CLR.Method
                     case OpCodeREnum.Callvirt:
                         {
                             var m = appdomain.GetMethod(ins.Operand);
-                            if (m is ILMethod ilm)
+                            if (m is ILMethod)
                             {
+                                ILMethod ilm = (ILMethod)m;
                                 //如果参数alreadyPrewarmed不为空,则不仅prewarm当前方法,还会递归prewarm所有子调用
                                 //如果参数alreadyPrewarmed为空,则只prewarm当前方法
                                 if (alreadyPrewarmed != null)
@@ -465,8 +468,9 @@ namespace ILRuntime.CLR.Method
                                     ilm.Prewarm(alreadyPrewarmed);
                                 }
                             }
-                            else if (m is CLRMethod clrm)
+                            else if (m is CLRMethod)
                             {
+                                CLRMethod clrm = (CLRMethod)m;
                                 ILRuntime.CLR.Utils.Extensions.GetTypeFlags(clrm.DeclearingType.TypeForCLR);
                             }
                         }
@@ -510,8 +514,9 @@ namespace ILRuntime.CLR.Method
                 {
                     t = appdomain.GetType(v.VariableType, DeclearingType, this);
                 }
-                if (t is CLRType ct)
+                if (t is CLRType)
                 {
+                    CLRType ct = (CLRType)t;
                     var fields = ct.Fields;
                     ILRuntime.CLR.Utils.Extensions.GetTypeFlags(ct.TypeForCLR);
                 }
@@ -961,5 +966,19 @@ namespace ILRuntime.CLR.Method
                 hashCode = System.Threading.Interlocked.Add(ref instance_id, 1);
             return hashCode;
         }
+
+
+        bool? isExtend;
+        public bool IsExtend
+        {
+            get
+            {
+                if (isExtend == null)
+                {
+                    isExtend = this.IsExtendMethod();
+                }
+                return isExtend.Value;
+            }
+        }
     }
 }

+ 3 - 0
Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethod.cs

@@ -29,5 +29,8 @@ namespace ILRuntime.CLR.Method
         bool IsStatic { get; }
 
         IMethod MakeGenericMethod(IType[] genericArguments);
+
+
+        bool IsExtend { get; }
     }
 }

+ 26 - 0
Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethodExtensions.cs

@@ -0,0 +1,26 @@
+namespace ILRuntime.CLR.Method
+{
+    public static class IMethodExtensions
+    {
+        public static bool IsExtendMethod(this ILMethod im)
+        {
+            if (!im.IsStatic || im.ParameterCount == 0)
+            {
+                return false;
+            }
+
+            return im.ReflectionMethodInfo.GetCustomAttributes(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false).Length > 0;
+
+        }
+        public static bool IsExtendMethod(this CLRMethod cm)
+        {
+            if (!cm.IsStatic || cm.ParameterCount == 0)
+            {
+                return false;
+            }
+
+            return cm.MethodInfo.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false);
+
+        }
+    }
+}

+ 11 - 0
Unity/Assets/ThirdParty/ILRuntime/CLR/Method/IMethodExtensions.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 155d92e1167d00242bc5e26fa584eb73
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 2 - 2
Unity/Assets/ThirdParty/ILRuntime/CLR/TypeSystem/CLRType.cs

@@ -346,7 +346,7 @@ namespace ILRuntime.CLR.TypeSystem
 
                     if (memberwiseClone != null)
                     {
-                        var del = (Func<object, object>)memberwiseClone.CreateDelegate(typeof(Func<object, object>));
+                        var del = (Func<object, object>)Delegate.CreateDelegate(typeof(Func<object, object>), memberwiseClone);
                         memberwiseCloneDelegate = (ref object t) => del(t);
                     }
                     else
@@ -967,7 +967,7 @@ namespace ILRuntime.CLR.TypeSystem
                     }
 
                     argString = argString.Substring(0, argString.Length - 2);
-                    throw new Exception($"MakeGenericType failed : {clrType.FullName}<{argString}>");
+                    throw new Exception(string.Format("MakeGenericType failed : {0}<{1}>", clrType.FullName, argString));
                 }
 #endif
                 var res = new CLRType(newType, appdomain);

+ 4 - 1
Unity/Assets/ThirdParty/ILRuntime/Other/UncheckedStack.cs

@@ -9,7 +9,10 @@ using System.Threading;
 [Serializable]
 [DebuggerDisplay("Count = {Count}")]
 [ComVisible(false)]
-public class UncheckedStack<T> : IEnumerable<T>, IEnumerable, ICollection, IReadOnlyCollection<T>
+public class UncheckedStack<T> : IEnumerable<T>, IEnumerable, ICollection
+#if NET_4_6 || NET_STANDARD_2_0
+	, IReadOnlyCollection<T>
+#endif
 {
 	[Serializable]
 	public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator

+ 9 - 2
Unity/Assets/ThirdParty/ILRuntime/Reflection/Extensions.cs

@@ -51,7 +51,8 @@ namespace ILRuntime.Reflection
                 {
                     foreach (var j in attribute.Fields)
                     {
-                        var field = it.GetField(j.Name, out int index);
+                        int index;
+                        var field = it.GetField(j.Name, out index);
                         if (field != null)
                             ((ILRuntime.Runtime.Intepreter.ILTypeInstance)ins)[index] = j.Argument.Value;
                     }
@@ -78,7 +79,13 @@ namespace ILRuntime.Reflection
                     foreach (var j in attribute.Properties)
                     {
                         var prop = at.TypeForCLR.GetProperty(j.Name);
-                        prop.SetValue(ins, j.Argument.Value, null);
+                        if (prop.PropertyType == typeof(Type) && j.Argument.Value != null)
+                        {
+                            var type = appdomain.GetType(j.Argument.Value, null, null);
+                            prop.SetValue(ins, type.TypeForCLR, null);
+                        }
+                        else
+                            prop.SetValue(ins, j.Argument.Value, null);
                     }
                 }
                 if(attribute.HasFields)

+ 13 - 1
Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeMethodInfo.cs

@@ -62,8 +62,15 @@ namespace ILRuntime.Reflection
             get
             {
                 MethodAttributes ma = MethodAttributes.Public;
+                if (definition.IsPrivate)
+                    ma = MethodAttributes.Private;
+                else if (definition.IsFamily)
+                    ma = MethodAttributes.Family;
                 if (method.IsStatic)
                     ma |= MethodAttributes.Static;
+                if (method.IsVirtual)
+                    ma |= MethodAttributes.Virtual;
+            
                 return ma;
             }
         }
@@ -171,10 +178,14 @@ namespace ILRuntime.Reflection
         {
             get
             {
-                return method.ReturnType?.ReflectionType;
+                if (method.ReturnType != null)
+                    return method.ReturnType.ReflectionType;
+                else
+                    return null;
             }
         }
 
+#if NET_4_6 || NET_STANDARD_2_0
         public override Delegate CreateDelegate(Type delegateType)
         {
             throw new NotSupportedException("please use CreateDelegate(Type delegateType, object target)");
@@ -209,5 +220,6 @@ namespace ILRuntime.Reflection
             }
             return del.GetConvertor(delegateType);
         }
+#endif
     }
 }

+ 7 - 1
Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimePropertyInfo.cs

@@ -141,7 +141,13 @@ namespace ILRuntime.Reflection
             }
         }
 
-        public ILRuntime.Mono.Cecil.TypeReference Definition => definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters [ 0 ].ParameterType;
+        public ILRuntime.Mono.Cecil.TypeReference Definition
+        {
+            get
+            {
+                return definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters[0].ParameterType;
+            }
+        }
 
 
         public override Type DeclaringType

+ 2 - 0
Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeType.cs

@@ -613,10 +613,12 @@ namespace ILRuntime.Reflection
         {
             return type.GetHashCode();
         }
+#if NET_4_6 || NET_STANDARD_2_0
         public override bool Equals(Type o)
         {
             return o is ILRuntimeType ? ((ILRuntimeType)o).type == type : false;
         }
+#endif
         public override bool Equals(object o)
         {
             return o is ILRuntimeType ? ((ILRuntimeType)o).type == type : false;

+ 1 - 1
Unity/Assets/ThirdParty/ILRuntime/Reflection/ILRuntimeWrapperType.cs

@@ -114,7 +114,7 @@ namespace ILRuntime.Reflection
 
         protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder binder, CallingConventions callConvention, Type[] types, ParameterModifier[] modifiers)
         {
-            return et.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers);
+            return types != null ? et.GetMethod(name, bindingAttr, binder, callConvention, types, modifiers) : et.GetMethod(name, bindingAttr);
         }
 
         public override MethodInfo[] GetMethods(BindingFlags bindingAttr)

+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 namespace ILRuntime.Runtime.CLRBinding
 {

+ 4 - 2
Unity/Assets/ThirdParty/ILRuntime/Runtime/Debugger/DebuggerServer/DebuggerServer.cs

@@ -17,6 +17,7 @@ namespace ILRuntime.Runtime.Debugger
         TcpListener listener;
         //HashSet<Session<T>> clients = new HashSet<Session<T>>();
         bool isUp = false;
+        bool shutdown = false;
         int maxNewConnections = 1;
         int port;
         Thread mainLoop;
@@ -42,6 +43,7 @@ namespace ILRuntime.Runtime.Debugger
 
         public virtual bool Start()
         {
+            shutdown = false;
             mainLoop = new Thread(new ThreadStart(this.NetworkLoop));
             mainLoop.Start();
 
@@ -58,9 +60,9 @@ namespace ILRuntime.Runtime.Debugger
         public virtual void Stop()
         {
             isUp = false;
+            shutdown = true;
             if (this.listener != null)
                 this.listener.Stop();
-            mainLoop.Abort();
             mainLoop = null;
             if (clientSocket != null)
                 clientSocket.Close();
@@ -68,7 +70,7 @@ namespace ILRuntime.Runtime.Debugger
 
         void NetworkLoop()
         {
-            while (true)
+            while (!shutdown)
             {
                 try
                 {

+ 21 - 7
Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/AppDomain.cs

@@ -186,6 +186,12 @@ namespace ILRuntime.Runtime.Enviorment
             RegisterCLRMethodRedirection(mi, CLRRedirections.GetTypeFromHandle);
             mi = typeof(object).GetMethod("GetType");
             RegisterCLRMethodRedirection(mi, CLRRedirections.ObjectGetType);
+            mi = typeof(Delegate).GetMethod("CreateDelegate", new Type[] { typeof(Type), typeof(MethodInfo) });
+            RegisterCLRMethodRedirection(mi, CLRRedirections.DelegateCreateDelegate);
+            mi = typeof(Delegate).GetMethod("CreateDelegate", new Type[] { typeof(Type), typeof(object), typeof(string) });
+            RegisterCLRMethodRedirection(mi, CLRRedirections.DelegateCreateDelegate2);
+            mi = typeof(Delegate).GetMethod("CreateDelegate", new Type[] { typeof(Type), typeof(object), typeof(MethodInfo) });
+            RegisterCLRMethodRedirection(mi, CLRRedirections.DelegateCreateDelegate3);
             dMgr = new DelegateManager(this);
             dMgr.RegisterDelegateConvertor<Action>((dele) =>
             {
@@ -795,7 +801,7 @@ namespace ILRuntime.Runtime.Enviorment
                     UnityEngine.Debug.Log("CLRBindingUtils.Initialize Done in thread..");
 #endif
                 });
-                thread.Name = $"CLRBindings-Thread #{thread.ManagedThreadId}";
+                thread.Name = string.Format("CLRBindings-Thread #{0}",thread.ManagedThreadId);
                 thread.Start();
             }
             else
@@ -847,8 +853,9 @@ namespace ILRuntime.Runtime.Enviorment
                     for (int i = 0; i < genericArguments.Length; i++)
                     {
                         string key = null;
-                        if (bt is ILType ilt)
+                        if (bt is ILType)
                         {
+                            ILType ilt = (ILType)bt;
                             key = ilt.TypeDefinition.GenericParameters[i].FullName;
                         }
                         else
@@ -974,10 +981,17 @@ namespace ILRuntime.Runtime.Enviorment
                         depth++;
                         if (depth == 1)
                         {
-                            baseType = sb.ToString();
-                            sb.Length = 0;
-                            genericParams = new List<string>();
-                            continue;
+                            if (isArray && sb.Length == 0)
+                            {
+                                continue;
+                            }
+                            else
+                            {
+                                baseType = sb.ToString();
+                                sb.Length = 0;
+                                genericParams = new List<string>();
+                                continue;
+                            }
                         }
                     }
                     if (i == ',' && depth == 1)
@@ -1562,7 +1576,7 @@ namespace ILRuntime.Runtime.Enviorment
                     genericArguments = new IType[gim.GenericArguments.Count];
                     for (int i = 0; i < genericArguments.Length; i++)
                     {
-                        if (gim.GenericArguments[i].IsGenericParameter)
+                        if (gim.GenericArguments[i].ContainsGenericParameter)
                             invalidToken = true;
                         var gt = GetType(gim.GenericArguments[i], contextType, contextMethod);
                         if (gt == null)

+ 208 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/CLRRedirections.cs

@@ -1249,7 +1249,7 @@ namespace ILRuntime.Runtime.Enviorment
             intp.Free(p);
 
             bool res = false;
-            if(ins is ILEnumTypeInstance)
+            if (ins is ILEnumTypeInstance)
             {
                 ILEnumTypeInstance enumIns = (ILEnumTypeInstance)ins;
                 int num = enumIns.Fields[0].Value;
@@ -1297,5 +1297,212 @@ namespace ILRuntime.Runtime.Enviorment
             return ret + 1;
         }
 #endif
+
+        public static StackObject* DelegateCreateDelegate(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
+        {
+            var ret = esp - 2;
+            AppDomain domain = intp.AppDomain;
+
+            var p = esp - 1;
+            MethodInfo mi = (MethodInfo)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            p = esp - 1 - 1;
+            Type t = (Type)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            if (t is ILRuntimeType)
+            {
+                ILType it = ((ILRuntimeType)t).ILType;
+                if (it.IsDelegate)
+                {
+                    var m = it.GetMethod("Invoke") as ILMethod;
+                    object ins = null;
+                    if (mi is ILRuntimeMethodInfo)
+                    {
+                        ILRuntimeMethodInfo imi = (ILRuntimeMethodInfo)mi;
+                        var ilMethod = imi.ILMethod;
+                        if (ilMethod.DelegateAdapter == null)
+                        {
+                            ilMethod.DelegateAdapter = domain.DelegateManager.FindDelegateAdapter(null, ilMethod, m);
+                        }
+                        ins = ilMethod.DelegateAdapter;
+                    }
+                    else
+                    {
+                        throw new NotSupportedException();
+                    }
+                    return ILIntepreter.PushObject(ret, mStack, ins, true);
+                }
+                else
+                    throw new NotSupportedException(string.Format("{0} is not Delegate", t.FullName));
+            }
+            else if (t is ILRuntimeWrapperType)
+            {
+                ILRuntimeWrapperType iwt = (ILRuntimeWrapperType)t;
+                object ins = null;
+                if (mi is ILRuntimeMethodInfo)
+                {
+                    ILRuntimeMethodInfo imi = (ILRuntimeMethodInfo)mi;
+
+                    ins = domain.DelegateManager.FindDelegateAdapter(iwt.CLRType, null, imi.ILMethod);
+                }
+                else
+                {
+                    ins = Delegate.CreateDelegate(iwt.RealType, mi);
+                }
+                return ILIntepreter.PushObject(ret, mStack, ins, true);
+            }
+            else
+                return ILIntepreter.PushObject(ret, mStack, Delegate.CreateDelegate(t, mi), true);
+        }
+
+        public static StackObject* DelegateCreateDelegate2(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
+        {
+            var ret = esp - 3;
+            AppDomain domain = intp.AppDomain;
+
+            var p = esp - 1;
+            string name = (string)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            p = esp - 2;
+            object obj = (object)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            p = esp - 3;
+            Type t = (Type)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            if (obj == null)
+                throw new ArgumentNullException("Argument target cannot be null");
+
+            if (t is ILRuntimeType)
+            {
+                ILType it = ((ILRuntimeType)t).ILType;
+                if (it.IsDelegate)
+                {
+                    var m = it.GetMethod("Invoke") as ILMethod;
+                    if (obj is ILTypeInstance)
+                    {
+                        ILTypeInstance ii = (ILTypeInstance)obj;
+                        var ilMethod = ii.Type.GetMethod(name) as ILMethod;
+                        if (ilMethod == null)
+                            throw new ArgumentException(string.Format("Cannot find method \"{0}\" in type {1}", name, it.FullName));
+                        if (ilMethod.DelegateAdapter == null)
+                        {
+                            ilMethod.DelegateAdapter = domain.DelegateManager.FindDelegateAdapter(ii, ilMethod, m);
+                        }
+                        object ins = ilMethod.DelegateAdapter;
+                        return ILIntepreter.PushObject(ret, mStack, ins, true);
+                    }
+                    else
+                        throw new NotSupportedException();
+
+                }
+                else
+                    throw new NotSupportedException(string.Format("{0} is not Delegate", t.FullName));
+            }
+            else if (t is ILRuntimeWrapperType)
+            {
+                ILRuntimeWrapperType iwt = (ILRuntimeWrapperType)t;
+                object ins = null;
+                if (obj is ILTypeInstance)
+                {
+                    ILTypeInstance ii = (ILTypeInstance)obj;
+                    var ilMethod = ii.Type.GetMethod(name) as ILMethod;
+                    if (ilMethod == null)
+                        throw new ArgumentException(string.Format("Cannot find method \"{0}\" in type {1}", name, ii.Type.FullName));
+                    ins = domain.DelegateManager.FindDelegateAdapter(iwt.CLRType, ii, ilMethod);
+                }
+                else
+                {
+                    ins = Delegate.CreateDelegate(iwt.RealType, obj, name);
+                }
+                return ILIntepreter.PushObject(ret, mStack, ins, true);
+            }
+            else
+                return ILIntepreter.PushObject(ret, mStack, Delegate.CreateDelegate(t, obj, name), true);
+        }
+
+        public static StackObject* DelegateCreateDelegate3(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
+        {
+            var ret = esp - 3;
+            AppDomain domain = intp.AppDomain;
+
+            var p = esp - 1;
+            MethodInfo mi = (MethodInfo)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            p = esp - 2;
+            object obj = (object)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            p = esp - 3;
+            Type t = (Type)StackObject.ToObject(p, domain, mStack);
+            intp.Free(p);
+
+            if (t is ILRuntimeType)
+            {
+                ILType it = ((ILRuntimeType)t).ILType;
+                if (it.IsDelegate)
+                {
+                    object ins = null;
+                    if (mi is ILRuntimeMethodInfo)
+                    {
+                        ILRuntimeMethodInfo imi = (ILRuntimeMethodInfo)mi;
+                        var ilMethod = imi.ILMethod;
+                        if (obj != null)
+                        {
+                            ins = ((ILTypeInstance)obj).GetDelegateAdapter(ilMethod);
+                            if (ins == null)
+                            {
+                                var invokeMethod = it.GetMethod("Invoke", ilMethod.ParameterCount);
+                                if (invokeMethod == null && ilMethod.IsExtend)
+                                {
+                                    invokeMethod = it.GetMethod("Invoke", ilMethod.ParameterCount - 1);
+                                }
+                                ins = domain.DelegateManager.FindDelegateAdapter(
+                                    (ILTypeInstance)obj, ilMethod, invokeMethod);
+                            }
+                        }
+                        else
+                        {
+                            if (ilMethod.DelegateAdapter == null)
+                            {
+                                var m = it.GetMethod("Invoke") as ILMethod;
+                                ilMethod.DelegateAdapter = domain.DelegateManager.FindDelegateAdapter(null, ilMethod, m);
+                            }
+                            ins = ilMethod.DelegateAdapter;
+                        }
+                    }
+                    else
+                    {
+                        throw new NotSupportedException();
+                    }
+                    return ILIntepreter.PushObject(ret, mStack, ins, true);
+                }
+                else
+                    throw new NotSupportedException(string.Format("{0} is not Delegate", t.FullName));
+            }
+            else if (t is ILRuntimeWrapperType)
+            {
+                ILRuntimeWrapperType iwt = (ILRuntimeWrapperType)t;
+                object ins = null;
+                if (mi is ILRuntimeMethodInfo)
+                {
+                    ILRuntimeMethodInfo imi = (ILRuntimeMethodInfo)mi;
+
+                    ins = domain.DelegateManager.FindDelegateAdapter(iwt.CLRType, obj as ILTypeInstance, imi.ILMethod);
+                }
+                else
+                {
+                    ins = Delegate.CreateDelegate(iwt.RealType, obj, mi);
+                }
+                return ILIntepreter.PushObject(ret, mStack, ins, true);
+            }
+            else
+                return ILIntepreter.PushObject(ret, mStack, Delegate.CreateDelegate(t, obj, mi), true);
+        }
     }
 }

+ 4 - 4
Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs

@@ -138,7 +138,7 @@ namespace ");
                         StringBuilder sBuilder = new StringBuilder();
                         var p = i.GetParameters()[0];
                         p.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true);
-                        pName = $"this [{realClsName + " " + p.Name}]";
+                        pName = string.Format("this [{0}]", realClsName + " " + p.Name);
 
                         isIndexFunc = true;
                     }
@@ -192,8 +192,8 @@ namespace ");
                     if (isProperty)
                     {
                         string baseMethodName = isIndexFunc
-                            ? $"base[{i.GetParameters()[0].Name}]"
-                            : $"base.{i.Name.Substring(4)}";
+                            ? string.Format("base[{0}]", i.GetParameters()[0].Name)
+                            : string.Format("base.{0}", i.Name.Substring(4));
                         if (isGetter)
                         {
                             sb.AppendLine(string.Format("                    return {0};", baseMethodName));
@@ -597,7 +597,7 @@ namespace ");
             {
                 if (p.IsOut)
                 {
-                    sb.AppendLine($"                    {p.Name} = default({p.ParameterType.GetElementType().FullName});");
+                    sb.AppendLine(string.Format("                    {0} = default({1});", p.Name, p.ParameterType.GetElementType().FullName));
                 }
             }
         }

+ 31 - 0
Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/DelegateManager.cs

@@ -229,6 +229,37 @@ namespace ILRuntime.Runtime.Enviorment
             }
         }
 
+        internal IDelegateAdapter FindDelegateAdapter(CLRType type, ILTypeInstance ins, ILMethod ilMethod)
+        {
+            IDelegateAdapter dele;
+            if (ins != null)
+            {
+                dele = (ins).GetDelegateAdapter(ilMethod);
+                if (dele == null)
+                {
+                    var invokeMethod =
+                        type.GetMethod("Invoke",
+                            ilMethod.ParameterCount);
+                    if (invokeMethod == null && ilMethod.IsExtend)
+                    {
+                        invokeMethod = type.GetMethod("Invoke", ilMethod.ParameterCount - 1);
+                    }
+                    dele = appdomain.DelegateManager.FindDelegateAdapter(
+                        ins, ilMethod, invokeMethod);
+                }
+            }
+            else
+            {
+                if (ilMethod.DelegateAdapter == null)
+                {
+                    var invokeMethod = type.GetMethod("Invoke", ilMethod.ParameterCount);
+                    ilMethod.DelegateAdapter = appdomain.DelegateManager.FindDelegateAdapter(null, ilMethod, invokeMethod);
+                }
+                dele = ilMethod.DelegateAdapter;
+            }
+            return dele;
+        }
+
         /// <summary>
         /// ilMethod代表的delegate会赋值给method对应的delegate,一般两者参数类型都一致,
         /// 但新版本的支持泛型协变之后,有些时候会不一致,所以此处判断是用method判断,而不是用ilMethod判断

+ 22 - 5
Unity/Assets/ThirdParty/ILRuntime/Runtime/Enviorment/InvocationContext.cs

@@ -215,16 +215,31 @@ namespace ILRuntime.Runtime.Enviorment
 
         internal StackObject* ESP
         {
-            get => esp;
+            get
+            {
+                return esp;
+            }
             set
             {
                 esp = value;
             }
         }
 
-        internal ILIntepreter Intepreter => intp;
+        internal ILIntepreter Intepreter
+        {
+            get
+            {
+                return intp;
+            }
+        }
 
-        internal IList<object> ManagedStack => mStack;
+        internal IList<object> ManagedStack
+        {
+            get
+            {
+                return mStack;
+            }
+        }
 
         public void PushBool(bool val)
         {
@@ -300,7 +315,8 @@ namespace ILRuntime.Runtime.Enviorment
             Type t = typeof(T);
             bool needPush = false;
             StackObject* res = default(StackObject*);
-            if (domain.ValueTypeBinders.TryGetValue(t, out var binder))
+            ValueTypeBinder binder;
+            if (domain.ValueTypeBinders.TryGetValue(t, out binder))
             {
                 var binderT = binder as ValueTypeBinder<T>;
                 if (binderT != null)
@@ -492,7 +508,8 @@ namespace ILRuntime.Runtime.Enviorment
             CheckReturnValue();
             Type t = typeof(T);
             T res = default(T);
-            if (domain.ValueTypeBinders.TryGetValue(t, out var binder))
+            ValueTypeBinder binder;
+            if (domain.ValueTypeBinders.TryGetValue(t, out binder))
             {
                 var binderT = binder as ValueTypeBinder<T>;
                 if (binderT != null)

+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/ILRuntimeJITAttribute.cs

@@ -2,7 +2,6 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 namespace ILRuntime.Runtime
 {

+ 127 - 18
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs

@@ -35,6 +35,13 @@ namespace ILRuntime.Runtime.Intepreter
             action = InvokeILMethod;
         }
 
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Func<TResult>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -99,7 +106,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Func<T1, TResult>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -167,7 +180,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Func<T1, T2, TResult>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -238,7 +257,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Func<T1, T2, T3, TResult>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -310,7 +335,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Func<T1, T2, T3, T4, TResult>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -380,6 +411,13 @@ namespace ILRuntime.Runtime.Intepreter
             action = InvokeILMethod;
         }
 
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action<T1>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -444,7 +482,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action<T1, T2>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -511,7 +555,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action<T1, T2, T3>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -580,7 +630,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action<T1, T2, T3, T4>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -652,7 +708,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action<T1, T2, T3, T4, T5>);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -712,7 +774,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             action = InvokeILMethod;
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                return typeof(Action);
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -764,7 +832,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             
         }
-
+        public override Type NativeDelegateType
+        {
+            get
+            {
+                throw new NotSupportedException();
+            }
+        }
         public override Delegate Delegate
         {
             get
@@ -817,6 +891,8 @@ namespace ILRuntime.Runtime.Intepreter
 
         public abstract Delegate Delegate { get; }
 
+        public abstract Type NativeDelegateType { get; }
+
         public IDelegateAdapter Next { get { return next; } }
 
         public ILTypeInstance Instance { get { return instance; } }
@@ -863,6 +939,11 @@ namespace ILRuntime.Runtime.Intepreter
             if (method.HasThis)
                 esp = ILIntepreter.PushObject(esp, mStack, instance);
             int paramCnt = method.ParameterCount;
+            if (method.IsExtend)
+            {
+                esp = ILIntepreter.PushObject(esp, mStack, instance);
+                paramCnt--;
+            }
             bool useRegister = method.ShouldUseRegisterVM;
             for (int i = paramCnt; i > 0; i--)
             {
@@ -892,6 +973,10 @@ namespace ILRuntime.Runtime.Intepreter
         unsafe StackObject* ClearStack(ILIntepreter intp, StackObject* esp, StackObject* ebp, IList<object> mStack)
         {
             int paramCnt = method.ParameterCount;
+            if (method.IsExtend)//如果是拓展方法,退一位
+            {
+                paramCnt--;
+            }
             object retObj = null;
             StackObject retSObj = StackObject.Null;
             bool hasReturn = method.ReturnType != appdomain.VoidType;
@@ -1008,16 +1093,40 @@ namespace ILRuntime.Runtime.Intepreter
         {
             if (type.IsDelegate)
             {
-                var im = type.GetMethod("Invoke", method.ParameterCount);
+                var method_count = method.IsExtend ? method.ParameterCount - 1 : method.ParameterCount;
+                var im = type.GetMethod("Invoke", method_count);
+                if (im == null)
+                {
+                    return false;
+                }
+                var ret_type = im.ReturnType;
+                if (im.ReturnType != appdomain.VoidType && type.IsGenericInstance)
+                {
+                    ret_type = type.GenericArguments[im.ParameterCount].Value;
+                }
                 if (im.IsDelegateInvoke)
                 {
-                    if (im.ParameterCount == method.ParameterCount && im.ReturnType == method.ReturnType)
+                    if (im.ParameterCount == method_count && ret_type == method.ReturnType)
                     {
-                        for (int i = 0; i < im.ParameterCount; i++)
-                        {
-                            if (im.Parameters[i] != method.Parameters[i])
-                                return false;
-                        }
+                            
+                            for (int i = 0; i < im.ParameterCount; i++)
+                            {
+                                var index = method.IsExtend ? i + 1 : i;
+                                if (type.IsGenericInstance)
+                                {
+                                    if (method.Parameters[index] != type.GenericArguments[i].Value)
+                                    {
+                                        return false;
+                                    }
+                                }
+                                else
+                                {
+                                    if (im.Parameters[i] != method.Parameters[index])
+                                        return false;
+                                }
+                              
+                            }
+
                         return true;
                     }
                     else
@@ -1108,7 +1217,7 @@ namespace ILRuntime.Runtime.Intepreter
     }
 
     unsafe interface IDelegateAdapter
-    {
+    {        Type NativeDelegateType { get; }
         Delegate Delegate { get; }
         IDelegateAdapter Next { get; }
         ILTypeInstance Instance { get; }

+ 43 - 37
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/ILIntepreter.cs

@@ -2650,6 +2650,10 @@ namespace ILRuntime.Runtime.Intepreter
                                                     if (dele == null)
                                                     {
                                                         var invokeMethod = type.GetMethod("Invoke", mi.ParameterCount);
+                                                        if (invokeMethod == null && ilMethod.IsExtend)
+                                                        {
+                                                            invokeMethod = type.GetMethod("Invoke", mi.ParameterCount - 1);
+                                                        }
                                                         dele = domain.DelegateManager.FindDelegateAdapter(
                                                             (ILTypeInstance) ins, ilMethod, invokeMethod);
                                                     }
@@ -2673,13 +2677,24 @@ namespace ILRuntime.Runtime.Intepreter
                                         }
                                         else
                                         {
-                                            a = esp - m.ParameterCount;
+                                            intVal = m.ParameterCount;
+                                            a = esp - intVal;
                                             obj = null;
-                                            bool isValueType = type.IsValueType;
-                                            var tmpIntp = domain.RequestILIntepreter();
-                                            tmpIntp.stack.ResetValueTypePointer();
-                                            esp = tmpIntp.stack.StackBase;
-                                            var tmStack = tmpIntp.stack.ManagedStack;
+                                            bool isValueType = type.IsValueType;                                            
+                                            ILIntepreter tmpIntp;
+                                            IList<object> tmStack;
+                                            if (isValueType && intVal > 0)
+                                            {
+                                                tmpIntp = domain.RequestILIntepreter();
+                                                tmpIntp.stack.ResetValueTypePointer();
+                                                esp = tmpIntp.stack.StackBase;
+                                                tmStack = tmpIntp.stack.ManagedStack;
+                                            }
+                                            else
+                                            {
+                                                tmpIntp = this;
+                                                tmStack = mStack;
+                                            }
                                             if (isValueType)
                                             {
                                                 tmpIntp.stack.AllocValueType(esp, type);
@@ -2694,7 +2709,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 objRef = PushObject(esp, tmStack, obj);//this parameter for constructor
                                             }
                                             esp = objRef;
-                                            for (int i = 0; i < m.ParameterCount; i++)
+                                            for (int i = 0; i < intVal; i++)
                                             {
                                                 tmpIntp.CopyToStack(esp, a + i, mStack, tmStack);
                                                 esp++;
@@ -2708,19 +2723,28 @@ namespace ILRuntime.Runtime.Intepreter
                                                 esp = tmpIntp.Execute((ILMethod)m, esp, out unhandledException);
 
                                             ValueTypeBasePointer = bp;
-                                            for (int i = m.ParameterCount - 1; i >= 0; i--)
+                                            for (int i = intVal - 1; i >= 0; i--)
                                             {
                                                 Free(Add(a, i));
                                             }
                                             if (isValueType)
                                             {
-                                                stack.AllocValueType(a, type);
-                                                CopyStackValueType(esp - 1, a, tmpIntp.stack.ManagedStack, mStack);
+                                                if (intVal > 0)
+                                                {
+                                                    stack.AllocValueType(a, type);
+                                                    CopyStackValueType(esp - 1, a, tmpIntp.stack.ManagedStack, mStack);
+                                                    domain.FreeILIntepreter(tmpIntp);
+                                                }
+                                                else
+                                                {
+                                                    var ins = objRef - 1 - 1;
+                                                    *a = *ins;
+                                                }
                                                 esp = a + 1;
                                             }
                                             else
                                                 esp = PushObject(a, mStack, obj);//new constructedObj
-                                            domain.FreeILIntepreter(tmpIntp);
+                                            
                                         }
                                         if (unhandledException)
                                             returned = true;
@@ -2751,27 +2775,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 var ilMethod = mi as ILMethod;
                                                 if (ilMethod != null)
                                                 {
-                                                    if (ins != null)
-                                                    {
-                                                        dele = ((ILTypeInstance)ins).GetDelegateAdapter(ilMethod);
-                                                        if (dele == null)
-                                                        {
-                                                            var invokeMethod =
-                                                                cm.DeclearingType.GetMethod("Invoke",
-                                                                    mi.ParameterCount);
-                                                            dele = domain.DelegateManager.FindDelegateAdapter(
-                                                                (ILTypeInstance) ins, ilMethod, invokeMethod);
-                                                        }
-                                                    }
-                                                    else
-                                                    {
-                                                        if (ilMethod.DelegateAdapter == null)
-                                                        {
-                                                            var invokeMethod = cm.DeclearingType.GetMethod("Invoke", mi.ParameterCount);
-                                                            ilMethod.DelegateAdapter = domain.DelegateManager.FindDelegateAdapter(null, ilMethod, invokeMethod);
-                                                        }
-                                                        dele = ilMethod.DelegateAdapter;
-                                                    }
+                                                    dele = domain.DelegateManager.FindDelegateAdapter((CLRType)cm.DeclearingType, (ILTypeInstance)ins, ilMethod);
                                                 }
                                                 else
                                                 {
@@ -4454,10 +4458,6 @@ namespace ILRuntime.Runtime.Intepreter
                     }
                     catch (Exception ex)
                     {
-                        if (unhandledException)
-                        {
-                            throw ex;
-                        }
                         if (ehs != null)
                         {
                             int addr = (int)(ip - ptr);
@@ -4523,11 +4523,15 @@ namespace ILRuntime.Runtime.Intepreter
                             {
                                 unhandledException = false;
                                 finallyEndAddress = -1;
-                                lastCaughtEx = new ILRuntimeException(ex.Message, this, method, ex);
+                                lastCaughtEx = ex is ILRuntimeException ? ex : new ILRuntimeException(ex.Message, this, method, ex);
                                 ip = ptr + eh.HandlerStart;
                                 continue;
                             }
                         }
+                        if (unhandledException)
+                        {
+                            throw ex;
+                        }
 
                         unhandledException = true;
                         returned = true;
@@ -5166,7 +5170,9 @@ namespace ILRuntime.Runtime.Intepreter
             if (obj == null)
                 arr.SetValue(null, idx);
             else
+            {
                 arr.SetValue(arr.GetType().GetElementType().CheckCLRTypes(obj), idx);
+            }
         }
 
         void StoreIntValueToArray(Array arr, StackObject* val, StackObject* idx)

+ 34 - 14
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs

@@ -21,9 +21,8 @@ namespace ILRuntime.Runtime.Intepreter
             for (int i = 0; i < fields.Length; i++)
             {
                 var ft = type.StaticFieldTypes[i];
-                var t = ft.TypeForCLR;
                 managedObjs.Add(null);
-                StackObject.Initialized(ref fields[i], i, t, ft, managedObjs);
+                StackObject.Initialized(ref fields[i], i, ft, managedObjs);
             }
             int idx = 0;
             foreach (var i in type.TypeDefinition.Fields)
@@ -136,6 +135,7 @@ namespace ILRuntime.Runtime.Intepreter
         protected StackObject[] fields;
         protected IList<object> managedObjs;
         object clrInstance;
+        ulong valueTypeMask;
         Dictionary<ILMethod, IDelegateAdapter> delegates;
 
         public ILType Type
@@ -176,8 +176,9 @@ namespace ILRuntime.Runtime.Intepreter
         {
             this.type = type;
             fields = new StackObject[type.TotalFieldCount];
-            managedObjs = new List<object>(fields.Length);
-            for (int i = 0; i < fields.Length; i++)
+            var cnt = fields.Length;
+            managedObjs = new List<object>(cnt);
+            for (int i = 0; i < cnt; i++)
             {
                 managedObjs.Add(null);
             }
@@ -405,8 +406,13 @@ namespace ILRuntime.Runtime.Intepreter
         {
             for (int i = 0; i < type.FieldTypes.Length; i++)
             {
+                var idx = type.FieldStartIndex + i;
                 var ft = type.FieldTypes[i];
-                StackObject.Initialized(ref fields[type.FieldStartIndex + i], type.FieldStartIndex + i, ft.TypeForCLR, ft, managedObjs);
+                if (ft.IsValueType && idx < 64)
+                {
+                    valueTypeMask |= (ulong)1 << idx;
+                }
+                StackObject.Initialized(ref fields[idx], idx, ft, managedObjs);
             }
             if (type.BaseType != null && type.BaseType is ILType)
                 InitializeFields((ILType)type.BaseType);
@@ -471,16 +477,30 @@ namespace ILRuntime.Runtime.Intepreter
             if (field.ObjectType >= ObjectTypes.Object)
             {
                 var obj = managedObjs[fieldIdx];
-                if (obj != null)
+                if (obj != null && (fieldIdx >= 64 || ((valueTypeMask & ((ulong)1 << fieldIdx)) != 0)))
                 {
-                    var ot = obj.GetType();
-                    ValueTypeBinder binder;
-                    if (ot.IsValueType && type.AppDomain.ValueTypeBinders.TryGetValue(ot, out binder))
+                    if (obj is ILTypeInstance)
                     {
-                        intp.AllocValueType(esp, binder.CLRType);
-                        var dst = ILIntepreter.ResolveReference(esp);
-                        binder.CopyValueTypeToStack(obj, dst, managedStack);
-                        return;
+                        ILTypeInstance ili = (ILTypeInstance)obj;
+                        if (ili.type.IsValueType)
+                        {
+                            intp.AllocValueType(esp, ili.type);
+                            var dst = ILIntepreter.ResolveReference(esp);
+                            ili.CopyValueTypeToStack(dst, managedStack);
+                            return;
+                        }
+                    }
+                    else
+                    {
+                        var ot = obj.GetType();
+                        ValueTypeBinder binder;
+                        if (ot.IsValueType && type.AppDomain.ValueTypeBinders.TryGetValue(ot, out binder))
+                        {
+                            intp.AllocValueType(esp, binder.CLRType);
+                            var dst = ILIntepreter.ResolveReference(esp);
+                            binder.CopyValueTypeToStack(obj, dst, managedStack);
+                            return;
+                        }
                     }
                 }
                 *esp = field;
@@ -544,7 +564,7 @@ namespace ILRuntime.Runtime.Intepreter
                 if (fieldIdx < maxIdx && fieldIdx >= curType.FieldStartIndex)
                 {
                     var ft = curType.FieldTypes[fieldIdx - curType.FieldStartIndex];
-                    StackObject.Initialized(ref fields[fieldIdx], fieldIdx, ft.TypeForCLR, ft, managedObjs);
+                    StackObject.Initialized(ref fields[fieldIdx], fieldIdx, ft, managedObjs);
                     return;
                 }
                 else

+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs

@@ -3,7 +3,6 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading;
-using System.Threading.Tasks;
 using ILRuntime.CLR.Method;
 
 namespace ILRuntime.Runtime.Intepreter.RegisterVM

+ 1 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/CodeBasicBlock.cs

@@ -109,7 +109,7 @@ namespace ILRuntime.Runtime.Intepreter.RegisterVM
                     }
                 }
                 cur.AddInstruction(i);
-                if (i.OpCode.Code == Code.Switch || i.OpCode.Code == Code.Throw || i.OpCode.OperandType == OperandType.InlineBrTarget || i.OpCode.OperandType == OperandType.ShortInlineBrTarget)
+                if (i.OpCode.Code == Code.Switch || i.OpCode.Code == Code.Throw || i.OpCode.OperandType == OperandType.InlineBrTarget || i.OpCode.OperandType == OperandType.ShortInlineBrTarget || i.OpCode.Code == Code.Endfinally)
                 {
                     if (cur.entry != null)
                     {

+ 10 - 26
Unity/Assets/ThirdParty/ILRuntime/Runtime/Intepreter/RegisterVM/ILIntepreter.Register.cs

@@ -3263,6 +3263,10 @@ namespace ILRuntime.Runtime.Intepreter
                                                     if (dele == null)
                                                     {
                                                         var invokeMethod = type.GetMethod("Invoke", mi.ParameterCount);
+                                                        if (invokeMethod == null && ilMethod.IsExtend)
+                                                        {
+                                                            invokeMethod = type.GetMethod("Invoke", mi.ParameterCount - 1);
+                                                        }
                                                         dele = domain.DelegateManager.FindDelegateAdapter(
                                                             (ILTypeInstance)ins, ilMethod, invokeMethod);
                                                     }
@@ -3368,27 +3372,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 var ilMethod = mi as ILMethod;
                                                 if (ilMethod != null)
                                                 {
-                                                    if (ins != null)
-                                                    {
-                                                        dele = ((ILTypeInstance)ins).GetDelegateAdapter(ilMethod);
-                                                        if (dele == null)
-                                                        {
-                                                            var invokeMethod =
-                                                                cm.DeclearingType.GetMethod("Invoke",
-                                                                    mi.ParameterCount);
-                                                            dele = domain.DelegateManager.FindDelegateAdapter(
-                                                                (ILTypeInstance)ins, ilMethod, invokeMethod);
-                                                        }
-                                                    }
-                                                    else
-                                                    {
-                                                        if (ilMethod.DelegateAdapter == null)
-                                                        {
-                                                            var invokeMethod = cm.DeclearingType.GetMethod("Invoke", mi.ParameterCount);
-                                                            ilMethod.DelegateAdapter = domain.DelegateManager.FindDelegateAdapter(null, ilMethod, invokeMethod);
-                                                        }
-                                                        dele = ilMethod.DelegateAdapter;
-                                                    }
+                                                    dele = domain.DelegateManager.FindDelegateAdapter((CLRType)cm.DeclearingType, (ILTypeInstance)ins, ilMethod);
                                                 }
                                                 else
                                                 {
@@ -5079,10 +5063,6 @@ namespace ILRuntime.Runtime.Intepreter
                     }
                     catch (Exception ex)
                     {
-                        if (unhandledException)
-                        {
-                            throw ex;
-                        }
                         if (ehs != null)
                         {
                             int addr = (int)(ip - ptr);
@@ -5149,11 +5129,15 @@ namespace ILRuntime.Runtime.Intepreter
                             {
                                 unhandledException = false;
                                 finallyEndAddress = -1;
-                                lastCaughtEx = new ILRuntimeException(ex.Message, this, method, ex);
+                                lastCaughtEx = ex is ILRuntimeException ? ex : new ILRuntimeException(ex.Message, this, method, ex);
                                 ip = ptr + eh.HandlerStart;
                                 continue;
                             }
                         }
+                        if (unhandledException)
+                        {
+                            throw ex;
+                        }
 
                         unhandledException = true;
                         returned = true;

+ 3 - 30
Unity/Assets/ThirdParty/ILRuntime/Runtime/Stack/StackObject.cs

@@ -120,36 +120,11 @@ namespace ILRuntime.Runtime.Stack
             }
         }
 
-        public unsafe static void Initialized(ref StackObject esp, int idx, Type t, IType fieldType, IList<object> mStack)
+        public unsafe static void Initialized(ref StackObject esp, int idx, IType fieldType, IList<object> mStack)
         {
-            if (t.IsPrimitive)
+            if (fieldType.IsPrimitive)
             {
-                if (t == typeof(int) || t == typeof(uint) || t == typeof(short) || t == typeof(ushort) || t == typeof(byte) || t == typeof(sbyte) || t == typeof(char) || t == typeof(bool))
-                {
-                    esp.ObjectType = ObjectTypes.Integer;
-                    esp.Value = 0;
-                    esp.ValueLow = 0;
-                }
-                else if (t == typeof(long) || t == typeof(ulong))
-                {
-                    esp.ObjectType = ObjectTypes.Long;
-                    esp.Value = 0;
-                    esp.ValueLow = 0;
-                }
-                else if (t == typeof(float))
-                {
-                    esp.ObjectType = ObjectTypes.Float;
-                    esp.Value = 0;
-                    esp.ValueLow = 0;
-                }
-                else if (t == typeof(double))
-                {
-                    esp.ObjectType = ObjectTypes.Double;
-                    esp.Value = 0;
-                    esp.ValueLow = 0;
-                }
-                else
-                    throw new NotImplementedException();
+                esp = fieldType.DefaultObject;
             }
             else
             {
@@ -193,8 +168,6 @@ namespace ILRuntime.Runtime.Stack
         //IL2CPP can't process esp->Initialized() properly, so I can only use static function for this
         public unsafe static void Initialized(StackObject* esp, IType type)
         {
-            var t = type.TypeForCLR;
-            
             if (type.IsPrimitive)
             {
                 *esp = type.DefaultObject;

+ 0 - 1
Unity/Assets/ThirdParty/ILRuntime/Runtime/Stack/StackObjectAllocator.cs

@@ -3,7 +3,6 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 namespace ILRuntime.Runtime.Stack
 {

+ 1 - 0
Unity/Unity.ThirdParty.csproj

@@ -423,6 +423,7 @@
      <Compile Include="Assets\ThirdParty\protobuf-net\Meta\ProtoSyntax.cs" />
      <Compile Include="Assets\ThirdParty\protobuf-net\Serializers\UInt16Serializer.cs" />
      <Compile Include="Assets\ThirdParty\ILRuntime\Mono.Cecil\Mono.Cecil\AssemblyFlags.cs" />
+     <Compile Include="Assets\ThirdParty\ILRuntime\CLR\Method\IMethodExtensions.cs" />
      <None Include="Assets\ThirdParty\Unity.ThirdParty.asmdef" />
      <Reference Include="UnityEngine">
      <HintPath>C:/Apps/Unity/2020.3.23f1c1/Editor/Data/Managed/UnityEngine/UnityEngine.dll</HintPath>