Skip to content

Commit

Permalink
[wasm][debugger] Debuger tests refactor 63667 (#63730)
Browse files Browse the repository at this point in the history
* Removed a dedicated StructGetter class.

* Used DerivedClass in protection level tests + fixed InvokeMethod + reverted StructureGetters removal.

* Exchange ElementType in DotnetObjectId for bool.

* Applied @radical's suggestion to log whole call stack instead of message.

* Applied @lewing's suggestions.

* Merge errors fixed.
  • Loading branch information
ilonatommy committed Mar 30, 2022
1 parent 445168a commit cbf3f9c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 60 deletions.
15 changes: 9 additions & 6 deletions src/mono/wasm/debugger/BrowserDebugProxy/DevToolsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ internal class DotnetObjectId
public string Scheme { get; }
public int Value { get; }
public int SubValue { get; set; }
public bool IsValueType { get; set; }

public static bool TryParse(JToken jToken, out DotnetObjectId objectId) => TryParse(jToken?.Value<string>(), out objectId);

public static bool TryParse(string id, out DotnetObjectId objectId)
{
objectId = null;
try {
try
{
if (id == null)
return false;

Expand All @@ -88,12 +90,13 @@ public static bool TryParse(string id, out DotnetObjectId objectId)
switch (objectId.Scheme)
{
case "methodId":
{
parts = id.Split(":");
if (parts.Length > 3)
if (parts.Length > 4)
{
objectId.SubValue = int.Parse(parts[3]);
break;
}
objectId.IsValueType = parts[4] == "ValueType";
return true;
}
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public async Task<JObject> Resolve(ElementAccessExpressionSyntax elementAccess,
case "object":
var typeIds = await context.SdbAgent.GetTypeIdFromObject(objectId.Value, true, token);
int methodId = await context.SdbAgent.GetMethodIdByName(typeIds[0], "ToArray", token);
var toArrayRetMethod = await context.SdbAgent.InvokeMethodInObject(objectId.Value, methodId, elementAccess.Expression.ToString(), token);
var toArrayRetMethod = await context.SdbAgent.InvokeMethodInObject(objectId, methodId, elementAccess.Expression.ToString(), token);
rootObject = await GetValueFromObject(toArrayRetMethod, token);
DotnetObjectId.TryParse(rootObject?["objectId"]?.Value<string>(), out DotnetObjectId arrayObjectId);
rootObject["value"] = await context.SdbAgent.GetArrayValues(arrayObjectId.Value, token);
Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ internal async Task<ValueOrError<JToken>> RuntimeGetPropertiesInternal(SessionId
return resValType switch
{
null => ValueOrError<JToken>.WithError($"Could not get properties for {objectId}"),
_ => ValueOrError<JToken>.WithValue(sortByAccessLevel ? JObject.FromObject(new { result = resValType }) : resValType)
_ => ValueOrError<JToken>.WithValue(resValType)
};
}
case "array":
Expand All @@ -771,7 +771,7 @@ internal async Task<ValueOrError<JToken>> RuntimeGetPropertiesInternal(SessionId
}
case "methodId":
{
var resMethod = await context.SdbAgent.InvokeMethodInObject(objectId.Value, objectId.SubValue, null, token);
var resMethod = await context.SdbAgent.InvokeMethodInObject(objectId, objectId.SubValue, null, token);
return ValueOrError<JToken>.WithValue(sortByAccessLevel ? JObject.FromObject(new { result = new JArray(resMethod) }) : new JArray(resMethod));
}
case "object":
Expand Down Expand Up @@ -799,7 +799,8 @@ internal async Task<ValueOrError<JToken>> RuntimeGetPropertiesInternal(SessionId
return ValueOrError<JToken>.WithError($"RuntimeGetProperties: unknown object id scheme: {objectId.Scheme}");
}
}
catch (Exception ex) {
catch (Exception ex)
{
return ValueOrError<JToken>.WithError($"RuntimeGetProperties: Failed to get properties for {objectId}: {ex}");
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,13 +1601,12 @@ public async Task<JObject> InvokeMethod(ArraySegment<byte> valueTypeBuffer, int
return await CreateJObjectForVariableValue(retDebuggerCmdReader, varName, false, -1, false, token);
}

public async Task<JObject> InvokeMethodInObject(int objectId, int methodId, string varName, CancellationToken token)
public async Task<JObject> InvokeMethodInObject(DotnetObjectId objectId, int methodId, string varName, CancellationToken token)
{
valueTypes.TryGetValue(objectId, out var valueType);
if (valueType != null)
return await InvokeMethod(valueType.valueTypeBuffer, methodId, varName, token);
if (objectId.IsValueType)
return await InvokeMethod(valueTypes[objectId.Value].valueTypeBuffer, methodId, varName, token);
using var commandParamsObjWriter = new MonoBinaryWriter();
commandParamsObjWriter.Write(ElementType.Class, objectId);
commandParamsObjWriter.Write(ElementType.Class, objectId.Value);
return await InvokeMethod(commandParamsObjWriter.GetParameterBuffer(), methodId, varName, token);
}

Expand Down Expand Up @@ -2655,7 +2654,7 @@ async Task<JArray> GetProperties(JArray props, List<FieldTypeClass> fields, int
case DebuggerBrowsableState.RootHidden:
DotnetObjectId rootObjId;
DotnetObjectId.TryParse(p["get"]["objectId"].Value<string>(), out rootObjId);
var rootObject = await InvokeMethodInObject(rootObjId.Value, rootObjId.SubValue, propName, token);
var rootObject = await InvokeMethodInObject(rootObjId, rootObjId.SubValue, propName, token);
await AppendRootHiddenChildren(rootObject, regularProps);
break;
case DebuggerBrowsableState.Collapsed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,23 +999,42 @@ await RuntimeEvaluateAndCheck(

[Fact]
public async Task EvaluateProtectionLevels() => await CheckInspectLocalsAtBreakpointSite(
"DebuggerTests.EvaluateProtectionLevels", "Evaluate", 2, "Evaluate",
"window.setTimeout(function() { invoke_static_method ('[debugger-test] DebuggerTests.EvaluateProtectionLevels:Evaluate'); })",
"DebuggerTests.GetPropertiesTests.DerivedClass", "InstanceMethod", 1, "InstanceMethod",
"window.setTimeout(function() { invoke_static_method ('[debugger-test] DebuggerTests.GetPropertiesTests.DerivedClass:run'); })",
wait_for_event_fn: async (pause_location) =>
{
var id = pause_location["callFrames"][0]["callFrameId"].Value<string>();
var (obj, _) = await EvaluateOnCallFrame(id, "testClass");
var (pub, internalAndProtected, priv) = await GetPropertiesSortedByProtectionLevels(obj["objectId"]?.Value<string>());
Assert.True(pub[0] != null);
Assert.True(internalAndProtected[0] != null);
Assert.True(internalAndProtected[1] != null);
Assert.True(priv[0] != null);
Assert.Equal(pub[0]["value"]["value"], "public");
Assert.Equal(internalAndProtected[0]["value"]["value"], "internal");
Assert.Equal(internalAndProtected[1]["value"]["value"], "protected");
Assert.Equal(priv[0]["value"]["value"], "private");
{
var id = pause_location["callFrames"][0]["callFrameId"].Value<string>();
var (obj, _) = await EvaluateOnCallFrame(id, "this");
var (pub, internalAndProtected, priv) = await GetPropertiesSortedByProtectionLevels(obj["objectId"]?.Value<string>());
await CheckProps(pub, new
{
a = TNumber(4),
Base_AutoStringPropertyForOverrideWithField = TString("DerivedClass#Base_AutoStringPropertyForOverrideWithField"),
Base_GetterForOverrideWithField = TString("DerivedClass#Base_GetterForOverrideWithField"),
BaseBase_MemberForOverride = TString("DerivedClass#BaseBase_MemberForOverride"),
DateTime = TGetter("DateTime", TDateTime(new DateTime(2200, 5, 6, 7, 18, 9))),
_DTProp = TGetter("_DTProp", TDateTime(new DateTime(2200, 5, 6, 7, 8, 9))),
FirstName = TGetter("FirstName", TString("DerivedClass#FirstName")),
_base_dateTime = TGetter("_base_dateTime", TDateTime(new DateTime(2134, 5, 7, 1, 9, 2))),
LastName = TGetter("LastName", TString("BaseClass#LastName"))
}, "public");
await CheckProps(internalAndProtected, new
{
base_num = TNumber(5)
}, "internalAndProtected");
await CheckProps(priv, new
{
_stringField = TString("DerivedClass#_stringField"),
_dateTime = TDateTime(new DateTime(2020, 7, 6, 5, 4, 3)),
AutoStringProperty = TString("DerivedClass#AutoStringProperty"),
StringPropertyForOverrideWithAutoProperty = TString("DerivedClass#StringPropertyForOverrideWithAutoProperty"),
_base_name = TString("private_name"),
Base_AutoStringProperty = TString("base#Base_AutoStringProperty"),
DateTimeForOverride = TGetter("DateTimeForOverride", TDateTime(new DateTime(2190, 9, 7, 5, 3, 2)))
}, "private");
});

[Fact]
Expand All @@ -1027,17 +1046,10 @@ public async Task StructureGetters() => await CheckInspectLocalsAtBreakpointSit
var id = pause_location["callFrames"][0]["callFrameId"].Value<string>();
var (obj, _) = await EvaluateOnCallFrame(id, "s");
var props = await GetProperties(obj["objectId"]?.Value<string>());
await CheckProps(props, new
{
Id = TGetter("Id", TNumber(123))
}, "s#1");
var getter = props.FirstOrDefault(p => p["name"]?.Value<string>() == "Id");
Assert.NotNull(getter);
var getterId = getter["get"]["objectId"]?.Value<string>();
var getterProps = await GetProperties(getterId);
Assert.Equal(getterProps[0]?["value"]?["value"]?.Value<int>(), 123);
});

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,27 +823,6 @@ public static void Evaluate()
}
}

public static class EvaluateProtectionLevels
{
public class TestClass
{
public string fieldPublic = "public";
private string fieldPrivate = "private";
internal string fieldInternal = "internal";
protected string fieldProtected = "protected";

public TestClass()
{
var a = fieldPrivate;
}
}

public static void Evaluate()
{
var testClass = new TestClass();
}
}

public static class StructureGetters
{
public struct Point
Expand Down

0 comments on commit cbf3f9c

Please sign in to comment.