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

Side by Side Diff: Source/bindings/core/v8/inspector/V8JavaScriptCallFrame.cpp

Issue 1157313006: DevTools: extract common inspector bindigns code into InspectorWrapper (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #include "config.h" 5 #include "config.h"
6 #include "bindings/core/v8/inspector/V8JavaScriptCallFrame.h" 6 #include "bindings/core/v8/inspector/V8JavaScriptCallFrame.h"
7 7
8 #include "bindings/core/v8/inspector/InspectorWrapper.h"
9 #include "core/inspector/JavaScriptCallFrame.h"
8 #include "wtf/RefPtr.h" 10 #include "wtf/RefPtr.h"
11 #include "wtf/StdLibExtras.h"
12 #include <algorithm>
9 13
10 namespace blink { 14 namespace blink {
11 15
12 namespace { 16 namespace {
13 17
14 void callerAttributeGetterCallback(v8::Local<v8::Name>, const v8::PropertyCallba ckInfo<v8::Value>& info) 18 void callerAttributeGetterCallback(v8::Local<v8::Name>, const v8::PropertyCallba ckInfo<v8::Value>& info)
15 { 19 {
16 JavaScriptCallFrame* impl = V8JavaScriptCallFrame::unwrap(info.Holder()); 20 JavaScriptCallFrame* impl = V8JavaScriptCallFrame::unwrap(info.Holder());
17 JavaScriptCallFrame* caller = impl->caller(); 21 JavaScriptCallFrame* caller = impl->caller();
18 if (!caller) 22 if (!caller)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 void scopeTypeMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info) 130 void scopeTypeMethodCallback(const v8::FunctionCallbackInfo<v8::Value>& info)
127 { 131 {
128 JavaScriptCallFrame* impl = V8JavaScriptCallFrame::unwrap(info.Holder()); 132 JavaScriptCallFrame* impl = V8JavaScriptCallFrame::unwrap(info.Holder());
129 v8::Maybe<int32_t> maybeScopeIndex = info[0]->Int32Value(info.GetIsolate()-> GetCurrentContext()); 133 v8::Maybe<int32_t> maybeScopeIndex = info[0]->Int32Value(info.GetIsolate()-> GetCurrentContext());
130 if (maybeScopeIndex.IsNothing()) 134 if (maybeScopeIndex.IsNothing())
131 return; 135 return;
132 int scopeIndex = maybeScopeIndex.FromJust(); 136 int scopeIndex = maybeScopeIndex.FromJust();
133 info.GetReturnValue().Set(impl->scopeType(scopeIndex)); 137 info.GetReturnValue().Set(impl->scopeType(scopeIndex));
134 } 138 }
135 139
136 struct V8AttributeConfiguration { 140 char hiddenPropertyName[] = "v8inspector::JavaScriptCallFrame";
137 const char* name; 141 char className[] = "V8JavaScriptCallFrame";
138 v8::AccessorNameGetterCallback callback; 142 using JavaScriptCallFrameWrapper = InspectorWrapper<JavaScriptCallFrame, hiddenP ropertyName, className>;
139 };
140 143
141 const V8AttributeConfiguration V8JavaScriptCallFrameAttributes[] = { 144 const JavaScriptCallFrameWrapper::V8AttributeConfiguration V8JavaScriptCallFrame Attributes[] = {
142 {"scopeChain", scopeChainAttributeGetterCallback}, 145 {"scopeChain", scopeChainAttributeGetterCallback},
143 {"thisObject", thisObjectAttributeGetterCallback}, 146 {"thisObject", thisObjectAttributeGetterCallback},
144 {"returnValue", returnValueAttributeGetterCallback}, 147 {"returnValue", returnValueAttributeGetterCallback},
145 148
146 {"caller", callerAttributeGetterCallback}, 149 {"caller", callerAttributeGetterCallback},
147 {"sourceID", sourceIDAttributeGetterCallback}, 150 {"sourceID", sourceIDAttributeGetterCallback},
148 {"line", lineAttributeGetterCallback}, 151 {"line", lineAttributeGetterCallback},
149 {"column", columnAttributeGetterCallback}, 152 {"column", columnAttributeGetterCallback},
150 {"stepInPositions", stepInPositionsAttributeGetterCallback}, 153 {"stepInPositions", stepInPositionsAttributeGetterCallback},
151 {"functionName", functionNameAttributeGetterCallback}, 154 {"functionName", functionNameAttributeGetterCallback},
152 {"functionLine", functionLineAttributeGetterCallback}, 155 {"functionLine", functionLineAttributeGetterCallback},
153 {"functionColumn", functionColumnAttributeGetterCallback}, 156 {"functionColumn", functionColumnAttributeGetterCallback},
154 {"isAtReturn", isAtReturnAttributeGetterCallback}, 157 {"isAtReturn", isAtReturnAttributeGetterCallback},
155 }; 158 };
156 159
157 struct V8MethodConfiguration { 160 const JavaScriptCallFrameWrapper::V8MethodConfiguration V8JavaScriptCallFrameMet hods[] = {
158 const char* name;
159 v8::FunctionCallback callback;
160 };
161
162 const V8MethodConfiguration V8JavaScriptCallFrameMethods[] = {
163 {"evaluateWithExceptionDetails", evaluateWithExceptionDetailsMethodCallback} , 161 {"evaluateWithExceptionDetails", evaluateWithExceptionDetailsMethodCallback} ,
164 {"restart", restartMethodCallback}, 162 {"restart", restartMethodCallback},
165 {"setVariableValue", setVariableValueMethodCallback}, 163 {"setVariableValue", setVariableValueMethodCallback},
166 {"scopeType", scopeTypeMethodCallback}, 164 {"scopeType", scopeTypeMethodCallback},
167 }; 165 };
168 166
169 class WeakCallbackData final {
170 public:
171 WeakCallbackData(v8::Isolate* isolate, PassRefPtr<JavaScriptCallFrame> frame , v8::Local<v8::Object> wrapper)
172 : m_frame(frame)
173 , m_persistent(isolate, wrapper)
174 {
175 m_persistent.SetWeak(this, &WeakCallbackData::weakCallback, v8::WeakCall backType::kParameter);
176 }
177
178 RefPtr<JavaScriptCallFrame> m_frame;
179
180 private:
181 static void weakCallback(const v8::WeakCallbackInfo<WeakCallbackData>& info)
182 {
183 delete info.GetParameter();
184 }
185
186 v8::Global<v8::Object> m_persistent;
187 };
188
189 } // namespace 167 } // namespace
190 168
191 v8::Local<v8::FunctionTemplate> V8JavaScriptCallFrame::createWrapperTemplate(v8: :Isolate* isolate) 169 v8::Local<v8::FunctionTemplate> V8JavaScriptCallFrame::createWrapperTemplate(v8: :Isolate* isolate)
192 { 170 {
193 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate::New (isolate); 171 Vector<InspectorWrapperBase::V8MethodConfiguration> methods(WTF_ARRAY_LENGTH (V8JavaScriptCallFrameMethods));
194 172 std::copy(V8JavaScriptCallFrameMethods, V8JavaScriptCallFrameMethods + WTF_A RRAY_LENGTH(V8JavaScriptCallFrameMethods), methods.begin());
195 functionTemplate->SetClassName(v8::String::NewFromUtf8(isolate, "JavaScriptC allFrame", v8::NewStringType::kInternalized).ToLocalChecked()); 173 Vector<InspectorWrapperBase::V8AttributeConfiguration> attributes(WTF_ARRAY_ LENGTH(V8JavaScriptCallFrameAttributes));
196 v8::Local<v8::ObjectTemplate> instanceTemplate = functionTemplate->InstanceT emplate(); 174 std::copy(V8JavaScriptCallFrameAttributes, V8JavaScriptCallFrameAttributes + WTF_ARRAY_LENGTH(V8JavaScriptCallFrameAttributes), attributes.begin());
197 for (auto& config : V8JavaScriptCallFrameAttributes) { 175 return JavaScriptCallFrameWrapper::createWrapperTemplate(isolate, methods, a ttributes);
198 v8::Local<v8::Name> v8name = v8::String::NewFromUtf8(isolate, config.nam e, v8::NewStringType::kInternalized).ToLocalChecked();
199 instanceTemplate->SetAccessor(v8name, config.callback);
200 }
201
202 for (auto& config : V8JavaScriptCallFrameMethods) {
203 v8::Local<v8::Name> v8name = v8::String::NewFromUtf8(isolate, config.nam e, v8::NewStringType::kInternalized).ToLocalChecked();
204 v8::Local<v8::FunctionTemplate> functionTemplate = v8::FunctionTemplate: :New(isolate, config.callback);
205 functionTemplate->RemovePrototype();
206 instanceTemplate->Set(v8name, functionTemplate);
207 }
208
209 return functionTemplate;
210 } 176 }
211 177
212 v8::Local<v8::Object> V8JavaScriptCallFrame::wrap(v8::Local<v8::FunctionTemplate > constructorTemplate, v8::Local<v8::Context> context, PassRefPtr<JavaScriptCall Frame> frame) 178 v8::Local<v8::Object> V8JavaScriptCallFrame::wrap(v8::Local<v8::FunctionTemplate > constructorTemplate, v8::Local<v8::Context> context, PassRefPtr<JavaScriptCall Frame> frame)
213 { 179 {
214 RefPtr<JavaScriptCallFrame> impl(frame);
215 v8::Local<v8::Function> function;
216 if (!constructorTemplate->GetFunction(context).ToLocal(&function))
217 return v8::Local<v8::Object>();
218
219 v8::MaybeLocal<v8::Object> maybeResult = function->NewInstance(context);
220 v8::Local<v8::Object> result;
221 if (!maybeResult.ToLocal(&result))
222 return v8::Local<v8::Object>();
223
224 v8::Isolate* isolate = context->GetIsolate();
225 v8::Local<v8::External> objectReference = v8::External::New(isolate, new Wea kCallbackData(isolate, impl, result));
226 result->SetHiddenValue(hiddenPropertyName(isolate), objectReference);
227
228 // Store template for .caller callback 180 // Store template for .caller callback
229 impl->setWrapperTemplate(constructorTemplate, isolate); 181 frame->setWrapperTemplate(constructorTemplate, context->GetIsolate());
230 182 return JavaScriptCallFrameWrapper::wrap(constructorTemplate, context, frame) ;
231 return result;
232 } 183 }
233 184
234 JavaScriptCallFrame* V8JavaScriptCallFrame::unwrap(v8::Local<v8::Object> object) 185 JavaScriptCallFrame* V8JavaScriptCallFrame::unwrap(v8::Local<v8::Object> object)
235 { 186 {
236 v8::Isolate* isolate = object->GetIsolate(); 187 return JavaScriptCallFrameWrapper::unwrap(object);
237 v8::Local<v8::Value> value = object->GetHiddenValue(hiddenPropertyName(isola te));
238 if (value.IsEmpty())
239 return nullptr;
240 if (!value->IsExternal())
241 return nullptr;
242 void* data = value.As<v8::External>()->Value();
243 return reinterpret_cast<WeakCallbackData*>(data)->m_frame.get();
244 }
245
246 v8::Local<v8::String> V8JavaScriptCallFrame::hiddenPropertyName(v8::Isolate* iso late)
247 {
248 return v8::String::NewFromUtf8(isolate, "v8inspector::JavaScriptCallFrame", v8::NewStringType::kInternalized).ToLocalChecked();
249 } 188 }
250 189
251 } // namespace blink 190 } // namespace blink
OLDNEW
« no previous file with comments | « Source/bindings/core/v8/inspector/V8JavaScriptCallFrame.h ('k') | Source/bindings/core/v8/v8.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698