Chromium Code Reviews| Index: src/collection.js |
| diff --git a/src/collection.js b/src/collection.js |
| index 0d8dd77f7bd3be944ffcd25b51544d8ebe740c00..f4bd549238cb96e89c27093dedd80f6ee0f732c3 100644 |
| --- a/src/collection.js |
| +++ b/src/collection.js |
| @@ -12,15 +12,60 @@ var $Set = global.Set; |
| var $Map = global.Map; |
| +// 7.4.1 CheckIterable ( obj ) |
|
arv (Not doing code reviews)
2014/06/18 21:02:05
This should probably go in v8natives.js once this
rossberg
2014/06/23 14:16:00
Agreed. Maybe add a TODO to that end?
We could al
arv (Not doing code reviews)
2014/06/23 15:43:56
Done.
|
| +function CheckIterable(obj) { |
| + if (!IS_SPEC_OBJECT(obj)) { |
| + return UNDEFINED; |
| + } |
| + return obj[symbolIterator]; |
| +} |
| + |
| + |
| +// 7.4.2 GetIterator ( obj, method ) |
| +function GetIterator(obj, method) { |
| + if (IS_UNDEFINED(method)) { |
| + method = CheckIterable(obj); |
| + } |
| + if (!IS_SPEC_FUNCTION(method)) { |
| + throw MakeTypeError('not_iterable', [obj]); |
| + } |
| + var iterator = %_CallFunction(obj, method); |
| + if (!IS_SPEC_OBJECT(iterator)) { |
| + throw MakeTypeError('not_an_iterator', [iterator]); |
| + } |
| + return iterator; |
| +} |
| + |
| + |
| // ------------------------------------------------------------------- |
| // Harmony Set |
| -function SetConstructor() { |
| - if (%_IsConstructCall()) { |
| - %SetInitialize(this); |
| - } else { |
| +function SetConstructor(iterable) { |
| + if (!%_IsConstructCall()) { |
| throw MakeTypeError('constructor_not_function', ['Set']); |
| } |
| + |
| + var iter, adder; |
| + |
| + if (!IS_NULL_OR_UNDEFINED(iterable)) { |
| + iter = GetIterator(iterable); |
| + adder = this.add; |
| + if (!IS_SPEC_FUNCTION(adder)) { |
| + throw MakeTypeError('property_not_function', ['add', this]); |
| + } |
| + } |
| + |
| + %SetInitialize(this); |
| + |
| + if (IS_UNDEFINED(iter)) return; |
| + |
| + var next, done; |
| + while (!(next = iter.next()).done) { |
| + if (!IS_SPEC_OBJECT(next)) { |
| + throw MakeTypeError('not_an_object', [next]); |
| + } |
| + %_CallFunction(this, next.value, adder); |
| + } |
| } |
| @@ -117,12 +162,36 @@ SetUpSet(); |
| // ------------------------------------------------------------------- |
| // Harmony Map |
| -function MapConstructor() { |
| - if (%_IsConstructCall()) { |
| - %MapInitialize(this); |
| - } else { |
| +function MapConstructor(iterable) { |
| + if (!%_IsConstructCall()) { |
| throw MakeTypeError('constructor_not_function', ['Map']); |
| } |
| + |
| + var iter, adder; |
| + |
| + if (!IS_NULL_OR_UNDEFINED(iterable)) { |
| + iter = GetIterator(iterable); |
| + adder = this.set; |
| + if (!IS_SPEC_FUNCTION(adder)) { |
| + throw MakeTypeError('property_not_function', ['set', this]); |
| + } |
| + } |
| + |
| + %MapInitialize(this); |
| + |
| + if (IS_UNDEFINED(iter)) return; |
| + |
| + var next, done, nextItem; |
| + while (!(next = iter.next()).done) { |
| + if (!IS_SPEC_OBJECT(next)) { |
| + throw MakeTypeError('not_an_object', [next]); |
|
rossberg
2014/06/23 14:15:59
We typically give more specific error messages in
arv (Not doing code reviews)
2014/06/23 15:43:56
Great idea.
Done.
|
| + } |
| + nextItem = next.value; |
| + if (!IS_SPEC_OBJECT(nextItem)) { |
| + throw MakeTypeError('not_an_object', [nextItem]); |
| + } |
| + %_CallFunction(this, nextItem[0], nextItem[1], adder); |
| + } |
| } |