From: Cheng Zhao Date: Sat, 27 Apr 2013 08:54:17 +0000 (+0800) Subject: Add API to return all keys of weak map. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=51db9494bdca39837d483bec500a027e5f4455e1;p=platform%2Fframework%2Fweb%2Fcrosswalk-tizen.git Add API to return all keys of weak map. --- diff --git a/common/api/atom_api_id_weak_map.cc b/common/api/atom_api_id_weak_map.cc index 3b4d975..6b356b8 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 @@ -103,6 +103,21 @@ v8::Handle IDWeakMap::Has(const v8::Arguments& args) { } // 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()) return node::ThrowTypeError("Bad argument"); @@ -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 ec28d57..c8904a3 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_;