OLD | NEW |
| (Empty) |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/inspector/V8InjectedScriptHost.h" | |
6 | |
7 #include "src/base/macros.h" | |
8 #include "src/inspector/InjectedScriptNative.h" | |
9 #include "src/inspector/StringUtil.h" | |
10 #include "src/inspector/V8Debugger.h" | |
11 #include "src/inspector/V8InspectorImpl.h" | |
12 #include "src/inspector/V8InternalValueType.h" | |
13 #include "src/inspector/V8ValueCopier.h" | |
14 | |
15 #include "include/v8-inspector.h" | |
16 | |
17 namespace v8_inspector { | |
18 | |
19 namespace { | |
20 | |
21 void setFunctionProperty(v8::Local<v8::Context> context, | |
22 v8::Local<v8::Object> obj, const char* name, | |
23 v8::FunctionCallback callback, | |
24 v8::Local<v8::External> external) { | |
25 v8::Local<v8::String> funcName = | |
26 toV8StringInternalized(context->GetIsolate(), name); | |
27 v8::Local<v8::Function> func; | |
28 if (!v8::Function::New(context, callback, external, 0, | |
29 v8::ConstructorBehavior::kThrow) | |
30 .ToLocal(&func)) | |
31 return; | |
32 func->SetName(funcName); | |
33 createDataProperty(context, obj, funcName, func); | |
34 } | |
35 | |
36 V8InspectorImpl* unwrapInspector( | |
37 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
38 DCHECK(!info.Data().IsEmpty()); | |
39 DCHECK(info.Data()->IsExternal()); | |
40 V8InspectorImpl* inspector = | |
41 static_cast<V8InspectorImpl*>(info.Data().As<v8::External>()->Value()); | |
42 DCHECK(inspector); | |
43 return inspector; | |
44 } | |
45 | |
46 } // namespace | |
47 | |
48 v8::Local<v8::Object> V8InjectedScriptHost::create( | |
49 v8::Local<v8::Context> context, V8InspectorImpl* inspector) { | |
50 v8::Isolate* isolate = inspector->isolate(); | |
51 v8::Local<v8::Object> injectedScriptHost = v8::Object::New(isolate); | |
52 bool success = injectedScriptHost->SetPrototype(context, v8::Null(isolate)) | |
53 .FromMaybe(false); | |
54 DCHECK(success); | |
55 USE(success); | |
56 v8::Local<v8::External> debuggerExternal = | |
57 v8::External::New(isolate, inspector); | |
58 setFunctionProperty(context, injectedScriptHost, "internalConstructorName", | |
59 V8InjectedScriptHost::internalConstructorNameCallback, | |
60 debuggerExternal); | |
61 setFunctionProperty( | |
62 context, injectedScriptHost, "formatAccessorsAsProperties", | |
63 V8InjectedScriptHost::formatAccessorsAsProperties, debuggerExternal); | |
64 setFunctionProperty(context, injectedScriptHost, "subtype", | |
65 V8InjectedScriptHost::subtypeCallback, debuggerExternal); | |
66 setFunctionProperty(context, injectedScriptHost, "getInternalProperties", | |
67 V8InjectedScriptHost::getInternalPropertiesCallback, | |
68 debuggerExternal); | |
69 setFunctionProperty(context, injectedScriptHost, "objectHasOwnProperty", | |
70 V8InjectedScriptHost::objectHasOwnPropertyCallback, | |
71 debuggerExternal); | |
72 setFunctionProperty(context, injectedScriptHost, "bind", | |
73 V8InjectedScriptHost::bindCallback, debuggerExternal); | |
74 setFunctionProperty(context, injectedScriptHost, "proxyTargetValue", | |
75 V8InjectedScriptHost::proxyTargetValueCallback, | |
76 debuggerExternal); | |
77 return injectedScriptHost; | |
78 } | |
79 | |
80 void V8InjectedScriptHost::internalConstructorNameCallback( | |
81 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
82 if (info.Length() < 1 || !info[0]->IsObject()) return; | |
83 | |
84 v8::Local<v8::Object> object = info[0].As<v8::Object>(); | |
85 info.GetReturnValue().Set(object->GetConstructorName()); | |
86 } | |
87 | |
88 void V8InjectedScriptHost::formatAccessorsAsProperties( | |
89 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
90 DCHECK_EQ(info.Length(), 2); | |
91 info.GetReturnValue().Set(false); | |
92 if (!info[1]->IsFunction()) return; | |
93 // Check that function is user-defined. | |
94 if (info[1].As<v8::Function>()->ScriptId() != v8::UnboundScript::kNoScriptId) | |
95 return; | |
96 info.GetReturnValue().Set( | |
97 unwrapInspector(info)->client()->formatAccessorsAsProperties(info[0])); | |
98 } | |
99 | |
100 void V8InjectedScriptHost::subtypeCallback( | |
101 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
102 if (info.Length() < 1) return; | |
103 | |
104 v8::Isolate* isolate = info.GetIsolate(); | |
105 v8::Local<v8::Value> value = info[0]; | |
106 if (value->IsObject()) { | |
107 v8::Local<v8::Value> internalType = v8InternalValueTypeFrom( | |
108 isolate->GetCurrentContext(), v8::Local<v8::Object>::Cast(value)); | |
109 if (internalType->IsString()) { | |
110 info.GetReturnValue().Set(internalType); | |
111 return; | |
112 } | |
113 } | |
114 if (value->IsArray() || value->IsArgumentsObject()) { | |
115 info.GetReturnValue().Set(toV8StringInternalized(isolate, "array")); | |
116 return; | |
117 } | |
118 if (value->IsTypedArray()) { | |
119 info.GetReturnValue().Set(toV8StringInternalized(isolate, "typedarray")); | |
120 return; | |
121 } | |
122 if (value->IsDate()) { | |
123 info.GetReturnValue().Set(toV8StringInternalized(isolate, "date")); | |
124 return; | |
125 } | |
126 if (value->IsRegExp()) { | |
127 info.GetReturnValue().Set(toV8StringInternalized(isolate, "regexp")); | |
128 return; | |
129 } | |
130 if (value->IsMap() || value->IsWeakMap()) { | |
131 info.GetReturnValue().Set(toV8StringInternalized(isolate, "map")); | |
132 return; | |
133 } | |
134 if (value->IsSet() || value->IsWeakSet()) { | |
135 info.GetReturnValue().Set(toV8StringInternalized(isolate, "set")); | |
136 return; | |
137 } | |
138 if (value->IsMapIterator() || value->IsSetIterator()) { | |
139 info.GetReturnValue().Set(toV8StringInternalized(isolate, "iterator")); | |
140 return; | |
141 } | |
142 if (value->IsGeneratorObject()) { | |
143 info.GetReturnValue().Set(toV8StringInternalized(isolate, "generator")); | |
144 return; | |
145 } | |
146 if (value->IsNativeError()) { | |
147 info.GetReturnValue().Set(toV8StringInternalized(isolate, "error")); | |
148 return; | |
149 } | |
150 if (value->IsProxy()) { | |
151 info.GetReturnValue().Set(toV8StringInternalized(isolate, "proxy")); | |
152 return; | |
153 } | |
154 if (value->IsPromise()) { | |
155 info.GetReturnValue().Set(toV8StringInternalized(isolate, "promise")); | |
156 return; | |
157 } | |
158 std::unique_ptr<StringBuffer> subtype = | |
159 unwrapInspector(info)->client()->valueSubtype(value); | |
160 if (subtype) { | |
161 info.GetReturnValue().Set(toV8String(isolate, subtype->string())); | |
162 return; | |
163 } | |
164 } | |
165 | |
166 void V8InjectedScriptHost::getInternalPropertiesCallback( | |
167 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
168 if (info.Length() < 1) return; | |
169 v8::Local<v8::Array> properties; | |
170 if (unwrapInspector(info) | |
171 ->debugger() | |
172 ->internalProperties(info.GetIsolate()->GetCurrentContext(), info[0]) | |
173 .ToLocal(&properties)) | |
174 info.GetReturnValue().Set(properties); | |
175 } | |
176 | |
177 void V8InjectedScriptHost::objectHasOwnPropertyCallback( | |
178 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
179 if (info.Length() < 2 || !info[0]->IsObject() || !info[1]->IsString()) return; | |
180 bool result = info[0] | |
181 .As<v8::Object>() | |
182 ->HasOwnProperty(info.GetIsolate()->GetCurrentContext(), | |
183 v8::Local<v8::String>::Cast(info[1])) | |
184 .FromMaybe(false); | |
185 info.GetReturnValue().Set(v8::Boolean::New(info.GetIsolate(), result)); | |
186 } | |
187 | |
188 void V8InjectedScriptHost::bindCallback( | |
189 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
190 if (info.Length() < 2 || !info[1]->IsString()) return; | |
191 InjectedScriptNative* injectedScriptNative = | |
192 InjectedScriptNative::fromInjectedScriptHost(info.GetIsolate(), | |
193 info.Holder()); | |
194 if (!injectedScriptNative) return; | |
195 | |
196 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); | |
197 v8::Local<v8::String> v8groupName = | |
198 info[1]->ToString(context).ToLocalChecked(); | |
199 String16 groupName = toProtocolStringWithTypeCheck(v8groupName); | |
200 int id = injectedScriptNative->bind(info[0], groupName); | |
201 info.GetReturnValue().Set(id); | |
202 } | |
203 | |
204 void V8InjectedScriptHost::proxyTargetValueCallback( | |
205 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
206 if (info.Length() != 1 || !info[0]->IsProxy()) { | |
207 UNREACHABLE(); | |
208 return; | |
209 } | |
210 v8::Local<v8::Object> target = info[0].As<v8::Proxy>(); | |
211 while (target->IsProxy()) | |
212 target = v8::Local<v8::Proxy>::Cast(target)->GetTarget(); | |
213 info.GetReturnValue().Set(target); | |
214 } | |
215 | |
216 } // namespace v8_inspector | |
OLD | NEW |