From 84c9f3e0d04abaca9372a35c69b173aa43257817 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 2 Oct 2021 14:20:39 +0100 Subject: [PATCH] LibJS: Add Object::set_prototype() This is just factoring out step "9. Set O.[[Prototype]] to V." of 10.1.2 [[SetPrototypeOf]] into its own method so that we don't have to use internal_set_prototype_of() for setting an object prototype in all cases. --- Userland/Libraries/LibJS/Runtime/Object.cpp | 17 ++++++++++++----- Userland/Libraries/LibJS/Runtime/Object.h | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index b793bc95c4..d8b34558f5 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -546,11 +546,7 @@ ThrowCompletionOr Object::internal_set_prototype_of(Object* new_prototype) } // 9. Set O.[[Prototype]] to V. - auto& shape = this->shape(); - if (shape.is_unique()) - shape.set_prototype_without_transition(new_prototype); - else - m_shape = shape.create_prototype_transition(new_prototype); + set_prototype(new_prototype); // 10. Return true. return true; @@ -983,6 +979,17 @@ void Object::storage_delete(PropertyName const& property_name) m_storage.remove(metadata->offset); } +void Object::set_prototype(Object* new_prototype) +{ + if (prototype() == new_prototype) + return; + auto& shape = this->shape(); + if (shape.is_unique()) + shape.set_prototype_without_transition(new_prototype); + else + m_shape = shape.create_prototype_transition(new_prototype); +} + void Object::define_native_accessor(PropertyName const& property_name, Function getter, Function setter, PropertyAttributes attribute) { auto& vm = this->vm(); diff --git a/Userland/Libraries/LibJS/Runtime/Object.h b/Userland/Libraries/LibJS/Runtime/Object.h index aa262f817f..cfeaecc7a6 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.h +++ b/Userland/Libraries/LibJS/Runtime/Object.h @@ -171,6 +171,8 @@ protected: explicit Object(GlobalObjectTag); Object(ConstructWithoutPrototypeTag, GlobalObject&); + void set_prototype(Object*); + // [[Extensible]] bool m_is_extensible { true };