Commit 2ed1eebe authored by neis's avatar neis Committed by Commit bot

[es6] Implement Reflect.enumerate.

R=rossberg
BUG=v8:3931
LOG=n

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

Cr-Commit-Position: refs/heads/master@{#31299}
parent 0937cdbf
// Copyright 2013 the V8 project authors. All rights reserved. // Copyright 2013-2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
...@@ -12,9 +12,16 @@ var GlobalReflect = global.Reflect; ...@@ -12,9 +12,16 @@ var GlobalReflect = global.Reflect;
var ReflectApply = utils.ImportNow("reflect_apply"); var ReflectApply = utils.ImportNow("reflect_apply");
var ReflectConstruct = utils.ImportNow("reflect_construct"); var ReflectConstruct = utils.ImportNow("reflect_construct");
function ReflectEnumerate(obj) {
if (!IS_SPEC_OBJECT(obj))
throw MakeTypeError(kCalledOnNonObject, "Reflect.enumerate")
return (function* () { for (var x in obj) yield x })();
}
utils.InstallFunctions(GlobalReflect, DONT_ENUM, [ utils.InstallFunctions(GlobalReflect, DONT_ENUM, [
"apply", ReflectApply, "apply", ReflectApply,
"construct", ReflectConstruct "construct", ReflectConstruct,
"enumerate", ReflectEnumerate
]); ]);
}) })
// Copyright 2010-2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Test that properties deleted during an enumeration do not show up in
// the enumeration. This is adapted from mjsunit/for-in-delete.js.
// Flags: --harmony-reflect
function f(o, expected, del) {
var index = 0;
for (p of Reflect.enumerate(o)) {
if (del) delete o[del];
assertEquals(expected[index], p);
index++;
}
assertEquals(expected.length, index);
}
var o = {}
o.a = 1;
o.b = 2;
o.c = 3;
o.d = 3;
f(o, ['a', 'b', 'c', 'd']);
f(o, ['a', 'b', 'c', 'd']);
f(o, ['a', 'c', 'd'], 'b');
f(o, ['a', 'c'], 'd');
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This is adapted from mjsunit/for-in-opt.js.
// Flags: --harmony-proxies --harmony-reflect --allow-natives-syntax
"use strict";
function f(o) {
var result = [];
for (var i of Reflect.enumerate(Object(o))) {
result.push(i);
}
return result;
}
assertEquals(["0"], f("a"));
assertEquals(["0"], f("a"));
%OptimizeFunctionOnNextCall(f);
assertEquals(["0","1","2"], f("bla"));
// Test the lazy deopt points.
var keys = ["a", "b", "c", "d"];
var has_keys = [];
var deopt_has = false;
var deopt_enum = false;
var handler = {
enumerate: function(target) {
if (deopt_enum) {
%DeoptimizeFunction(f2);
deopt_enum = false;
}
return keys;
},
getPropertyDescriptor: function(k) {
if (deopt_has) {
%DeoptimizeFunction(f2);
deopt_has = false;
}
has_keys.push(k);
return {value: 10, configurable: true, writable: false, enumerable: true};
}
};
var proxy = Proxy.create(handler);
var o = {__proto__: proxy};
function f2(o) {
var result = [];
for (var i of Reflect.enumerate(o)) {
result.push(i);
}
return result;
}
function check_f2() {
assertEquals(keys, f2(o));
assertEquals(keys, has_keys);
has_keys.length = 0;
}
check_f2();
check_f2();
// Test lazy deopt after GetPropertyNamesFast
%OptimizeFunctionOnNextCall(f2);
deopt_enum = true;
check_f2();
// Test lazy deopt after FILTER_KEY
%OptimizeFunctionOnNextCall(f2);
deopt_has = true;
check_f2();
// Copyright 2008-2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This is adapted from mjsunit/for-in-special-cases.js.
// Flags: --harmony-reflect
function Accumulate(x) {
var accumulator = "";
for (var i of Reflect.enumerate(Object(x))) {
accumulator += i;
}
return accumulator;
}
for (var i = 0; i < 3; ++i) {
var elements = Accumulate("abcd");
// We do not assume that enumerate enumerates elements in order.
assertTrue(-1 != elements.indexOf("0"));
assertTrue(-1 != elements.indexOf("1"));
assertTrue(-1 != elements.indexOf("2"));
assertTrue(-1 != elements.indexOf("3"));
assertEquals(4, elements.length);
}
function for_in_string_prototype() {
var x = new String("abc");
x.foo = 19;
function B() {
this.bar = 5;
this[7] = 4;
}
B.prototype = x;
var y = new B();
y.gub = 13;
var elements = Accumulate(y);
var elements1 = Accumulate(y);
// If enumerate returns elements in a different order on multiple calls, this
// assert will fail. If that happens, consider if that behavior is OK.
assertEquals(elements, elements1, "Enumeration not the same both times.");
// We do not assume that enumerate enumerates elements in order.
assertTrue(-1 != elements.indexOf("0"));
assertTrue(-1 != elements.indexOf("1"));
assertTrue(-1 != elements.indexOf("2"));
assertTrue(-1 != elements.indexOf("7"));
assertTrue(-1 != elements.indexOf("foo"));
assertTrue(-1 != elements.indexOf("bar"));
assertTrue(-1 != elements.indexOf("gub"));
assertEquals(13, elements.length);
elements = Accumulate(x);
assertTrue(-1 != elements.indexOf("0"));
assertTrue(-1 != elements.indexOf("1"));
assertTrue(-1 != elements.indexOf("2"));
assertTrue(-1 != elements.indexOf("foo"));
assertEquals(6, elements.length);
}
for_in_string_prototype();
for_in_string_prototype();
// Copyright 2008-2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This is adapted from mjsunit/for-in.js.
// Flags: --harmony-reflect
function props(x) {
var array = [];
for (var p of Reflect.enumerate(x)) array.push(p);
return array.sort();
}
assertEquals(0, props({}).length, "olen0");
assertEquals(1, props({x:1}).length, "olen1");
assertEquals(2, props({x:1, y:2}).length, "olen2");
assertArrayEquals(["x"], props({x:1}), "x");
assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy");
assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom");
assertEquals(0, props([]).length, "alen0");
assertEquals(1, props([1]).length, "alen1");
assertEquals(2, props([1,2]).length, "alen2");
assertArrayEquals(["0"], props([1]), "0");
assertArrayEquals(["0", "1"], props([1,2]), "01");
assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012");
var o = {};
var a = [];
for (var i = 0x0020; i < 0x01ff; i+=2) {
var s = 'char:' + String.fromCharCode(i);
a.push(s);
o[s] = i;
}
assertArrayEquals(a, props(o), "charcodes");
var a = [];
assertEquals(0, props(a).length, "proplen0");
a[Math.pow(2,30)-1] = 0;
assertEquals(1, props(a).length, "proplen1");
a[Math.pow(2,31)-1] = 0;
assertEquals(2, props(a).length, "proplen2");
a[1] = 0;
assertEquals(3, props(a).length, "proplen3");
var result = '';
for (var p of Reflect.enumerate({a : [0], b : 1})) { result += p; }
assertEquals('ab', result, "ab");
var result = '';
for (var p of Reflect.enumerate({a : {v:1}, b : 1})) { result += p; }
assertEquals('ab', result, "ab-nodeep");
var result = '';
for (var p of Reflect.enumerate({ get a() {}, b : 1})) { result += p; }
assertEquals('ab', result, "abget");
var result = '';
for (var p of Reflect.enumerate({ get a() {}, set a(x) {}, b : 1})) {
result += p;
}
assertEquals('ab', result, "abgetset");
(function() {
var large_key = 2147483650;
var o = {__proto__: {}};
o[large_key] = 1;
o.__proto__[large_key] = 1;
var keys = [];
for (var k of Reflect.enumerate(o)) {
keys.push(k);
}
assertEquals(["2147483650"], keys);
})();
...@@ -256,3 +256,23 @@ function prepare(tgt) { ...@@ -256,3 +256,23 @@ function prepare(tgt) {
assertFalse(Reflect.isExtensible(tgt)); assertFalse(Reflect.isExtensible(tgt));
} }
})(); })();
////////////////////////////////////////////////////////////////////////////////
// Reflect.enumerate
(function testReflectEnumerateArity() {
assertEquals(1, Reflect.enumerate.length);
})();
(function testReflectEnumerateOnNonObject() {
assertThrows(function() { Reflect.enumerate(); }, TypeError);
assertThrows(function() { Reflect.enumerate(42); }, TypeError);
assertThrows(function() { Reflect.enumerate(null); }, TypeError);
})();
// See reflect-enumerate*.js for further tests.
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