// 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 #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 "native_mate/dictionary.h" #include "v8/include/v8-profiler.h" namespace { v8::Local GetHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key) { v8::Local context = isolate->GetCurrentContext(); v8::Local privateKey = v8::Private::ForApi(isolate, key); v8::Local value; v8::Maybe result = object->HasPrivate(context, privateKey); if (!(result.IsJust() && result.FromJust())) return v8::Local(); if (object->GetPrivate(context, privateKey).ToLocal(&value)) return value; return v8::Local(); } void SetHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key, v8::Local value) { if (value.IsEmpty()) return; v8::Local context = isolate->GetCurrentContext(); v8::Local privateKey = v8::Private::ForApi(isolate, key); object->SetPrivate(context, privateKey, value); } void DeleteHiddenValue(v8::Isolate* isolate, v8::Local object, v8::Local key) { v8::Local context = isolate->GetCurrentContext(); v8::Local 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 object) { return object->GetIdentityHash(); } void TakeHeapSnapshot(v8::Isolate* isolate) { isolate->GetHeapProfiler()->TakeHeapSnapshot(); } void Initialize(v8::Local exports, v8::Local unused, v8::Local 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); } } // namespace NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_v8_util, Initialize)