129 lines
4.5 KiB
C++
129 lines
4.5 KiB
C++
// Copyright (c) 2013 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "atom/common/api/atom_api_key_weak_map.h"
|
|
#include "atom/common/api/remote_callback_freer.h"
|
|
#include "atom/common/api/remote_object_freer.h"
|
|
#include "atom/common/native_mate_converters/content_converter.h"
|
|
#include "atom/common/node_includes.h"
|
|
#include "base/hash.h"
|
|
#include "native_mate/dictionary.h"
|
|
#include "v8/include/v8-profiler.h"
|
|
|
|
// Following code should be removed after we upgraded to Chrome 50.
|
|
#if !defined(COMPILER_MSVC)
|
|
namespace base {
|
|
|
|
template <typename T1, typename T2>
|
|
inline size_t HashInts(T1 value1, T2 value2) {
|
|
// This condition is expected to be compile-time evaluated and optimised away
|
|
// in release builds.
|
|
if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t)))
|
|
return HashInts64(value1, value2);
|
|
|
|
return HashInts32(value1, value2);
|
|
}
|
|
|
|
} // namespace base
|
|
|
|
namespace std {
|
|
|
|
// The hash function used by DoubleIDWeakMap.
|
|
template <typename Type1, typename Type2>
|
|
struct hash<std::pair<Type1, Type2>> {
|
|
std::size_t operator()(std::pair<Type1, Type2> value) const {
|
|
return base::HashInts<Type1, Type2>(value.first, value.second);
|
|
}
|
|
};
|
|
|
|
} // namespace std
|
|
#endif // defined(COMPILER_MSVC)
|
|
|
|
namespace mate {
|
|
|
|
template<typename Type1, typename Type2>
|
|
struct Converter<std::pair<Type1, Type2>> {
|
|
static bool FromV8(v8::Isolate* isolate,
|
|
v8::Local<v8::Value> val,
|
|
std::pair<Type1, Type2>* out) {
|
|
if (!val->IsArray())
|
|
return false;
|
|
|
|
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
|
|
if (array->Length() != 2)
|
|
return false;
|
|
return Converter<Type1>::FromV8(isolate, array->Get(0), &out->first) &&
|
|
Converter<Type2>::FromV8(isolate, array->Get(1), &out->second);
|
|
}
|
|
};
|
|
|
|
} // namespace mate
|
|
|
|
namespace {
|
|
|
|
v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
|
|
v8::Local<v8::Object> object,
|
|
v8::Local<v8::String> key) {
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
v8::Local<v8::Value> value;
|
|
v8::Maybe<bool> result = object->HasPrivate(context, privateKey);
|
|
if (!(result.IsJust() && result.FromJust()))
|
|
return v8::Local<v8::Value>();
|
|
if (object->GetPrivate(context, privateKey).ToLocal(&value))
|
|
return value;
|
|
return v8::Local<v8::Value>();
|
|
}
|
|
|
|
void SetHiddenValue(v8::Isolate* isolate,
|
|
v8::Local<v8::Object> object,
|
|
v8::Local<v8::String> key,
|
|
v8::Local<v8::Value> value) {
|
|
if (value.IsEmpty())
|
|
return;
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
object->SetPrivate(context, privateKey, value);
|
|
}
|
|
|
|
void DeleteHiddenValue(v8::Isolate* isolate,
|
|
v8::Local<v8::Object> object,
|
|
v8::Local<v8::String> key) {
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
|
|
// Actually deleting the value would make force the object into
|
|
// dictionary mode which is unnecessarily slow. Instead, we replace
|
|
// the hidden value with "undefined".
|
|
object->SetPrivate(context, privateKey, v8::Undefined(isolate));
|
|
}
|
|
|
|
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
|
return object->GetIdentityHash();
|
|
}
|
|
|
|
void TakeHeapSnapshot(v8::Isolate* isolate) {
|
|
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
|
}
|
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context, void* priv) {
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
|
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
|
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
|
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
|
dict.SetMethod("setRemoteCallbackFreer", &atom::RemoteCallbackFreer::BindTo);
|
|
dict.SetMethod("setRemoteObjectFreer", &atom::RemoteObjectFreer::BindTo);
|
|
dict.SetMethod("createIDWeakMap", &atom::api::KeyWeakMap<int32_t>::Create);
|
|
dict.SetMethod("createDoubleIDWeakMap",
|
|
&atom::api::KeyWeakMap<std::pair<int32_t, int32_t>>::Create);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_v8_util, Initialize)
|