// Copyright (c) 2015 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include "atom/common/id_weak_map.h" #include #include "native_mate/converter.h" namespace atom { IDWeakMap::IDWeakMap() : next_id_(0) { } IDWeakMap::~IDWeakMap() { } int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local object) { int32_t id = GetNextID(); object->SetHiddenValue(mate::StringToSymbol(isolate, "IDWeakMapKey"), mate::Converter::ToV8(isolate, id)); auto global = make_linked_ptr(new v8::Global(isolate, object)); global->SetWeak(this, &WeakCallback); map_.emplace(id, global); return id; } v8::MaybeLocal IDWeakMap::Get(v8::Isolate* isolate, int32_t id) { auto iter = map_.find(id); if (iter == map_.end()) return v8::MaybeLocal(); else return v8::Local::New(isolate, *iter->second); } bool IDWeakMap::Has(int32_t id) const { return map_.find(id) != map_.end(); } std::vector IDWeakMap::Keys() const { std::vector keys; keys.reserve(map_.size()); for (const auto& iter : map_) keys.emplace_back(iter.first); return keys; } void IDWeakMap::Remove(int32_t id) { auto iter = map_.find(id); if (iter == map_.end()) LOG(WARNING) << "Removing unexist object with ID " << id; else map_.erase(iter); } void IDWeakMap::Clear() { map_.clear(); } int32_t IDWeakMap::GetNextID() { return ++next_id_; } // static void IDWeakMap::WeakCallback( const v8::WeakCallbackData& data) { int32_t id = data.GetValue()->GetHiddenValue( mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value(); data.GetParameter()->Remove(id); } } // namespace atom