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] Support passing negative/positive numbers to methods. #92754

Merged
merged 4 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
radical marked this conversation as resolved.
Show resolved Hide resolved
{
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing the short argument.

Copy link
Member Author

@ilonatommy ilonatommy Oct 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short is optional param of value -32768, the test is checking for optional params to be used correctly and I wanted to keep this convention for this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                    ("test.GetDefaultAndRequiredParam(3, +2)", TNumber(5)),
                    ("test.GetDefaultAndRequiredParam(3, -2)", TNumber(1)),
                    ("test.GetDefaultAndRequiredParam(-123l, -1.1f)", TNumber("-124.1", isDecimal: true)),
  • These 3 are not testing optional parameters.
  • Also, for the cases that call GetDefaultAndRequiredParam - add comments mentioning what optional type is being tested.

Do the changes in this PR also affect passing a negative number as an argument to a method?

("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