Skip to content

Commit

Permalink
Simplify element type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Nov 12, 2021
1 parent 9683b28 commit 5a7dcd9
Showing 1 changed file with 59 additions and 29 deletions.
88 changes: 59 additions & 29 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,19 @@ protected unsafe void WriteBigEndian<T>(T val) where T : struct
base.Write(data);
}

private void Write<T>(ElementType type, T value) where T : struct => Write((byte)type, value);

private void Write<T1, T2>(T1 type, T2 value) where T1 : struct where T2 : struct
{
WriteBigEndian(type);
WriteBigEndian(value);
}

public void WriteObj(DotnetObjectId objectId, MonoSDBHelper SdbHelper)
{
if (objectId.Scheme == "object")
{
Write((byte)ElementType.Class);
Write(int.Parse(objectId.Value));
Write(ElementType.Class, int.Parse(objectId.Value));
}
else if (objectId.Scheme == "valuetype")
{
Expand All @@ -474,27 +481,60 @@ public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHe
{
case SyntaxKind.NumericLiteralExpression:
{
Write((byte)ElementType.I4);
Write((int)constValue.Token.Value);
switch (constValue.Token.Value) {
case double d:
Write(ElementType.R8, d);
break;
case float f:
Write(ElementType.R4, f);
break;
case long l:
Write(ElementType.I8, l);
break;
case ulong ul:
Write(ElementType.U8, ul);
break;
case byte b:
Write(ElementType.U1, (int)b);
break;
case sbyte sb:
Write(ElementType.I1, (uint)sb);
break;
case ushort us:
Write(ElementType.U2, (int)us);
break;
case short s:
Write(ElementType.I2, (uint)s);
break;
case uint ui:
Write(ElementType.U4, ui);
break;
case IntPtr ip:
Write(ElementType.I, (int)ip);
break;
case UIntPtr up:
Write(ElementType.U, (uint)up);
break;
default:
Write(ElementType.I4, (int)constValue.Token.Value);
break;
}
return true;
}
case SyntaxKind.StringLiteralExpression:
{
int stringId = await SdbHelper.CreateString((string)constValue.Token.Value, token);
Write((byte)ElementType.String);
Write((int)stringId);
Write(ElementType.String, stringId);
return true;
}
case SyntaxKind.TrueLiteralExpression:
{
Write((byte)ElementType.Boolean);
Write((int)1);
Write(ElementType.Boolean, 1);
return true;
}
case SyntaxKind.FalseLiteralExpression:
{
Write((byte)ElementType.Boolean);
Write((int)0);
Write(ElementType.Boolean, 0);
return true;
}
case SyntaxKind.NullLiteralExpression:
Expand All @@ -506,8 +546,7 @@ public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHe
}
case SyntaxKind.CharacterLiteralExpression:
{
Write((byte)ElementType.Char);
Write((int)(char)constValue.Token.Value);
Write(ElementType.Char, (int)(char)constValue.Token.Value);
return true;
}
}
Expand All @@ -520,24 +559,18 @@ public async Task<bool> WriteJsonValue(JObject objValue, MonoSDBHelper SdbHelper
{
case "number":
{
Write((byte)ElementType.I4);
Write(objValue["value"].Value<int>());
Write(ElementType.I4, objValue["value"].Value<int>());
return true;
}
case "string":
{
int stringId = await SdbHelper.CreateString(objValue["value"].Value<string>(), token);
Write((byte)ElementType.String);
Write((int)stringId);
Write(ElementType.String, stringId);
return true;
}
case "boolean":
{
Write((byte)ElementType.Boolean);
if (objValue["value"].Value<bool>())
Write((int)1);
else
Write((int)0);
Write(ElementType.Boolean, objValue["value"].Value<bool>() ? 1 : 0);
return true;
}
case "object":
Expand Down Expand Up @@ -780,7 +813,7 @@ public async Task<bool> EnableReceiveRequests(EventKind eventKind, CancellationT
}

internal async Task<MonoBinaryReader> SendDebuggerAgentCommand<T>(T command, MonoBinaryWriter arguments, CancellationToken token) =>
MonoBinaryReader.From (await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetId(), (int)GetCommandSetForCommand(command), (int)(object)command, arguments is not null ? arguments.ToBase64().data : string.Empty), token));
MonoBinaryReader.From (await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetId(), (int)GetCommandSetForCommand(command), (int)(object)command, arguments?.ToBase64().data ?? string.Empty), token));

internal CommandSet GetCommandSetForCommand<T>(T command) =>
command switch {
Expand Down Expand Up @@ -1070,7 +1103,7 @@ public async Task<bool> Step(int thread_id, StepKind kind, CancellationToken tok
commandParamsWriter.Write((int)kind);
commandParamsWriter.Write((int)(StepFilter.StaticCtor | StepFilter.DebuggerHidden)); //filter
using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdEventRequest.Set, commandParamsWriter, token);
if (retDebuggerCmdReader == null)
if (retDebuggerCmdReader.HasError)
return false;
var isBPOnManagedCode = retDebuggerCmdReader.ReadInt32();
if (isBPOnManagedCode == 0)
Expand All @@ -1085,10 +1118,7 @@ public async Task<bool> ClearSingleStep(int req_id, CancellationToken token)
commandParamsWriter.Write((int) req_id);

using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdEventRequest.Clear, commandParamsWriter, token);

if (retDebuggerCmdReader != null)
return true;
return false;
return !retDebuggerCmdReader.HasError ? true : false;
}

public async Task<JObject> GetFieldValue(int typeId, int fieldId, CancellationToken token)
Expand Down Expand Up @@ -1127,10 +1157,10 @@ public async Task<MonoBinaryReader> GetTypePropertiesReader(int typeId, Cancella
{
var typeInfo = await GetTypeInfo(typeId, token);

if (typeInfo == null)
if (typeInfo is null)
return null;

if (typeInfo.PropertiesBuffer != null)
if (typeInfo.PropertiesBuffer is not null)
return new MonoBinaryReader(typeInfo.PropertiesBuffer);

var commandParamsWriter = new MonoBinaryWriter();
Expand Down

0 comments on commit 5a7dcd9

Please sign in to comment.