Skip to content

Commit

Permalink
Fix bug with array destructuring (closes #60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohansi committed Jun 19, 2017
1 parent fee6dac commit c7304bc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 29 deletions.
26 changes: 26 additions & 0 deletions Mond.Tests/Expressions/DestructuringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,32 @@ public void ArrayEllipsisNotEnough()
Assert.AreEqual(MondValue.Undefined, result[4], "e");
}

[Test]
public void ArrayEllipsisNotEnoughTailOnly()
{
var result = Script.Run(@"
var [ ...x, y, z ] = [ 1, 2 ];
return [ x, y, z ];
");

CollectionAssert.AreEqual(new MondValue[0], result[0].Array);
Assert.AreEqual((MondValue)1, result.Array[1]);
Assert.AreEqual((MondValue)2, result.Array[2]);
}

[Test]
public void ArrayEllipsisTailOnly()
{
var result = Script.Run(@"
var [ ...x, y, z ] = [ 1, 2, 3 ];
return [ x, y, z ];
");

CollectionAssert.AreEqual(new MondValue[] { 1 }, result[0].Array);
Assert.AreEqual((MondValue)2, result.Array[1]);
Assert.AreEqual((MondValue)3, result.Array[2]);
}

[Test]
public void ArrayMultipleEllipsis()
{
Expand Down
73 changes: 44 additions & 29 deletions Mond/Compiler/Expressions/Statements/DestructuredArrayExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,57 +35,73 @@ public DestructuredArrayExpression(Token token, IList<Index> indices, Expression
public override int Compile(FunctionContext context)
{
context.Position(Token);

var i = 0;
var startIndex = 0;
var stack = Initializer?.Compile(context) ?? 1;

var stack = Initializer?.Compile(context) ?? 0;
var global = context.ArgIndex == 0 && context.Compiler.Options.MakeRootDeclarationsGlobal;

var hasSlice = false;
var headSize = 0;
var tailSize = 0;

foreach (var index in Indices)
{
var assign = context.MakeLabel("arrayDestructureAssign");
var destruct = context.MakeLabel("arrayDestructureIndex");
var remaining = Indices.Skip(i + 1).Count();

stack += context.Dup();
stack += context.Dup();
stack += context.LoadField(context.String("length"));
stack += context.Call(0, new List<ImmediateOperand>());
stack += context.Load(context.Number(1));
stack += context.BinaryOperation(TokenType.Subtract);

if (index.IsSlice)
{
stack += context.Load(context.Number(Math.Abs(startIndex)));
stack += context.BinaryOperation(TokenType.Subtract);
stack += context.Load(context.Number(remaining));
if (hasSlice)
throw new InvalidOperationException($"Multiple slices in {nameof(DestructuredArrayExpression)}");

hasSlice = true;
}
else if (hasSlice)
{
tailSize++;
}
else
{
stack += context.Load(context.Number(Math.Abs(startIndex)));
headSize++;
}
}

stack += context.BinaryOperation(TokenType.GreaterThanOrEqual);
var fixedSize = headSize + tailSize;

stack += context.Dup();
stack += context.LoadField(context.String("length"));
stack += context.Call(0, new List<ImmediateOperand>());

var inTail = false;
var fixedI = 0;
for (var i = 0; i < Indices.Count; i++)
{
var index = Indices[i];
var assign = context.MakeLabel("arrayDestructureAssign");
var destruct = context.MakeLabel("arrayDestructureIndex");

if (index.IsSlice)
inTail = true;

if (i < Indices.Count - 1)
stack += context.Dup2(); // -> init.length(), init

stack += context.Load(context.Number(index.IsSlice ? fixedSize : fixedI));
stack += context.BinaryOperation(TokenType.GreaterThan);
stack += context.JumpTrue(destruct);
stack += context.Drop();
stack += context.Drop(); // drops initializer
stack += index.IsSlice ? context.NewArray(0) : context.LoadUndefined();
stack += context.Jump(assign);

stack += context.Bind(destruct);
stack += context.Load(context.Number(startIndex));

if (index.IsSlice)
{
startIndex = -remaining;

stack += context.Load(context.Number(startIndex - 1));
stack += context.Load(context.Number(fixedI));
stack += context.Load(context.Number(-tailSize - 1));
stack += context.LoadUndefined();
stack += context.Slice();
}
else
{
stack += context.Load(context.Number(inTail ? fixedI - fixedSize : fixedI));
stack += context.LoadArray();
startIndex++;
}

stack += context.Bind(assign);
Expand All @@ -103,11 +119,10 @@ public override int Compile(FunctionContext context)
stack += context.Store(context.Identifier(index.Name));
}

i++;
if (!index.IsSlice)
fixedI++;
}

stack += context.Drop();

CheckStack(stack, 0);
return -1;
}
Expand Down

0 comments on commit c7304bc

Please sign in to comment.