mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:27:45 +00:00
LibJS: Start implementing object shapes
This patch adds JS::Shape, which implements a transition tree for our Object class. Object property keys, prototypes and attributes are now stored in a Shape, and each Object has a Shape. When adding a property to an Object, we make a transition from the old Shape to a new Shape. If we've made the same exact transition in the past (with another Object), we reuse the same transition and both objects may now share a Shape. This will become the foundation of inline caching and other engine optimizations in the future. :^)
This commit is contained in:
parent
3906d2b46a
commit
5e6e1fd482
10 changed files with 252 additions and 29 deletions
|
@ -40,6 +40,9 @@ public:
|
|||
Object();
|
||||
virtual ~Object();
|
||||
|
||||
Shape& shape() { return *m_shape; }
|
||||
const Shape& shape() const { return *m_shape; }
|
||||
|
||||
Optional<Value> get(const FlyString& property_name) const;
|
||||
void put(const FlyString& property_name, Value);
|
||||
|
||||
|
@ -60,9 +63,9 @@ public:
|
|||
virtual const char* class_name() const override { return "Object"; }
|
||||
virtual void visit_children(Cell::Visitor&) override;
|
||||
|
||||
Object* prototype() { return m_prototype; }
|
||||
const Object* prototype() const { return m_prototype; }
|
||||
void set_prototype(Object* prototype) { m_prototype = prototype; }
|
||||
Object* prototype();
|
||||
const Object* prototype() const;
|
||||
void set_prototype(Object*);
|
||||
bool has_prototype(const Object* prototype) const;
|
||||
|
||||
bool has_own_property(const FlyString& property_name) const;
|
||||
|
@ -76,11 +79,13 @@ public:
|
|||
virtual Value to_primitive(PreferredType preferred_type = PreferredType::Default) const;
|
||||
virtual Value to_string() const;
|
||||
|
||||
const HashMap<FlyString, Value>& own_properties() const { return m_properties; }
|
||||
Value get_direct(size_t index) const { return m_storage[index]; }
|
||||
|
||||
private:
|
||||
HashMap<FlyString, Value> m_properties;
|
||||
Object* m_prototype { nullptr };
|
||||
void set_shape(Shape&);
|
||||
|
||||
Shape* m_shape { nullptr };
|
||||
Vector<Value> m_storage;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue