Skip to content

Commit

Permalink
[wasm][debugger] Support passing negative/positive numbers to methods. (
Browse files Browse the repository at this point in the history
#92754)

* Fix.

* Fix for positive.

* @radical's feedback
  • Loading branch information
ilonatommy committed Oct 5, 2023
1 parent f64bd37 commit 52aca8c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ public async Task<JObject> Resolve(InvocationExpressionSyntax method, Dictionary
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 PrefixUnaryExpressionSyntax negativeLiteral)
{
if (!commandParamsObjWriter.WriteConst(negativeLiteral))
throw new InternalErrorException($"Unable to evaluate method '{methodName}'. Unable to write PrefixUnaryExpressionSyntax into binary writer.");
}
else if (arg.Expression is IdentifierNameSyntax identifierName)
{
if (!memberAccessValues.TryGetValue(identifierName.Identifier.Text, out JObject argValue))
Expand Down
121 changes: 84 additions & 37 deletions src/mono/wasm/debugger/BrowserDebugProxy/MonoSDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -650,51 +650,53 @@ public async Task<bool> WriteConst(ElementType? type, object value, MonoSDBHelpe
return false;
}

public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHelper SdbHelper, CancellationToken token)
public bool WriteConst(PrefixUnaryExpressionSyntax constValue)
{
switch (constValue.Kind())
{
case SyntaxKind.NumericLiteralExpression:
case SyntaxKind.UnaryMinusExpression:
{
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);
switch (constValue.Operand)
{
case LiteralExpressionSyntax les:
{
return WriteNumber(les.Token.Value, convertToNegative: true);
}
default:
{
// not supported yet
break;
}
}
break;
}
case SyntaxKind.UnaryPlusExpression:
{
switch (constValue.Operand)
{
case LiteralExpressionSyntax les:
{
return WriteNumber(les.Token.Value, convertToNegative: false);
}
default:
Write(ElementType.I4, (int)constValue.Token.Value);
{
// not supported yet
break;
}
}
return true;
break;
}
}
return false;
}

public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHelper SdbHelper, CancellationToken token)
{
switch (constValue.Kind())
{
case SyntaxKind.NumericLiteralExpression:
{
return WriteNumber(constValue.Token.Value);
}
case SyntaxKind.StringLiteralExpression:
{
Expand Down Expand Up @@ -728,6 +730,51 @@ public async Task<bool> WriteConst(LiteralExpressionSyntax constValue, MonoSDBHe
return false;
}

public bool WriteNumber(object number, bool convertToNegative=false)
{
int coeff = convertToNegative ? -1 : 1;
switch (number)
{
case double d:
Write(ElementType.R8, d * coeff);
break;
case float f:
Write(ElementType.R4, f * coeff);
break;
case long l:
Write(ElementType.I8, l * coeff);
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 * coeff);
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)number * coeff);
break;
}
return true;
}

public async Task<bool> WriteJsonValue(JObject objValue, MonoSDBHelper SdbHelper, ElementType? expectedType, CancellationToken token)
{
switch (objValue["type"].Value<string>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,10 @@ await EvaluateOnCallFrameAndCheck(id,
("test.GetDefaultAndRequiredParam(2)", TNumber(5)),
("test.GetDefaultAndRequiredParam(3, 2)", TNumber(5)),
("test.GetDefaultAndRequiredParam(3, +2)", TNumber(5)),
("test.GetDefaultAndRequiredParam(3, -2)", TNumber(1)),
("test.GetDefaultAndRequiredParam(-123l, -1.1f)", TNumber("-124.1", isDecimal: true)), // long, float
("test.GetDefaultAndRequiredParam(-0.23)", TNumber("-32768.23", isDecimal: true)), // double, short
("test.GetDefaultAndRequiredParamMixedTypes(\"a\")", TString("a; -1; False")),
("test.GetDefaultAndRequiredParamMixedTypes(\"a\", 23)", TString("a; 23; False")),
("test.GetDefaultAndRequiredParamMixedTypes(\"a\", 23, true)", TString("a; 23; True"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,8 @@ public class TestClass

public bool GetNull(object param = null) => param == null ? true : false;
public int GetDefaultAndRequiredParam(int requiredParam, int optionalParam = 3) => requiredParam + optionalParam;
public float GetDefaultAndRequiredParam(long requiredParam, float optionalParam = 3.3f) => requiredParam + optionalParam;
public double GetDefaultAndRequiredParam(double requiredParam, short optionalParam = -32768) => requiredParam + optionalParam;
public string GetDefaultAndRequiredParamMixedTypes(string requiredParam, int optionalParamFirst = -1, bool optionalParamSecond = false) => $"{requiredParam}; {optionalParamFirst}; {optionalParamSecond}";
}

Expand Down

0 comments on commit 52aca8c

Please sign in to comment.