summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Set.h
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-11-30 09:18:27 -0500
committerTim Flynn <trflynn89@pm.me>2022-11-30 13:05:57 -0500
commitc0952e3670e02a2b344cb2f96232ef06221fc8e1 (patch)
treebdd8da033550dd0d515be7057f035f2b320a9c64 /Userland/Libraries/LibJS/Runtime/Set.h
parent715e56a74cca71f1e8e0ce7b87e424edbf1e0c6a (diff)
downloadserenity-c0952e3670e02a2b344cb2f96232ef06221fc8e1.zip
LibJS: Do not allocate in Set's constructor
We are currently allocating in Set's constructor to create the set's underlying Map. This can cause GC to occur before the member is actually initialized, thus we will crash in Set::visit_edges trying to visit a member that does not exist. Instead, create the Map in Set::initialize, where we can allocate. Also change Map to be stored as a normal JS heap-allocated object, rather than as a stack variable.
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Set.h')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Set.h19
1 files changed, 10 insertions, 9 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Set.h b/Userland/Libraries/LibJS/Runtime/Set.h
index 5acf6ab5ab..05523aee96 100644
--- a/Userland/Libraries/LibJS/Runtime/Set.h
+++ b/Userland/Libraries/LibJS/Runtime/Set.h
@@ -19,28 +19,29 @@ class Set : public Object {
public:
static Set* create(Realm&);
+ virtual void initialize(Realm&) override;
virtual ~Set() override = default;
// NOTE: Unlike what the spec says, we implement Sets using an underlying map,
// so all the functions below do not directly implement the operations as
// defined by the specification.
- void set_clear() { m_values.map_clear(); }
- bool set_remove(Value const& value) { return m_values.map_remove(value); }
- bool set_has(Value const& key) const { return m_values.map_has(key); }
- void set_add(Value const& key) { m_values.map_set(key, js_undefined()); }
- size_t set_size() const { return m_values.map_size(); }
+ void set_clear() { m_values->map_clear(); }
+ bool set_remove(Value const& value) { return m_values->map_remove(value); }
+ bool set_has(Value const& key) const { return m_values->map_has(key); }
+ void set_add(Value const& key) { m_values->map_set(key, js_undefined()); }
+ size_t set_size() const { return m_values->map_size(); }
- auto begin() const { return m_values.begin(); }
- auto begin() { return m_values.begin(); }
- auto end() const { return m_values.end(); }
+ auto begin() const { return const_cast<Map const&>(*m_values).begin(); }
+ auto begin() { return m_values->begin(); }
+ auto end() const { return m_values->end(); }
private:
explicit Set(Object& prototype);
virtual void visit_edges(Visitor& visitor) override;
- Map m_values;
+ GCPtr<Map> m_values;
};
}