Commit 7db35036 authored by Marina Sakai's avatar Marina Sakai Committed by Commit Bot

Create a new class which is dedicated to private property keys and add a new API using the class

Currently the API |GetSymbol| in V8PrivateProperty is used to get Symbol using HashMap with a static variable address as a key.
However, this key is not suitable for tracking private properties because the type is void*.

We can improve the tracking ability by using an instance of private property key dedicated class instead of void* type.

This CL creates a new class which is dedicated to private property keys, and adds a new API to get Symbol using an instance of the class.

Bug: 715418
Change-Id: Ieac862e8e2b5406a2c35c1a1f3368494833c3287
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877490
Commit-Queue: Marina Sakai <marinasakai@google.com>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709335}
parent 2d653d73
......@@ -75,9 +75,9 @@ class ScriptWrappable;
// private_property.set(object, value);
//
// Usage 2) Slow path to create a global private symbol.
// const char symbol_name[] = "Interface#PrivateKeyName";
// auto private_property =
// V8PrivateProperty::GetSymbol(isolate, symbol_name);
// /* |desc| is a description of the private property. */
// static const SymbolKey key(desc);
// auto private_property = V8PrivateProperty::GetSymbol(isolate, key);
// ...
class PLATFORM_EXPORT V8PrivateProperty {
USING_FAST_MALLOC(V8PrivateProperty);
......@@ -143,14 +143,33 @@ class PLATFORM_EXPORT V8PrivateProperty {
v8::Isolate* isolate_;
};
// This class is used for a key to get Symbol.
//
// We can improve ability of tracking private properties by using an instance
// of this class. |desc_| is a description of the private property.
class PLATFORM_EXPORT SymbolKey {
public:
// Note that, unlike a string class, the lifetime of |desc| must be longer
// than this SymbolKey, i.e. this SymbolKey does not copy |desc| nor have
// |desc| alive enough long.
explicit SymbolKey(const char* desc) : desc_(desc) {}
const char* GetDescription() const { return desc_; }
private:
SymbolKey(const SymbolKey&) = delete;
SymbolKey& operator=(const SymbolKey&) = delete;
const char* const desc_;
};
#define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \
static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(/* // NOLINT */ \
InterfaceName, KeyName)( \
v8::Isolate * isolate) { \
static int private_property_key; \
return GetSymbol( \
isolate, &private_property_key, \
static const SymbolKey private_property_key( \
V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName)); \
return GetSymbol(isolate, private_property_key); \
}
V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER)
......@@ -211,6 +230,23 @@ class PLATFORM_EXPORT V8PrivateProperty {
return Symbol(isolate, v8_private);
}
// Returns a Symbol to access a private property. Symbol instances from same
// |key| are guaranteed to access the same property.
static Symbol GetSymbol(v8::Isolate* isolate, const SymbolKey& key) {
V8PrivateProperty* private_prop =
V8PerIsolateData::From(isolate)->PrivateProperty();
auto& symbol_map = private_prop->symbol_map_;
auto iter = symbol_map.find(&key);
v8::Local<v8::Private> v8_private;
if (UNLIKELY(iter == symbol_map.end())) {
v8_private = CreateV8Private(isolate, key.GetDescription());
symbol_map.insert(&key, v8::Eternal<v8::Private>(isolate, v8_private));
} else {
v8_private = iter->value.Get(isolate);
}
return Symbol(isolate, v8_private);
}
// This function is always called after NOTREACHED(). The Symbol returned from
// this function must not be used.
static Symbol GetEmptySymbol() {
......@@ -230,7 +266,7 @@ class PLATFORM_EXPORT V8PrivateProperty {
// requirement.
ScopedPersistent<v8::Private> symbol_window_document_cached_accessor_;
WTF::HashMap<void*, v8::Eternal<v8::Private>> symbol_map_;
WTF::HashMap<const void*, v8::Eternal<v8::Private>> symbol_map_;
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