electron/atom/common/api/atom_api_id_weak_map.cc

81 lines
2 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2013-04-25 13:46:04 +00:00
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/common/api/atom_api_id_weak_map.h"
2013-04-25 13:46:04 +00:00
#include "native_mate/constructor.h"
#include "native_mate/object_template_builder.h"
#include "atom/common/node_includes.h"
2014-04-16 03:58:48 +00:00
2013-04-25 13:46:04 +00:00
namespace atom {
namespace api {
IDWeakMap::IDWeakMap() {
2013-04-25 13:46:04 +00:00
}
IDWeakMap::~IDWeakMap() {
}
2015-05-22 11:11:22 +00:00
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
return map_.Add(isolate, object);
2013-04-25 13:46:04 +00:00
}
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> IDWeakMap::Get(v8::Isolate* isolate, int32_t key) {
v8::MaybeLocal<v8::Object> result = map_.Get(isolate, key);
if (result.IsEmpty()) {
isolate->ThrowException(v8::Exception::Error(
mate::StringToV8(isolate, "Invalid key")));
return v8::Undefined(isolate);
} else {
return result.ToLocalChecked();
}
2013-04-25 13:46:04 +00:00
}
bool IDWeakMap::Has(int32_t key) const {
return map_.Has(key);
2013-04-25 13:46:04 +00:00
}
std::vector<int32_t> IDWeakMap::Keys() const {
return map_.Keys();
}
void IDWeakMap::Remove(int32_t key) {
map_.Remove(key);
2013-04-25 13:46:04 +00:00
}
2014-04-16 03:58:48 +00:00
// static
void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
2015-05-22 11:11:22 +00:00
v8::Local<v8::ObjectTemplate> prototype) {
2014-04-16 03:58:48 +00:00
mate::ObjectTemplateBuilder(isolate, prototype)
.SetMethod("add", &IDWeakMap::Add)
.SetMethod("get", &IDWeakMap::Get)
.SetMethod("has", &IDWeakMap::Has)
.SetMethod("keys", &IDWeakMap::Keys)
.SetMethod("remove", &IDWeakMap::Remove);
}
2013-04-25 13:46:04 +00:00
} // namespace api
} // namespace atom
2014-04-16 03:58:48 +00:00
namespace {
2015-05-22 11:11:22 +00:00
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
2014-04-16 03:58:48 +00:00
using atom::api::IDWeakMap;
v8::Isolate* isolate = context->GetIsolate();
2014-04-16 03:58:48 +00:00
v8::Local<v8::Function> constructor = mate::CreateConstructor<IDWeakMap>(
isolate,
"IDWeakMap",
base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
exports->Set(mate::StringToSymbol(isolate, "IDWeakMap"), constructor);
2014-04-16 03:58:48 +00:00
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_id_weak_map, Initialize)