|  | 
|  | 1 | +// Licensed to the .NET Foundation under one or more agreements. | 
|  | 2 | +// The .NET Foundation licenses this file to you under the MIT license. | 
|  | 3 | + | 
|  | 4 | +using System.Runtime.InteropServices.JavaScript; | 
|  | 5 | +using System; | 
|  | 6 | +using System.Reflection; | 
|  | 7 | +using System.Text; | 
|  | 8 | +using System.Threading.Tasks; | 
|  | 9 | +using System.Threading; | 
|  | 10 | +using System.IO; | 
|  | 11 | +using System.Runtime.CompilerServices; | 
|  | 12 | +using System.Runtime.InteropServices; | 
|  | 13 | +using System.Collections.Generic; | 
|  | 14 | + | 
|  | 15 | +namespace DebuggerTests | 
|  | 16 | +{ | 
|  | 17 | +    // this is fake implementation of legacy `bind_static_method` | 
|  | 18 | +    // so that we don't have to rewrite all the tests which use it via `invoke_static_method` | 
|  | 19 | +    public sealed partial class BindStaticMethod | 
|  | 20 | +    { | 
|  | 21 | + | 
|  | 22 | +        [JSExport] | 
|  | 23 | +        [return: JSMarshalAs<JSType.Any>()] | 
|  | 24 | +        public static object GetMethodInfo(string monoMethodName) | 
|  | 25 | +        { | 
|  | 26 | +            return GetMethodInfoImpl(monoMethodName); | 
|  | 27 | +        } | 
|  | 28 | + | 
|  | 29 | +        [JSExport] | 
|  | 30 | +        public static unsafe IntPtr GetMonoMethodPtr(string monoMethodName) | 
|  | 31 | +        { | 
|  | 32 | +            var methodInfo = GetMethodInfoImpl(monoMethodName); | 
|  | 33 | +            var temp = new IntPtrAndHandle { methodHandle = methodInfo.MethodHandle }; | 
|  | 34 | +            return temp.ptr; | 
|  | 35 | +        } | 
|  | 36 | + | 
|  | 37 | +        public static MethodInfo GetMethodInfoImpl(string monoMethodName) | 
|  | 38 | +        { | 
|  | 39 | +            ArgumentNullException.ThrowIfNullOrEmpty(monoMethodName, nameof(monoMethodName)); | 
|  | 40 | +            // [debugger-test] DebuggerTests.ArrayTestsClass:ObjectArrayMembers | 
|  | 41 | +            var partsA = monoMethodName.Split(' '); | 
|  | 42 | +            var assemblyName = partsA[0].Substring(1, partsA[0].Length - 2); | 
|  | 43 | +            var partsN = partsA[1].Split(':'); | 
|  | 44 | +            var className = partsN[0]; | 
|  | 45 | +            var methodName = partsN[1]; | 
|  | 46 | + | 
|  | 47 | +            var typeName = $"{className}, {assemblyName}"; | 
|  | 48 | +            Type type = Type.GetType(typeName); | 
|  | 49 | +            if (type == null) | 
|  | 50 | +            { | 
|  | 51 | +                throw new ArgumentException($"Type not found {typeName}"); | 
|  | 52 | +            } | 
|  | 53 | + | 
|  | 54 | +            var method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); | 
|  | 55 | +            if (method == null) | 
|  | 56 | +            { | 
|  | 57 | +                throw new ArgumentException($"Method not found {className}.{methodName}"); | 
|  | 58 | +            } | 
|  | 59 | + | 
|  | 60 | +            return method; | 
|  | 61 | +        } | 
|  | 62 | + | 
|  | 63 | +        [JSExport] | 
|  | 64 | +        public static string GetSignature([JSMarshalAs<JSType.Any>()] object methodInfo) | 
|  | 65 | +        { | 
|  | 66 | +            var method = (MethodInfo)methodInfo; | 
|  | 67 | +            var sb = new StringBuilder("Invoke"); | 
|  | 68 | +            foreach (var p in method.GetParameters()) | 
|  | 69 | +            { | 
|  | 70 | +                sb.Append("_"); | 
|  | 71 | +                if (typeof(Task).IsAssignableFrom(p.ParameterType)) | 
|  | 72 | +                { | 
|  | 73 | +                    sb.Append("Task"); | 
|  | 74 | +                } | 
|  | 75 | +                else if (p.ParameterType.GenericTypeArguments.Length > 0) | 
|  | 76 | +                { | 
|  | 77 | +                    throw new NotImplementedException($"Parameter {p.Name} type {p.ParameterType.FullName}"); | 
|  | 78 | +                } | 
|  | 79 | +                else | 
|  | 80 | +                { | 
|  | 81 | +                    sb.Append(p.ParameterType.Name); | 
|  | 82 | +                } | 
|  | 83 | +            } | 
|  | 84 | + | 
|  | 85 | +            sb.Append("_"); | 
|  | 86 | +            if (typeof(Task).IsAssignableFrom(method.ReturnType)) | 
|  | 87 | +            { | 
|  | 88 | +                sb.Append("Task"); | 
|  | 89 | +            } | 
|  | 90 | +            else if (method.ReturnType.GenericTypeArguments.Length > 0) | 
|  | 91 | +            { | 
|  | 92 | +                throw new NotImplementedException($"Method return type {method.ReturnType.FullName}"); | 
|  | 93 | +            } | 
|  | 94 | +            else | 
|  | 95 | +            { | 
|  | 96 | +                sb.Append(method.ReturnType.Name); | 
|  | 97 | +            } | 
|  | 98 | + | 
|  | 99 | +            return sb.ToString(); | 
|  | 100 | +        } | 
|  | 101 | + | 
|  | 102 | +        [JSExport] | 
|  | 103 | +        public static void Invoke_Void([JSMarshalAs<JSType.Any>()] object methodInfo) | 
|  | 104 | +        { | 
|  | 105 | +            var method = (MethodInfo)methodInfo; | 
|  | 106 | +            method.Invoke(null, null); | 
|  | 107 | +        } | 
|  | 108 | + | 
|  | 109 | +        [JSExport] | 
|  | 110 | +        public static Task Invoke_Task([JSMarshalAs<JSType.Any>()] object methodInfo) | 
|  | 111 | +        { | 
|  | 112 | +            var method = (MethodInfo)methodInfo; | 
|  | 113 | +            return (Task)method.Invoke(null, null); | 
|  | 114 | +        } | 
|  | 115 | + | 
|  | 116 | +        [JSExport] | 
|  | 117 | +        public static string Invoke_String([JSMarshalAs<JSType.Any>()] object methodInfo) | 
|  | 118 | +        { | 
|  | 119 | +            var method = (MethodInfo)methodInfo; | 
|  | 120 | +            return (string)method.Invoke(null, null); | 
|  | 121 | +        } | 
|  | 122 | + | 
|  | 123 | +        [JSExport] | 
|  | 124 | +        public static void Invoke_Boolean_Void([JSMarshalAs<JSType.Any>()] object methodInfo, bool p1) | 
|  | 125 | +        { | 
|  | 126 | +            var method = (MethodInfo)methodInfo; | 
|  | 127 | +            method.Invoke(null, new object[] { p1 }); | 
|  | 128 | +        } | 
|  | 129 | + | 
|  | 130 | +        [JSExport] | 
|  | 131 | +        public static Task Invoke_Boolean_Task([JSMarshalAs<JSType.Any>()] object methodInfo, bool p1) | 
|  | 132 | +        { | 
|  | 133 | +            var method = (MethodInfo)methodInfo; | 
|  | 134 | +            return (Task)method.Invoke(null, new object[] { p1 }); | 
|  | 135 | +        } | 
|  | 136 | + | 
|  | 137 | +        [JSExport] | 
|  | 138 | +        public static void Invoke_Int32_Void([JSMarshalAs<JSType.Any>()] object methodInfo, int p1) | 
|  | 139 | +        { | 
|  | 140 | +            var method = (MethodInfo)methodInfo; | 
|  | 141 | +            method.Invoke(null, new object[] { p1 }); | 
|  | 142 | +        } | 
|  | 143 | + | 
|  | 144 | +        [JSExport] | 
|  | 145 | +        public static void Invoke_Int32_Int32_Void([JSMarshalAs<JSType.Any>()] object methodInfo, int p1, int p2) | 
|  | 146 | +        { | 
|  | 147 | +            var method = (MethodInfo)methodInfo; | 
|  | 148 | +            method.Invoke(null, new object[] { p1, p2 }); | 
|  | 149 | +        } | 
|  | 150 | + | 
|  | 151 | +        [JSExport] | 
|  | 152 | +        public static void Invoke_Int32_Int32_Int32_Void([JSMarshalAs<JSType.Any>()] object methodInfo, int p1, int p2, int p3) | 
|  | 153 | +        { | 
|  | 154 | +            var method = (MethodInfo)methodInfo; | 
|  | 155 | +            method.Invoke(null, new object[] { p1, p2, p3 }); | 
|  | 156 | +        } | 
|  | 157 | + | 
|  | 158 | +        [JSExport] | 
|  | 159 | +        public static int Invoke_Int32([JSMarshalAs<JSType.Any>()] object methodInfo) | 
|  | 160 | +        { | 
|  | 161 | +            var method = (MethodInfo)methodInfo; | 
|  | 162 | +            return (int)method.Invoke(null, null); | 
|  | 163 | +        } | 
|  | 164 | + | 
|  | 165 | +        [JSExport] | 
|  | 166 | +        public static int Invoke_Int32_Int32([JSMarshalAs<JSType.Any>()] object methodInfo, int p1) | 
|  | 167 | +        { | 
|  | 168 | +            var method = (MethodInfo)methodInfo; | 
|  | 169 | +            return (int)method.Invoke(null, new object[] { p1 }); | 
|  | 170 | +        } | 
|  | 171 | + | 
|  | 172 | +        [JSExport] | 
|  | 173 | +        public static int Invoke_Int32_Int32_Int32([JSMarshalAs<JSType.Any>()] object methodInfo, int p1, int p2) | 
|  | 174 | +        { | 
|  | 175 | +            var method = (MethodInfo)methodInfo; | 
|  | 176 | +            return (int)method.Invoke(null, new object[] { p1, p2 }); | 
|  | 177 | +        } | 
|  | 178 | + | 
|  | 179 | +        [JSExport] | 
|  | 180 | +        public static void Invoke_String_Void([JSMarshalAs<JSType.Any>()] object methodInfo, string p1) | 
|  | 181 | +        { | 
|  | 182 | +            var method = (MethodInfo)methodInfo; | 
|  | 183 | +            method.Invoke(null, new object[] { p1 }); | 
|  | 184 | +        } | 
|  | 185 | + | 
|  | 186 | +        [JSExport] | 
|  | 187 | +        public static void Invoke_String_String_Void([JSMarshalAs<JSType.Any>()] object methodInfo, string p1, string p2) | 
|  | 188 | +        { | 
|  | 189 | +            var method = (MethodInfo)methodInfo; | 
|  | 190 | +            method.Invoke(null, new object[] { p1, p2 }); | 
|  | 191 | +        } | 
|  | 192 | + | 
|  | 193 | +        [JSExport] | 
|  | 194 | +        public static void Invoke_String_String_String_String_Void([JSMarshalAs<JSType.Any>()] object methodInfo, string p1, string p2, string p3, string p4) | 
|  | 195 | +        { | 
|  | 196 | +            var method = (MethodInfo)methodInfo; | 
|  | 197 | +            method.Invoke(null, new object[] { p1, p2, p3, p4 }); | 
|  | 198 | +        } | 
|  | 199 | + | 
|  | 200 | +        [JSExport] | 
|  | 201 | +        public static string Invoke_String_String_String([JSMarshalAs<JSType.Any>()] object methodInfo, string p1, string p2) | 
|  | 202 | +        { | 
|  | 203 | +            var method = (MethodInfo)methodInfo; | 
|  | 204 | +            return (string)method.Invoke(null, new object[] { p1, p2 }); | 
|  | 205 | +        } | 
|  | 206 | + | 
|  | 207 | +        [JSExport] | 
|  | 208 | +        public static void Invoke_String_String_String_String_String_String_String_String_Void([JSMarshalAs<JSType.Any>()] object methodInfo, string p1, string p2, string p3, string p4, string p5, string p6, string p7, string p8) | 
|  | 209 | +        { | 
|  | 210 | +            var method = (MethodInfo)methodInfo; | 
|  | 211 | +            method.Invoke(null, new object[] { p1, p2, p3, p4, p5, p6, p7, p8 }); | 
|  | 212 | +        } | 
|  | 213 | + | 
|  | 214 | +        [StructLayout(LayoutKind.Explicit)] | 
|  | 215 | +        private struct IntPtrAndHandle | 
|  | 216 | +        { | 
|  | 217 | +            [FieldOffset(0)] | 
|  | 218 | +            internal IntPtr ptr; | 
|  | 219 | + | 
|  | 220 | +            [FieldOffset(0)] | 
|  | 221 | +            internal RuntimeMethodHandle methodHandle; | 
|  | 222 | + | 
|  | 223 | +            [FieldOffset(0)] | 
|  | 224 | +            internal RuntimeTypeHandle typeHandle; | 
|  | 225 | +        } | 
|  | 226 | +    } | 
|  | 227 | +} | 
0 commit comments