LibJS: Implement Object.getOwnPropertyDescriptors()

This commit is contained in:
Idan Horowitz 2021-07-06 19:41:54 +03:00 committed by Linus Groh
parent 0f91883b17
commit 5e621e494f
Notes: sideshowbarker 2024-07-18 10:16:40 +09:00
3 changed files with 37 additions and 0 deletions

View file

@ -156,6 +156,7 @@ namespace JS {
P(getMinutes) \
P(getMonth) \
P(getOwnPropertyDescriptor) \
P(getOwnPropertyDescriptors) \
P(getOwnPropertyNames) \
P(getOwnPropertySymbols) \
P(getPrototypeOf) \

View file

@ -37,6 +37,7 @@ void ObjectConstructor::initialize(GlobalObject& global_object)
define_native_function(vm.names.defineProperties, define_properties, 2, attr);
define_native_function(vm.names.is, is, 2, attr);
define_native_function(vm.names.getOwnPropertyDescriptor, get_own_property_descriptor, 2, attr);
define_native_function(vm.names.getOwnPropertyDescriptors, get_own_property_descriptors, 1, attr);
define_native_function(vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
define_native_function(vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
define_native_function(vm.names.getPrototypeOf, get_prototype_of, 1, attr);
@ -304,6 +305,40 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
return from_property_descriptor(global_object, descriptor);
}
// 20.1.2.9 Object.getOwnPropertyDescriptors ( O ), https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptors)
{
// 1. Let obj be ? ToObject(O).
auto* object = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
// 2. Let ownKeys be ? obj.[[OwnPropertyKeys]]().
auto own_keys = object->internal_own_property_keys();
if (vm.exception())
return {};
// 3. Let descriptors be ! OrdinaryObjectCreate(%Object.prototype%).
auto* descriptors = Object::create(global_object, global_object.object_prototype());
// 4. For each element key of ownKeys, do
for (auto& key : own_keys) {
auto property_name = PropertyName::from_value(global_object, key);
// a. Let desc be ? obj.[[GetOwnProperty]](key).
auto desc = object->internal_get_own_property(property_name);
if (vm.exception())
return {};
// b. Let descriptor be ! FromPropertyDescriptor(desc).
auto descriptor = from_property_descriptor(global_object, desc);
// c. If descriptor is not undefined, perform ! CreateDataPropertyOrThrow(descriptors, key, descriptor).
if (!descriptor.is_undefined())
descriptors->create_data_property_or_throw(property_name, descriptor);
}
// 5. Return descriptors.
return descriptors;
}
// 20.1.2.4 Object.defineProperty ( O, P, Attributes ), https://tc39.es/ecma262/#sec-object.defineproperty
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property)
{

View file

@ -29,6 +29,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(define_properties);
JS_DECLARE_NATIVE_FUNCTION(is);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_descriptor);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_descriptors);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_names);
JS_DECLARE_NATIVE_FUNCTION(get_own_property_symbols);
JS_DECLARE_NATIVE_FUNCTION(get_prototype_of);