LibJS: Add ObjectPrototype and implement hasOwnProperty()

All Objects will now have ObjectPrototype as their prototype, unless
overridden.
This commit is contained in:
Andreas Kling 2020-03-15 15:25:43 +01:00
parent f1f14945cf
commit 23b1d97b0d
Notes: sideshowbarker 2024-07-19 08:17:46 +09:00
8 changed files with 114 additions and 0 deletions

View file

@ -26,6 +26,7 @@
#include <AK/String.h>
#include <LibJS/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/NativeFunction.h>
#include <LibJS/Object.h>
#include <LibJS/Value.h>
@ -34,6 +35,7 @@ namespace JS {
Object::Object()
{
set_prototype(interpreter().object_prototype());
}
Object::~Object()
@ -44,6 +46,7 @@ Value Object::get(String property_name) const
{
const Object* object = this;
while (object) {
dbg() << "Object::get() trying '" << property_name << "' in " << object->class_name();
auto value = object->m_properties.get(property_name);
if (value.has_value())
return value.value();
@ -71,4 +74,9 @@ void Object::visit_children(Cell::Visitor& visitor)
visitor.visit(it.value);
}
bool Object::has_own_property(const String& property_name) const
{
return m_properties.get(property_name).has_value();
}
}