common: use v8::private symbols as identifiers for object properties

This commit is contained in:
Robo 2016-03-24 01:24:01 +05:30
parent 6041c7edf9
commit 5fccbfc7c6
2 changed files with 31 additions and 7 deletions

View file

@ -11,20 +11,40 @@
namespace {
v8::Local<v8::Value> GetHiddenValue(v8::Local<v8::Object> object,
v8::Local<v8::Value> GetHiddenValue(v8::Isolate* isolate,
v8::Local<v8::Object> object,
v8::Local<v8::String> key) {
return object->GetHiddenValue(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::Local<v8::Object> object,
void SetHiddenValue(v8::Isolate* isolate,
v8::Local<v8::Object> object,
v8::Local<v8::String> key,
v8::Local<v8::Value> value) {
object->SetHiddenValue(key, 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::Local<v8::Object> object,
void DeleteHiddenValue(v8::Isolate* isolate,
v8::Local<v8::Object> object,
v8::Local<v8::String> key) {
object->DeleteHiddenValue(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) {

View file

@ -39,7 +39,11 @@ bool GetIPCObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object>* ipc) {
v8::Local<v8::String> key = mate::StringToV8(isolate, "ipc");
v8::Local<v8::Value> value = context->Global()->GetHiddenValue(key);
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
v8::Local<v8::Object> global_object = context->Global();
v8::Local<v8::Value> value;
if (!global_object->GetPrivate(context, privateKey).ToLocal(&value))
return false;
if (value.IsEmpty() || !value->IsObject())
return false;
*ipc = value->ToObject();