* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
#include "CodeBlock.h"
#include "CallFrame.h"
+#include "ErrorHandlingScope.h"
+#include "Exception.h"
#include "JSGlobalObjectFunctions.h"
-#include "JSObject.h"
#include "JSNotAnObject.h"
#include "Interpreter.h"
#include "Nodes.h"
+#include "JSCInlines.h"
+#include "RuntimeType.h"
+#include <wtf/text/StringBuilder.h>
+#include <wtf/text/StringView.h>
namespace JSC {
-class InterruptedExecutionError : public JSObject {
-public:
- InterruptedExecutionError(JSGlobalData* globalData)
- : JSObject(globalData->interruptedExecutionErrorStructure)
- {
- }
-
- virtual bool isWatchdogException() const { return true; }
+STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(TerminatedExecutionError);
- virtual UString toString(ExecState*) const { return "JavaScript execution exceeded timeout."; }
-};
+const ClassInfo TerminatedExecutionError::s_info = { "TerminatedExecutionError", &Base::s_info, 0, CREATE_METHOD_TABLE(TerminatedExecutionError) };
-JSValue createInterruptedExecutionException(JSGlobalData* globalData)
+JSValue TerminatedExecutionError::defaultValue(const JSObject*, ExecState* exec, PreferredPrimitiveType hint)
{
- return new (globalData) InterruptedExecutionError(globalData);
+ if (hint == PreferString)
+ return jsNontrivialString(exec, String(ASCIILiteral("JavaScript execution terminated.")));
+ return JSValue(PNaN);
}
-static JSValue createError(ExecState* exec, ErrorType e, const char* msg)
+JSObject* createTerminatedExecutionException(VM* vm)
{
- return Error::create(exec, e, msg, -1, -1, 0);
+ return TerminatedExecutionError::create(*vm);
}
-JSValue createStackOverflowError(ExecState* exec)
+bool isTerminatedExecutionException(Exception* exception)
{
- return createError(exec, RangeError, "Maximum call stack size exceeded.");
+ return exception->value().inherits(TerminatedExecutionError::info());
}
-JSValue createTypeError(ExecState* exec, const char* message)
+JSObject* createStackOverflowError(ExecState* exec)
{
- return createError(exec, TypeError, message);
+ return createRangeError(exec, ASCIILiteral("Maximum call stack size exceeded."));
}
-JSValue createUndefinedVariableError(ExecState* exec, const Identifier& ident, unsigned bytecodeOffset, CodeBlock* codeBlock)
+JSObject* createUndefinedVariableError(ExecState* exec, const Identifier& ident)
{
- int startOffset = 0;
- int endOffset = 0;
- int divotPoint = 0;
- int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
- JSObject* exception = Error::create(exec, ReferenceError, makeString("Can't find variable: ", ident.ustring()), 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);
- return exception;
+ if (exec->propertyNames().isPrivateName(ident)) {
+ String message(makeString("Can't find private variable: @", exec->propertyNames().getPublicName(ident).string()));
+ return createReferenceError(exec, message);
+ }
+ String message(makeString("Can't find variable: ", ident.string()));
+ return createReferenceError(exec, message);
}
-static UString createErrorMessage(ExecState* exec, CodeBlock* codeBlock, int, int expressionStart, int expressionStop, JSValue value, UString error)
-{
- if (!expressionStop || expressionStart > codeBlock->source()->length())
- return makeString(value.toString(exec), " is ", error);
- if (expressionStart < expressionStop)
- return makeString("Result of expression '", codeBlock->source()->getRange(expressionStart, expressionStop), "' [", value.toString(exec), "] is ", error, ".");
-
- // No range information, so give a few characters of context
- const UChar* data = codeBlock->source()->data();
- int dataLength = codeBlock->source()->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) && data[start - 1] != '\n')
- start--;
- while (start < (expressionStart - 1) && isStrWhiteSpace(data[start]))
- start++;
- while (stop < dataLength && (stop - expressionStart < 20) && data[stop] != '\n')
- stop++;
- while (stop > expressionStart && isStrWhiteSpace(data[stop]))
- stop--;
- return makeString("Result of expression near '...", codeBlock->source()->getRange(start, stop), "...' [", value.toString(exec), "] is ", error, ".");
-}
-
-JSObject* createInvalidParamError(ExecState* exec, const char* op, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
-{
- int startOffset = 0;
- int endOffset = 0;
- int divotPoint = 0;
- int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
- UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint, divotPoint + endOffset, value, makeString("not a valid argument for '", op, "'"));
- 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);
- return exception;
+JSString* errorDescriptionForValue(ExecState* exec, JSValue v)
+{
+ if (v.isString())
+ return jsNontrivialString(exec, makeString('"', asString(v)->value(exec), '"'));
+ if (v.isObject()) {
+ CallData callData;
+ JSObject* object = asObject(v);
+ if (object->methodTable()->getCallData(object, callData) != CallTypeNone)
+ return exec->vm().smallStrings.functionString();
+ return jsString(exec, JSObject::calculatedClassName(object));
+ }
+ return v.toString(exec);
+}
+
+static String defaultApproximateSourceError(const String& originalMessage, const String& sourceText)
+{
+ return makeString(originalMessage, " (near '...", sourceText, "...')");
}
-JSObject* createNotAConstructorError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
+static String defaultSourceAppender(const String& originalMessage, const String& sourceText, RuntimeType, ErrorInstance::SourceTextWhereErrorOccurred occurrence)
{
- int startOffset = 0;
- int endOffset = 0;
- int divotPoint = 0;
- int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
+ if (occurrence == ErrorInstance::FoundApproximateSource)
+ return defaultApproximateSourceError(originalMessage, sourceText);
- // 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());
return exception;
}
-JSValue createNotAFunctionError(ExecState* exec, JSValue value, unsigned bytecodeOffset, CodeBlock* codeBlock)
+JSObject* createInvalidFunctionApplyParameterError(ExecState* exec, JSValue value)
{
- int startOffset = 0;
- int endOffset = 0;
- int divotPoint = 0;
- int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
- UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, value, "not a function");
- 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);
+ JSObject* exception = createTypeError(exec, makeString("second argument to Function.prototype.apply must be an Array-like object"), defaultSourceAppender, runtimeTypeForValue(value));
+ ASSERT(exception->isErrorInstance());
return exception;
}
-JSNotAnObjectErrorStub* createNotAnObjectErrorStub(ExecState* exec, bool isNull)
+JSObject* createInvalidInParameterError(ExecState* exec, JSValue value)
{
- return new (exec) JSNotAnObjectErrorStub(exec, isNull);
+ return createError(exec, value, makeString("is not an Object."), invalidParameterInSourceAppender);
}
-JSObject* createNotAnObjectError(ExecState* exec, JSNotAnObjectErrorStub* error, unsigned bytecodeOffset, CodeBlock* codeBlock)
+JSObject* createInvalidInstanceofParameterError(ExecState* exec, JSValue value)
{
- // Both op_construct and op_instanceof require a use of op_get_by_id to get
- // the prototype property from an object. The exception messages for exceptions
- // thrown by these instances op_get_by_id need to reflect this.
- OpcodeID followingOpcodeID;
- if (codeBlock->getByIdExceptionInfoForBytecodeOffset(exec, bytecodeOffset, followingOpcodeID)) {
- ASSERT(followingOpcodeID == op_construct || followingOpcodeID == op_instanceof);
- if (followingOpcodeID == op_construct)
- return createNotAConstructorError(exec, error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
- return createInvalidParamError(exec, "instanceof", error->isNull() ? jsNull() : jsUndefined(), bytecodeOffset, codeBlock);
- }
+ return createError(exec, value, makeString("is not a function."), invalidParameterInstanceofSourceAppender);
+}
- int startOffset = 0;
- int endOffset = 0;
- int divotPoint = 0;
- int line = codeBlock->expressionRangeForBytecodeOffset(exec, bytecodeOffset, divotPoint, startOffset, endOffset);
- UString errorMessage = createErrorMessage(exec, codeBlock, line, divotPoint - startOffset, divotPoint, error->isNull() ? jsNull() : jsUndefined(), "not an object");
- 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);
- return exception;
+JSObject* createNotAConstructorError(ExecState* exec, JSValue value)
+{
+ return createError(exec, value, ASCIILiteral("is not a constructor"), defaultSourceAppender);
+}
+
+JSObject* createNotAFunctionError(ExecState* exec, JSValue value)
+{
+ return createError(exec, value, ASCIILiteral("is not a function"), notAFunctionSourceAppender);
+}
+
+JSObject* createNotAnObjectError(ExecState* exec, JSValue value)
+{
+ return createError(exec, value, ASCIILiteral("is not an object"), defaultSourceAppender);
+}
+
+JSObject* createErrorForInvalidGlobalAssignment(ExecState* exec, const String& propertyName)
+{
+ return createReferenceError(exec, makeString("Strict mode forbids implicit creation of global property '", propertyName, '\''));
+}
+
+JSObject* throwOutOfMemoryError(ExecState* exec)
+{
+ return exec->vm().throwException(exec, createOutOfMemoryError(exec));
+}
+
+JSObject* throwStackOverflowError(ExecState* exec)
+{
+ VM& vm = exec->vm();
+ ErrorHandlingScope errorScope(vm);
+ return vm.throwException(exec, createStackOverflowError(exec));
+}
+
+JSObject* throwTerminatedExecutionException(ExecState* exec)
+{
+ VM& vm = exec->vm();
+ ErrorHandlingScope errorScope(vm);
+ return vm.throwException(exec, createTerminatedExecutionException(&vm));
}
} // namespace JSC