Skip to content

Commit

Permalink
Reflect.enumerate does not necessarily wait until the first `next()…
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 11, 2015
1 parent 7861339 commit 072c887
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
8 changes: 1 addition & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,7 @@

var ObjectIterator = function (object, kind) {
this.object = object;
// Don't generate keys yet.
this.array = null;
this.array = getAllKeys(object);
this.kind = kind;
};

Expand All @@ -867,11 +866,6 @@
throw new TypeError('Not an ObjectIterator');
}

// Keys not generated
if (array === null) {
array = this.array = getAllKeys(this.object);
}

// Find next key in the object
while (ES.ToLength(array.length) > 0) {
key = _shift(array);
Expand Down
14 changes: 10 additions & 4 deletions test/reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('Reflect', function () {
});

describe('keys determined at first next() call', function () {
it('does not yield a key added after next()', function () {
it('likely does not yield a key added after next()', function () {
var obj = { a: 1, b: 2 },
iter = Reflect.enumerate(obj);

Expand All @@ -326,15 +326,21 @@ describe('Reflect', function () {
expect(iter.next()).to.deep.equal({ value: undefined, done: true });
});

it('yields a key added after enumerate(), before next()', function () {
it('may not yield a key added after enumerate(), before next()', function () {
var obj = { a: 1, b: 2 };
var iter = Reflect.enumerate(obj);

obj.c = 3;
expect(iter.next()).to.deep.equal({ value: 'a', done: false });
expect(iter.next()).to.deep.equal({ value: 'b', done: false });
expect(iter.next()).to.deep.equal({ value: 'c', done: false });
expect(iter.next()).to.deep.equal({ value: undefined, done: true });
var thirdResult = iter.next();
// Webkit's implementation of Reflect.enumerate locks the keys at iterator creation
if (thirdResult.done) {
expect(iter.next()).to.deep.equal({ value: undefined, done: true });
} else {
expect(iter.next()).to.deep.equal({ value: 'c', done: false });
expect(iter.next()).to.deep.equal({ value: undefined, done: true });
}
});

it('does not yield an unyielded key deleted after first next()', function () {
Expand Down

0 comments on commit 072c887

Please sign in to comment.