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 08:30:31 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-12-29 02:29:48 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/api/object_life_monitor.h"
|
2015-09-07 08:29:54 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2015-12-29 02:29:48 +00:00
|
|
|
#include "base/stl_util.h"
|
2014-04-16 07:31:59 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
2013-08-27 09:47:44 +00:00
|
|
|
#include "v8/include/v8-profiler.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2013-04-25 08:30:31 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-12-29 02:40:10 +00:00
|
|
|
// A Persistent that can be copied and will not free itself.
|
|
|
|
template<class T>
|
|
|
|
struct LeakedPersistentTraits {
|
|
|
|
typedef v8::Persistent<T, LeakedPersistentTraits<T> > LeakedPersistent;
|
|
|
|
static const bool kResetInDestructor = false;
|
|
|
|
template<class S, class M>
|
|
|
|
static V8_INLINE void Copy(const v8::Persistent<S, M>& source,
|
|
|
|
LeakedPersistent* dest) {
|
|
|
|
// do nothing, just allow copy
|
|
|
|
}
|
|
|
|
};
|
2015-12-29 02:29:48 +00:00
|
|
|
|
|
|
|
// The handles are leaked on purpose.
|
2015-12-29 02:40:10 +00:00
|
|
|
using FunctionTemplateHandle =
|
|
|
|
LeakedPersistentTraits<v8::FunctionTemplate>::LeakedPersistent;
|
2015-12-29 02:29:48 +00:00
|
|
|
std::map<std::string, FunctionTemplateHandle> function_templates_;
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Object> CreateObjectWithName(v8::Isolate* isolate,
|
2015-12-28 14:51:01 +00:00
|
|
|
const std::string& name) {
|
|
|
|
if (name == "Object")
|
|
|
|
return v8::Object::New(isolate);
|
2015-12-29 02:29:48 +00:00
|
|
|
|
|
|
|
if (ContainsKey(function_templates_, name))
|
|
|
|
return v8::Local<v8::FunctionTemplate>::New(
|
|
|
|
isolate, function_templates_[name])->GetFunction()->NewInstance();
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
|
2015-12-28 14:51:01 +00:00
|
|
|
t->SetClassName(mate::StringToV8(isolate, name));
|
2015-12-29 02:29:48 +00:00
|
|
|
function_templates_[name] = FunctionTemplateHandle(isolate, t);
|
2014-04-16 07:31:59 +00:00
|
|
|
return t->GetFunction()->NewInstance();
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Value> GetHiddenValue(v8::Local<v8::Object> object,
|
2015-12-28 14:51:01 +00:00
|
|
|
v8::Local<v8::String> key) {
|
2014-04-16 07:31:59 +00:00
|
|
|
return object->GetHiddenValue(key);
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void SetHiddenValue(v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::String> key,
|
|
|
|
v8::Local<v8::Value> value) {
|
2014-04-16 07:31:59 +00:00
|
|
|
object->SetHiddenValue(key, value);
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-27 11:01:34 +00:00
|
|
|
void DeleteHiddenValue(v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::String> key) {
|
|
|
|
object->DeleteHiddenValue(key);
|
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
int32_t GetObjectHash(v8::Local<v8::Object> object) {
|
2014-04-16 07:31:59 +00:00
|
|
|
return object->GetIdentityHash();
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
void SetDestructor(v8::Isolate* isolate,
|
2015-05-22 11:11:22 +00:00
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::Function> callback) {
|
2014-06-28 14:33:00 +00:00
|
|
|
atom::ObjectLifeMonitor::BindTo(isolate, object, callback);
|
2013-04-25 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
void TakeHeapSnapshot(v8::Isolate* isolate) {
|
2015-05-22 07:24:34 +00:00
|
|
|
isolate->GetHeapProfiler()->TakeHeapSnapshot();
|
2013-08-27 09:47:44 +00:00
|
|
|
}
|
|
|
|
|
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-06-29 12:48:44 +00:00
|
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("createObjectWithName", &CreateObjectWithName);
|
|
|
|
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
|
|
|
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
2015-08-27 11:01:34 +00:00
|
|
|
dict.SetMethod("deleteHiddenValue", &DeleteHiddenValue);
|
2014-04-16 07:31:59 +00:00
|
|
|
dict.SetMethod("getObjectHash", &GetObjectHash);
|
|
|
|
dict.SetMethod("setDestructor", &SetDestructor);
|
|
|
|
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
2013-04-25 08:30:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 07:31:59 +00:00
|
|
|
} // namespace
|
2013-04-25 08:30:31 +00:00
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_v8_util, Initialize)
|