From 51db9494bdca39837d483bec500a027e5f4455e1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 27 Apr 2013 16:54:17 +0800 Subject: [PATCH] Add API to return all keys of weak map. --- common/api/atom_api_id_weak_map.cc | 22 +++++++++++++++++++--- common/api/atom_api_id_weak_map.h | 3 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/common/api/atom_api_id_weak_map.cc b/common/api/atom_api_id_weak_map.cc index 3b4d975fadcc..6b356b8a22e1 100644 --- a/common/api/atom_api_id_weak_map.cc +++ b/common/api/atom_api_id_weak_map.cc @@ -10,14 +10,14 @@ namespace atom { namespace api { IDWeakMap::IDWeakMap() - : nextId_(0) { + : next_id_(0) { } IDWeakMap::~IDWeakMap() { v8::Isolate* isolate = v8::Isolate::GetCurrent(); auto copied_map = map_; - for (auto el : copied_map) + for (const auto& el : copied_map) Erase(isolate, el.first); } @@ -35,7 +35,7 @@ void IDWeakMap::Erase(v8::Isolate* isolate, int key) { } int IDWeakMap::GetNextID() { - return ++nextId_; + return ++next_id_; } // static @@ -102,6 +102,21 @@ v8::Handle IDWeakMap::Has(const v8::Arguments& args) { return v8::Boolean::New(obj->Has(key)); } +// static +v8::Handle IDWeakMap::Keys(const v8::Arguments& args) { + IDWeakMap* obj = ObjectWrap::Unwrap(args.This()); + + v8::Handle keys = v8::Array::New(obj->map_.size()); + + int i = 0; + for (const auto& el : obj->map_) { + keys->Set(i, v8::Integer::New(el.first)); + ++i; + } + + return keys; +} + // static v8::Handle IDWeakMap::Remove(const v8::Arguments& args) { if (!args[0]->IsNumber()) @@ -128,6 +143,7 @@ void IDWeakMap::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "add", Add); NODE_SET_PROTOTYPE_METHOD(t, "get", Get); NODE_SET_PROTOTYPE_METHOD(t, "has", Has); + NODE_SET_PROTOTYPE_METHOD(t, "keys", Keys); NODE_SET_PROTOTYPE_METHOD(t, "remove", Remove); target->Set(v8::String::NewSymbol("IDWeakMap"), t->GetFunction()); diff --git a/common/api/atom_api_id_weak_map.h b/common/api/atom_api_id_weak_map.h index ec28d57f4eee..c8904a345ca8 100644 --- a/common/api/atom_api_id_weak_map.h +++ b/common/api/atom_api_id_weak_map.h @@ -35,9 +35,10 @@ class IDWeakMap : public node::ObjectWrap { static v8::Handle Add(const v8::Arguments& args); static v8::Handle Get(const v8::Arguments& args); static v8::Handle Has(const v8::Arguments& args); + static v8::Handle Keys(const v8::Arguments& args); static v8::Handle Remove(const v8::Arguments& args); - int nextId_; + int next_id_; std::map> map_;