forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemberReferenceResolver.cs
More file actions
799 lines (720 loc) · 40.5 KB
/
MemberReferenceResolver.cs
File metadata and controls
799 lines (720 loc) · 40.5 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Net.WebSockets;
using BrowserDebugProxy;
using System.Globalization;
namespace Microsoft.WebAssembly.Diagnostics
{
internal sealed class MemberReferenceResolver
{
private static int evaluationResultObjectId;
private SessionId sessionId;
private int scopeId;
private MonoProxy proxy;
private ExecutionContext context;
private PerScopeCache scopeCache;
private ILogger logger;
private bool localsFetched;
private int linqTypeId;
public MemberReferenceResolver(MonoProxy proxy, ExecutionContext ctx, SessionId sessionId, int scopeId, ILogger logger)
{
this.sessionId = sessionId;
this.scopeId = scopeId;
this.proxy = proxy;
this.context = ctx;
this.logger = logger;
scopeCache = ctx.GetCacheForScope(scopeId);
linqTypeId = -1;
}
public MemberReferenceResolver(MonoProxy proxy, ExecutionContext ctx, SessionId sessionId, JArray objectValues, ILogger logger)
{
this.sessionId = sessionId;
scopeId = -1;
this.proxy = proxy;
this.context = ctx;
this.logger = logger;
scopeCache = new PerScopeCache(objectValues);
localsFetched = true;
linqTypeId = -1;
}
public async Task<JObject> GetValueFromObject(JToken objRet, CancellationToken token)
{
if (objRet["value"]?["className"]?.Value<string>() == "System.Exception")
{
if (DotnetObjectId.TryParse(objRet?["value"]?["objectId"]?.Value<string>(), out DotnetObjectId objectId))
{
GetMembersResult exceptionObject = await MemberObjectsExplorer.GetTypeMemberValues(context.SdbAgent, objectId, GetObjectCommandOptions.WithProperties | GetObjectCommandOptions.OwnProperties, token);
var exceptionObjectMessage = exceptionObject.FirstOrDefault(attr => attr["name"].Value<string>().Equals("_message"));
exceptionObjectMessage["value"]["value"] = objRet["value"]?["className"]?.Value<string>() + ": " + exceptionObjectMessage["value"]?["value"]?.Value<string>();
return exceptionObjectMessage["value"]?.Value<JObject>();
}
return objRet["value"]?.Value<JObject>();
}
if (objRet["value"]?.Value<JObject>() != null)
return objRet["value"]?.Value<JObject>();
if (objRet["get"]?.Value<JObject>() != null &&
DotnetObjectId.TryParse(objRet?["get"]?["objectId"]?.Value<string>(), out DotnetObjectId getterObjectId))
{
var ret = await context.SdbAgent.InvokeMethod(getterObjectId, token);
return await GetValueFromObject(ret, token);
}
return null;
}
public async Task<(JObject containerObject, ArraySegment<string> remaining)> ResolveStaticMembersInStaticTypes(ArraySegment<string> expressionParts, CancellationToken token)
{
var store = await proxy.LoadStore(sessionId, false, token);
var methodInfo = context.CallStack.FirstOrDefault(s => s.Id == scopeId)?.Method?.Info;
if (methodInfo == null)
return (null, null);
string[] parts = expressionParts.ToArray();
string fullName = methodInfo.IsAsync == 0 ? methodInfo.TypeInfo.FullName : StripAsyncPartOfFullName(methodInfo.TypeInfo.FullName);
string[] fullNameParts = fullName.Split(".", StringSplitOptions.TrimEntries).ToArray();
for (int i = 0; i < fullNameParts.Length; i++)
{
string[] fullNamePrefix = fullNameParts[..^i];
var (memberObject, remaining) = await FindStaticMemberMatchingParts(parts, fullNamePrefix);
if (memberObject != null)
return (memberObject, remaining);
}
return await FindStaticMemberMatchingParts(parts);
async Task<(JObject, ArraySegment<string>)> FindStaticMemberMatchingParts(string[] parts, string[] fullNameParts = null)
{
string classNameToFind = fullNameParts == null ? "" : string.Join(".", fullNameParts);
int typeId = -1;
for (int i = 0; i < parts.Length; i++)
{
if (!string.IsNullOrEmpty(methodInfo.TypeInfo.Namespace))
{
typeId = await FindStaticTypeId(methodInfo.TypeInfo.Namespace + "." + classNameToFind);
if (typeId != -1)
continue;
}
typeId = await FindStaticTypeId(classNameToFind);
string part = parts[i];
if (typeId != -1)
{
JObject memberObject = await FindStaticMemberInType(classNameToFind, part, typeId);
if (memberObject != null)
{
ArraySegment<string> remaining = null;
if (i < parts.Length - 1)
remaining = parts[i..];
return (memberObject, remaining);
}
// Didn't find a member named `part` in `typeId`.
// Could be a nested type. Let's continue the search
// with `part` added to the type name
typeId = -1;
}
if (classNameToFind.Length > 0)
classNameToFind += ".";
classNameToFind += part;
}
return (null, null);
}
// async function full name has a form: namespaceName.<currentFrame'sMethodName>d__integer
static string StripAsyncPartOfFullName(string fullName)
=> fullName.IndexOf(".<") is int index && index < 0
? fullName
: fullName.Substring(0, index);
async Task<JObject> FindStaticMemberInType(string classNameToFind, string name, int typeId)
{
var fields = await context.SdbAgent.GetTypeFields(typeId, token);
foreach (var field in fields)
{
if (field.Name != name)
continue;
var isInitialized = await context.SdbAgent.TypeIsInitialized(typeId, token);
if (isInitialized == 0)
{
isInitialized = await context.SdbAgent.TypeInitialize(typeId, token);
}
try
{
var staticFieldValue = await context.SdbAgent.GetFieldValue(typeId, field.Id, token);
var valueRet = await GetValueFromObject(staticFieldValue, token);
// we need the full name here
valueRet["className"] = classNameToFind;
return valueRet;
}
catch (Exception ex)
{
logger.LogDebug(ex, $"Failed to get value of field {field.Name} on {classNameToFind} " +
$"because {field.Name} is not a static member of {classNameToFind}.");
}
return null;
}
var methodId = await context.SdbAgent.GetPropertyMethodIdByName(typeId, name, token);
if (methodId != -1)
{
using var commandParamsObjWriter = new MonoBinaryWriter();
commandParamsObjWriter.Write(0); //param count
try
{
var retMethod = await context.SdbAgent.InvokeMethod(commandParamsObjWriter.GetParameterBuffer(), methodId, token);
return await GetValueFromObject(retMethod, token);
}
catch (Exception ex)
{
logger.LogDebug(ex, $"Failed to invoke getter of id={methodId} on {classNameToFind}.{name} " +
$"because {name} is not a static member of {classNameToFind}.");
}
}
return null;
}
async Task<int> FindStaticTypeId(string typeName)
{
foreach (var asm in store.assemblies)
{
var type = asm.GetTypeByName(typeName);
if (type == null)
continue;
int id = await context.SdbAgent.GetTypeIdFromToken(await asm.GetDebugId(context.SdbAgent, token), type.Token, token);
if (id != -1)
return id;
}
return -1;
}
}
// Checks Locals, followed by `this`
public async Task<JObject> Resolve(string varName, CancellationToken token)
{
// question mark at the end of expression is invalid
if (varName[^1] == '?')
throw new ReturnAsErrorException($"Expected expression.", "ReferenceError");
//has method calls
if (varName.Contains('('))
return null;
if (scopeCache.MemberReferences.TryGetValue(varName, out JObject ret))
return ret;
if (scopeCache.ObjectFields.TryGetValue(varName, out JObject valueRet))
return await GetValueFromObject(valueRet, token);
string[] parts = varName.Split(".", StringSplitOptions.TrimEntries);
if (parts.Length == 0 || string.IsNullOrEmpty(parts[0]))
throw new ReturnAsErrorException($"Failed to resolve expression: {varName}", "ReferenceError");
JObject retObject = await ResolveAsLocalOrThisMember(parts[0]);
bool throwOnNullReference = parts[0][^1] != '?';
if (retObject != null && parts.Length > 1)
retObject = await ResolveAsInstanceMember(parts, retObject, throwOnNullReference);
if (retObject == null)
{
(retObject, ArraySegment<string> remaining) = await ResolveStaticMembersInStaticTypes(parts, token);
if (remaining != null && remaining.Count != 0)
{
if (retObject.IsNullValuedObject())
{
// NRE on null.$remaining
retObject = null;
}
else
{
retObject = await ResolveAsInstanceMember(remaining, retObject, throwOnNullReference);
}
}
}
scopeCache.MemberReferences[varName] = retObject;
return retObject;
async Task<JObject> ResolveAsLocalOrThisMember(string name)
{
if (scopeCache.Locals.Count == 0 && !localsFetched)
{
Result scope_res = await proxy.GetScopeProperties(sessionId, scopeId, token);
if (!scope_res.IsOk)
throw new ExpressionEvaluationFailedException($"BUG: Unable to get properties for scope: {scopeId}. {scope_res}");
localsFetched = true;
}
// remove null-condition, otherwise TryGet by name fails
if (name[^1] == '?' || name[^1] == '!')
name = name.Remove(name.Length - 1);
if (scopeCache.Locals.TryGetValue(name, out JObject obj))
return obj["value"]?.Value<JObject>();
if (!scopeCache.Locals.TryGetValue("this", out JObject objThis))
return null;
if (!DotnetObjectId.TryParse(objThis?["value"]?["objectId"]?.Value<string>(), out DotnetObjectId objectId))
return null;
ValueOrError<GetMembersResult> valueOrError = await proxy.RuntimeGetObjectMembers(sessionId, objectId, null, token);
if (valueOrError.IsError)
{
logger.LogDebug($"ResolveAsLocalOrThisMember failed with : {valueOrError.Error}");
return null;
}
JToken objRet = valueOrError.Value.FirstOrDefault(objPropAttr => objPropAttr["name"].Value<string>() == name);
if (objRet != null)
return await GetValueFromObject(objRet, token);
return null;
}
async Task<JObject> ResolveAsInstanceMember(ArraySegment<string> parts, JObject baseObject, bool throwOnNullReference)
{
JObject resolvedObject = baseObject;
// parts[0] - name of baseObject
for (int i = 1; i < parts.Count; i++)
{
string part = parts[i];
if (part.Length == 0)
return null;
bool hasCurrentPartNullCondition = part[^1] == '?';
// current value of resolvedObject is on parts[i - 1]
if (resolvedObject.IsNullValuedObject())
{
// trying null.$member
if (throwOnNullReference)
throw new ReturnAsErrorException($"Expression threw NullReferenceException trying to access \"{part}\" on a null-valued object.", "ReferenceError");
if (i == parts.Count - 1)
{
// this is not ideal, it returns the last object
// that had objectId and was null-valued,
// so the class/description of object are not of the last part
return resolvedObject;
}
// check if null condition is correctly applied: should we throw or return null-object
throwOnNullReference = !hasCurrentPartNullCondition;
continue;
}
if (!DotnetObjectId.TryParse(resolvedObject?["objectId"]?.Value<string>(), out DotnetObjectId objectId))
{
if (!throwOnNullReference)
throw new ReturnAsErrorException($"Operation '?' not allowed on primitive type - '{parts[i - 1]}'", "ReferenceError");
throw new ReturnAsErrorException($"Cannot find member '{part}' on a primitive type", "ReferenceError");
}
var args = JObject.FromObject(new { forDebuggerDisplayAttribute = true });
ValueOrError<GetMembersResult> valueOrError = await proxy.RuntimeGetObjectMembers(sessionId, objectId, args, token);
if (valueOrError.IsError)
{
logger.LogDebug($"ResolveAsInstanceMember failed with : {valueOrError.Error}");
return null;
}
if (part[^1] == '!' || part[^1] == '?')
part = part.Remove(part.Length - 1);
JToken objRet = valueOrError.Value.FirstOrDefault(objPropAttr => objPropAttr["name"]?.Value<string>() == part);
if (objRet == null)
return null;
resolvedObject = await GetValueFromObject(objRet, token);
if (resolvedObject == null)
return null;
throwOnNullReference = !hasCurrentPartNullCondition;
}
return resolvedObject;
}
}
public async Task<JObject> Resolve(ElementAccessExpressionSyntax elementAccess, Dictionary<string, JObject> memberAccessValues, JObject indexObject, List<string> variableDefinitions, CancellationToken token)
{
try
{
JObject rootObject = null;
string elementAccessStrExpression = elementAccess.Expression.ToString();
var multiDimensionalArray = false;
rootObject = await Resolve(elementAccessStrExpression, token);
if (rootObject == null)
{
rootObject = indexObject;
indexObject = null;
}
if (rootObject != null)
{
string elementIdxStr = null;
int elementIdx = 0;
var elementAccessStr = elementAccess.ToString();
// x[1] or x[a] or x[a.b]
if (indexObject == null)
{
if (elementAccess.ArgumentList != null)
{
for (int i = 0 ; i < elementAccess.ArgumentList.Arguments.Count; i++)
{
var arg = elementAccess.ArgumentList.Arguments[i];
if (i != 0)
{
elementIdxStr += ", ";
multiDimensionalArray = true;
}
// e.g. x[1]
if (arg.Expression is LiteralExpressionSyntax)
{
var argParm = arg.Expression as LiteralExpressionSyntax;
elementIdxStr += argParm.ToString();
}
// e.g. x[a] or x[a.b]
else if (arg.Expression is IdentifierNameSyntax)
{
var argParm = arg.Expression as IdentifierNameSyntax;
// x[a.b]
memberAccessValues.TryGetValue(argParm.Identifier.Text, out indexObject);
// x[a]
indexObject ??= await Resolve(argParm.Identifier.Text, token);
elementIdxStr += indexObject["value"].ToString();
}
// indexing with expressions, e.g. x[a + 1]
else
{
string expression = arg.ToString();
indexObject = await ExpressionEvaluator.EvaluateSimpleExpression(this, expression, expression, variableDefinitions, logger, token);
string type = indexObject["type"].Value<string>();
if (type != "number")
throw new InvalidOperationException($"Cannot index with an object of type '{type}'");
elementIdxStr += indexObject["value"].ToString();
}
}
}
}
// e.g. x[a[0]], x[a[b[1]]] etc.
else
{
elementIdxStr = indexObject["value"].ToString();
}
if (elementIdxStr != null)
{
var type = rootObject?["type"]?.Value<string>();
if (!DotnetObjectId.TryParse(rootObject?["objectId"]?.Value<string>(), out DotnetObjectId objectId))
throw new InvalidOperationException($"Cannot apply indexing with [] to a primitive object of type '{type}'");
switch (objectId.Scheme)
{
case "array":
rootObject["value"] = await context.SdbAgent.GetArrayValues(objectId.Value, token);
if (!multiDimensionalArray)
{
int.TryParse(elementIdxStr, out elementIdx);
return (JObject)rootObject["value"][elementIdx]["value"];
}
else
{
return (JObject)(((JArray)rootObject["value"]).FirstOrDefault(x => x["name"].Value<string>() == elementIdxStr)["value"]);
}
case "object":
if (multiDimensionalArray)
throw new InvalidOperationException($"Cannot apply indexing with [,] to an object of type '{type}'");
int.TryParse(elementIdxStr, out elementIdx);
if (type == "string")
{
// ToArray() does not exist on string
var eaExpressionFormatted = elementAccessStrExpression.Replace('.', '_'); // instance_str
variableDefinitions.Add(ExpressionEvaluator.ConvertJSToCSharpLocalVariableAssignment(eaExpressionFormatted, rootObject));
var eaFormatted = elementAccessStr.Replace('.', '_'); // instance_str[1]
return await ExpressionEvaluator.EvaluateSimpleExpression(this, eaFormatted, elementAccessStr, variableDefinitions, logger, token);
}
var typeIds = await context.SdbAgent.GetTypeIdsForObject(objectId.Value, true, token);
// int[] methodIds = await context.SdbAgent.GetMethodIdsByName(typeIds[0], "ToArray", token);
// int allowedParamCnt = 0;
// if (methodIds == null)
// {
int allowedParamCnt = 1;
int[] methodIds = await context.SdbAgent.GetMethodIdsByName(typeIds[0], "get_Item", token);
if (methodIds == null)
throw new InvalidOperationException($"Type '{rootObject?["className"]?.Value<string>()}' cannot be indexed.");
// }
// ToArray / get_Item should not have an overload, but if user defined it, take the default one: without params / with one param: key
int toArrayId = methodIds[0];
if (methodIds.Length > 1)
{
Console.WriteLine($"methodIds.Length > 1");
foreach (var methodId in methodIds)
{
MethodInfoWithDebugInformation methodInfo = await context.SdbAgent.GetMethodInfo(methodId, token);
ParameterInfo[] paramInfo = methodInfo.GetParametersInfo();
Console.WriteLine($"name = {methodInfo.Name}; paramInfo = {paramInfo.Length}");
if (paramInfo.Length == allowedParamCnt)
{
toArrayId = methodId;
break;
}
}
}
try
{
Console.WriteLine($"methodIds.Length == 1");
Console.WriteLine($"rootObject = {rootObject}, elementIdxStr = {elementIdxStr}");
if (allowedParamCnt == 0) // ToArray result
{
JObject toArrayRetObj = await context.SdbAgent.InvokeMethod(objectId.Value, toArrayId, isValueType: false, token);
rootObject = await GetValueFromObject(toArrayRetObj, token);
DotnetObjectId.TryParse(rootObject?["objectId"]?.Value<string>(), out DotnetObjectId arrayObjectId);
rootObject["value"] = await context.SdbAgent.GetArrayValues(arrayObjectId.Value, token);
return (JObject)rootObject["value"][elementIdx]["value"];
}
// get_Item result
using var ctorArgsWriter = new MonoBinaryWriter();
ctorArgsWriter.WriteObj(objectId, context.SdbAgent);
ctorArgsWriter.Write(1); // number of method args
// if we learn how to check properly the indexing obj type and pass elementIdx / elementIdxStr when we should, then ToArray won't be needed
if (!await ctorArgsWriter.WriteConst(ElementType.I4, elementIdx, context.SdbAgent, token)) // string? how do you know?
throw new InternalErrorException($"Unable to write index parameter to invoke the method in the runtime.");
JObject getItemRetObj = await context.SdbAgent.InvokeMethod(ctorArgsWriter.GetParameterBuffer(), toArrayId, token);
return (JObject)getItemRetObj["value"];
}
catch (Exception ex)
{
Console.WriteLine($"ex = {ex}");
throw new InvalidOperationException($"Cannot apply indexing with [] to an object of type '{rootObject?["className"]?.Value<string>()}'");
}
default:
throw new InvalidOperationException($"Cannot apply indexing with [] to an expression of scheme '{objectId.Scheme}'");
}
}
}
return null;
}
catch (Exception ex) when (ex is not ExpressionEvaluationFailedException)
{
throw new ExpressionEvaluationFailedException($"Unable to evaluate element access '{elementAccess}': {ex.Message}", ex);
}
}
public async Task<(JObject, string)> ResolveInvocationInfo(InvocationExpressionSyntax method, CancellationToken token)
{
var methodName = "";
try
{
JObject rootObject = null;
var expr = method.Expression;
if (expr is MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
rootObject = await Resolve(memberAccessExpressionSyntax.Expression.ToString(), token);
methodName = memberAccessExpressionSyntax.Name.ToString();
if (rootObject.IsNullValuedObject())
throw new ExpressionEvaluationFailedException($"Expression '{memberAccessExpressionSyntax}' evaluated to null");
}
else if (expr is IdentifierNameSyntax && scopeCache.ObjectFields.TryGetValue("this", out JObject thisValue))
{
rootObject = await GetValueFromObject(thisValue, token);
methodName = expr.ToString();
}
return (rootObject, methodName);
}
catch (Exception ex) when (ex is not (ExpressionEvaluationFailedException or ReturnAsErrorException))
{
throw new Exception($"Unable to evaluate method '{methodName}'", ex);
}
}
private static readonly string[] primitiveTypes = new string[] { "string", "number", "boolean", "symbol" };
public async Task<JObject> Resolve(InvocationExpressionSyntax method, Dictionary<string, JObject> memberAccessValues, CancellationToken token)
{
(JObject rootObject, string methodName) = await ResolveInvocationInfo(method, token);
if (rootObject == null)
throw new ReturnAsErrorException($"Failed to resolve root object for {method}", "ReferenceError");
// primitives don't have objectId
if (!DotnetObjectId.TryParse(rootObject["objectId"]?.Value<string>(), out DotnetObjectId objectId) &&
primitiveTypes.Contains(rootObject["type"]?.Value<string>()))
return null;
if (method.ArgumentList == null)
throw new InternalErrorException($"Failed to resolve method call for {method}, list of arguments is null.");
bool isExtensionMethod = false;
try
{
List<int> typeIds;
if (objectId.IsValueType)
{
if (!context.SdbAgent.ValueCreator.TryGetValueTypeById(objectId.Value, out ValueTypeClass valueType))
throw new Exception($"Could not find valuetype {objectId}");
typeIds = new List<int>(1) { valueType.TypeId };
}
else
{
typeIds = await context.SdbAgent.GetTypeIdsForObject(objectId.Value, true, token);
}
int[] methodIds = await context.SdbAgent.GetMethodIdsByName(typeIds[0], methodName, token);
if (methodIds == null)
{
//try to search on System.Linq.Enumerable
int methodId = await FindMethodIdOnLinqEnumerable(typeIds, methodName);
if (methodId == 0)
{
var typeName = await context.SdbAgent.GetTypeName(typeIds[0], token);
throw new ReturnAsErrorException($"Method '{methodName}' not found in type '{typeName}'", "ReferenceError");
}
methodIds = new int[] { methodId };
}
// get information about params in all overloads for *methodName*
List<MethodInfoWithDebugInformation> methodInfos = await GetMethodParamInfosForMethods(methodIds);
int passedArgsCnt = method.ArgumentList.Arguments.Count;
int maxMethodParamsCnt = methodInfos.Max(v => v.GetParametersInfo().Length);
if (isExtensionMethod)
{
// implicit *this* parameter
maxMethodParamsCnt--;
}
if (passedArgsCnt > maxMethodParamsCnt)
throw new ReturnAsErrorException($"Unable to evaluate method '{methodName}'. Too many arguments passed.", "ArgumentError");
foreach (var methodInfo in methodInfos)
{
ParameterInfo[] methodParamsInfo = methodInfo.GetParametersInfo();
int methodParamsCnt = isExtensionMethod ? methodParamsInfo.Length - 1 : methodParamsInfo.Length;
int optionalParams = methodParamsInfo.Count(v => v.Value != null);
if (passedArgsCnt > methodParamsCnt || passedArgsCnt < methodParamsCnt - optionalParams)
{
// this overload does not match the number of params passed, try another one
continue;
}
int methodId = methodInfo.DebugId;
using var commandParamsObjWriter = new MonoBinaryWriter();
if (isExtensionMethod)
{
commandParamsObjWriter.Write(methodParamsCnt + 1);
commandParamsObjWriter.WriteObj(objectId, context.SdbAgent);
}
else
{
// instance method
commandParamsObjWriter.WriteObj(objectId, context.SdbAgent);
commandParamsObjWriter.Write(methodParamsCnt);
}
int argIndex = 0;
// explicitly passed arguments
for (; argIndex < passedArgsCnt; argIndex++)
{
var arg = method.ArgumentList.Arguments[argIndex];
if (arg.Expression is LiteralExpressionSyntax literal)
{
if (!await commandParamsObjWriter.WriteConst(literal, context.SdbAgent, token))
throw new InternalErrorException($"Unable to evaluate method '{methodName}'. Unable to write LiteralExpressionSyntax into binary writer.");
}
else if (arg.Expression is IdentifierNameSyntax identifierName)
{
if (!await commandParamsObjWriter.WriteJsonValue(memberAccessValues[identifierName.Identifier.Text], context.SdbAgent, token))
throw new InternalErrorException($"Unable to evaluate method '{methodName}'. Unable to write IdentifierNameSyntax into binary writer.");
}
else
{
throw new InternalErrorException($"Unable to evaluate method '{methodName}'. Unable to write into binary writer, not recognized expression type: {arg.Expression.GetType().Name}");
}
}
// optional arguments that were not overwritten
for (; argIndex < methodParamsCnt; argIndex++)
{
if (!await commandParamsObjWriter.WriteConst(methodParamsInfo[argIndex].TypeCode, methodParamsInfo[argIndex].Value, context.SdbAgent, token))
throw new InternalErrorException($"Unable to write optional parameter {methodParamsInfo[argIndex].Name} value in method '{methodName}' to the mono buffer.");
}
try
{
var retMethod = await context.SdbAgent.InvokeMethod(commandParamsObjWriter.GetParameterBuffer(), methodId, token);
return await GetValueFromObject(retMethod, token);
}
catch
{
// try further methodIds, we're looking for a method with the same type of params that the user passed
logger.LogDebug($"InvokeMethod failed due to parameter type mismatch for {methodName} with {methodParamsCnt} parameters, including {optionalParams} optional.");
continue;
}
}
throw new ReturnAsErrorException($"No implementation of method '{methodName}' matching '{method}' found in type {rootObject["className"]}.", "ArgumentError");
}
catch (Exception ex) when (ex is not (ExpressionEvaluationFailedException or ReturnAsErrorException))
{
throw new ExpressionEvaluationFailedException($"Unable to evaluate method '{method}': {ex.Message}", ex);
}
async Task<int> FindMethodIdOnLinqEnumerable(IList<int> typeIds, string methodName)
{
if (linqTypeId == -1)
{
linqTypeId = await context.SdbAgent.GetTypeByName("System.Linq.Enumerable", token);
if (linqTypeId == 0)
{
logger.LogDebug($"Cannot find type 'System.Linq.Enumerable'");
return 0;
}
}
int[] newMethodIds = await context.SdbAgent.GetMethodIdsByName(linqTypeId, methodName, token);
if (newMethodIds == null)
return 0;
foreach (int typeId in typeIds)
{
List<int> genericTypeArgs = await context.SdbAgent.GetTypeParamsOrArgsForGenericType(typeId, token);
if (genericTypeArgs.Count > 0)
{
isExtensionMethod = true;
return await context.SdbAgent.MakeGenericMethod(newMethodIds[0], genericTypeArgs, token);
}
}
return 0;
}
async Task<List<MethodInfoWithDebugInformation>> GetMethodParamInfosForMethods(int[] methodIds)
{
List<MethodInfoWithDebugInformation> allMethodInfos = new();
for (int i = 0; i < methodIds.Length; i++)
{
var ithMethodInfo = await context.SdbAgent.GetMethodInfo(methodIds[i], token);
if (ithMethodInfo != null)
allMethodInfos.Add(ithMethodInfo);
}
return allMethodInfos;
}
}
public JObject ConvertCSharpToJSType(object v, Type type)
{
if (v is JObject jobj)
return jobj;
if (v is null)
return JObjectValueCreator.CreateNull("<unknown>")?["value"] as JObject;
if (v is Array arr)
{
return CacheEvaluationResult(
JObject.FromObject(
new
{
type = "object",
subtype = "array",
value = new JArray(arr.Cast<object>().Select((val, idx) => JObject.FromObject(
new
{
value = ConvertCSharpToJSType(val, val.GetType()),
name = $"{idx}"
}))),
description = v.ToString(),
className = type.ToString()
}));
}
string typeName = v.GetType().ToString();
jobj = JObjectValueCreator.CreateFromPrimitiveType(v);
return jobj is not null
? jobj["value"] as JObject
: JObjectValueCreator.Create<object>(value: null,
type: "object",
description: v.ToString(),
className: typeName)?["value"] as JObject;
}
private JObject CacheEvaluationResult(JObject value)
{
if (IsDuplicated(value, out JObject duplicate))
return value;
var evalResultId = Interlocked.Increment(ref evaluationResultObjectId);
string id = $"dotnet:evaluationResult:{evalResultId}";
if (!value.TryAdd("objectId", id))
{
logger.LogWarning($"EvaluationResult cache request passed with ID: {value["objectId"].Value<string>()}. Overwritting it with a automatically assigned ID: {id}.");
value["objectId"] = id;
}
scopeCache.EvaluationResults.Add(id, value);
return value;
bool IsDuplicated(JObject er, out JObject duplicate)
{
var type = er["type"].Value<string>();
var subtype = er["subtype"].Value<string>();
var value = er["value"];
var description = er["description"].Value<string>();
var className = er["className"].Value<string>();
duplicate = scopeCache.EvaluationResults.FirstOrDefault(
pair => pair.Value["type"].Value<string>() == type
&& pair.Value["subtype"].Value<string>() == subtype
&& pair.Value["description"].Value<string>() == description
&& pair.Value["className"].Value<string>() == className
&& JToken.DeepEquals(pair.Value["value"], value)).Value;
return duplicate != null;
}
}
public JObject TryGetEvaluationResult(string id)
{
JObject val;
if (!scopeCache.EvaluationResults.TryGetValue(id, out val))
logger.LogError($"EvaluationResult of ID: {id} does not exist in the cache.");
return val;
}
}
}