Skip to content

Commit

Permalink
compiler: return "if-test and throw" when compiling 'assert'
Browse files Browse the repository at this point in the history
'assert' statements were turned into method calls to a method that was
removed in 992b515, resulting in compile errors.

'assert' is still used in a few places, and this commit turns them into
"if-test and throw" in debug builds (noop in release builds) to fix the
compile errors.

    assert VALUE;

becomes when compiled:

    if (IS_DEBUG_BUILD && !VALUE)
        throw new Uno.InvalidOperationException(
            "Assertion failed: VALUE"
        );
  • Loading branch information
mortend committed May 5, 2023
1 parent 0993a9d commit 5ef11db
Showing 1 changed file with 9 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Uno.Compiler.API.Domain.AST.Expressions;
using Uno.Compiler.API.Domain.AST.Statements;
Expand Down Expand Up @@ -501,54 +500,15 @@ public Statement CompileStatement(AstStatement e)
if (!Environment.Debug)
return new NoOp(e.Source, "Stripped assert");

var s = e as AstValueStatement;
var value = CompileExpression(s.Value);
var args = new List<Expression>
{
value,
new Constant(s.Source, Essentials.String, value.ToString()),
new Constant(s.Source, Essentials.String, s.Source.File.ToString().Replace('\\', '/')),
new Constant(s.Source, Essentials.Int, s.Source.Line),
};
var locals = new List<StoreLocal>();

switch (value.ExpressionType)
{
case ExpressionType.CallUnOp:
{
var o = value as CallUnOp;
args.Add(CreateAssertIndirection(ref o.Operand, locals, Namescope));
break;
}
case ExpressionType.CallBinOp:
{
var o = value as CallBinOp;
args.Add(CreateAssertIndirection(ref o.Left, locals, Namescope));
args.Add(CreateAssertIndirection(ref o.Right, locals, Namescope));
break;
}
case ExpressionType.BranchOp:
{
var o = value as BranchOp;
args.Add(CreateAssertIndirection(ref o.Left, locals, Namescope));
args.Add(CreateAssertIndirection(ref o.Right, locals, Namescope));
break;
}
case ExpressionType.CallMethod:
{
var o = value as CallMethod;
for (int i = 0; i < o.Arguments.Length; i++)
args.Add(CreateAssertIndirection(ref o.Arguments[i], locals, Namescope));
break;
}
}
// Return "if-test and throw":
// if (!value) throw new Uno.InvalidOperationException("Assertion failed: {value}");

var result = ILFactory.CallMethod(s.Source, "Uno.Diagnostics.Debug", "Assert", args.ToArray());

while (locals.Count > 0)
result = new SequenceOp(locals.RemoveLast(), result);

return result;
var s = (AstValueStatement)e;
var value = CompileExpression(s.Value);
var cond = ILFactory.CallOperator(value.ReturnType, "!", value);
var exception = ILFactory.NewObject("Uno.InvalidOperationException",
new Constant(Essentials.String, $"Assertion failed: {value}"));
return new IfElse(s.Source, cond, new Throw(s.Source, exception));
}
case AstStatementType.DebugLog:
{
Expand Down

0 comments on commit 5ef11db

Please sign in to comment.