Skip to content

Commit 66d9125

Browse files
committed
[MERGE chakra-core#1008] Array Destructuring issue on .next function.
Merge pull request chakra-core#1008 from akroshg:destruct2 The iterator.next should not be called if the last slot is empty slot. Fixed that and added the test.
2 parents c20bf6d + cb3f37f commit 66d9125

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

lib/Runtime/ByteCode/ByteCodeEmitter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6166,9 +6166,10 @@ void EmitDestructuredArray(
61666166
elem = list;
61676167
}
61686168

6169-
if (elem->nop == knopEllipsis)
6169+
if (elem->nop == knopEllipsis
6170+
|| (elem->nop == knopEmpty && list->nop == knopEmpty))
61706171
{
6171-
// Rest must be the last argument - no need to continue.
6172+
// Rest and last empty slot do not require any more processing.
61726173
break;
61736174
}
61746175

@@ -6190,6 +6191,7 @@ void EmitDestructuredArray(
61906191
break;
61916192
}
61926193

6194+
61936195
byteCodeGenerator->StartStatement(elem);
61946196

61956197
Js::RegSlot itemLocation = funcInfo->AcquireTmpRegister();

test/es6/destructuring.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,40 @@ var tests = [
581581
}
582582
assert.isTrue(foo(), "Array destructuring pattern on for..in is initialized correctly");
583583
}
584+
585+
{
586+
let obj1 = {};
587+
obj1[Symbol.iterator] = function () {
588+
return {
589+
next: function() {
590+
assert.fail("next function should not be called");
591+
}
592+
};
593+
};
594+
595+
// Empty slot should not call next on the iterator.
596+
var [] = obj1;
597+
}
598+
599+
{
600+
let obj1 = {};
601+
obj1[Symbol.iterator] = function () {
602+
return {
603+
next: function() {
604+
this.counter++;
605+
if (this.counter > 1)
606+
{
607+
assert.fail("next function should not be called for the second time");
608+
}
609+
return {value : undefined, done: false};
610+
},
611+
counter : 0
612+
};
613+
};
614+
615+
// Second empty slot should not call next on the iterator.
616+
var [,] = obj1;
617+
}
584618

585619
}
586620
}

0 commit comments

Comments
 (0)