Skip to content

Commit

Permalink
Fix nested object member destructuring (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Nov 13, 2022
1 parent 402a431 commit f840b09
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 28 additions & 0 deletions Jint.Tests/Runtime/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,32 @@ class C {}
var engine = new Engine();
Assert.True(engine.Evaluate(Script).AsBoolean());
}

[Fact]
public void CanDestructureNestedMembers()
{
const string Script = @"
class Board {
constructor () {
this.grid = {width: 10, height: 10}
}
get width () {
const {grid} = this
return grid.width
}
get doubleWidth () {
const {width} = this
return width * 2
}
}";

var engine = new Engine();
engine.Execute(Script);

engine.Evaluate("var board = new Board()");
Assert.Equal(10, engine.Evaluate("board.width"));
Assert.Equal(20, engine.Evaluate("board.doubleWidth "));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private static bool ConsumeFromIterator(IteratorInstance it, out JsValue value,
processedProperties?.Add(sourceKey);
if (p.Value is AssignmentPattern assignmentPattern)
{
source.TryGetValue(sourceKey, out var value);
var value = source.Get(sourceKey);
if (value.IsUndefined())
{
var jintExpression = Build(assignmentPattern.Right);
Expand Down Expand Up @@ -353,21 +353,21 @@ private static bool ConsumeFromIterator(IteratorInstance it, out JsValue value,
}
else if (p.Value is BindingPattern bindingPattern)
{
source.TryGetValue(sourceKey, out var value);
var value = source.Get(sourceKey);
ProcessPatterns(context, bindingPattern, value, environment);
}
else if (p.Value is MemberExpression memberExpression)
{
var reference = GetReferenceFromMember(context, memberExpression);
source.TryGetValue(sourceKey, out var value);
var value = source.Get(sourceKey);
AssignToReference(context.Engine, reference, value, environment);
}
else
{
var identifierReference = p.Value as Identifier;
var target = identifierReference ?? identifier;
source.TryGetValue(sourceKey, out var v);
AssignToIdentifier(context.Engine, target!.Name, v, environment, checkReference);
var value = source.Get(sourceKey);
AssignToIdentifier(context.Engine, target!.Name, value, environment, checkReference);
}
}
else
Expand Down

0 comments on commit f840b09

Please sign in to comment.