diff options
author | Linus Groh <mail@linusgroh.de> | 2021-10-02 14:20:39 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-10-02 14:53:06 +0100 |
commit | 84c9f3e0d04abaca9372a35c69b173aa43257817 (patch) | |
tree | 123982831d4a2016903073555aca18eea6be6a1a | |
parent | c500ebdcd58b5f4ff04775c84d13f205e63c5228 (diff) | |
download | serenity-84c9f3e0d04abaca9372a35c69b173aa43257817.zip |
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.
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/Object.cpp | 17 | ||||
-rw-r--r-- | 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<bool> 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<Value(VM&, GlobalObject&)> getter, Function<Value(VM&, GlobalObject&)> 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 }; |