| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2007-2011 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #include "config.h" | |
| 32 #include "bindings/core/v8/V8InjectedScriptHost.h" | |
| 33 | |
| 34 #include "bindings/core/v8/BindingSecurity.h" | |
| 35 #include "bindings/core/v8/ExceptionState.h" | |
| 36 #include "bindings/core/v8/ScriptValue.h" | |
| 37 #include "bindings/core/v8/V8AbstractEventListener.h" | |
| 38 #include "bindings/core/v8/V8Binding.h" | |
| 39 #include "bindings/core/v8/V8DOMException.h" | |
| 40 #include "bindings/core/v8/V8DOMTokenList.h" | |
| 41 #include "bindings/core/v8/V8Debugger.h" | |
| 42 #include "bindings/core/v8/V8Event.h" | |
| 43 #include "bindings/core/v8/V8EventTarget.h" | |
| 44 #include "bindings/core/v8/V8EventTarget.h" | |
| 45 #include "bindings/core/v8/V8HTMLAllCollection.h" | |
| 46 #include "bindings/core/v8/V8HTMLCollection.h" | |
| 47 #include "bindings/core/v8/V8Node.h" | |
| 48 #include "bindings/core/v8/V8NodeList.h" | |
| 49 #include "bindings/core/v8/V8ScriptRunner.h" | |
| 50 #include "core/events/EventTarget.h" | |
| 51 #include "core/inspector/EventListenerInfo.h" | |
| 52 #include "core/inspector/InjectedScript.h" | |
| 53 #include "core/inspector/InjectedScriptHost.h" | |
| 54 #include "core/inspector/JavaScriptCallFrame.h" | |
| 55 #include "platform/JSONValues.h" | |
| 56 | |
| 57 namespace blink { | |
| 58 | |
| 59 Node* InjectedScriptHost::scriptValueAsNode(ScriptState* scriptState, ScriptValu
e value) | |
| 60 { | |
| 61 ScriptState::Scope scope(scriptState); | |
| 62 if (!value.isObject() || value.isNull()) | |
| 63 return 0; | |
| 64 return V8Node::toImpl(v8::Local<v8::Object>::Cast(value.v8Value())); | |
| 65 } | |
| 66 | |
| 67 ScriptValue InjectedScriptHost::nodeAsScriptValue(ScriptState* scriptState, Node
* node) | |
| 68 { | |
| 69 ScriptState::Scope scope(scriptState); | |
| 70 v8::Isolate* isolate = scriptState->isolate(); | |
| 71 ExceptionState exceptionState(ExceptionState::ExecutionContext, "nodeAsScrip
tValue", "InjectedScriptHost", scriptState->context()->Global(), isolate); | |
| 72 if (!BindingSecurity::shouldAllowAccessToNode(isolate, node, exceptionState)
) | |
| 73 return ScriptValue(scriptState, v8::Null(isolate)); | |
| 74 return ScriptValue(scriptState, toV8(node, scriptState->context()->Global(),
isolate)); | |
| 75 } | |
| 76 | |
| 77 static EventTarget* eventTargetFromScriptValue(v8::Isolate* isolate, v8::Local<v
8::Value> value) | |
| 78 { | |
| 79 EventTarget* target = V8EventTarget::toImplWithTypeCheck(isolate, value); | |
| 80 // We need to handle LocalDOMWindow specially, because LocalDOMWindow wrappe
r exists on prototype chain. | |
| 81 if (!target) | |
| 82 target = toDOMWindow(isolate, value); | |
| 83 if (!target || !target->executionContext()) | |
| 84 return nullptr; | |
| 85 return target; | |
| 86 } | |
| 87 | |
| 88 EventTarget* InjectedScriptHost::scriptValueAsEventTarget(ScriptState* scriptSta
te, ScriptValue value) | |
| 89 { | |
| 90 ScriptState::Scope scope(scriptState); | |
| 91 if (value.isNull() || !value.isObject()) | |
| 92 return nullptr; | |
| 93 return eventTargetFromScriptValue(scriptState->isolate(), value.v8Value()); | |
| 94 } | |
| 95 | |
| 96 void V8InjectedScriptHost::inspectedObjectMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) | |
| 97 { | |
| 98 if (info.Length() < 1) | |
| 99 return; | |
| 100 | |
| 101 if (!info[0]->IsInt32()) { | |
| 102 V8ThrowException::throwTypeError(info.GetIsolate(), "argument has to be
an integer"); | |
| 103 return; | |
| 104 } | |
| 105 | |
| 106 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 107 InjectedScriptHost::InspectableObject* object = host->inspectedObject(info[0
].As<v8::Int32>()->Value()); | |
| 108 v8SetReturnValue(info, object->get(ScriptState::current(info.GetIsolate())).
v8Value()); | |
| 109 } | |
| 110 | |
| 111 static v8::Local<v8::String> functionDisplayName(v8::Local<v8::Function> functio
n) | |
| 112 { | |
| 113 v8::Local<v8::Value> value = function->GetDisplayName(); | |
| 114 if (value->IsString() && v8::Local<v8::String>::Cast(value)->Length()) | |
| 115 return v8::Local<v8::String>::Cast(value); | |
| 116 | |
| 117 value = function->GetName(); | |
| 118 if (value->IsString() && v8::Local<v8::String>::Cast(value)->Length()) | |
| 119 return v8::Local<v8::String>::Cast(value); | |
| 120 | |
| 121 value = function->GetInferredName(); | |
| 122 if (value->IsString() && v8::Local<v8::String>::Cast(value)->Length()) | |
| 123 return v8::Local<v8::String>::Cast(value); | |
| 124 | |
| 125 return v8::Local<v8::String>(); | |
| 126 } | |
| 127 | |
| 128 void V8InjectedScriptHost::internalConstructorNameMethodCustom(const v8::Functio
nCallbackInfo<v8::Value>& info) | |
| 129 { | |
| 130 if (info.Length() < 1 || !info[0]->IsObject()) | |
| 131 return; | |
| 132 | |
| 133 v8::Local<v8::Object> object = info[0].As<v8::Object>(); | |
| 134 v8::Local<v8::String> result = object->GetConstructorName(); | |
| 135 | |
| 136 if (!result.IsEmpty() && toCoreStringWithUndefinedOrNullCheck(result) == "Ob
ject") { | |
| 137 v8::Local<v8::String> constructorSymbol = v8AtomicString(info.GetIsolate
(), "constructor"); | |
| 138 if (object->HasRealNamedProperty(constructorSymbol) && !object->HasRealN
amedCallbackProperty(constructorSymbol)) { | |
| 139 v8::TryCatch tryCatch; | |
| 140 v8::Local<v8::Value> constructor = object->GetRealNamedProperty(cons
tructorSymbol); | |
| 141 if (!constructor.IsEmpty() && constructor->IsFunction()) { | |
| 142 v8::Local<v8::String> constructorName = functionDisplayName(v8::
Local<v8::Function>::Cast(constructor)); | |
| 143 if (!constructorName.IsEmpty() && !tryCatch.HasCaught()) | |
| 144 result = constructorName; | |
| 145 } | |
| 146 } | |
| 147 if (toCoreStringWithUndefinedOrNullCheck(result) == "Object" && object->
IsFunction()) | |
| 148 result = v8AtomicString(info.GetIsolate(), "Function"); | |
| 149 } | |
| 150 | |
| 151 v8SetReturnValue(info, result); | |
| 152 } | |
| 153 | |
| 154 void V8InjectedScriptHost::isDOMWrapperMethodCustom(const v8::FunctionCallbackIn
fo<v8::Value>& info) | |
| 155 { | |
| 156 if (info.Length() < 1) | |
| 157 return; | |
| 158 | |
| 159 v8SetReturnValue(info, V8DOMWrapper::isWrapper(info.GetIsolate(), info[0])); | |
| 160 } | |
| 161 | |
| 162 void V8InjectedScriptHost::isHTMLAllCollectionMethodCustom(const v8::FunctionCal
lbackInfo<v8::Value>& info) | |
| 163 { | |
| 164 if (info.Length() < 1) | |
| 165 return; | |
| 166 | |
| 167 if (!info[0]->IsObject()) { | |
| 168 v8SetReturnValue(info, false); | |
| 169 return; | |
| 170 } | |
| 171 | |
| 172 v8SetReturnValue(info, V8HTMLAllCollection::hasInstance(info[0], info.GetIso
late())); | |
| 173 } | |
| 174 | |
| 175 void V8InjectedScriptHost::isTypedArrayMethodCustom(const v8::FunctionCallbackIn
fo<v8::Value>& info) | |
| 176 { | |
| 177 if (info.Length() < 1) | |
| 178 return; | |
| 179 v8SetReturnValue(info, info[0]->IsTypedArray()); | |
| 180 } | |
| 181 | |
| 182 void V8InjectedScriptHost::subtypeMethodCustom(const v8::FunctionCallbackInfo<v8
::Value>& info) | |
| 183 { | |
| 184 if (info.Length() < 1) | |
| 185 return; | |
| 186 v8::Isolate* isolate = info.GetIsolate(); | |
| 187 | |
| 188 v8::Local<v8::Value> value = info[0]; | |
| 189 if (value->IsArray() || value->IsTypedArray() || value->IsArgumentsObject())
{ | |
| 190 v8SetReturnValue(info, v8AtomicString(isolate, "array")); | |
| 191 return; | |
| 192 } | |
| 193 if (value->IsDate()) { | |
| 194 v8SetReturnValue(info, v8AtomicString(isolate, "date")); | |
| 195 return; | |
| 196 } | |
| 197 if (value->IsRegExp()) { | |
| 198 v8SetReturnValue(info, v8AtomicString(isolate, "regexp")); | |
| 199 return; | |
| 200 } | |
| 201 if (value->IsMap() || value->IsWeakMap()) { | |
| 202 v8SetReturnValue(info, v8AtomicString(isolate, "map")); | |
| 203 return; | |
| 204 } | |
| 205 if (value->IsSet() || value->IsWeakSet()) { | |
| 206 v8SetReturnValue(info, v8AtomicString(isolate, "set")); | |
| 207 return; | |
| 208 } | |
| 209 if (value->IsMapIterator() || value->IsSetIterator()) { | |
| 210 v8SetReturnValue(info, v8AtomicString(isolate, "iterator")); | |
| 211 return; | |
| 212 } | |
| 213 if (value->IsGeneratorObject()) { | |
| 214 v8SetReturnValue(info, v8AtomicString(isolate, "generator")); | |
| 215 return; | |
| 216 } | |
| 217 if (V8Node::hasInstance(value, isolate)) { | |
| 218 v8SetReturnValue(info, v8AtomicString(isolate, "node")); | |
| 219 return; | |
| 220 } | |
| 221 if (V8NodeList::hasInstance(value, isolate) | |
| 222 || V8DOMTokenList::hasInstance(value, isolate) | |
| 223 || V8HTMLCollection::hasInstance(value, isolate) | |
| 224 || V8HTMLAllCollection::hasInstance(value, isolate)) { | |
| 225 v8SetReturnValue(info, v8AtomicString(isolate, "array")); | |
| 226 return; | |
| 227 } | |
| 228 if (value->IsNativeError() || V8DOMException::hasInstance(value, isolate)) { | |
| 229 v8SetReturnValue(info, v8AtomicString(isolate, "error")); | |
| 230 return; | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 void V8InjectedScriptHost::functionDetailsMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) | |
| 235 { | |
| 236 if (info.Length() < 1 || !info[0]->IsFunction()) | |
| 237 return; | |
| 238 | |
| 239 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(info[0]); | |
| 240 int lineNumber = function->GetScriptLineNumber(); | |
| 241 int columnNumber = function->GetScriptColumnNumber(); | |
| 242 | |
| 243 v8::Isolate* isolate = info.GetIsolate(); | |
| 244 v8::Local<v8::Object> location = v8::Object::New(isolate); | |
| 245 location->Set(v8AtomicString(isolate, "lineNumber"), v8::Integer::New(isolat
e, lineNumber)); | |
| 246 location->Set(v8AtomicString(isolate, "columnNumber"), v8::Integer::New(isol
ate, columnNumber)); | |
| 247 location->Set(v8AtomicString(isolate, "scriptId"), v8::Integer::New(isolate,
function->ScriptId())->ToString(isolate)); | |
| 248 | |
| 249 v8::Local<v8::Object> result = v8::Object::New(isolate); | |
| 250 result->Set(v8AtomicString(isolate, "location"), location); | |
| 251 | |
| 252 v8::Local<v8::String> name = functionDisplayName(function); | |
| 253 result->Set(v8AtomicString(isolate, "functionName"), name.IsEmpty() ? v8Atom
icString(isolate, "") : name); | |
| 254 | |
| 255 result->Set(v8AtomicString(isolate, "isGenerator"), v8::Boolean::New(isolate
, function->IsGeneratorFunction())); | |
| 256 | |
| 257 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 258 V8Debugger& debugger = host->debugger(); | |
| 259 v8::Local<v8::Value> scopes = debugger.functionScopes(function); | |
| 260 if (!scopes.IsEmpty() && scopes->IsArray()) | |
| 261 result->Set(v8AtomicString(isolate, "rawScopes"), scopes); | |
| 262 | |
| 263 v8SetReturnValue(info, result); | |
| 264 } | |
| 265 | |
| 266 void V8InjectedScriptHost::generatorObjectDetailsMethodCustom(const v8::Function
CallbackInfo<v8::Value>& info) | |
| 267 { | |
| 268 if (info.Length() < 1 || !info[0]->IsObject()) | |
| 269 return; | |
| 270 | |
| 271 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(info[0]); | |
| 272 | |
| 273 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 274 V8Debugger& debugger = host->debugger(); | |
| 275 v8SetReturnValue(info, debugger.generatorObjectDetails(object)); | |
| 276 } | |
| 277 | |
| 278 void V8InjectedScriptHost::collectionEntriesMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) | |
| 279 { | |
| 280 if (info.Length() < 1 || !info[0]->IsObject()) | |
| 281 return; | |
| 282 | |
| 283 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(info[0]); | |
| 284 | |
| 285 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 286 V8Debugger& debugger = host->debugger(); | |
| 287 v8SetReturnValue(info, debugger.collectionEntries(object)); | |
| 288 } | |
| 289 | |
| 290 void V8InjectedScriptHost::getInternalPropertiesMethodCustom(const v8::FunctionC
allbackInfo<v8::Value>& info) | |
| 291 { | |
| 292 if (info.Length() < 1 || !info[0]->IsObject()) | |
| 293 return; | |
| 294 | |
| 295 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(info[0]); | |
| 296 v8::MaybeLocal<v8::Array> properties = v8::Debug::GetInternalProperties(info
.GetIsolate(), object); | |
| 297 v8SetReturnValue(info, properties); | |
| 298 } | |
| 299 | |
| 300 static v8::Local<v8::Array> getJSListenerFunctions(v8::Isolate* isolate, Executi
onContext* executionContext, const EventListenerInfo& listenerInfo) | |
| 301 { | |
| 302 v8::Local<v8::Array> result = v8::Array::New(isolate); | |
| 303 size_t handlersCount = listenerInfo.eventListenerVector.size(); | |
| 304 for (size_t i = 0, outputIndex = 0; i < handlersCount; ++i) { | |
| 305 RefPtr<EventListener> listener = listenerInfo.eventListenerVector[i].lis
tener; | |
| 306 if (listener->type() != EventListener::JSEventListenerType) { | |
| 307 ASSERT_NOT_REACHED(); | |
| 308 continue; | |
| 309 } | |
| 310 V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListene
r*>(listener.get()); | |
| 311 v8::Local<v8::Context> context = toV8Context(executionContext, v8Listene
r->world()); | |
| 312 // Hide listeners from other contexts. | |
| 313 if (context != isolate->GetCurrentContext()) | |
| 314 continue; | |
| 315 v8::Local<v8::Object> function; | |
| 316 { | |
| 317 // getListenerObject() may cause JS in the event attribute to get co
mpiled, potentially unsuccessfully. | |
| 318 v8::TryCatch block; | |
| 319 function = v8Listener->getListenerObject(executionContext); | |
| 320 if (block.HasCaught()) | |
| 321 continue; | |
| 322 } | |
| 323 ASSERT(!function.IsEmpty()); | |
| 324 v8::Local<v8::Object> listenerEntry = v8::Object::New(isolate); | |
| 325 listenerEntry->Set(v8AtomicString(isolate, "listener"), function); | |
| 326 listenerEntry->Set(v8AtomicString(isolate, "useCapture"), v8::Boolean::N
ew(isolate, listenerInfo.eventListenerVector[i].useCapture)); | |
| 327 result->Set(v8::Number::New(isolate, outputIndex++), listenerEntry); | |
| 328 } | |
| 329 return result; | |
| 330 } | |
| 331 | |
| 332 void V8InjectedScriptHost::getEventListenersMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) | |
| 333 { | |
| 334 if (info.Length() < 1) | |
| 335 return; | |
| 336 | |
| 337 EventTarget* target = eventTargetFromScriptValue(info.GetIsolate(), info[0])
; | |
| 338 if (!target) | |
| 339 return; | |
| 340 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 341 Vector<EventListenerInfo> listenersArray; | |
| 342 host->getEventListenersImpl(target, listenersArray); | |
| 343 | |
| 344 v8::Local<v8::Object> result = v8::Object::New(info.GetIsolate()); | |
| 345 for (size_t i = 0; i < listenersArray.size(); ++i) { | |
| 346 v8::Local<v8::Array> listeners = getJSListenerFunctions(info.GetIsolate(
), target->executionContext(), listenersArray[i]); | |
| 347 if (!listeners->Length()) | |
| 348 continue; | |
| 349 AtomicString eventType = listenersArray[i].eventType; | |
| 350 result->Set(v8String(info.GetIsolate(), eventType), listeners); | |
| 351 } | |
| 352 | |
| 353 v8SetReturnValue(info, result); | |
| 354 } | |
| 355 | |
| 356 void V8InjectedScriptHost::inspectMethodCustom(const v8::FunctionCallbackInfo<v8
::Value>& info) | |
| 357 { | |
| 358 if (info.Length() < 2) | |
| 359 return; | |
| 360 | |
| 361 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 362 ScriptState* scriptState = ScriptState::current(info.GetIsolate()); | |
| 363 ScriptValue object(scriptState, info[0]); | |
| 364 ScriptValue hints(scriptState, info[1]); | |
| 365 host->inspectImpl(toJSONValue(object), toJSONValue(hints)); | |
| 366 } | |
| 367 | |
| 368 void V8InjectedScriptHost::evalMethodCustom(const v8::FunctionCallbackInfo<v8::V
alue>& info) | |
| 369 { | |
| 370 v8::Isolate* isolate = info.GetIsolate(); | |
| 371 if (info.Length() < 1) { | |
| 372 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "One argument expected."))); | |
| 373 return; | |
| 374 } | |
| 375 | |
| 376 v8::Local<v8::String> expression = info[0]->ToString(isolate); | |
| 377 if (expression.IsEmpty()) { | |
| 378 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "The argument must be a string."))); | |
| 379 return; | |
| 380 } | |
| 381 | |
| 382 ASSERT(isolate->InContext()); | |
| 383 v8::TryCatch tryCatch; | |
| 384 v8::Local<v8::Value> result; | |
| 385 if (!v8Call(V8ScriptRunner::compileAndRunInternalScript(expression, info.Get
Isolate()), result, tryCatch)) { | |
| 386 v8SetReturnValue(info, tryCatch.ReThrow()); | |
| 387 return; | |
| 388 } | |
| 389 v8SetReturnValue(info, result); | |
| 390 } | |
| 391 | |
| 392 static void setExceptionAsReturnValue(const v8::FunctionCallbackInfo<v8::Value>&
info, v8::Local<v8::Object> returnValue, v8::TryCatch& tryCatch) | |
| 393 { | |
| 394 v8::Isolate* isolate = info.GetIsolate(); | |
| 395 returnValue->Set(v8::String::NewFromUtf8(isolate, "result"), tryCatch.Except
ion()); | |
| 396 returnValue->Set(v8::String::NewFromUtf8(isolate, "exceptionDetails"), JavaS
criptCallFrame::createExceptionDetails(isolate, tryCatch.Message())); | |
| 397 v8SetReturnValue(info, returnValue); | |
| 398 } | |
| 399 | |
| 400 void V8InjectedScriptHost::evaluateWithExceptionDetailsMethodCustom(const v8::Fu
nctionCallbackInfo<v8::Value>& info) | |
| 401 { | |
| 402 v8::Isolate* isolate = info.GetIsolate(); | |
| 403 if (info.Length() < 1) { | |
| 404 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "One argument expected."))); | |
| 405 return; | |
| 406 } | |
| 407 | |
| 408 v8::Local<v8::String> expression = info[0]->ToString(isolate); | |
| 409 if (expression.IsEmpty()) { | |
| 410 isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(iso
late, "The argument must be a string."))); | |
| 411 return; | |
| 412 } | |
| 413 | |
| 414 ASSERT(isolate->InContext()); | |
| 415 v8::Local<v8::Object> wrappedResult = v8::Object::New(isolate); | |
| 416 if (wrappedResult.IsEmpty()) | |
| 417 return; | |
| 418 v8::TryCatch tryCatch; | |
| 419 v8::Local<v8::Script> script; | |
| 420 v8::Local<v8::Value> result; | |
| 421 if (!v8Call(V8ScriptRunner::compileScript(expression, String(), String(), Te
xtPosition(), isolate), script, tryCatch)) { | |
| 422 setExceptionAsReturnValue(info, wrappedResult, tryCatch); | |
| 423 return; | |
| 424 } | |
| 425 if (!v8Call(V8ScriptRunner::runCompiledScript(isolate, script, currentExecut
ionContext(isolate)), result, tryCatch)) { | |
| 426 setExceptionAsReturnValue(info, wrappedResult, tryCatch); | |
| 427 return; | |
| 428 } | |
| 429 | |
| 430 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "result"), result); | |
| 431 wrappedResult->Set(v8::String::NewFromUtf8(isolate, "exceptionDetails"), v8:
:Undefined(isolate)); | |
| 432 v8SetReturnValue(info, wrappedResult); | |
| 433 } | |
| 434 | |
| 435 void V8InjectedScriptHost::setFunctionVariableValueMethodCustom(const v8::Functi
onCallbackInfo<v8::Value>& info) | |
| 436 { | |
| 437 if (info.Length() < 4 || !info[0]->IsFunction() || !info[1]->IsInt32() || !i
nfo[2]->IsString()) | |
| 438 return; | |
| 439 | |
| 440 v8::Local<v8::Value> functionValue = info[0]; | |
| 441 int scopeIndex = info[1].As<v8::Int32>()->Value(); | |
| 442 String variableName = toCoreStringWithUndefinedOrNullCheck(info[2]); | |
| 443 v8::Local<v8::Value> newValue = info[3]; | |
| 444 | |
| 445 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 446 V8Debugger& debugger = host->debugger(); | |
| 447 v8SetReturnValue(info, debugger.setFunctionVariableValue(functionValue, scop
eIndex, variableName, newValue)); | |
| 448 } | |
| 449 | |
| 450 static bool getFunctionLocation(const v8::FunctionCallbackInfo<v8::Value>& info,
String* scriptId, int* lineNumber, int* columnNumber) | |
| 451 { | |
| 452 if (info.Length() < 1 || !info[0]->IsFunction()) | |
| 453 return false; | |
| 454 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(info[0]); | |
| 455 *lineNumber = function->GetScriptLineNumber(); | |
| 456 *columnNumber = function->GetScriptColumnNumber(); | |
| 457 if (*lineNumber == v8::Function::kLineOffsetNotFound || *columnNumber == v8:
:Function::kLineOffsetNotFound) | |
| 458 return false; | |
| 459 *scriptId = String::number(function->ScriptId()); | |
| 460 return true; | |
| 461 } | |
| 462 | |
| 463 void V8InjectedScriptHost::debugFunctionMethodCustom(const v8::FunctionCallbackI
nfo<v8::Value>& info) | |
| 464 { | |
| 465 String scriptId; | |
| 466 int lineNumber; | |
| 467 int columnNumber; | |
| 468 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) | |
| 469 return; | |
| 470 | |
| 471 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 472 host->debugFunction(scriptId, lineNumber, columnNumber); | |
| 473 } | |
| 474 | |
| 475 void V8InjectedScriptHost::undebugFunctionMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) | |
| 476 { | |
| 477 String scriptId; | |
| 478 int lineNumber; | |
| 479 int columnNumber; | |
| 480 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) | |
| 481 return; | |
| 482 | |
| 483 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 484 host->undebugFunction(scriptId, lineNumber, columnNumber); | |
| 485 } | |
| 486 | |
| 487 void V8InjectedScriptHost::monitorFunctionMethodCustom(const v8::FunctionCallbac
kInfo<v8::Value>& info) | |
| 488 { | |
| 489 String scriptId; | |
| 490 int lineNumber; | |
| 491 int columnNumber; | |
| 492 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) | |
| 493 return; | |
| 494 | |
| 495 v8::Local<v8::Value> name; | |
| 496 if (info.Length() > 0 && info[0]->IsFunction()) { | |
| 497 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(info[0]
); | |
| 498 name = function->GetName(); | |
| 499 if (!name->IsString() || !v8::Local<v8::String>::Cast(name)->Length()) | |
| 500 name = function->GetInferredName(); | |
| 501 } | |
| 502 | |
| 503 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 504 host->monitorFunction(scriptId, lineNumber, columnNumber, toCoreStringWithUn
definedOrNullCheck(name)); | |
| 505 } | |
| 506 | |
| 507 void V8InjectedScriptHost::unmonitorFunctionMethodCustom(const v8::FunctionCallb
ackInfo<v8::Value>& info) | |
| 508 { | |
| 509 String scriptId; | |
| 510 int lineNumber; | |
| 511 int columnNumber; | |
| 512 if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) | |
| 513 return; | |
| 514 | |
| 515 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 516 host->unmonitorFunction(scriptId, lineNumber, columnNumber); | |
| 517 } | |
| 518 | |
| 519 void V8InjectedScriptHost::callFunctionMethodCustom(const v8::FunctionCallbackIn
fo<v8::Value>& info) | |
| 520 { | |
| 521 if (info.Length() < 2 || info.Length() > 3 || !info[0]->IsFunction()) { | |
| 522 ASSERT_NOT_REACHED(); | |
| 523 return; | |
| 524 } | |
| 525 | |
| 526 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(info[0]); | |
| 527 v8::Local<v8::Value> receiver = info[1]; | |
| 528 | |
| 529 if (info.Length() < 3 || info[2]->IsUndefined()) { | |
| 530 v8::Local<v8::Value> result = function->Call(receiver, 0, 0); | |
| 531 v8SetReturnValue(info, result); | |
| 532 return; | |
| 533 } | |
| 534 | |
| 535 if (!info[2]->IsArray()) { | |
| 536 ASSERT_NOT_REACHED(); | |
| 537 return; | |
| 538 } | |
| 539 | |
| 540 v8::Local<v8::Array> arguments = v8::Local<v8::Array>::Cast(info[2]); | |
| 541 size_t argc = arguments->Length(); | |
| 542 OwnPtr<v8::Local<v8::Value>[]> argv = adoptArrayPtr(new v8::Local<v8::Value>
[argc]); | |
| 543 for (size_t i = 0; i < argc; ++i) { | |
| 544 if (!arguments->Get(info.GetIsolate()->GetCurrentContext(), v8::Integer:
:New(info.GetIsolate(), i)).ToLocal(&argv[i])) | |
| 545 return; | |
| 546 } | |
| 547 | |
| 548 v8::Local<v8::Value> result = function->Call(receiver, argc, argv.get()); | |
| 549 v8SetReturnValue(info, result); | |
| 550 } | |
| 551 | |
| 552 void V8InjectedScriptHost::suppressWarningsAndCallFunctionMethodCustom(const v8:
:FunctionCallbackInfo<v8::Value>& info) | |
| 553 { | |
| 554 InjectedScriptHost* host = V8InjectedScriptHost::toImpl(info.Holder()); | |
| 555 host->client()->muteWarningsAndDeprecations(); | |
| 556 | |
| 557 callFunctionMethodCustom(info); | |
| 558 | |
| 559 host->client()->unmuteWarningsAndDeprecations(); | |
| 560 } | |
| 561 | |
| 562 void V8InjectedScriptHost::setNonEnumPropertyMethodCustom(const v8::FunctionCall
backInfo<v8::Value>& info) | |
| 563 { | |
| 564 if (info.Length() < 3 || !info[0]->IsObject() || !info[1]->IsString()) | |
| 565 return; | |
| 566 | |
| 567 v8::Local<v8::Object> object = info[0].As<v8::Object>(); | |
| 568 object->ForceSet(info.GetIsolate()->GetCurrentContext(), info[1], info[2], v
8::DontEnum); | |
| 569 } | |
| 570 | |
| 571 void V8InjectedScriptHost::bindMethodCustom(const v8::FunctionCallbackInfo<v8::V
alue>& info) | |
| 572 { | |
| 573 if (info.Length() < 2 || !info[1]->IsString()) | |
| 574 return; | |
| 575 InjectedScriptNative* injectedScriptNative = InjectedScriptNative::fromInjec
tedScriptHost(info.Holder()); | |
| 576 if (!injectedScriptNative) | |
| 577 return; | |
| 578 | |
| 579 v8::Local<v8::String> v8groupName = info[1]->ToString(info.GetIsolate()); | |
| 580 String groupName = toCoreStringWithUndefinedOrNullCheck(v8groupName); | |
| 581 int id = injectedScriptNative->bind(info[0], groupName); | |
| 582 info.GetReturnValue().Set(id); | |
| 583 } | |
| 584 | |
| 585 void V8InjectedScriptHost::objectForIdMethodCustom(const v8::FunctionCallbackInf
o<v8::Value>& info) | |
| 586 { | |
| 587 if (info.Length() < 1 || !info[0]->IsInt32()) | |
| 588 return; | |
| 589 InjectedScriptNative* injectedScriptNative = InjectedScriptNative::fromInjec
tedScriptHost(info.Holder()); | |
| 590 if (!injectedScriptNative) | |
| 591 return; | |
| 592 int id = info[0].As<v8::Int32>()->Value(); | |
| 593 v8::Local<v8::Value> value = injectedScriptNative->objectForId(id); | |
| 594 if (!value.IsEmpty()) | |
| 595 info.GetReturnValue().Set(value); | |
| 596 } | |
| 597 | |
| 598 void V8InjectedScriptHost::idToObjectGroupNameMethodCustom(const v8::FunctionCal
lbackInfo<v8::Value>& info) | |
| 599 { | |
| 600 if (info.Length() < 1 || !info[0]->IsInt32()) | |
| 601 return; | |
| 602 InjectedScriptNative* injectedScriptNative = InjectedScriptNative::fromInjec
tedScriptHost(info.Holder()); | |
| 603 if (!injectedScriptNative) | |
| 604 return; | |
| 605 int id = info[0].As<v8::Int32>()->Value(); | |
| 606 String groupName = injectedScriptNative->groupName(id); | |
| 607 if (!groupName.isEmpty()) | |
| 608 info.GetReturnValue().Set(v8String(info.GetIsolate(), groupName)); | |
| 609 } | |
| 610 | |
| 611 } // namespace blink | |
| OLD | NEW |