refactor: simplify KeyWeakMap impl (#43483)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2024-08-26 18:09:12 -04:00 committed by GitHub
parent 9a3618e912
commit 1fcd981e5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,9 +6,9 @@
#define ELECTRON_SHELL_COMMON_KEY_WEAK_MAP_H_ #define ELECTRON_SHELL_COMMON_KEY_WEAK_MAP_H_
#include <unordered_map> #include <unordered_map>
#include <utility>
#include <vector> #include <vector>
#include "base/containers/to_vector.h"
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "v8/include/v8-forward.h" #include "v8/include/v8-forward.h"
@ -18,10 +18,10 @@ namespace electron {
template <typename K> template <typename K>
class KeyWeakMap { class KeyWeakMap {
public: public:
KeyWeakMap() {} KeyWeakMap() = default;
~KeyWeakMap() { ~KeyWeakMap() {
for (auto& p : map_) for (auto& p : map_)
p.second.second.ClearWeak(); p.second.value.ClearWeak();
} }
// disable copy // disable copy
@ -29,55 +29,47 @@ class KeyWeakMap {
KeyWeakMap& operator=(const KeyWeakMap&) = delete; KeyWeakMap& operator=(const KeyWeakMap&) = delete;
// Sets the object to WeakMap with the given |key|. // Sets the object to WeakMap with the given |key|.
void Set(v8::Isolate* isolate, const K& key, v8::Local<v8::Object> object) { void Set(v8::Isolate* isolate, const K& key, v8::Local<v8::Object> value) {
KeyObject key_object = {key, this}; auto& mapped = map_[key] =
auto& p = map_[key] = Mapped{this, key, v8::Global<v8::Object>(isolate, value)};
std::make_pair(key_object, v8::Global<v8::Object>(isolate, object)); mapped.value.SetWeak(&mapped, OnObjectGC, v8::WeakCallbackType::kParameter);
p.second.SetWeak(&(p.first), OnObjectGC, v8::WeakCallbackType::kParameter);
} }
// Gets the object from WeakMap by its |key|. // Gets the object from WeakMap by its |key|.
v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, const K& key) { v8::MaybeLocal<v8::Object> Get(v8::Isolate* isolate, const K& key) {
if (auto iter = map_.find(key); iter != map_.end()) if (auto iter = map_.find(key); iter != map_.end())
return v8::Local<v8::Object>::New(isolate, iter->second.second); return v8::Local<v8::Object>::New(isolate, iter->second.value);
return {}; return {};
} }
// Returns all objects. // Returns all objects.
std::vector<v8::Local<v8::Object>> Values(v8::Isolate* isolate) const { std::vector<v8::Local<v8::Object>> Values(v8::Isolate* isolate) const {
std::vector<v8::Local<v8::Object>> values; return base::ToVector(map_, [isolate](const auto& iter) {
values.reserve(map_.size()); return v8::Local<v8::Object>::New(isolate, iter.second.value);
for (const auto& it : map_) });
values.emplace_back(
v8::Local<v8::Object>::New(isolate, it.second.second));
return values;
} }
// Remove object with |key| in the WeakMap. // Remove object with |key| in the WeakMap.
void Remove(const K& key) { void Remove(const K& key) {
auto iter = map_.find(key); if (auto item = map_.extract(key))
if (iter == map_.end()) item.mapped().value.ClearWeak();
return;
iter->second.second.ClearWeak();
map_.erase(iter);
} }
private: private:
// Records the key and self, used by SetWeak. // Records the key and self, used by SetWeak.
struct KeyObject { struct Mapped {
K key;
raw_ptr<KeyWeakMap> self; raw_ptr<KeyWeakMap> self;
K key;
v8::Global<v8::Object> value;
}; };
static void OnObjectGC( static void OnObjectGC(const v8::WeakCallbackInfo<Mapped>& data) {
const v8::WeakCallbackInfo<typename KeyWeakMap<K>::KeyObject>& data) { auto* mapped = data.GetParameter();
KeyWeakMap<K>::KeyObject* key_object = data.GetParameter(); mapped->self->Remove(mapped->key);
key_object->self->Remove(key_object->key);
} }
// Map of stored objects. // Map of stored objects.
std::unordered_map<K, std::pair<KeyObject, v8::Global<v8::Object>>> map_; std::unordered_map<K, Mapped> map_;
}; };
} // namespace electron } // namespace electron