-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathHostItem.InvokeMethod.cs
More file actions
601 lines (503 loc) · 23.4 KB
/
HostItem.InvokeMethod.cs
File metadata and controls
601 lines (503 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.ClearScript.Util;
using Binder = Microsoft.CSharp.RuntimeBinder.Binder;
namespace Microsoft.ClearScript
{
internal partial class HostItem
{
#region data
private static readonly ConcurrentDictionary<BindSignature, object> coreBindCache = new();
private static long coreBindCount;
#endregion
#region internal members
private object InvokeMethod(string name, object[] args, object[] bindArgs)
{
var typeArgs = GetTypeArgs(args).ToArray();
if (typeArgs.Length > 0)
{
var mergedArgs = args;
var argOffset = typeArgs.Length;
args = args.Skip(argOffset).ToArray();
bindArgs = bindArgs.Skip(argOffset).ToArray();
var result = InvokeMethod(name, typeArgs, args, bindArgs);
for (var index = 0; index < args.Length; index++)
{
mergedArgs[argOffset + index] = args[index];
}
return result;
}
return InvokeMethod(name, typeArgs, args, bindArgs);
}
private object InvokeMethod(string name, Type[] typeArgs, object[] args, object[] bindArgs)
{
var bindResult = BindMethod(name, typeArgs, args, bindArgs);
if (!bindResult.IsSuccess && Target.GetFlags(this).HasAllFlags(HostTargetFlags.AllowExtensionMethods))
{
var targetArg = Target.Target.ToEnumerable();
var extensionArgs = targetArg.Concat(args).ToArray();
var targetBindArg = new object[] { Target };
var extensionBindArgs = targetBindArg.Concat(bindArgs).ToArray();
foreach (var type in ExtensionMethodSummary.Types)
{
var extensionHostItem = (HostItem)Wrap(Engine, HostType.Wrap(type));
var extensionBindResult = extensionHostItem.BindMethod(name, typeArgs, extensionArgs, extensionBindArgs);
if (extensionBindResult.IsSuccess)
{
var result = extensionBindResult.Invoke(extensionHostItem);
for (var index = 1; index < extensionArgs.Length; index++)
{
args[index - 1] = extensionArgs[index];
}
return result;
}
}
}
return bindResult.Invoke(this);
}
private static IEnumerable<Type> GetTypeArgs(object[] args)
{
foreach (var arg in args)
{
var hostType = arg as HostType;
if (hostType is null)
{
yield break;
}
var typeArg = hostType.GetTypeArgNoThrow();
if (typeArg is null)
{
yield break;
}
yield return typeArg;
}
}
private MethodBindResult BindMethod(string name, Type[] typeArgs, object[] args, object[] bindArgs)
{
var bindFlags = GetMethodBindFlags();
// WARNING: BindSignature holds on to the specified typeArgs; subsequent modification
// will result in bugs that are difficult to diagnose. Create a copy if necessary.
var signature = new BindSignature(AccessContext, bindFlags, Target, name, typeArgs, bindArgs);
MethodBindResult result;
if (Engine.TryGetCachedMethodBindResult(signature, out var rawResult))
{
result = MethodBindResult.Create(name, bindFlags, rawResult, Target, args);
}
else
{
var forceReflection = Engine.DisableDynamicBinding;
if (forceReflection)
{
result = MethodBindResult.CreateFailure(() => new MissingMethodException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
}
else
{
result = BindMethodInternal(signature, AccessContext, bindFlags, Target, name, typeArgs, args, bindArgs);
if (!result.IsPreferredMethod(this, name))
{
if (result.IsSuccess)
{
result = MethodBindResult.CreateFailure(() => new MissingMethodException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
}
foreach (var altName in GetAltMethodNames(name, bindFlags))
{
var altResult = BindMethodInternal(null, AccessContext, bindFlags, Target, altName, typeArgs, args, bindArgs);
if (altResult.IsUnblockedMethod(this))
{
result = altResult;
break;
}
}
}
}
if (!result.IsSuccess && (forceReflection || Engine.UseReflectionBindFallback))
{
var reflectionResult = BindMethodUsingReflection(bindFlags, Target, name, typeArgs, args, bindArgs);
if (reflectionResult.IsSuccess || forceReflection)
{
result = reflectionResult;
}
}
Engine.CacheMethodBindResult(signature, result.RawResult);
}
return result;
}
private static MethodBindResult BindMethodInternal(BindSignature signature, Type bindContext, BindingFlags bindFlags, HostTarget target, string name, Type[] typeArgs, object[] args, object[] bindArgs)
{
if (signature is null)
{
// WARNING: BindSignature holds on to the specified typeArgs; subsequent modification
// will result in bugs that are difficult to diagnose. Create a copy if necessary.
signature = new BindSignature(bindContext, bindFlags, target, name, typeArgs, bindArgs);
}
MethodBindResult result;
if (coreBindCache.TryGetValue(signature, out var rawResult))
{
result = MethodBindResult.Create(name, bindFlags, rawResult, target, args);
}
else
{
result = BindMethodCore(bindContext, bindFlags, target, name, typeArgs, args, bindArgs);
coreBindCache.TryAdd(signature, result.RawResult);
}
return result;
}
private static MethodBindResult BindMethodCore(Type bindContext, BindingFlags bindFlags, HostTarget target, string name, Type[] typeArgs, object[] args, object[] bindArgs)
{
Interlocked.Increment(ref coreBindCount);
// create C# member invocation binder
const CSharpBinderFlags binderFlags = CSharpBinderFlags.InvokeSimpleName | CSharpBinderFlags.ResultDiscarded;
var binder = (InvokeMemberBinder)Binder.InvokeMember(binderFlags, name, typeArgs, bindContext, CreateArgInfoEnum(target, bindArgs));
// perform default binding
var rawResult = BindMethodRaw(bindFlags, binder, target, bindArgs);
var result = MethodBindResult.Create(name, bindFlags, rawResult, target, args);
if (!result.IsSuccess && (target is not HostType) && target.Type.IsInterface)
{
// binding through interface failed; try base interfaces
foreach (var interfaceType in target.Type.GetInterfaces())
{
var baseInterfaceTarget = HostObject.Wrap(target.InvokeTarget, interfaceType);
rawResult = BindMethodRaw(bindFlags, binder, baseInterfaceTarget, bindArgs);
var baseInterfaceResult = MethodBindResult.Create(name, bindFlags, rawResult, target, args);
if (baseInterfaceResult.IsSuccess)
{
return baseInterfaceResult;
}
}
// binding through base interfaces failed; try System.Object
var objectTarget = HostObject.Wrap(target.InvokeTarget, typeof(object));
rawResult = BindMethodRaw(bindFlags, binder, objectTarget, bindArgs);
var objectResult = MethodBindResult.Create(name, bindFlags, rawResult, target, args);
if (objectResult.IsSuccess)
{
return objectResult;
}
}
return result;
}
private static object BindMethodRaw(BindingFlags bindFlags, InvokeMemberBinder binder, HostTarget target, object[] bindArgs)
{
var expr = DynamicHelpers.Bind(binder, target, bindArgs).Expression;
// ReSharper disable ConditionIsAlwaysTrueOrFalse
// ReSharper disable HeuristicUnreachableCode
if (expr is null)
{
return new Func<Exception>(() => new MissingMethodException(MiscHelpers.FormatInvariant("The object has no method named '{0}'", binder.Name)));
}
// ReSharper restore HeuristicUnreachableCode
// ReSharper restore ConditionIsAlwaysTrueOrFalse
if (expr.NodeType == ExpressionType.Dynamic)
{
// The binding result is a dynamic call, which is indicative of COM interop. This
// sort of binding is not very useful here; it can't be resolved to a MethodInfo
// instance, and caching it is problematic because it includes argument bindings.
// Falling back to reflection should work in most cases because COM interfaces
// support neither generic nor overloaded methods.
try
{
var method = target.Type.GetMethod(binder.Name, bindFlags);
return (object)method ?? new Func<Exception>(() => new MissingMethodException(MiscHelpers.FormatInvariant("The object has no method named '{0}'", binder.Name)));
}
catch (AmbiguousMatchException exception)
{
return new Func<Exception>(() => new AmbiguousMatchException(exception.Message));
}
}
return (new MethodBindingVisitor(target.InvokeTarget, binder.Name, expr)).Result;
}
private IEnumerable<string> GetAltMethodNames(string name, BindingFlags bindFlags)
{
return GetAltMethodNamesInternal(name, bindFlags).Distinct();
}
private IEnumerable<string> GetAltMethodNamesInternal(string name, BindingFlags bindFlags)
{
foreach (var method in Target.Type.GetScriptableMethods(this, name, bindFlags))
{
var methodName = method.GetShortName();
if (methodName != name)
{
yield return methodName;
}
}
}
private static IEnumerable<CSharpArgumentInfo> CreateArgInfoEnum(HostTarget target, object[] args)
{
if (target is HostType)
{
yield return CreateStaticTypeArgInfo();
}
else
{
yield return CreateArgInfo(target.DynamicInvokeTarget);
}
foreach (var arg in args)
{
yield return CreateArgInfo(arg);
}
}
private static CSharpArgumentInfo CreateArgInfo(object arg)
{
var flags = CSharpArgumentInfoFlags.None;
if (arg is not null)
{
flags |= CSharpArgumentInfoFlags.UseCompileTimeType;
if (arg is HostObject hostObject)
{
if ((hostObject.Type == typeof(int)) || (hostObject.Type.IsValueType && hostObject.Target.IsZero()))
{
flags |= CSharpArgumentInfoFlags.Constant;
}
}
else if (arg is HostVariable hostVariable)
{
if ((hostVariable.Type == typeof(int)) || (hostVariable.Type.IsValueType && hostVariable.Target.IsZero()))
{
flags |= CSharpArgumentInfoFlags.Constant;
}
}
else if (arg is int || arg.IsZero())
{
flags |= CSharpArgumentInfoFlags.Constant;
}
else if (arg is IOutArg)
{
flags |= CSharpArgumentInfoFlags.IsOut;
}
else if (arg is IRefArg)
{
flags |= CSharpArgumentInfoFlags.IsRef;
}
}
return CSharpArgumentInfo.Create(flags, null);
}
private static CSharpArgumentInfo CreateStaticTypeArgInfo()
{
return CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.IsStaticType, null);
}
private MethodBindResult BindMethodUsingReflection(BindingFlags bindFlags, HostTarget hostTarget, string name, Type[] typeArgs, object[] args, object[] bindArgs)
{
var candidates = GetReflectionCandidates(bindFlags, hostTarget, name, typeArgs).Distinct().ToArray();
if (candidates.Length > 0)
{
try
{
var rawResult = TypeHelpers.BindToMember(this, candidates, bindFlags, args, bindArgs);
if (rawResult is not null)
{
return MethodBindResult.Create(name, bindFlags, rawResult, hostTarget, args);
}
}
catch (AmbiguousMatchException)
{
return MethodBindResult.CreateFailure(() => new AmbiguousMatchException(MiscHelpers.FormatInvariant("The object has multiple methods named '{0}' that match the specified arguments", name)));
}
}
return MethodBindResult.CreateFailure(() => new MissingMethodException(MiscHelpers.FormatInvariant("The object has no method named '{0}' that matches the specified arguments", name)));
}
private IEnumerable<MethodInfo> GetReflectionCandidates(BindingFlags bindFlags, HostTarget hostTarget, string name, Type[] typeArgs)
{
foreach (var method in GetReflectionCandidates(bindFlags, hostTarget.Type, name, typeArgs))
{
yield return method;
}
if ((hostTarget is not HostType) && hostTarget.Type.IsInterface)
{
foreach (var interfaceType in hostTarget.Type.GetInterfaces())
{
foreach (var method in GetReflectionCandidates(bindFlags, interfaceType, name, typeArgs))
{
yield return method;
}
}
foreach (var method in GetReflectionCandidates(bindFlags, typeof(object), name, typeArgs))
{
yield return method;
}
}
}
private IEnumerable<MethodInfo> GetReflectionCandidates(BindingFlags bindFlags, Type type, string name, Type[] typeArgs)
{
foreach (var method in type.GetScriptableMethods(this, name, bindFlags))
{
MethodInfo tempMethod = null;
if (method.ContainsGenericParameters)
{
try
{
tempMethod = method.MakeGenericMethod(typeArgs);
}
catch (ArgumentException)
{
continue;
}
catch (NotSupportedException)
{
continue;
}
}
else if (typeArgs.Length < 1)
{
tempMethod = method;
}
if ((tempMethod is not null) && !tempMethod.ContainsGenericParameters)
{
yield return tempMethod;
}
}
}
#endregion
#region unit test support
internal static void ClearCoreBindCache()
{
coreBindCache.Clear();
Interlocked.Exchange(ref coreBindCount, 0);
}
internal static long GetCoreBindCount()
{
return Interlocked.Read(ref coreBindCount);
}
#endregion
#region Nested type: MethodBindResult
private readonly ref struct MethodBindResult
{
private readonly HostTarget hostTarget;
private readonly MethodInfo method;
private readonly object[] args;
private readonly Func<Exception> exceptionFactory;
private static readonly MethodInfo[] reflectionMethods =
{
typeof(object).GetMethod("GetType"),
typeof(System.Runtime.InteropServices._Exception).GetMethod("GetType"),
typeof(Exception).GetMethod("GetType")
};
private MethodBindResult(HostTarget hostTarget, MethodInfo method, object[] args)
{
this.hostTarget = hostTarget;
this.method = method;
this.args = args;
exceptionFactory = null;
}
private MethodBindResult(Func<Exception> exceptionFactory)
{
hostTarget = null;
method = null;
args = null;
this.exceptionFactory = exceptionFactory;
}
public static MethodBindResult Create(string name, BindingFlags bindFlags, object rawResult, HostTarget hostTarget, object[] args)
{
var method = rawResult as MethodInfo;
if (method != null)
{
if (method.IsStatic && !bindFlags.HasAllFlags(BindingFlags.Static))
{
return new MethodBindResult(() => new InvalidOperationException(MiscHelpers.FormatInvariant("Cannot access static method '{0}' in non-static context", method.Name)));
}
return new MethodBindResult(hostTarget, method, args);
}
return new MethodBindResult((rawResult as Func<Exception>) ?? (() => new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name))));
}
public static MethodBindResult CreateFailure(Func<Exception> exceptionFactory) => new(exceptionFactory);
public bool IsSuccess => method != null;
public object RawResult => IsSuccess ? method : exceptionFactory;
public bool IsPreferredMethod(HostItem hostItem, string name)
{
return IsSuccess && IsUnblockedMethod(hostItem) && (method.GetScriptName(hostItem) == name);
}
public bool IsUnblockedMethod(HostItem hostItem)
{
return IsSuccess && !method.IsBlockedFromScript(hostItem, hostItem.DefaultAccess);
}
public object Invoke(HostItem hostItem)
{
if (!IsSuccess)
{
throw exceptionFactory();
}
if (reflectionMethods.Contains(method, MemberComparer<MethodInfo>.Instance))
{
hostItem.Engine.CheckReflection();
}
return InvokeHelpers.InvokeMethod(hostItem, method, hostTarget.InvokeTarget, args, method.GetScriptMemberFlags(hostItem));
}
}
#endregion
#region Nested type: MethodBindingVisitor
private sealed class MethodBindingVisitor : ExpressionVisitor
{
private readonly object target;
private readonly string name;
private readonly List<object> results = new();
public MethodBindingVisitor(object target, string name, Expression expression)
{
this.target = target;
this.name = name;
Visit(expression);
if (results.Count != 1)
{
results.Clear();
AddResult(() => new NotSupportedException(MiscHelpers.FormatInvariant("Invocation of method '{0}' failed (unrecognized binding)", name)));
}
else
{
var method = results[0] as MethodInfo;
if (method is not null)
{
Debug.Assert(method.Name == name);
}
}
}
public object Result => results[0];
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.Name == name)
{
AddResult(node.Method);
}
return base.VisitMethodCall(node);
}
protected override Expression VisitInvocation(InvocationExpression node)
{
if (target is Delegate targetDelegate)
{
var del = DynamicHelpers.Invoke(node.Expression) as Delegate;
if (del == targetDelegate)
{
AddResult(del.GetType().GetMethod("Invoke"));
}
}
return base.VisitInvocation(node);
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (node.NodeType == ExpressionType.Throw)
{
if (DynamicHelpers.Invoke(node.Operand) is Exception)
{
AddResult(() => (Exception)DynamicHelpers.Invoke(node.Operand));
}
}
return base.VisitUnary(node);
}
private void AddResult(MethodInfo method)
{
results.Add(method);
}
private void AddResult(Func<Exception> exceptionFactory)
{
results.Add(exceptionFactory);
}
}
#endregion
}
}