void MainThreadDebugger::querySelectorAllCallback( const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 1) return; String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); if (selector.isEmpty()) return; Node* node = secondArgumentAsNode(info); if (!node || !node->isContainerNode()) return; ExceptionState exceptionState(ExceptionState::ExecutionContext, "$$", "CommandLineAPI", info.Holder(), info.GetIsolate()); // toV8(elementList) doesn't work here, since we need a proper Array instance, // not NodeList. StaticElementList* elementList = toContainerNode(node)->querySelectorAll( AtomicString(selector), exceptionState); if (exceptionState.hadException() || !elementList) return; v8::Isolate* isolate = info.GetIsolate(); v8::Local<v8::Context> context = isolate->GetCurrentContext(); v8::Local<v8::Array> nodes = v8::Array::New(isolate, elementList->length()); for (size_t i = 0; i < elementList->length(); ++i) { Element* element = elementList->item(i); if (!createDataPropertyInArray( context, nodes, i, toV8(element, info.Holder(), info.GetIsolate())) .FromMaybe(false)) return; } info.GetReturnValue().Set(nodes); }
String InspectorOverlay::evaluateInOverlayForTest(const String& script) { ScriptForbiddenScope::AllowUserAgentScript allowScript; v8::HandleScope handleScope(toIsolate(overlayMainFrame())); v8::Local<v8::Value> string = toLocalFrame(overlayPage()->mainFrame())->script().executeScriptInMainWorldAndReturnValue(ScriptSourceCode(script), ScriptController::ExecuteScriptWhenScriptsDisabled); return toCoreStringWithUndefinedOrNullCheck(string); }
String JavaScriptCallFrame::callV8FunctionReturnString(const char* name) const { v8::HandleScope handleScope(m_isolate); v8::Handle<v8::Object> callFrame = m_callFrame.newLocal(m_isolate); v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(callFrame->Get(v8AtomicString(m_isolate, name))); v8::Handle<v8::Value> result = func->Call(callFrame, 0, 0); return toCoreStringWithUndefinedOrNullCheck(result); }
void V8Window::showModalDialogMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info) { DOMWindow* impl = V8Window::toNative(info.Holder()); ExceptionState exceptionState(ExceptionState::ExecutionContext, "showModalDialog", "Window", info.Holder(), info.GetIsolate()); if (!BindingSecurity::shouldAllowAccessToFrame(impl->frame(), exceptionState)) { exceptionState.throwIfNeeded(); return; } // FIXME: Handle exceptions properly. String urlString = toCoreStringWithUndefinedOrNullCheck(info[0]); DialogHandler handler(info[1]); String dialogFeaturesString = toCoreStringWithUndefinedOrNullCheck(info[2]); impl->showModalDialog(urlString, dialogFeaturesString, activeDOMWindow(), firstDOMWindow(), setUpDialog, &handler); v8SetReturnValue(info, handler.returnValue(info.GetIsolate())); }
// static std::unique_ptr<SourceLocation> SourceLocation::fromFunction( v8::Local<v8::Function> function) { if (!function.IsEmpty()) return SourceLocation::create( toCoreStringWithUndefinedOrNullCheck( function->GetScriptOrigin().ResourceName()), function->GetScriptLineNumber() + 1, function->GetScriptColumnNumber() + 1, nullptr, function->ScriptId()); return SourceLocation::create(String(), 0, 0, nullptr, 0); }
String JavaScriptCallFrame::callV8FunctionReturnString(const char* name) const { v8::HandleScope handleScope(m_isolate); v8::Context::Scope contextScope(m_debuggerContext.newLocal(m_isolate)); v8::Local<v8::Object> callFrame = m_callFrame.newLocal(m_isolate); v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(callFrame->Get(v8AtomicString(m_isolate, name))); v8::Local<v8::Value> result; if (!V8ScriptRunner::callInternalFunction(func, callFrame, 0, 0, m_isolate).ToLocal(&result)) return String(); return toCoreStringWithUndefinedOrNullCheck(result); }
void V8InjectedScriptHost::bindCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 2 || !info[1]->IsString()) return; InjectedScriptNative* injectedScriptNative = InjectedScriptNative::fromInjectedScriptHost(info.Holder()); if (!injectedScriptNative) return; v8::Local<v8::String> v8groupName = info[1]->ToString(info.GetIsolate()); String groupName = toCoreStringWithUndefinedOrNullCheck(v8groupName); int id = injectedScriptNative->bind(info[0], groupName); info.GetReturnValue().Set(id); }
void V8InjectedScriptHost::setFunctionVariableValueCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 4 || !info[0]->IsFunction() || !info[1]->IsInt32() || !info[2]->IsString()) return; v8::Local<v8::Value> functionValue = info[0]; int scopeIndex = info[1].As<v8::Int32>()->Value(); String variableName = toCoreStringWithUndefinedOrNullCheck(info[2]); v8::Local<v8::Value> newValue = info[3]; InjectedScriptHost* host = V8InjectedScriptHost::unwrap(info.GetIsolate()->GetCurrentContext(), info.Holder()); V8DebuggerImpl& debugger = static_cast<V8DebuggerImpl&>(host->debugger()); v8SetReturnValue(info, debugger.setFunctionVariableValue(functionValue, scopeIndex, variableName, newValue)); }
void InjectedScript::makeCallWithExceptionDetails(ScriptFunctionCall& function, RefPtr<JSONValue>* result, RefPtr<TypeBuilder::Debugger::ExceptionDetails>* exceptionDetails) { ScriptState::Scope scope(injectedScriptObject().scriptState()); v8::TryCatch tryCatch; ScriptValue resultValue = function.callWithoutExceptionHandling(); if (tryCatch.HasCaught()) { v8::Local<v8::Message> message = tryCatch.Message(); String text = !message.IsEmpty() ? toCoreStringWithUndefinedOrNullCheck(message->Get()) : "Internal error"; *exceptionDetails = TypeBuilder::Debugger::ExceptionDetails::create().setText(text); } else { *result = toJSONValue(resultValue); if (!*result) *result = JSONString::create(String::format("Object has too long reference chain(must not be longer than %d)", JSONValue::maxDepth)); } }
void MainThreadDebugger::xpathSelectorCallback( const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 1) return; String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); if (selector.isEmpty()) return; Node* node = secondArgumentAsNode(info); if (!node || !node->isContainerNode()) return; ExceptionState exceptionState(ExceptionState::ExecutionContext, "$x", "CommandLineAPI", info.Holder(), info.GetIsolate()); XPathResult* result = XPathEvaluator::create()->evaluate( selector, node, nullptr, XPathResult::kAnyType, ScriptValue(), exceptionState); if (exceptionState.hadException() || !result) return; if (result->resultType() == XPathResult::kNumberType) { info.GetReturnValue().Set(toV8(result->numberValue(exceptionState), info.Holder(), info.GetIsolate())); } else if (result->resultType() == XPathResult::kStringType) { info.GetReturnValue().Set(toV8(result->stringValue(exceptionState), info.Holder(), info.GetIsolate())); } else if (result->resultType() == XPathResult::kBooleanType) { info.GetReturnValue().Set(toV8(result->booleanValue(exceptionState), info.Holder(), info.GetIsolate())); } else { v8::Isolate* isolate = info.GetIsolate(); v8::Local<v8::Context> context = isolate->GetCurrentContext(); v8::Local<v8::Array> nodes = v8::Array::New(isolate); size_t index = 0; while (Node* node = result->iterateNext(exceptionState)) { if (exceptionState.hadException()) return; if (!createDataPropertyInArray( context, nodes, index++, toV8(node, info.Holder(), info.GetIsolate())) .FromMaybe(false)) return; } info.GetReturnValue().Set(nodes); } }
void MainThreadDebugger::querySelectorCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { if (info.Length() < 1) return; String selector = toCoreStringWithUndefinedOrNullCheck(info[0]); if (selector.isEmpty()) return; Node* node = secondArgumentAsNode(info); if (!node || !node->isContainerNode()) return; ExceptionState exceptionState(ExceptionState::ExecutionContext, "$", "CommandLineAPI", info.Holder(), info.GetIsolate()); Element* element = toContainerNode(node)->querySelector(AtomicString(selector), exceptionState); if (exceptionState.throwIfNeeded()) return; if (element) info.GetReturnValue().Set(toV8(element, info.Holder(), info.GetIsolate())); else info.GetReturnValue().Set(v8::Null(info.GetIsolate())); }
void V8InjectedScriptHost::monitorFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { String scriptId; int lineNumber; int columnNumber; if (!getFunctionLocation(info, &scriptId, &lineNumber, &columnNumber)) return; v8::Local<v8::Value> name; if (info.Length() > 0 && info[0]->IsFunction()) { v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(info[0]); name = function->GetName(); if (!name->IsString() || !v8::Local<v8::String>::Cast(name)->Length()) name = function->GetInferredName(); } InjectedScriptHost* host = V8InjectedScriptHost::unwrap(info.GetIsolate()->GetCurrentContext(), info.Holder()); host->monitorFunction(scriptId, lineNumber, columnNumber, toCoreStringWithUndefinedOrNullCheck(name)); }
// static std::unique_ptr<SourceLocation> SourceLocation::fromMessage( v8::Isolate* isolate, v8::Local<v8::Message> message, ExecutionContext* executionContext) { v8::Local<v8::StackTrace> stack = message->GetStackTrace(); std::unique_ptr<v8_inspector::V8StackTrace> stackTrace = nullptr; V8PerIsolateData* data = V8PerIsolateData::from(isolate); if (data && data->threadDebugger()) stackTrace = data->threadDebugger()->v8Inspector()->createStackTrace(stack); int scriptId = message->GetScriptOrigin().ScriptID()->Value(); if (!stack.IsEmpty() && stack->GetFrameCount() > 0) { int topScriptId = stack->GetFrame(0)->GetScriptId(); if (topScriptId == scriptId) scriptId = 0; } int lineNumber = 0; int columnNumber = 0; if (v8Call(message->GetLineNumber(isolate->GetCurrentContext()), lineNumber) && v8Call(message->GetStartColumn(isolate->GetCurrentContext()), columnNumber)) ++columnNumber; if ((!scriptId || !lineNumber) && stackTrace && !stackTrace->isEmpty()) return SourceLocation::createFromNonEmptyV8StackTrace(std::move(stackTrace), 0); String url = toCoreStringWithUndefinedOrNullCheck( message->GetScriptOrigin().ResourceName()); if (url.isNull()) url = executionContext->url(); return SourceLocation::create(url, lineNumber, columnNumber, std::move(stackTrace), scriptId); }