Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm][debugger] Debuger tests refactor 63667 #63730

Merged
merged 8 commits into from
Mar 30, 2022
13 changes: 8 additions & 5 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)
ilonatommy marked this conversation as resolved.
Show resolved Hide resolved
{
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 @@ -348,7 +348,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.Value, methodId, objectId.IsValueType, 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
6 changes: 4 additions & 2 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ internal async Task<JToken> RuntimeGetPropertiesInternal(SessionId id, DotnetObj
}
case "methodId":
{
var resMethod = await context.SdbAgent.InvokeMethodInObject(objectId.Value, objectId.SubValue, null, token);
var resMethod = await context.SdbAgent.InvokeMethodInObject(objectId.Value, objectId.SubValue, objectId.IsValueType, null, token);
return sortByAccessLevel ? JObject.FromObject(new { result = new JArray(resMethod) }) : new JArray(resMethod);
}
case "object":
Expand All @@ -781,7 +781,9 @@ internal async Task<JToken> RuntimeGetPropertiesInternal(SessionId id, DotnetObj

}
}
catch (Exception) {
catch (Exception ex)
{
logger.LogDebug($"Error on getting properties of object {objectId} with scheme {objectId.Scheme}. {ex}.");
return null;
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,11 +1588,10 @@ 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(int objectId, int methodId, bool isValueType, string varName, CancellationToken token)
ilonatommy marked this conversation as resolved.
Show resolved Hide resolved
{
valueTypes.TryGetValue(objectId, out var valueType);
if (valueType != null)
return await InvokeMethod(valueType.valueTypeBuffer, methodId, varName, token);
if (isValueType)
return await InvokeMethod(valueTypes[objectId].valueTypeBuffer, methodId, varName, token);
using var commandParamsObjWriter = new MonoBinaryWriter();
commandParamsObjWriter.Write(ElementType.Class, objectId);
return await InvokeMethod(commandParamsObjWriter.GetParameterBuffer(), methodId, varName, token);
Expand Down Expand Up @@ -2641,7 +2640,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.Value, rootObjId.SubValue, rootObjId.IsValueType, propName, token);
await AppendRootHiddenChildren(rootObject, regularProps);
break;
case DebuggerBrowsableState.Collapsed:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1001,23 +1001,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 @@ -1029,17 +1048,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