Chromium Code Reviews| Index: src/v8natives.js |
| diff --git a/src/v8natives.js b/src/v8natives.js |
| index b715e8923ff26184b43d2f5f432e9e5a3cf778d3..05b09ba3f79923a44660b027fa3ed8fb11102f70 100644 |
| --- a/src/v8natives.js |
| +++ b/src/v8natives.js |
| @@ -1038,48 +1038,61 @@ function ToNameArray(obj, trap, includeSymbols) { |
| } |
| -// ES5 section 15.2.3.4. |
| -function ObjectGetOwnPropertyNames(obj) { |
| - if (!IS_SPEC_OBJECT(obj)) { |
| - throw MakeTypeError("called_on_non_object", ["Object.getOwnPropertyNames"]); |
| - } |
| - // Special handling for proxies. |
| - if (%IsJSProxy(obj)) { |
| - var handler = %GetHandler(obj); |
| - var names = CallTrap0(handler, "getOwnPropertyNames", UNDEFINED); |
| - return ToNameArray(names, "getOwnPropertyNames", false); |
| +function FilterKeyNames(array, symbolsOnly) { |
|
rossberg
2013/12/18 16:50:12
Why can't we fold this into the loop at line 1108,
arv (Not doing code reviews)
2013/12/18 18:07:12
Done.
That would make more sense. And now I under
rossberg
2013/12/19 10:28:04
Yes, sorry if that was too cryptic.
|
| + var result = new InternalArray(); |
| + var j = 0; |
| + for (var i = 0; i < array.length; i++) { |
| + var key = array[i]; |
| + if (IS_SYMBOL(key)) { |
| + if (symbolsOnly && !IS_PRIVATE(key)) { |
| + result[j++] = key; |
| + } |
| + } else if (!symbolsOnly) { |
| + result[j++] = key; |
| + } |
| } |
| + result.length = j; |
| + return result; |
| +} |
| + |
| +function ObjectGetOwnPropertyKeys(obj, symbolsOnly) { |
| var nameArrays = new InternalArray(); |
| + var filter = symbolsOnly ? |
| + PROPERTY_ATTRIBUTES_STRING | PROPERTY_ATTRIBUTES_PRIVATE_SYMBOL : |
| + PROPERTY_ATTRIBUTES_SYMBOLIC; |
| // Find all the indexed properties. |
| - // Get the local element names. |
| - var localElementNames = %GetLocalElementNames(obj); |
| - for (var i = 0; i < localElementNames.length; ++i) { |
| - localElementNames[i] = %_NumberToString(localElementNames[i]); |
| - } |
| - nameArrays.push(localElementNames); |
| - |
| - // Get names for indexed interceptor properties. |
| - var interceptorInfo = %GetInterceptorInfo(obj); |
| - if ((interceptorInfo & 1) != 0) { |
| - var indexedInterceptorNames = %GetIndexedInterceptorElementNames(obj); |
| - if (!IS_UNDEFINED(indexedInterceptorNames)) { |
| - nameArrays.push(indexedInterceptorNames); |
| + // Only get the local element names if we want to include string keys. |
| + if (!symbolsOnly) { |
| + var localElementNames = %GetLocalElementNames(obj); |
| + for (var i = 0; i < localElementNames.length; ++i) { |
| + localElementNames[i] = %_NumberToString(localElementNames[i]); |
| + } |
| + nameArrays.push(localElementNames); |
| + |
| + // Get names for indexed interceptor properties. |
| + var interceptorInfo = %GetInterceptorInfo(obj); |
| + if ((interceptorInfo & 1) != 0) { |
| + var indexedInterceptorNames = %GetIndexedInterceptorElementNames(obj); |
| + if (!IS_UNDEFINED(indexedInterceptorNames)) { |
| + nameArrays.push(indexedInterceptorNames); |
| + } |
| } |
| } |
| // Find all the named properties. |
| // Get the local property names. |
| - nameArrays.push(%GetLocalPropertyNames(obj, false)); |
| + nameArrays.push(%GetLocalPropertyNames(obj, filter)); |
| // Get names for named interceptor properties if any. |
| if ((interceptorInfo & 2) != 0) { |
| - var namedInterceptorNames = %GetNamedInterceptorPropertyNames(obj); |
| + var namedInterceptorNames = |
| + %GetNamedInterceptorPropertyNames(obj); |
| if (!IS_UNDEFINED(namedInterceptorNames)) { |
| - nameArrays.push(namedInterceptorNames); |
| + nameArrays.push(FilterKeyNames(namedInterceptorNames, symbolsOnly)); |
| } |
| } |
| @@ -1090,18 +1103,15 @@ function ObjectGetOwnPropertyNames(obj) { |
| // Property names are expected to be unique strings, |
| // but interceptors can interfere with that assumption. |
| if (interceptorInfo != 0) { |
| - var propertySet = { __proto__: null }; |
| + var seenKeys = { __proto__: null }; |
| var j = 0; |
| for (var i = 0; i < propertyNames.length; ++i) { |
| - if (IS_SYMBOL(propertyNames[i])) continue; |
| - var name = ToString(propertyNames[i]); |
| - // We need to check for the exact property value since for intrinsic |
| - // properties like toString if(propertySet["toString"]) will always |
| - // succeed. |
| - if (propertySet[name] === true) { |
| - continue; |
| + var name = propertyNames[i]; |
| + if (!symbolsOnly) { |
| + name = ToString(name); |
| } |
| - propertySet[name] = true; |
| + if (seenKeys[name]) continue; |
| + seenKeys[name] = true; |
| propertyNames[j++] = name; |
| } |
| propertyNames.length = j; |
| @@ -1111,6 +1121,35 @@ function ObjectGetOwnPropertyNames(obj) { |
| } |
| +// ES6 19.1.2.8 |
| +function ObjectGetOwnPropertySymbols(obj) { |
| + if (!IS_SPEC_OBJECT(obj)) { |
| + throw MakeTypeError("called_on_non_object", |
| + ["Object.getOwnPropertySymbols"]); |
| + } |
| + |
| + // TODO(arv): Proxies use a shared trap for String and Symbol keys. |
| + |
| + return ObjectGetOwnPropertyKeys(obj, true); |
| +} |
| + |
| + |
| +// ES5 section 15.2.3.4. |
| +function ObjectGetOwnPropertyNames(obj) { |
| + if (!IS_SPEC_OBJECT(obj)) { |
| + throw MakeTypeError("called_on_non_object", ["Object.getOwnPropertyNames"]); |
| + } |
| + // Special handling for proxies. |
| + if (%IsJSProxy(obj)) { |
| + var handler = %GetHandler(obj); |
| + var names = CallTrap0(handler, "getOwnPropertyNames", UNDEFINED); |
| + return ToNameArray(names, "getOwnPropertyNames", false); |
| + } |
| + |
| + return ObjectGetOwnPropertyKeys(obj, false); |
| +} |
| + |
| + |
| // ES5 section 15.2.3.5. |
| function ObjectCreate(proto, properties) { |
| if (!IS_SPEC_OBJECT(proto) && proto !== null) { |
| @@ -1420,12 +1459,14 @@ function SetUpObject() { |
| "getPrototypeOf", ObjectGetPrototypeOf, |
| "getOwnPropertyDescriptor", ObjectGetOwnPropertyDescriptor, |
| "getOwnPropertyNames", ObjectGetOwnPropertyNames, |
| + // getOwnPropertySymbols is added in symbol.js. |
| "is", ObjectIs, |
| "isExtensible", ObjectIsExtensible, |
| "isFrozen", ObjectIsFrozen, |
| "isSealed", ObjectIsSealed, |
| "preventExtensions", ObjectPreventExtension, |
| "seal", ObjectSeal |
| + // observe is added in object-observe.js. |
| )); |
| } |