Skip to content

Commit 5a7dcd9

Browse files
committed
Simplify element type handling
1 parent 9683b28 commit 5a7dcd9

1 file changed

Lines changed: 59 additions & 29 deletions

File tree

‎src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs‎

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,19 @@ protected unsafe void WriteBigEndian<T>(T val) where T : struct
456456
base.Write(data);
457457
}
458458

459+
private void Write<T>(ElementType type, T value) where T : struct => Write((byte)type, value);
460+
461+
private void Write<T1, T2>(T1 type, T2 value) where T1 : struct where T2 : struct
462+
{
463+
WriteBigEndian(type);
464+
WriteBigEndian(value);
465+
}
466+
459467
public void WriteObj(DotnetObjectId objectId, MonoSDBHelper SdbHelper)
460468
{
461469
if (objectId.Scheme == "object")
462470
{
463-
Write((byte)ElementType.Class);
464-
Write(int.Parse(objectId.Value));
471+
Write(ElementType.Class, int.Parse(objectId.Value));
465472
}
466473
else if (objectId.Scheme == "valuetype")
467474
{
@@ -474,27 +481,60 @@ public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHe
474481
{
475482
case SyntaxKind.NumericLiteralExpression:
476483
{
477-
Write((byte)ElementType.I4);
478-
Write((int)constValue.Token.Value);
484+
switch (constValue.Token.Value) {
485+
case double d:
486+
Write(ElementType.R8, d);
487+
break;
488+
case float f:
489+
Write(ElementType.R4, f);
490+
break;
491+
case long l:
492+
Write(ElementType.I8, l);
493+
break;
494+
case ulong ul:
495+
Write(ElementType.U8, ul);
496+
break;
497+
case byte b:
498+
Write(ElementType.U1, (int)b);
499+
break;
500+
case sbyte sb:
501+
Write(ElementType.I1, (uint)sb);
502+
break;
503+
case ushort us:
504+
Write(ElementType.U2, (int)us);
505+
break;
506+
case short s:
507+
Write(ElementType.I2, (uint)s);
508+
break;
509+
case uint ui:
510+
Write(ElementType.U4, ui);
511+
break;
512+
case IntPtr ip:
513+
Write(ElementType.I, (int)ip);
514+
break;
515+
case UIntPtr up:
516+
Write(ElementType.U, (uint)up);
517+
break;
518+
default:
519+
Write(ElementType.I4, (int)constValue.Token.Value);
520+
break;
521+
}
479522
return true;
480523
}
481524
case SyntaxKind.StringLiteralExpression:
482525
{
483526
int stringId = await SdbHelper.CreateString((string)constValue.Token.Value, token);
484-
Write((byte)ElementType.String);
485-
Write((int)stringId);
527+
Write(ElementType.String, stringId);
486528
return true;
487529
}
488530
case SyntaxKind.TrueLiteralExpression:
489531
{
490-
Write((byte)ElementType.Boolean);
491-
Write((int)1);
532+
Write(ElementType.Boolean, 1);
492533
return true;
493534
}
494535
case SyntaxKind.FalseLiteralExpression:
495536
{
496-
Write((byte)ElementType.Boolean);
497-
Write((int)0);
537+
Write(ElementType.Boolean, 0);
498538
return true;
499539
}
500540
case SyntaxKind.NullLiteralExpression:
@@ -506,8 +546,7 @@ public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHe
506546
}
507547
case SyntaxKind.CharacterLiteralExpression:
508548
{
509-
Write((byte)ElementType.Char);
510-
Write((int)(char)constValue.Token.Value);
549+
Write(ElementType.Char, (int)(char)constValue.Token.Value);
511550
return true;
512551
}
513552
}
@@ -520,24 +559,18 @@ public async Task<bool> WriteJsonValue(JObject objValue, MonoSDBHelper SdbHelper
520559
{
521560
case "number":
522561
{
523-
Write((byte)ElementType.I4);
524-
Write(objValue["value"].Value<int>());
562+
Write(ElementType.I4, objValue["value"].Value<int>());
525563
return true;
526564
}
527565
case "string":
528566
{
529567
int stringId = await SdbHelper.CreateString(objValue["value"].Value<string>(), token);
530-
Write((byte)ElementType.String);
531-
Write((int)stringId);
568+
Write(ElementType.String, stringId);
532569
return true;
533570
}
534571
case "boolean":
535572
{
536-
Write((byte)ElementType.Boolean);
537-
if (objValue["value"].Value<bool>())
538-
Write((int)1);
539-
else
540-
Write((int)0);
573+
Write(ElementType.Boolean, objValue["value"].Value<bool>() ? 1 : 0);
541574
return true;
542575
}
543576
case "object":
@@ -780,7 +813,7 @@ public async Task<bool> EnableReceiveRequests(EventKind eventKind, CancellationT
780813
}
781814

782815
internal async Task<MonoBinaryReader> SendDebuggerAgentCommand<T>(T command, MonoBinaryWriter arguments, CancellationToken token) =>
783-
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));
816+
MonoBinaryReader.From (await proxy.SendMonoCommand(sessionId, MonoCommands.SendDebuggerAgentCommand(GetId(), (int)GetCommandSetForCommand(command), (int)(object)command, arguments?.ToBase64().data ?? string.Empty), token));
784817

785818
internal CommandSet GetCommandSetForCommand<T>(T command) =>
786819
command switch {
@@ -1070,7 +1103,7 @@ public async Task<bool> Step(int thread_id, StepKind kind, CancellationToken tok
10701103
commandParamsWriter.Write((int)kind);
10711104
commandParamsWriter.Write((int)(StepFilter.StaticCtor | StepFilter.DebuggerHidden)); //filter
10721105
using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdEventRequest.Set, commandParamsWriter, token);
1073-
if (retDebuggerCmdReader == null)
1106+
if (retDebuggerCmdReader.HasError)
10741107
return false;
10751108
var isBPOnManagedCode = retDebuggerCmdReader.ReadInt32();
10761109
if (isBPOnManagedCode == 0)
@@ -1085,10 +1118,7 @@ public async Task<bool> ClearSingleStep(int req_id, CancellationToken token)
10851118
commandParamsWriter.Write((int) req_id);
10861119

10871120
using var retDebuggerCmdReader = await SendDebuggerAgentCommand(CmdEventRequest.Clear, commandParamsWriter, token);
1088-
1089-
if (retDebuggerCmdReader != null)
1090-
return true;
1091-
return false;
1121+
return !retDebuggerCmdReader.HasError ? true : false;
10921122
}
10931123

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

1130-
if (typeInfo == null)
1160+
if (typeInfo is null)
11311161
return null;
11321162

1133-
if (typeInfo.PropertiesBuffer != null)
1163+
if (typeInfo.PropertiesBuffer is not null)
11341164
return new MonoBinaryReader(typeInfo.PropertiesBuffer);
11351165

11361166
var commandParamsWriter = new MonoBinaryWriter();

0 commit comments

Comments
 (0)