2021-06-12 05:28:30 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/HashMap.h>
|
2024-11-15 04:01:23 +13:00
|
|
|
#include <LibGC/WeakContainer.h>
|
2025-08-03 14:05:09 -07:00
|
|
|
#include <LibJS/Export.h>
|
2021-06-12 05:28:30 +03:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/Object.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-08-03 14:05:09 -07:00
|
|
|
class JS_API WeakMap final
|
2021-06-12 05:28:30 +03:00
|
|
|
: public Object
|
2024-11-15 04:01:23 +13:00
|
|
|
, public GC::WeakContainer {
|
2021-06-12 05:28:30 +03:00
|
|
|
JS_OBJECT(WeakMap, Object);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(WeakMap);
|
2021-06-12 05:28:30 +03:00
|
|
|
|
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<WeakMap> create(Realm&);
|
2021-06-12 05:28:30 +03:00
|
|
|
|
2022-03-14 10:25:06 -06:00
|
|
|
virtual ~WeakMap() override = default;
|
2021-06-12 05:28:30 +03:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
HashMap<GC::Ptr<Cell>, Value> const& values() const { return m_values; }
|
|
|
|
HashMap<GC::Ptr<Cell>, Value>& values() { return m_values; }
|
2021-06-12 05:28:30 +03:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
virtual void remove_dead_cells(Badge<GC::Heap>) override;
|
2021-06-12 05:28:30 +03:00
|
|
|
|
|
|
|
private:
|
2022-08-28 23:51:28 +02:00
|
|
|
explicit WeakMap(Object& prototype);
|
|
|
|
|
2021-09-11 19:19:40 +03:00
|
|
|
void visit_edges(Visitor&) override;
|
2021-09-11 16:44:40 +02:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
HashMap<GC::Ptr<Cell>, Value> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping
|
2021-06-12 05:28:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|