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