Skip to content

Commit

Permalink
Implemented an ObjectIterator and made Reflect.enumerate use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Victorystick committed Jan 4, 2015
1 parent 6527c14 commit 6fce302
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,11 @@
defineProperty(Array, 'from', ArrayShims.from, true);
}

// iterator result
var iterator_result = function (x) {
return { value: x, done: !arguments.length };
}

// Our ArrayIterator is private; see
// https://github.com/paulmillr/es6-shim/issues/252
ArrayIterator = function (array, kind) {
Expand Down Expand Up @@ -756,6 +761,64 @@
});
addIterator(ArrayIterator.prototype);

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

defineProperties(ObjectIterator.prototype, {
next: function () {
var key, array = this.array;
if (!(this instanceof ObjectIterator)) {
throw new TypeError('Not an ObjectIterator');
}

if (typeof array !== 'undefined') {
// Keys not generated
if (array === null) {
array = this.array = [];

for (key in this.object) {
array.push(key);
}
}

var retval, len = ES.ToLength(array.length);

// Find current index, then update i
while (this.i < len) {
key = array[this.i];
this.i++;

// The candidate key isn't defined on object.
// Must have been deleted, or object[[Prototype]]
// has been modified.
if (!(key in this.object)) {
continue;
}

switch (this.kind) {
case 'key':
return iterator_result(key);
case 'value':
return iterator_result(this.object[key]);
case 'entry':
return iterator_result([key, this.object[key]]);
}
}

// End of key array.
this.array = void 0;
}

return iterator_result();
}
});
addIterator(ObjectIterator.prototype);

var ArrayPrototypeShims = {
copyWithin: function (target, start) {
var end = arguments[2]; // copyWithin.length must be 2
Expand Down Expand Up @@ -2181,13 +2244,7 @@
throw new TypeError('target must be an object');
}

var keys = [];

for (var key in target) {
keys.push(key);
}

return new ArrayIterator(keys, 'value');
return new ObjectIterator(target, 'key');
},

// New operator in a functional form.
Expand Down

0 comments on commit 6fce302

Please sign in to comment.