Commit 1300b269 authored by Marina Sakai's avatar Marina Sakai Committed by Commit Bot

Create a new API in V8PrivateProperty to get Symbol

To access private properties of a V8 object, we need a v8::Private as a key for each property.  V8PrivateProperty provides its wrapper class "Symbol" as a shorthand API.

Currently, V8PrivateProperty has two types of APIs to get Symbol.  One is a macro-generated function, which needs to be managed by hand.  And the other is string-based mapping, which defers the management of the mapping to V8.

This CL adds a new API to get a Symbol (wired with a unique v8::Private) from a key pointer.  All the current Symbol getters will be replaced with this new API.
Also uses it to access private properties to keep-alive objects.

Bug: 715418, 1005601
Change-Id: I29735fdf5b8748c6dc7f57f91f8c5ebd5e5991b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1810511
Commit-Queue: Marina Sakai <marinasakai@google.com>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698417}
parent 0fa0b89d
...@@ -177,8 +177,10 @@ const v8::FunctionCallbackInfo<v8::Value>& info ...@@ -177,8 +177,10 @@ const v8::FunctionCallbackInfo<v8::Value>& info
if ({{attribute.cpp_value}} && DOMDataStore::SetReturnValue{{world_suffix}}(info.GetReturnValue(), {{attribute.cpp_value_to_script_wrappable}})) if ({{attribute.cpp_value}} && DOMDataStore::SetReturnValue{{world_suffix}}(info.GetReturnValue(), {{attribute.cpp_value_to_script_wrappable}}))
return; return;
v8::Local<v8::Value> v8_value(ToV8({{attribute.cpp_value_to_script_wrappable}}, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8({{attribute.cpp_value_to_script_wrappable}}, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#{{interface_name}}#{{attribute.name}}") info.GetIsolate(), &private_property_key, "KeepAlive#{{interface_name}}#{{attribute.name}}")
.Set(holder, v8_value); .Set(holder, v8_value);
{% endif %} {% endif %}
......
...@@ -166,8 +166,10 @@ static void ReadonlyTestInterfaceEmptyAttributeAttributeGetter(const v8::Functio ...@@ -166,8 +166,10 @@ static void ReadonlyTestInterfaceEmptyAttributeAttributeGetter(const v8::Functio
if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value)) if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value))
return; return;
v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#TestObject#readonlyTestInterfaceEmptyAttribute") info.GetIsolate(), &private_property_key, "KeepAlive#TestObject#readonlyTestInterfaceEmptyAttribute")
.Set(holder, v8_value); .Set(holder, v8_value);
V8SetReturnValue(info, v8_value); V8SetReturnValue(info, v8_value);
...@@ -2482,8 +2484,10 @@ static void PerWorldBindingsReadonlyTestInterfaceEmptyAttributeAttributeGetter(c ...@@ -2482,8 +2484,10 @@ static void PerWorldBindingsReadonlyTestInterfaceEmptyAttributeAttributeGetter(c
if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value)) if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value))
return; return;
v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#TestObject#perWorldBindingsReadonlyTestInterfaceEmptyAttribute") info.GetIsolate(), &private_property_key, "KeepAlive#TestObject#perWorldBindingsReadonlyTestInterfaceEmptyAttribute")
.Set(holder, v8_value); .Set(holder, v8_value);
V8SetReturnValue(info, v8_value); V8SetReturnValue(info, v8_value);
...@@ -2501,8 +2505,10 @@ static void PerWorldBindingsReadonlyTestInterfaceEmptyAttributeAttributeGetterFo ...@@ -2501,8 +2505,10 @@ static void PerWorldBindingsReadonlyTestInterfaceEmptyAttributeAttributeGetterFo
if (cpp_value && DOMDataStore::SetReturnValueForMainWorld(info.GetReturnValue(), cpp_value)) if (cpp_value && DOMDataStore::SetReturnValueForMainWorld(info.GetReturnValue(), cpp_value))
return; return;
v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#TestObject#perWorldBindingsReadonlyTestInterfaceEmptyAttribute") info.GetIsolate(), &private_property_key, "KeepAlive#TestObject#perWorldBindingsReadonlyTestInterfaceEmptyAttribute")
.Set(holder, v8_value); .Set(holder, v8_value);
V8SetReturnValue(info, v8_value); V8SetReturnValue(info, v8_value);
...@@ -4061,8 +4067,10 @@ static void SameObjectAttributeAttributeGetter(const v8::FunctionCallbackInfo<v8 ...@@ -4061,8 +4067,10 @@ static void SameObjectAttributeAttributeGetter(const v8::FunctionCallbackInfo<v8
if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value)) if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value))
return; return;
v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#TestObject#sameObjectAttribute") info.GetIsolate(), &private_property_key, "KeepAlive#TestObject#sameObjectAttribute")
.Set(holder, v8_value); .Set(holder, v8_value);
V8SetReturnValue(info, v8_value); V8SetReturnValue(info, v8_value);
...@@ -4095,8 +4103,10 @@ static void SaveSameObjectAttributeAttributeGetter(const v8::FunctionCallbackInf ...@@ -4095,8 +4103,10 @@ static void SaveSameObjectAttributeAttributeGetter(const v8::FunctionCallbackInf
if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value)) if (cpp_value && DOMDataStore::SetReturnValue(info.GetReturnValue(), cpp_value))
return; return;
v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate())); v8::Local<v8::Value> v8_value(ToV8(cpp_value, holder, info.GetIsolate()));
// This key is used for uniquely identifying v8::Private.
static int private_property_key;
V8PrivateProperty::GetSymbol( V8PrivateProperty::GetSymbol(
info.GetIsolate(), "KeepAlive#TestObject#saveSameObjectAttribute") info.GetIsolate(), &private_property_key, "KeepAlive#TestObject#saveSameObjectAttribute")
.Set(holder, v8_value); .Set(holder, v8_value);
V8SetReturnValue(info, v8_value); V8SetReturnValue(info, v8_value);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h" #include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
namespace blink { namespace blink {
...@@ -200,6 +201,24 @@ class PLATFORM_EXPORT V8PrivateProperty { ...@@ -200,6 +201,24 @@ class PLATFORM_EXPORT V8PrivateProperty {
return Symbol(isolate, CreateCachedV8Private(isolate, symbol)); return Symbol(isolate, CreateCachedV8Private(isolate, symbol));
} }
// Returns a Symbol to access a private property. Symbol instances from same
// |key|s are guaranteed to access the same property. |desc| is a description
// of the property.
static Symbol GetSymbol(v8::Isolate* isolate, void* key, const char* desc) {
V8PrivateProperty* private_prop =
V8PerIsolateData::From(isolate)->PrivateProperty();
auto& symbol_map = private_prop->symbol_map_;
auto itr = symbol_map.find(key);
v8::Local<v8::Private> v8_private;
if (UNLIKELY(itr == symbol_map.end())) {
v8_private = CreateV8Private(isolate, desc);
symbol_map.insert(key, v8::Eternal<v8::Private>(isolate, v8_private));
} else {
v8_private = itr->value.Get(isolate);
}
return Symbol(isolate, v8_private);
}
private: private:
static v8::Local<v8::Private> CreateV8Private(v8::Isolate*, static v8::Local<v8::Private> CreateV8Private(v8::Isolate*,
const char* symbol); const char* symbol);
...@@ -219,6 +238,8 @@ class PLATFORM_EXPORT V8PrivateProperty { ...@@ -219,6 +238,8 @@ class PLATFORM_EXPORT V8PrivateProperty {
// requirement. // requirement.
ScopedPersistent<v8::Private> symbol_window_document_cached_accessor_; ScopedPersistent<v8::Private> symbol_window_document_cached_accessor_;
WTF::HashMap<void*, v8::Eternal<v8::Private>> symbol_map_;
DISALLOW_COPY_AND_ASSIGN(V8PrivateProperty); DISALLOW_COPY_AND_ASSIGN(V8PrivateProperty);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment