- // We're in a "new" expression, so we need to skip over the "new.." part
- int startPoint = divotPoint - (startOffset ? startOffset - 4 : 0); // -4 for "new "
- const UChar* data = codeBlock->source()->data();
- while (startPoint < divotPoint && isStrWhiteSpace(data[startPoint]))
- startPoint++;
-
- UString errorMessage = createErrorMessage(exec, codeBlock, line, startPoint, divotPoint, value, "not a constructor");
- JSObject* exception = Error::create(exec, TypeError, errorMessage, line, codeBlock->ownerExecutable()->sourceID(), codeBlock->ownerExecutable()->sourceURL());
- exception->putWithAttributes(exec, Identifier(exec, expressionBeginOffsetPropertyName), jsNumber(exec, divotPoint - startOffset), ReadOnly | DontDelete);
- exception->putWithAttributes(exec, Identifier(exec, expressionCaretOffsetPropertyName), jsNumber(exec, divotPoint), ReadOnly | DontDelete);
- exception->putWithAttributes(exec, Identifier(exec, expressionEndOffsetPropertyName), jsNumber(exec, divotPoint + endOffset), ReadOnly | DontDelete);
+ ASSERT(occurrence == ErrorInstance::FoundExactSource);
+ return makeString(originalMessage, " (evaluating '", sourceText, "')");
+}
+
+static String functionCallBase(const String& sourceText)
+{
+ // This function retrieves the 'foo.bar' substring from 'foo.bar(baz)'.
+ // FIXME: This function has simple processing of /* */ style comments.
+ // It doesn't properly handle embedded comments of string literals that contain
+ // parenthesis or comment constructs, e.g. foo.bar("/abc\)*/").
+ // https://bugs.webkit.org/show_bug.cgi?id=146304
+
+ unsigned sourceLength = sourceText.length();
+ unsigned idx = sourceLength - 1;
+ if (sourceLength < 2 || sourceText[idx] != ')') {
+ // For function calls that have many new lines in between their open parenthesis
+ // and their closing parenthesis, the text range passed into the message appender
+ // will not inlcude the text in between these parentheses, it will just be the desired
+ // text that precedes the parentheses.
+ return sourceText;
+ }
+
+ unsigned parenStack = 1;
+ bool isInMultiLineComment = false;
+ idx -= 1;
+ // Note that we're scanning text right to left instead of the more common left to right,
+ // so syntax detection is backwards.
+ while (parenStack > 0) {
+ UChar curChar = sourceText[idx];
+ if (isInMultiLineComment) {
+ if (idx > 0 && curChar == '*' && sourceText[idx - 1] == '/') {
+ isInMultiLineComment = false;
+ idx -= 1;
+ }
+ } else if (curChar == '(')
+ parenStack -= 1;
+ else if (curChar == ')')
+ parenStack += 1;
+ else if (idx > 0 && curChar == '/' && sourceText[idx - 1] == '*') {
+ isInMultiLineComment = true;
+ idx -= 1;
+ }
+
+ if (!idx)
+ break;
+
+ idx -= 1;
+ }
+
+ return sourceText.left(idx + 1);
+}
+
+static String notAFunctionSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType type, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
+{
+ ASSERT(type != TypeFunction);
+
+ if (occurrence == ErrorInstance::FoundApproximateSource)
+ return defaultApproximateSourceError(originalMessage, sourceText);
+
+ ASSERT(occurrence == ErrorInstance::FoundExactSource);
+ auto notAFunctionIndex = originalMessage.reverseFind("is not a function");
+ RELEASE_ASSERT(notAFunctionIndex != notFound);
+ StringView displayValue;
+ if (originalMessage.is8Bit())
+ displayValue = StringView(originalMessage.characters8(), notAFunctionIndex - 1);
+ else
+ displayValue = StringView(originalMessage.characters16(), notAFunctionIndex - 1);
+
+ String base = functionCallBase(sourceText);
+ StringBuilder builder;
+ builder.append(base);
+ builder.appendLiteral(" is not a function. (In '");
+ builder.append(sourceText);
+ builder.appendLiteral("', '");
+ builder.append(base);
+ builder.appendLiteral("' is ");
+ if (type == TypeObject)
+ builder.appendLiteral("an instance of ");
+ builder.append(displayValue);
+ builder.appendLiteral(")");
+
+ return builder.toString();
+}
+
+static String invalidParameterInSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType type, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
+{
+ ASSERT_UNUSED(type, type != TypeObject);
+
+ if (occurrence == ErrorInstance::FoundApproximateSource)
+ return defaultApproximateSourceError(originalMessage, sourceText);
+
+ ASSERT(occurrence == ErrorInstance::FoundExactSource);
+ auto inIndex = sourceText.reverseFind("in");
+ RELEASE_ASSERT(inIndex != notFound);
+ if (sourceText.find("in") != inIndex)
+ return makeString(originalMessage, " (evaluating '", sourceText, "')");
+
+ static const unsigned inLength = 2;
+ String rightHandSide = sourceText.substring(inIndex + inLength).simplifyWhiteSpace();
+ return makeString(rightHandSide, " is not an Object. (evaluating '", sourceText, "')");
+}
+
+static String invalidParameterInstanceofSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
+{
+ if (occurrence == ErrorInstance::FoundApproximateSource)
+ return defaultApproximateSourceError(originalMessage, sourceText);
+
+ ASSERT(occurrence == ErrorInstance::FoundExactSource);
+ auto instanceofIndex = sourceText.reverseFind("instanceof");
+ RELEASE_ASSERT(instanceofIndex != notFound);
+ if (sourceText.find("instanceof") != instanceofIndex)
+ return makeString(originalMessage, " (evaluating '", sourceText, "')");
+
+ static const unsigned instanceofLength = 10;
+ String rightHandSide = sourceText.substring(instanceofIndex + instanceofLength).simplifyWhiteSpace();
+ return makeString(rightHandSide, " is not a function. (evaluating '", sourceText, "')");
+}
+
+JSObject* createError(ExecState* exec, JSValue value, const String& message, ErrorInstance::SourceAppender appender)
+{
+ String errorMessage = makeString(errorDescriptionForValue(exec, value)->value(exec), ' ', message);
+ JSObject* exception = createTypeError(exec, errorMessage, appender, runtimeTypeForValue(value));
+ ASSERT(exception->isErrorInstance());