Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
7 changes: 7 additions & 0 deletions src/Unluau/Lifter/Expressions/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public BinaryExpression(Expression left, BinaryOperation operation, Expression r

public override void Write(Output output)
{
// Add null checks
if (Left == null || Right == null)
{
output.Write("nil");
return;
}

Left.Write(output);
output.Write($" {BinaryOperationChar(Operation)} ");
Right.Write(output);
Expand Down
6 changes: 5 additions & 1 deletion src/Unluau/Lifter/Expressions/Closure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public override void Write(Output output)

output.WriteLine(")");

Block.Write(output);
// Add null check for Block
if (Block != null)
Block.Write(output);
else
output.WriteLine("-- [Unluau: Missing function body]");

output.Write("end");
}
Expand Down
7 changes: 7 additions & 0 deletions src/Unluau/Lifter/Expressions/NameIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public NameIndex(Expression expression, string name, bool isSelf = false)

public override void Write(Output output)
{
// Add null check
if (Expression == null)
{
output.Write("nil");
return;
}

Expression.Write(output);
output.Write((IsSelf ? ":" : ".") + Name);
}
Expand Down
39 changes: 31 additions & 8 deletions src/Unluau/Lifter/Lifter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,23 @@ private Block LiftBlock(Function function, Registers registers, int pcStart = 0,

if (options.PerferStringInterpolation)
{
// If we perfer string interpolation set the expression as an interpolated string
string format = (((LocalExpression)nameIndex!.Expression).Expression as StringLiteral)!.Value;
expression = new InterpolatedStringLiteral(format, new List<Expression>());
// Safely extract the format string
var localExpr = nameIndex?.Expression as LocalExpression;
var stringLiteral = localExpr?.Expression as StringLiteral;
if (stringLiteral != null)
{
string format = stringLiteral.Value;
expression = new InterpolatedStringLiteral(format, new List<Expression>());
}
}
else
{
// Otherwise add an expression group around the string literal
nameIndex!.Expression = new ExpressionGroup(nameIndex!.Expression);
expression = nameIndex;
if (nameIndex != null)
{
nameIndex.Expression = new ExpressionGroup(nameIndex.Expression);
expression = nameIndex;
}
}
}

Expand Down Expand Up @@ -293,7 +301,13 @@ private Block LiftBlock(Function function, Registers registers, int pcStart = 0,
case OpCode.SETTABLEKS:
{
StringConstant target = (StringConstant)function.GetConstant(++pc);
Expression table = registers.GetExpression(instruction.B), tableValue = ((LocalExpression)table).Expression;
Expression table = registers.GetExpression(instruction.B);
if (table == null || !(table is LocalExpression))
{
// Skip this instruction if table is null or not a LocalExpression
break;
}
Expression tableValue = ((LocalExpression)table).Expression;

if (options.InlineTableDefintions && tableValue is TableLiteral)
{
Expand All @@ -317,7 +331,12 @@ private Block LiftBlock(Function function, Registers registers, int pcStart = 0,
case OpCode.SETTABLE:
{
Expression expression = registers.GetRefExpressionValue(instruction.C), value = registers.GetExpression(instruction.A);
Expression table = registers.GetExpression(instruction.B, false), tableValue = ((LocalExpression)table).Expression;
Expression table = registers.GetExpression(instruction.B, false);
if (table == null || !(table is LocalExpression))
{
break;
}
Expression tableValue = ((LocalExpression)table).Expression;

if (options.InlineTableDefintions && tableValue is TableLiteral)
{
Expand Down Expand Up @@ -682,7 +701,11 @@ private Block LiftBlock(Function function, Registers registers, int pcStart = 0,

private int IsSelf(Expression expression)
{
Expression? value = expression.GetValue();
Expression? value = expression?.GetValue();

// Add null check here
if (value == null)
return 0;

if (value is NameIndex nameIndex)
return nameIndex.IsSelf ? 1 : 0;
Expand Down
7 changes: 7 additions & 0 deletions src/Unluau/Lifter/Statements/Assignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public Assignment(Expression variable, Expression value)

public override void Write(Output output)
{
// Add null checks
if (Variable == null || Value == null)
{
output.Write("-- [Unluau: Failed to decompile assignment]");
return;
}

if (Value is LocalExpression && ((LocalExpression)Value).Expression is Closure)
{
output.Write("function ");
Expand Down
7 changes: 7 additions & 0 deletions src/Unluau/Lifter/Statements/IfElse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@
public Block IfBody { get; set; }
public Statement ElseBody { get; set; }

public IfElse(Expression condition, Block ifBody)

Check warning on line 18 in src/Unluau/Lifter/Statements/IfElse.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ElseBody' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
Condition = condition;
IfBody = ifBody;
ElseBody = null;

Check warning on line 22 in src/Unluau/Lifter/Statements/IfElse.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
}

public override void Write(Output output)
{
// Add null checks for Condition and IfBody
if (Condition == null || IfBody == null)
{
output.WriteLine("-- [Unluau: Failed to decompile if statement - missing condition or body]");
return;
}

output.Write("if ");
Condition.Write(output);
output.WriteLine(" then");
Expand Down
7 changes: 7 additions & 0 deletions src/Unluau/Lifter/Statements/LocalAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
{
if (Expression is LocalExpression)
{
variable = Expression as LocalExpression;

Check warning on line 33 in src/Unluau/Lifter/Statements/LocalAssignment.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
return true;
}

variable = null;

Check warning on line 37 in src/Unluau/Lifter/Statements/LocalAssignment.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
return false;
}

Expand All @@ -42,16 +42,23 @@
{
if (Expression is ExpressionList)
{
variable = Expression as ExpressionList;

Check warning on line 45 in src/Unluau/Lifter/Statements/LocalAssignment.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
return true;
}

variable = null;

Check warning on line 49 in src/Unluau/Lifter/Statements/LocalAssignment.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
return false;
}

public override void Write(Output output)
{
// Add null checks
if (Expression == null || Value == null)
{
output.Write("-- [Unluau: Failed to decompile local assignment]");
return;
}

output.Write("local ");

if (Value is Closure)
Expand Down
30 changes: 17 additions & 13 deletions src/Unluau/Lifter/Statements/Return.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ public Return()

public override void Write(Output output)
{
output.Write("return ");

bool first = true;

foreach (Expression expression in Expressions)
output.Write("return");

if (Expressions != null && Expressions.Count > 0)
{
if (expression is null)
continue;

expression.Write(output);

if (first)
output.Write(" ");
bool first = true;

foreach (Expression expression in Expressions)
{
if (!first)
output.Write(", ");

if (expression == null)
output.Write("nil");
else
expression.Write(output);

first = false;
else
output.Write(",");
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Unluau/Lifter/Statements/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public static void WriteSequence(Output output, IList<Statement> statements)
for (int i = 0; i < statements.Count; i++)
{
var statement = statements[i];

// Add null check for statement
if (statement == null)
{
output.WriteLine("-- [Unluau: null statement]");
continue;
}

if (statement.Comment != null)
output.WriteLine("-- " + statement.Comment);

Expand Down
Loading