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); }
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); }
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 messageHandlerInMainThread(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { ASSERT(isMainThread()); // It's possible that messageHandlerInMainThread() is invoked while we're initializing a window. // In that half-baked situation, we don't have a valid context nor a valid world, // so just return immediately. if (DOMWrapperWorld::windowIsBeingInitialized()) return; v8::Isolate* isolate = v8::Isolate::GetCurrent(); // If called during context initialization, there will be no entered window. LocalDOMWindow* enteredWindow = enteredDOMWindow(isolate); if (!enteredWindow) return; String errorMessage = toCoreString(message->Get()); v8::Handle<v8::StackTrace> stackTrace = message->GetStackTrace(); RefPtr<ScriptCallStack> callStack = nullptr; int scriptId = message->GetScriptOrigin().ScriptID()->Value(); // Currently stack trace is only collected when inspector is open. if (!stackTrace.IsEmpty() && stackTrace->GetFrameCount() > 0) { callStack = createScriptCallStack(stackTrace, ScriptCallStack::maxCallStackSizeToCapture, isolate); bool success = false; int topScriptId = callStack->at(0).scriptId().toInt(&success); if (success && topScriptId == scriptId) scriptId = 0; } else { Vector<ScriptCallFrame> callFrames; callStack = ScriptCallStack::create(callFrames); } v8::Handle<v8::Value> resourceName = message->GetScriptOrigin().ResourceName(); bool shouldUseDocumentURL = resourceName.IsEmpty() || !resourceName->IsString(); String resource = shouldUseDocumentURL ? enteredWindow->document()->url() : toCoreString(resourceName.As<v8::String>()); ScriptState* scriptState = ScriptState::current(isolate); RefPtr<ErrorEvent> event = ErrorEvent::create(errorMessage, resource, message->GetLineNumber(), message->GetStartColumn() + 1, &scriptState->world()); if (V8DOMWrapper::isDOMWrapper(data)) { v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(data); const WrapperTypeInfo* type = toWrapperTypeInfo(obj); if (V8DOMException::wrapperTypeInfo.isSubclass(type)) { DOMException* exception = V8DOMException::toNative(obj); if (exception && !exception->messageForConsole().isEmpty()) event->setUnsanitizedMessage("Uncaught " + exception->toStringForConsole()); } } // This method might be called while we're creating a new context. In this case, we // avoid storing the exception object, as we can't create a wrapper during context creation. // FIXME: Can we even get here during initialization now that we bail out when GetEntered returns an empty handle? LocalFrame* frame = enteredWindow->document()->frame(); if (frame && frame->script().existingWindowProxy(scriptState->world())) { V8ErrorHandler::storeExceptionOnErrorEventWrapper(event.get(), data, scriptState->context()->Global(), isolate); } enteredWindow->document()->reportException(event.release(), scriptId, callStack); }
static void V8ErrorMessageCallback(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data) { v8::HandleScope handle_scope; std::string error = + " Javascript error on line " + OSS::string_from_number(message->GetLineNumber()) + " : " + toString(message->GetSourceLine()); OSS::log_error(error); }
static ScriptCallFrame toScriptCallFrame(v8::Handle<v8::StackFrame> frame) { String sourceName; v8::Local<v8::String> sourceNameValue(frame->GetScriptNameOrSourceURL()); if (!sourceNameValue.IsEmpty()) sourceName = toWebCoreString(sourceNameValue); String functionName; v8::Local<v8::String> functionNameValue(frame->GetFunctionName()); if (!functionNameValue.IsEmpty()) functionName = toWebCoreString(functionNameValue); int sourceLineNumber = frame->GetLineNumber(); int sourceColumn = frame->GetColumn(); return ScriptCallFrame(functionName, sourceName, sourceLineNumber, sourceColumn); }
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); }
static ScriptCallFrame toScriptCallFrame(v8::Handle<v8::StackFrame> frame) { StringBuilder stringBuilder; stringBuilder.appendNumber(frame->GetScriptId()); String scriptId = stringBuilder.toString(); String sourceName; v8::Local<v8::String> sourceNameValue(frame->GetScriptNameOrSourceURL()); if (!sourceNameValue.IsEmpty()) { int length = sourceNameValue->Length(); sourceName = StringTraits<String>::fromV8String<V8StringOneByteTrait>(sourceNameValue, length); } String functionName; v8::Local<v8::String> functionNameValue(frame->GetFunctionName()); if (!functionNameValue.IsEmpty()) functionName = toCoreString(functionNameValue); int sourceLineNumber = frame->GetLineNumber(); int sourceColumn = frame->GetColumn(); return ScriptCallFrame(functionName, scriptId, sourceName, sourceLineNumber, sourceColumn); }
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); }