Reviewers: Kevin Millikin,
Description:
Implement Object.keys for proxies.
[email protected]
BUG=
TEST=
Please review this at http://codereview.chromium.org/7321004/
SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge
Affected files:
M src/proxy.js
M src/v8natives.js
M test/mjsunit/harmony/proxies.js
Index: src/proxy.js
diff --git a/src/proxy.js b/src/proxy.js
index
cb9c020e35c60c9b0cdcf15e961ca9a8fdb73789..72b5c225652a71494c24d671bfcc67b5ad86891f
100644
--- a/src/proxy.js
+++ b/src/proxy.js
@@ -135,3 +135,10 @@ function DerivedSetTrap(receiver, name, val) {
function DerivedHasTrap(name) {
return !!this.getPropertyDescriptor(name)
}
+
+function DerivedKeysTrap() {
+ return this.getOwnPropertyNames().filter(
+ function(name) {
+ return this.getOwnPropertyDescriptor('' + name).enumerable
+ }.bind(this));
+}
Index: src/v8natives.js
diff --git a/src/v8natives.js b/src/v8natives.js
index
831dd14ef84b233764946a02aef650ac86a8ea04..2759819a5fca408128e241975eb53b2b9f66adf1
100644
--- a/src/v8natives.js
+++ b/src/v8natives.js
@@ -308,6 +308,13 @@ function ObjectLookupSetter(name) {
function ObjectKeys(obj) {
if (!IS_SPEC_OBJECT(obj))
throw MakeTypeError("obj_ctor_property_non_object", ["keys"]);
+ if (%IsJSProxy(obj)) {
+ var handler = %GetHandler(obj);
+ var keys = handler.keys;
+ if (IS_UNDEFINED(keys)) keys = DerivedKeysTrap;
+ var names = keys.call(handler);
+ return ToStringArray(names);
+ }
return %LocalKeys(obj);
}
Index: test/mjsunit/harmony/proxies.js
diff --git a/test/mjsunit/harmony/proxies.js
b/test/mjsunit/harmony/proxies.js
index
490877c05fa03ab74bd088950cd7c6727f842eb8..4d27c8e604887e1a83fc7126b14541307c5927ec
100644
--- a/test/mjsunit/harmony/proxies.js
+++ b/test/mjsunit/harmony/proxies.js
@@ -309,7 +309,7 @@ TestPrototype()
-// Property names (Object.getOwnPropertyNames).
+// Property names (Object.getOwnPropertyNames, Object.keys).
function TestPropertyNames(names, handler) {
var p = Proxy.create(handler)
@@ -331,3 +331,51 @@ TestPropertyNames(["[object Object]"], {
return function() { return [{}] }
}
})
+
+
+function TestKeys(names, handler) {
+ var p = Proxy.create(handler)
+ assertArrayEquals(names, Object.keys(p))
+}
+
+TestKeys([], {
+ keys: function() { return [] }
+})
+TestKeys(["a", "zz", " ", "0"], {
+ keys: function() { return ["a", "zz", " ", 0] }
+})
+TestKeys(["throw", "function "], {
+ keys: function() { return this.keys2() },
+ keys2: function() { return ["throw", "function "] }
+})
+TestKeys(["[object Object]"], {
+ get keys() {
+ return function() { return [{}] }
+ }
+})
+TestKeys(["a", "0"], {
+ getOwnPropertyNames: function() { return ["a", 23, "zz", "", 0] },
+ getOwnPropertyDescriptor: function(k) { return {enumerable: k.length ==
1} }
+})
+TestKeys(["23", "zz", ""], {
+ getOwnPropertyNames: function() { return this.getOwnPropertyNames2() },
+ getOwnPropertyNames2: function() { return ["a", 23, "zz", "", 0] },
+ getOwnPropertyDescriptor: function(k) {
+ return this.getOwnPropertyDescriptor2(k)
+ },
+ getOwnPropertyDescriptor2: function(k) { return {enumerable: k.length !=
1} }
+})
+TestKeys(["a", "b", "c", "5"], {
+ get getOwnPropertyNames() {
+ return function() { return ["0", 4, "a", "b", "c", 5] }
+ },
+ get getOwnPropertyDescriptor() {
+ return function(k) { return {enumerable: k >= "44"} }
+ }
+})
+TestKeys([], {
+ get getOwnPropertyNames() {
+ return function() { return ["a", "b", "c"] }
+ },
+ getOwnPropertyDescriptor: function(k) { return {} }
+})
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev