Skip to content

Commit

Permalink
Fix awaited range array
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Jan 13, 2022
1 parent be97fb9 commit 6d960ed
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions Fluid/Ast/RangeExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@ public override ValueTask<FluidValue> EvaluateAsync(TemplateContext context)
start = Convert.ToInt32(startTask.Result.ToNumberValue());
end = Convert.ToInt32(endTask.Result.ToNumberValue());

// If end < start, we create an empty array
var list = new FluidValue[Math.Max(0, end - start + 1)];

for (var i = 0; i < list.Length; i++)
{
list[i] = NumberValue.Create(start + i);
}

return new ArrayValue(list);
return BuildArray(start, end);
}
else
{
return Awaited(startTask, endTask);
}
}

private static FluidValue BuildArray(int start, int end)
{
// If end < start, we create an empty array
var list = new FluidValue[Math.Max(0, end - start + 1)];

for (var i = 0; i < list.Length; i++)
{
list[i] = NumberValue.Create(start + i);
}

return new ArrayValue(list);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private async ValueTask<FluidValue> Awaited(
ValueTask<FluidValue> leftTask,
Expand All @@ -54,14 +59,7 @@ private async ValueTask<FluidValue> Awaited(
var start = Convert.ToInt32((await leftTask).ToNumberValue());
var end = Convert.ToInt32((await rightTask).ToNumberValue());

var list = new FluidValue[Math.Max(1, end - start)];

for (var i = start; i <= end; i++)
{
list[i] = NumberValue.Create(i);
}

return new ArrayValue(list);
return BuildArray(start, end);
}
}
}

0 comments on commit 6d960ed

Please sign in to comment.