diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-05-24 08:28:08 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2016-05-24 08:28:08 +0000 |
commit | a4e969f4965059196ca948db781e52f7cfebf19e (patch) | |
tree | 6ca352808c8fdc52006a0f33f6ae3c593b23867d /Source/JavaScriptCore/runtime/ErrorInstance.cpp | |
parent | 41386e9cb918eed93b3f13648cbef387e371e451 (diff) | |
download | WebKitGtk-tarball-a4e969f4965059196ca948db781e52f7cfebf19e.tar.gz |
webkitgtk-2.12.3webkitgtk-2.12.3
Diffstat (limited to 'Source/JavaScriptCore/runtime/ErrorInstance.cpp')
-rw-r--r-- | Source/JavaScriptCore/runtime/ErrorInstance.cpp | 119 |
1 files changed, 112 insertions, 7 deletions
diff --git a/Source/JavaScriptCore/runtime/ErrorInstance.cpp b/Source/JavaScriptCore/runtime/ErrorInstance.cpp index c831f9183..164258266 100644 --- a/Source/JavaScriptCore/runtime/ErrorInstance.cpp +++ b/Source/JavaScriptCore/runtime/ErrorInstance.cpp @@ -21,30 +21,135 @@ #include "config.h" #include "ErrorInstance.h" +#include "CodeBlock.h" +#include "InlineCallFrame.h" #include "JSScope.h" -#include "Operations.h" +#include "JSCInlines.h" +#include "JSGlobalObjectFunctions.h" +#include <wtf/Vector.h> namespace JSC { STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ErrorInstance); -const ClassInfo ErrorInstance::s_info = { "Error", &JSNonFinalObject::s_info, 0, 0, CREATE_METHOD_TABLE(ErrorInstance) }; +const ClassInfo ErrorInstance::s_info = { "Error", &JSNonFinalObject::s_info, 0, CREATE_METHOD_TABLE(ErrorInstance) }; ErrorInstance::ErrorInstance(VM& vm, Structure* structure) : JSNonFinalObject(vm, structure) - , m_appendSourceToMessage(false) { } -void ErrorInstance::finishCreation(VM& vm, const String& message, Vector<StackFrame> stackTrace) +static void appendSourceToError(CallFrame* callFrame, ErrorInstance* exception, unsigned bytecodeOffset) +{ + ErrorInstance::SourceAppender appender = exception->sourceAppender(); + exception->clearSourceAppender(); + RuntimeType type = exception->runtimeTypeForCause(); + exception->clearRuntimeTypeForCause(); + + if (!callFrame->codeBlock()->hasExpressionInfo()) + return; + + int startOffset = 0; + int endOffset = 0; + int divotPoint = 0; + unsigned line = 0; + unsigned column = 0; + + CodeBlock* codeBlock; + CodeOrigin codeOrigin = callFrame->codeOrigin(); + if (codeOrigin && codeOrigin.inlineCallFrame) + codeBlock = baselineCodeBlockForInlineCallFrame(codeOrigin.inlineCallFrame); + else + codeBlock = callFrame->codeBlock(); + + codeBlock->expressionRangeForBytecodeOffset(bytecodeOffset, divotPoint, startOffset, endOffset, line, column); + + int expressionStart = divotPoint - startOffset; + int expressionStop = divotPoint + endOffset; + + StringView sourceString = codeBlock->source()->source(); + if (!expressionStop || expressionStart > static_cast<int>(sourceString.length())) + return; + + VM* vm = &callFrame->vm(); + JSValue jsMessage = exception->getDirect(*vm, vm->propertyNames->message); + if (!jsMessage || !jsMessage.isString()) + return; + + String message = asString(jsMessage)->value(callFrame); + if (expressionStart < expressionStop) + message = appender(message, codeBlock->source()->getRange(expressionStart, expressionStop).toString(), type, ErrorInstance::FoundExactSource); + else { + // No range information, so give a few characters of context. + int dataLength = sourceString.length(); + int start = expressionStart; + int stop = expressionStart; + // Get up to 20 characters of context to the left and right of the divot, clamping to the line. + // Then strip whitespace. + while (start > 0 && (expressionStart - start < 20) && sourceString[start - 1] != '\n') + start--; + while (start < (expressionStart - 1) && isStrWhiteSpace(sourceString[start])) + start++; + while (stop < dataLength && (stop - expressionStart < 20) && sourceString[stop] != '\n') + stop++; + while (stop > expressionStart && isStrWhiteSpace(sourceString[stop - 1])) + stop--; + message = appender(message, codeBlock->source()->getRange(start, stop).toString(), type, ErrorInstance::FoundApproximateSource); + } + exception->putDirect(*vm, vm->propertyNames->message, jsString(vm, message)); + +} + +class FindFirstCallerFrameWithCodeblockFunctor { +public: + FindFirstCallerFrameWithCodeblockFunctor(CallFrame* startCallFrame) + : m_startCallFrame(startCallFrame) + , m_foundCallFrame(nullptr) + , m_foundStartCallFrame(false) + , m_index(0) + { } + + StackVisitor::Status operator()(StackVisitor& visitor) + { + if (!m_foundStartCallFrame && (visitor->callFrame() == m_startCallFrame)) + m_foundStartCallFrame = true; + + if (m_foundStartCallFrame) { + if (visitor->callFrame()->codeBlock()) { + m_foundCallFrame = visitor->callFrame(); + return StackVisitor::Done; + } + m_index++; + } + + return StackVisitor::Continue; + } + + CallFrame* foundCallFrame() const { return m_foundCallFrame; } + unsigned index() const { return m_index; } + +private: + CallFrame* m_startCallFrame; + CallFrame* m_foundCallFrame; + bool m_foundStartCallFrame; + unsigned m_index; +}; + +void ErrorInstance::finishCreation(ExecState* exec, VM& vm, const String& message, bool useCurrentFrame) { Base::finishCreation(vm); ASSERT(inherits(info())); if (!message.isNull()) putDirect(vm, vm.propertyNames->message, jsString(&vm, message), DontEnum); - - if (!stackTrace.isEmpty()) - putDirect(vm, vm.propertyNames->stack, vm.interpreter->stackTraceAsString(vm.topCallFrame, stackTrace), DontEnum); + + unsigned bytecodeOffset = hasSourceAppender(); + CallFrame* callFrame = nullptr; + bool hasTrace = addErrorInfoAndGetBytecodeOffset(exec, vm, this, useCurrentFrame, callFrame, bytecodeOffset); + + if (hasTrace && callFrame && hasSourceAppender()) { + if (callFrame && callFrame->codeBlock()) + appendSourceToError(callFrame, this, bytecodeOffset); + } } } // namespace JSC |