1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:38:11 +00:00

LibJS: Implement Object.create()

This commit is contained in:
Linus Groh 2021-04-10 18:40:12 +02:00 committed by Andreas Kling
parent da8a35a79e
commit 9cd010167a
4 changed files with 87 additions and 0 deletions

View file

@ -64,6 +64,7 @@ void ObjectConstructor::initialize(GlobalObject& global_object)
define_native_function(vm.names.keys, keys, 1, attr);
define_native_function(vm.names.values, values, 1, attr);
define_native_function(vm.names.entries, entries, 1, attr);
define_native_function(vm.names.create, create, 2, attr);
}
ObjectConstructor::~ObjectConstructor()
@ -311,4 +312,31 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::entries)
return Array::create_from(global_object, obj_arg->get_enumerable_own_property_names(PropertyKind::KeyAndValue));
}
// 20.1.2.2 Object.create, https://tc39.es/ecma262/#sec-object.create
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::create)
{
auto prototype_value = vm.argument(0);
auto properties = vm.argument(1);
Object* prototype;
if (prototype_value.is_null()) {
prototype = nullptr;
} else if (prototype_value.is_object()) {
prototype = &prototype_value.as_object();
} else {
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeWrongType);
return {};
}
auto* object = Object::create_empty(global_object);
object->set_prototype(prototype);
if (!properties.is_undefined()) {
object->define_properties(properties);
if (vm.exception())
return {};
}
return object;
}
}