2014-10-31 18:17:05 +00:00
|
|
|
// 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
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
#include "native_mate/constructor.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
2013-07-29 11:14:35 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#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 {
|
|
|
|
|
2015-06-24 05:35:39 +00:00
|
|
|
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) {
|
2015-06-24 05:35:39 +00:00
|
|
|
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) {
|
2015-06-24 05:35:39 +00:00
|
|
|
v8::MaybeLocal<v8::Object> result = map_.Get(isolate, key);
|
|
|
|
if (result.IsEmpty()) {
|
|
|
|
isolate->ThrowException(v8::Exception::Error(
|
|
|
|
mate::StringToV8(isolate, "Invalid key")));
|
2014-06-28 14:33:00 +00:00
|
|
|
return v8::Undefined(isolate);
|
2015-06-24 05:35:39 +00:00
|
|
|
} else {
|
|
|
|
return result.ToLocalChecked();
|
2014-04-16 01:54:01 +00:00
|
|
|
}
|
2013-04-25 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
bool IDWeakMap::Has(int32_t key) const {
|
2015-06-24 05:35:39 +00:00
|
|
|
return map_.Has(key);
|
2013-04-25 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
std::vector<int32_t> IDWeakMap::Keys() const {
|
2015-06-24 05:35:39 +00:00
|
|
|
return map_.Keys();
|
2013-04-27 08:54:17 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 01:54:01 +00:00
|
|
|
void IDWeakMap::Remove(int32_t key) {
|
2015-06-24 05:35:39 +00:00
|
|
|
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);
|
|
|
|
}
|
2014-04-16 01:54:01 +00:00
|
|
|
|
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;
|
2014-06-29 12:48:44 +00:00
|
|
|
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>));
|
2015-06-24 05:35:39 +00:00
|
|
|
exports->Set(mate::StringToSymbol(isolate, "IDWeakMap"), constructor);
|
2014-04-16 03:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_id_weak_map, Initialize)
|