void V8ConsoleMessage::handler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { // Use the frame where JavaScript is called from. Frame* frame = V8Proxy::retrieveFrameForEnteredContext(); if (!frame) return; Page* page = frame->page(); if (!page) return; v8::Handle<v8::String> errorMessageString = message->Get(); ASSERT(!errorMessageString.IsEmpty()); String errorMessage = toWebCoreString(errorMessageString); v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace(); RefPtr<ScriptCallStack> callStack; // Currently stack trace is only collected when inspector is open. if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallStackSizeToCapture); v8::Handle<v8::Value> resourceName = message->GetScriptResourceName(); bool useURL = resourceName.IsEmpty() || !resourceName->IsString(); String resourceNameString = useURL ? frame->document()->url() : toWebCoreString(resourceName); V8ConsoleMessage consoleMessage(errorMessage, resourceNameString, message->GetLineNumber()); consoleMessage.dispatchNow(page, callStack); }
static void messageHandlerInMainThread(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { DOMWindow* firstWindow = firstDOMWindow(); if (!firstWindow->isCurrentlyDisplayedInFrame()) return; String errorMessage = toWebCoreString(message->Get()); v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace(); RefPtr<ScriptCallStack> callStack; // Currently stack trace is only collected when inspector is open. if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallStackSizeToCapture); v8::Handle<v8::Value> resourceName = message->GetScriptResourceName(); bool shouldUseDocumentURL = resourceName.IsEmpty() || !resourceName->IsString(); String resource = shouldUseDocumentURL ? firstWindow->document()->url() : toWebCoreString(resourceName); RefPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, resource, message->GetLineNumber(), message->GetStartColumn()); // messageHandlerInMainThread can be called while we're creating a new context. // Since we cannot create a wrapper in the intermediate timing, we need to skip // creating a wrapper for |event|. DOMWrapperWorld* world = DOMWrapperWorld::current(); Frame* frame = firstWindow->document()->frame(); if (world && frame && frame->script()->existingWindowShell(world)) { v8::Local<v8::Value> wrappedEvent = toV8(event.get(), v8::Handle<v8::Object>(), v8::Isolate::GetCurrent()); if (!wrappedEvent.IsEmpty()) { ASSERT(wrappedEvent->IsObject()); v8::Local<v8::Object>::Cast(wrappedEvent)->SetHiddenValue(V8HiddenPropertyName::error(), data); } } AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? SharableCrossOrigin : NotSharableCrossOrigin; firstWindow->document()->reportException(event.release(), callStack, corsStatus); }
Value Interpreter::eval(const String& data, const String& name) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::HandleScope scope(isolate); v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, mData->context); v8::Context::Scope contextScope(context); v8::Handle<v8::String> fileName = v8::String::NewFromUtf8(isolate, name.constData()); v8::Handle<v8::String> source = v8::String::NewFromUtf8(isolate, data.constData()); v8::TryCatch try_catch; v8::Handle<v8::Script> script = v8::Script::Compile(source, fileName); if (script.IsEmpty()) { error() << "script" << name << "didn't compile"; return Value(); } const v8::Handle<v8::Value> result = script->Run(); if (try_catch.HasCaught()) { const v8::Handle<v8::Message> msg = try_catch.Message(); { const v8::String::Utf8Value str(msg->Get()); error() << ToCString(str); } { const v8::String::Utf8Value str(msg->GetScriptResourceName()); error() << String::format<64>("At %s:%d", ToCString(str), msg->GetLineNumber()); } return Value(); } return mData->v8ValueToValue(result); }
static void messageHandlerInWorker(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { static bool isReportingException = false; // Exceptions that occur in error handler should be ignored since in that case // WorkerGlobalScope::reportException will send the exception to the worker object. if (isReportingException) return; isReportingException = true; // During the frame teardown, there may not be a valid context. if (ScriptExecutionContext* context = getScriptExecutionContext()) { String errorMessage = toWebCoreString(message->Get()); String sourceURL = toWebCoreString(message->GetScriptResourceName()); RefPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, sourceURL, message->GetLineNumber(), message->GetStartColumn()); v8::Local<v8::Value> wrappedEvent = toV8(event.get(), v8::Handle<v8::Object>(), v8::Isolate::GetCurrent()); if (!wrappedEvent.IsEmpty()) { ASSERT(wrappedEvent->IsObject()); v8::Local<v8::Object>::Cast(wrappedEvent)->SetHiddenValue(V8HiddenPropertyName::error(), data); } AccessControlStatus corsStatus = message->IsSharedCrossOrigin() ? SharableCrossOrigin : NotSharableCrossOrigin; context->reportException(event.release(), 0, corsStatus); } isReportingException = false; }
static void reportUncaughtException(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { DOMWindow* firstWindow = firstDOMWindow(BindingState::instance()); if (!firstWindow->isCurrentlyDisplayedInFrame()) return; String errorMessage = toWebCoreString(message->Get()); v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace(); RefPtr<ScriptCallStack> callStack; // Currently stack trace is only collected when inspector is open. if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallStackSizeToCapture); v8::Handle<v8::Value> resourceName = message->GetScriptResourceName(); bool shouldUseDocumentURL = resourceName.IsEmpty() || !resourceName->IsString(); String resource = shouldUseDocumentURL ? firstWindow->document()->url() : toWebCoreString(resourceName); firstWindow->document()->reportException(errorMessage, message->GetLineNumber(), resource, callStack); }
static void v8MessageHandler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { static bool isReportingException = false; // Exceptions that occur in error handler should be ignored since in that case // WorkerContext::reportException will send the exception to the worker object. if (isReportingException) return; isReportingException = true; // During the frame teardown, there may not be a valid context. if (ScriptExecutionContext* context = getScriptExecutionContext()) { String errorMessage = toWebCoreString(message->Get()); int lineNumber = message->GetLineNumber(); String sourceURL = toWebCoreString(message->GetScriptResourceName()); context->reportException(errorMessage, lineNumber, sourceURL, 0); } isReportingException = false; }
void V8ConsoleMessage::handler(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { // Use the frame where JavaScript is called from. Frame* frame = V8Proxy::retrieveFrameForEnteredContext(); if (!frame) return; Page* page = frame->page(); if (!page) return; v8::Handle<v8::String> errorMessageString = message->Get(); ASSERT(!errorMessageString.IsEmpty()); String errorMessage = toWebCoreString(errorMessageString); v8::Handle<v8::Value> resourceName = message->GetScriptResourceName(); bool useURL = resourceName.IsEmpty() || !resourceName->IsString(); String resourceNameString = useURL ? frame->document()->url() : toWebCoreString(resourceName); V8ConsoleMessage consoleMessage(errorMessage, resourceNameString, message->GetLineNumber()); consoleMessage.dispatchNow(page); }
void QQmlJavaScriptExpression::exceptionToError(v8::Handle<v8::Message> message, QQmlError &error) { Q_ASSERT(!message.IsEmpty()); v8::Handle<v8::Value> name = message->GetScriptResourceName(); v8::Handle<v8::String> description = message->Get(); int lineNumber = message->GetLineNumber(); v8::Local<v8::String> file = name->IsString()?name->ToString():v8::Local<v8::String>(); if (file.IsEmpty() || file->Length() == 0) error.setUrl(QUrl()); else error.setUrl(QUrl(QV8Engine::toStringStatic(file))); error.setLine(lineNumber); error.setColumn(-1); QString qDescription = QV8Engine::toStringStatic(description); if (qDescription.startsWith(QLatin1String("Uncaught "))) qDescription = qDescription.mid(9 /* strlen("Uncaught ") */); error.setDescription(qDescription); }
static void AppendExceptionLine(Environment* env, v8::Handle<v8::Value> er, v8::Handle<v8::Message> message) { if (message.IsEmpty()) return; v8::HandleScope scope(env->isolate()); v8::Local<v8::Object> err_obj; if (!er.IsEmpty() && er->IsObject()) { err_obj = er.As<v8::Object>(); // Do it only once per message if (!err_obj->GetHiddenValue(env->processed_string()).IsEmpty()) return; err_obj->SetHiddenValue(env->processed_string(), True(env->isolate())); } static char arrow[1024]; // Print (filename):(line number): (message). v8::String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = *filename; int linenum = message->GetLineNumber(); // Print line of source code. v8::String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = *sourceline; // Because of how node modules work, all scripts are wrapped with a // "function (module, exports, __filename, ...) {" // to provide script local variables. // // When reporting errors on the first line of a script, this wrapper // function is leaked to the user. There used to be a hack here to // truncate off the first 62 characters, but it caused numerous other // problems when vm.runIn*Context() methods were used for non-module // code. // // If we ever decide to re-instate such a hack, the following steps // must be taken: // // 1. Pass a flag around to say "this code was wrapped" // 2. Update the stack frame output so that it is also correct. // // It would probably be simpler to add a line rather than add some // number of characters to the first line, since V8 truncates the // sourceline to 78 characters, and we end up not providing very much // useful debugging info to the user if we remove 62 characters. int start = message->GetStartColumn(); int end = message->GetEndColumn(); int off = snprintf(arrow, sizeof(arrow), "%s:%i\n%s\n", filename_string, linenum, sourceline_string); assert(off >= 0); // Print wavy underline (GetUnderline is deprecated). for (int i = 0; i < start; i++) { if (sourceline_string[i] == '\0' || static_cast<size_t>(off) >= sizeof(arrow)) { break; } assert(static_cast<size_t>(off) < sizeof(arrow)); arrow[off++] = (sourceline_string[i] == '\t') ? '\t' : ' '; } for (int i = start; i < end; i++) { if (sourceline_string[i] == '\0' || static_cast<size_t>(off) >= sizeof(arrow)) { break; } assert(static_cast<size_t>(off) < sizeof(arrow)); arrow[off++] = '^'; } assert(static_cast<size_t>(off - 1) <= sizeof(arrow) - 1); arrow[off++] = '\n'; arrow[off] = '\0'; v8::Local<v8::String> arrow_str = v8::String::NewFromUtf8(env->isolate(), arrow); v8::Local<v8::Value> msg; v8::Local<v8::Value> stack; // Allocation failed, just print it out if (arrow_str.IsEmpty() || err_obj.IsEmpty() || !err_obj->IsNativeError()) goto print; msg = err_obj->Get(env->message_string()); stack = err_obj->Get(env->stack_string()); if (msg.IsEmpty() || stack.IsEmpty()) goto print; err_obj->Set(env->message_string(), v8::String::Concat(arrow_str, msg->ToString())); err_obj->Set(env->stack_string(), v8::String::Concat(arrow_str, stack->ToString())); return; print: if (env->printed_error()) return; env->set_printed_error(true); // uv_tty_reset_mode(); fprintf(stderr, "\n%s", arrow); }