Commit f7c49baf authored by rossberg@chromium.org's avatar rossberg@chromium.org

ES6: Implement WeakMap and WeakSet constructor logic

Now that iterators are enabled by default we need to correctly
handle the parameter for WeakMap and WeakSet. If provided then the
argument is iterated over to add entries to the WeakMap and WeakSet.

BUG=v8:3399
LOG=Y
R=adamk@chromium.org, rossberg@chromium.org

Review URL: https://codereview.chromium.org/448013005

Patch from Erik Arvidsson <arv@chromium.org>.

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22999 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 8627bd53
......@@ -12,35 +12,6 @@ var $Set = global.Set;
var $Map = global.Map;
// TODO(arv): Move these general functions to v8natives.js when Map and Set are
// no longer experimental.
// 7.4.1 CheckIterable ( obj )
function ToIterable(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 = ToIterable(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
......
......@@ -1875,3 +1875,33 @@ function SetUpFunction() {
}
SetUpFunction();
// ----------------------------------------------------------------------------
// Iterator related spec functions.
// ES6 rev 26, 2014-07-18
// 7.4.1 CheckIterable ( obj )
function ToIterable(obj) {
if (!IS_SPEC_OBJECT(obj)) {
return UNDEFINED;
}
return obj[symbolIterator];
}
// ES6 rev 26, 2014-07-18
// 7.4.2 GetIterator ( obj, method )
function GetIterator(obj, method) {
if (IS_UNDEFINED(method)) {
method = ToIterable(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;
}
......@@ -15,12 +15,36 @@ var $WeakSet = global.WeakSet;
// -------------------------------------------------------------------
// Harmony WeakMap
function WeakMapConstructor() {
if (%_IsConstructCall()) {
%WeakCollectionInitialize(this);
} else {
function WeakMapConstructor(iterable) {
if (!%_IsConstructCall()) {
throw MakeTypeError('constructor_not_function', ['WeakMap']);
}
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]);
}
}
%WeakCollectionInitialize(this);
if (IS_UNDEFINED(iter)) return;
var next, done, nextItem;
while (!(next = iter.next()).done) {
if (!IS_SPEC_OBJECT(next)) {
throw MakeTypeError('iterator_result_not_an_object', [next]);
}
nextItem = next.value;
if (!IS_SPEC_OBJECT(nextItem)) {
throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
}
%_CallFunction(this, nextItem[0], nextItem[1], adder);
}
}
......@@ -107,12 +131,32 @@ SetUpWeakMap();
// -------------------------------------------------------------------
// Harmony WeakSet
function WeakSetConstructor() {
if (%_IsConstructCall()) {
%WeakCollectionInitialize(this);
} else {
function WeakSetConstructor(iterable) {
if (!%_IsConstructCall()) {
throw MakeTypeError('constructor_not_function', ['WeakSet']);
}
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]);
}
}
%WeakCollectionInitialize(this);
if (IS_UNDEFINED(iter)) return;
var next, done;
while (!(next = iter.next()).done) {
if (!IS_SPEC_OBJECT(next)) {
throw MakeTypeError('iterator_result_not_an_object', [next]);
}
%_CallFunction(this, next.value, adder);
}
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment