X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..a253471d7f8e4d91bf6ebabab00155c3b387d3d0:/runtime/Error.cpp diff --git a/runtime/Error.cpp b/runtime/Error.cpp index ddd4bc4..bae0744 100644 --- a/runtime/Error.cpp +++ b/runtime/Error.cpp @@ -26,109 +26,164 @@ #include "ConstructData.h" #include "ErrorConstructor.h" +#include "ExceptionHelpers.h" +#include "FunctionPrototype.h" +#include "JSArray.h" #include "JSFunction.h" #include "JSGlobalObject.h" #include "JSObject.h" #include "JSString.h" #include "NativeErrorConstructor.h" +#include "SourceCode.h" + +#include namespace JSC { -const char* expressionBeginOffsetPropertyName = "expressionBeginOffset"; -const char* expressionCaretOffsetPropertyName = "expressionCaretOffset"; -const char* expressionEndOffsetPropertyName = "expressionEndOffset"; - -JSObject* Error::create(ExecState* exec, ErrorType type, const UString& message, int lineNumber, intptr_t sourceID, const UString& sourceURL) -{ - JSObject* constructor; - const char* name; - switch (type) { - case EvalError: - constructor = exec->lexicalGlobalObject()->evalErrorConstructor(); - name = "Evaluation error"; - break; - case RangeError: - constructor = exec->lexicalGlobalObject()->rangeErrorConstructor(); - name = "Range error"; - break; - case ReferenceError: - constructor = exec->lexicalGlobalObject()->referenceErrorConstructor(); - name = "Reference error"; - break; - case SyntaxError: - constructor = exec->lexicalGlobalObject()->syntaxErrorConstructor(); - name = "Syntax error"; - break; - case TypeError: - constructor = exec->lexicalGlobalObject()->typeErrorConstructor(); - name = "Type error"; - break; - case URIError: - constructor = exec->lexicalGlobalObject()->URIErrorConstructor(); - name = "URI error"; - break; - default: - constructor = exec->lexicalGlobalObject()->errorConstructor(); - name = "Error"; - break; - } - - MarkedArgumentBuffer args; - if (message.isEmpty()) - args.append(jsString(exec, name)); - else - args.append(jsString(exec, message)); - ConstructData constructData; - ConstructType constructType = constructor->getConstructData(constructData); - JSObject* error = construct(exec, constructor, constructType, constructData, args); - - if (lineNumber != -1) - error->putWithAttributes(exec, Identifier(exec, "line"), jsNumber(exec, lineNumber), ReadOnly | DontDelete); - if (sourceID != -1) - error->putWithAttributes(exec, Identifier(exec, "sourceId"), jsNumber(exec, sourceID), ReadOnly | DontDelete); +static const char* linePropertyName = "line"; +static const char* sourceURLPropertyName = "sourceURL"; + +JSObject* createError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message); +} + +JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message); +} + +JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message); +} + +JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message); +} + +JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message); +} + +JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message); +} + +JSObject* createNotEnoughArgumentsError(JSGlobalObject* globalObject) +{ + return createTypeError(globalObject, "Not enough arguments"); +} + +JSObject* createURIError(JSGlobalObject* globalObject, const UString& message) +{ + ASSERT(!message.isEmpty()); + return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message); +} + +JSObject* createError(ExecState* exec, const UString& message) +{ + return createError(exec->lexicalGlobalObject(), message); +} + +JSObject* createEvalError(ExecState* exec, const UString& message) +{ + return createEvalError(exec->lexicalGlobalObject(), message); +} + +JSObject* createRangeError(ExecState* exec, const UString& message) +{ + return createRangeError(exec->lexicalGlobalObject(), message); +} + +JSObject* createReferenceError(ExecState* exec, const UString& message) +{ + return createReferenceError(exec->lexicalGlobalObject(), message); +} + +JSObject* createSyntaxError(ExecState* exec, const UString& message) +{ + return createSyntaxError(exec->lexicalGlobalObject(), message); +} + +JSObject* createTypeError(ExecState* exec, const UString& message) +{ + return createTypeError(exec->lexicalGlobalObject(), message); +} + +JSObject* createNotEnoughArgumentsError(ExecState* exec) +{ + return createNotEnoughArgumentsError(exec->lexicalGlobalObject()); +} + +JSObject* createURIError(ExecState* exec, const UString& message) +{ + return createURIError(exec->lexicalGlobalObject(), message); +} + +JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source) +{ + JSGlobalData* globalData = &callFrame->globalData(); + const UString& sourceURL = source.provider()->url(); + + if (line != -1) + error->putDirect(*globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete); if (!sourceURL.isNull()) - error->putWithAttributes(exec, Identifier(exec, "sourceURL"), jsString(exec, sourceURL), ReadOnly | DontDelete); + error->putDirect(*globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete); + + globalData->interpreter->addStackTraceIfNecessary(callFrame, error); return error; } -JSObject* Error::create(ExecState* exec, ErrorType type, const char* message) + +bool hasErrorInfo(ExecState* exec, JSObject* error) { - return create(exec, type, message, -1, -1, NULL); + return error->hasProperty(exec, Identifier(exec, linePropertyName)) + || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName)); } -JSObject* throwError(ExecState* exec, JSObject* error) +JSValue throwError(ExecState* exec, JSValue error) { - exec->setException(error); + if (error.isObject()) + return throwError(exec, asObject(error)); + exec->globalData().exception = error; return error; } -JSObject* throwError(ExecState* exec, ErrorType type) +JSObject* throwError(ExecState* exec, JSObject* error) { - JSObject* error = Error::create(exec, type, UString(), -1, -1, NULL); - exec->setException(error); + Interpreter::addStackTraceIfNecessary(exec, error); + exec->globalData().exception = error; return error; } -JSObject* throwError(ExecState* exec, ErrorType type, const UString& message) +JSObject* throwTypeError(ExecState* exec) { - JSObject* error = Error::create(exec, type, message, -1, -1, NULL); - exec->setException(error); - return error; + return throwError(exec, createTypeError(exec, "Type error")); } -JSObject* throwError(ExecState* exec, ErrorType type, const char* message) +JSObject* throwSyntaxError(ExecState* exec) { - JSObject* error = Error::create(exec, type, message, -1, -1, NULL); - exec->setException(error); - return error; + return throwError(exec, createSyntaxError(exec, "Syntax error")); } -JSObject* throwError(ExecState* exec, ErrorType type, const UString& message, int line, intptr_t sourceID, const UString& sourceURL) +ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction); + +const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) }; + +void StrictModeTypeErrorFunction::destroy(JSCell* cell) { - JSObject* error = Error::create(exec, type, message, line, sourceID, sourceURL); - exec->setException(error); - return error; + jsCast(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction(); } } // namespace JSC