Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/V8PrivateProperty.h

Issue 2785943002: [Bindings] Simplify V8PrivateProperty::Symbol methods (Closed)
Patch Set: . Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8PrivateProperty_h 5 #ifndef V8PrivateProperty_h
6 #define V8PrivateProperty_h 6 #define V8PrivateProperty_h
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "bindings/core/v8/ScriptPromiseProperties.h" 10 #include "bindings/core/v8/ScriptPromiseProperties.h"
11 #include "bindings/core/v8/V8BindingMacros.h" 11 #include "bindings/core/v8/V8BindingMacros.h"
12 #include "bindings/core/v8/V8PerIsolateData.h" 12 #include "bindings/core/v8/V8PerIsolateData.h"
13 #include "core/CoreExport.h" 13 #include "core/CoreExport.h"
14 #include "v8/include/v8.h" 14 #include "v8/include/v8.h"
15 #include "wtf/Allocator.h" 15 #include "wtf/Allocator.h"
16 #include "wtf/PtrUtil.h" 16 #include "wtf/PtrUtil.h"
17 17
18 namespace blink { 18 namespace blink {
19 19
20 class ScriptState;
21 class ScriptWrappable; 20 class ScriptWrappable;
22 21
23 // Apply |X| for each pair of (InterfaceName, PrivateKeyName). 22 // Apply |X| for each pair of (InterfaceName, PrivateKeyName).
24 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ 23 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \
25 X(CustomEvent, Detail) \ 24 X(CustomEvent, Detail) \
26 X(CustomElement, AdoptedCallback) \ 25 X(CustomElement, AdoptedCallback) \
27 X(CustomElement, AttributeChangedCallback) \ 26 X(CustomElement, AttributeChangedCallback) \
28 X(CustomElement, ConnectedCallback) \ 27 X(CustomElement, ConnectedCallback) \
29 X(CustomElement, DisconnectedCallback) \ 28 X(CustomElement, DisconnectedCallback) \
30 X(CustomElement, Document) \ 29 X(CustomElement, Document) \
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 public: 80 public:
82 // Provides fast access to V8's private properties. 81 // Provides fast access to V8's private properties.
83 // 82 //
84 // Retrieving/creating a global private symbol from a string is very 83 // Retrieving/creating a global private symbol from a string is very
85 // expensive compared to get or set a private property. This class 84 // expensive compared to get or set a private property. This class
86 // provides a way to cache a private symbol and re-use it. 85 // provides a way to cache a private symbol and re-use it.
87 class CORE_EXPORT Symbol { 86 class CORE_EXPORT Symbol {
88 STACK_ALLOCATED(); 87 STACK_ALLOCATED();
89 88
90 public: 89 public:
91 bool hasValue(v8::Local<v8::Context> context, 90 bool hasValue(v8::Local<v8::Object> object) const {
92 v8::Local<v8::Object> object) const { 91 return v8CallBoolean(object->HasPrivate(context(), m_privateSymbol));
93 return v8CallBoolean(object->HasPrivate(context, m_privateSymbol));
94 } 92 }
95 93
96 // Returns the value of the private property if set, or undefined. 94 // Returns the value of the private property if set, or undefined.
97 v8::Local<v8::Value> getOrUndefined(v8::Local<v8::Context> context, 95 v8::Local<v8::Value> getOrUndefined(v8::Local<v8::Object> object) const {
98 v8::Local<v8::Object> object) const { 96 return object->GetPrivate(context(), m_privateSymbol).ToLocalChecked();
99 return object->GetPrivate(context, m_privateSymbol).ToLocalChecked();
100 } 97 }
101 98
102 // Returns the value of the private property if set, or an empty handle. 99 // Returns the value of the private property if set, or an empty handle.
103 v8::Local<v8::Value> get(v8::Local<v8::Context> context, 100 v8::Local<v8::Value> getOrEmpty(v8::Local<v8::Object> object) const {
104 v8::Local<v8::Object> object) const { 101 if (!hasValue(object))
105 if (!v8CallBoolean(object->HasPrivate(context, m_privateSymbol)))
106 return v8::Local<v8::Value>(); 102 return v8::Local<v8::Value>();
107 v8::Local<v8::Value> value; 103 v8::Local<v8::Value> value;
108 if (v8Call(object->GetPrivate(context, m_privateSymbol), value)) 104 if (v8Call(object->GetPrivate(context(), m_privateSymbol), value))
Yuki 2017/03/30 07:25:42 v8Call is deprecated, so it's better to explicitly
peria 2017/03/30 07:41:37 Done.
109 return value; 105 return value;
110 return v8::Local<v8::Value>(); 106 return v8::Local<v8::Value>();
111 } 107 }
112 108
113 bool set(v8::Local<v8::Context> context, 109 bool set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) const {
114 v8::Local<v8::Object> object, 110 return v8CallBoolean(
115 v8::Local<v8::Value> value) const { 111 object->SetPrivate(context(), m_privateSymbol, value));
116 return v8CallBoolean(object->SetPrivate(context, m_privateSymbol, value));
117 } 112 }
118 113
119 v8::Local<v8::Private> getPrivate() { return m_privateSymbol; } 114 v8::Local<v8::Private> getPrivate() const { return m_privateSymbol; }
120 115
121 private: 116 private:
122 friend class V8PrivateProperty; 117 friend class V8PrivateProperty;
123 // The following classes are exceptionally allowed to call to 118 // The following classes are exceptionally allowed to call to
124 // getFromMainWorld. 119 // getFromMainWorld.
125 friend class V8CustomEvent; 120 friend class V8CustomEvent;
126 friend class V8ServiceWorkerMessageEventInternal; 121 friend class V8ServiceWorkerMessageEventInternal;
127 122
128 explicit Symbol(v8::Local<v8::Private> privateSymbol) 123 Symbol(const v8::Eternal<v8::Private>& eternalSymbol, v8::Isolate* isolate)
Yuki 2017/03/30 07:25:41 This looks a little tricky. Taking v8::Local<v8::
Yuki 2017/03/30 07:25:42 nit: Our convention is to take v8::Context / v8::I
peria 2017/03/30 07:41:36 Done.
129 : m_privateSymbol(privateSymbol) {} 124 : m_privateSymbol(eternalSymbol.Get(isolate)), m_isolate(isolate) {}
125
126 v8::Local<v8::Context> context() const {
127 return m_isolate->GetCurrentContext();
128 }
130 129
131 // Only friend classes are allowed to use this API. 130 // Only friend classes are allowed to use this API.
132 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); 131 v8::Local<v8::Value> getFromMainWorld(ScriptWrappable*);
133 132
134 v8::Local<v8::Private> m_privateSymbol; 133 v8::Local<v8::Private> m_privateSymbol;
134 v8::Isolate* m_isolate;
135 }; 135 };
136 136
137 static std::unique_ptr<V8PrivateProperty> create() { 137 static std::unique_ptr<V8PrivateProperty> create() {
138 return WTF::wrapUnique(new V8PrivateProperty()); 138 return WTF::wrapUnique(new V8PrivateProperty());
139 } 139 }
140 140
141 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ 141 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \
142 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)( \ 142 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME( \
peria 2017/03/30 07:08:26 A presubmit error warns for "V8_PRIVATE_PROPERTY_G
Yuki 2017/03/30 07:25:41 /* NOLINT(readability/naming/underscores) */ did w
peria 2017/03/30 07:41:36 Correct. Presubmit checker works only for "// NOL
143 v8::Isolate * isolate) /* NOLINT(readability/naming/underscores) */ \ 143 InterfaceName, KeyName)(v8::Isolate * isolate) { \
144 { \ 144 V8PrivateProperty* privateProp = \
145 V8PrivateProperty* privateProp = \ 145 V8PerIsolateData::from(isolate)->privateProperty(); \
146 V8PerIsolateData::from(isolate)->privateProperty(); \ 146 v8::Eternal<v8::Private>& eternalProperty = \
Yuki 2017/03/30 07:25:42 nit: Hungarian notation is not recommended in gene
peria 2017/03/30 07:41:37 Done.
147 if (UNLIKELY(privateProp \ 147 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); \
148 ->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ 148 if (UNLIKELY(eternalProperty.IsEmpty())) { \
149 .IsEmpty())) { \ 149 static constexpr char kSymbolName[] = \
150 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ 150 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName); \
151 .Set( \ 151 eternalProperty.Set(isolate, createV8Private(isolate, kSymbolName, \
152 isolate, \ 152 sizeof(kSymbolName))); \
153 createV8Private( \ 153 } \
154 isolate, \ 154 return Symbol(eternalProperty, isolate); \
155 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ 155 } // NOLINT
156 sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ 156
157 InterfaceName, \
158 KeyName))); /* NOLINT(readability/naming/underscores) */ \
159 } \
160 return Symbol( \
161 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \
162 .Get(isolate)); \
163 }
164 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) 157 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER)
165 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER 158 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER
166 159
167 private: 160 private:
168 V8PrivateProperty() {} 161 V8PrivateProperty() {}
169 162
170 static v8::Local<v8::Private> createV8Private(v8::Isolate*, 163 static v8::Local<v8::Private> createV8Private(v8::Isolate*,
171 const char* symbol, 164 const char* symbol,
172 size_t length); 165 size_t length);
173 166
174 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ 167 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \
175 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ 168 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \
176 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) 169 InterfaceName, KeyName); // NOLINT(readability/naming/underscores)
177 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) 170 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER)
178 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER 171 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER
179 }; 172 };
180 173
181 } // namespace blink 174 } // namespace blink
182 175
183 #endif // V8PrivateProperty_h 176 #endif // V8PrivateProperty_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698