| 1 | /* |
| 2 | * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
| 4 | * Copyright (C) 2003, 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
| 5 | * Copyright (C) 2007 Eric Seidel (eric@webkit.org) |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Library General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Library General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Library General Public License |
| 18 | * along with this library; see the file COPYING.LIB. If not, write to |
| 19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 | * Boston, MA 02110-1301, USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include "config.h" |
| 25 | #include "Error.h" |
| 26 | |
| 27 | #include "ConstructData.h" |
| 28 | #include "ErrorConstructor.h" |
| 29 | #include "JSFunction.h" |
| 30 | #include "JSGlobalObject.h" |
| 31 | #include "JSObject.h" |
| 32 | #include "JSString.h" |
| 33 | #include "NativeErrorConstructor.h" |
| 34 | |
| 35 | namespace JSC { |
| 36 | |
| 37 | const char* expressionBeginOffsetPropertyName = "expressionBeginOffset" ; |
| 38 | const char* expressionCaretOffsetPropertyName = "expressionCaretOffset" ; |
| 39 | const char* expressionEndOffsetPropertyName = "expressionEndOffset" ; |
| 40 | |
| 41 | JSObject* Error::create(ExecState* exec, ErrorType type, const UString& message, int lineNumber, intptr_t sourceID, const UString& sourceURL) |
| 42 | { |
| 43 | JSObject* constructor; |
| 44 | const char* name; |
| 45 | switch (type) { |
| 46 | case EvalError: |
| 47 | constructor = exec->lexicalGlobalObject()->evalErrorConstructor(); |
| 48 | name = "Evaluation error" ; |
| 49 | break; |
| 50 | case RangeError: |
| 51 | constructor = exec->lexicalGlobalObject()->rangeErrorConstructor(); |
| 52 | name = "Range error" ; |
| 53 | break; |
| 54 | case ReferenceError: |
| 55 | constructor = exec->lexicalGlobalObject()->referenceErrorConstructor(); |
| 56 | name = "Reference error" ; |
| 57 | break; |
| 58 | case SyntaxError: |
| 59 | constructor = exec->lexicalGlobalObject()->syntaxErrorConstructor(); |
| 60 | name = "Syntax error" ; |
| 61 | break; |
| 62 | case TypeError: |
| 63 | constructor = exec->lexicalGlobalObject()->typeErrorConstructor(); |
| 64 | name = "Type error" ; |
| 65 | break; |
| 66 | case URIError: |
| 67 | constructor = exec->lexicalGlobalObject()->URIErrorConstructor(); |
| 68 | name = "URI error" ; |
| 69 | break; |
| 70 | default: |
| 71 | constructor = exec->lexicalGlobalObject()->errorConstructor(); |
| 72 | name = "Error" ; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | MarkedArgumentBuffer args; |
| 77 | if (message.isEmpty()) |
| 78 | args.append(v: jsString(exec, s: name)); |
| 79 | else |
| 80 | args.append(v: jsString(exec, s: message)); |
| 81 | ConstructData constructData; |
| 82 | ConstructType constructType = constructor->getConstructData(constructData); |
| 83 | JSObject* error = construct(exec, constructor, constructType, constructData, args); |
| 84 | |
| 85 | if (lineNumber != -1) |
| 86 | error->putWithAttributes(exec, propertyName: Identifier(exec, JSC_ERROR_LINENUMBER_PROPERTYNAME), value: jsNumber(exec, i: lineNumber), attributes: ReadOnly | DontDelete); |
| 87 | if (sourceID != -1) |
| 88 | error->putWithAttributes(exec, propertyName: Identifier(exec, "sourceId" ), value: jsNumber(exec, i: sourceID), attributes: ReadOnly | DontDelete); |
| 89 | if (!sourceURL.isNull()) |
| 90 | error->putWithAttributes(exec, propertyName: Identifier(exec, JSC_ERROR_FILENAME_PROPERTYNAME), value: jsString(exec, s: sourceURL), attributes: ReadOnly | DontDelete); |
| 91 | |
| 92 | return error; |
| 93 | } |
| 94 | |
| 95 | JSObject* Error::create(ExecState* exec, ErrorType type, const char* message) |
| 96 | { |
| 97 | return create(exec, type, message, lineNumber: -1, sourceID: -1, NULL); |
| 98 | } |
| 99 | |
| 100 | JSObject* throwError(ExecState* exec, JSObject* error) |
| 101 | { |
| 102 | exec->setException(error); |
| 103 | return error; |
| 104 | } |
| 105 | |
| 106 | JSObject* throwError(ExecState* exec, ErrorType type) |
| 107 | { |
| 108 | JSObject* error = Error::create(exec, type, message: UString(), lineNumber: -1, sourceID: -1, NULL); |
| 109 | exec->setException(error); |
| 110 | return error; |
| 111 | } |
| 112 | |
| 113 | JSObject* throwError(ExecState* exec, ErrorType type, const UString& message) |
| 114 | { |
| 115 | JSObject* error = Error::create(exec, type, message, lineNumber: -1, sourceID: -1, NULL); |
| 116 | exec->setException(error); |
| 117 | return error; |
| 118 | } |
| 119 | |
| 120 | JSObject* throwError(ExecState* exec, ErrorType type, const char* message) |
| 121 | { |
| 122 | JSObject* error = Error::create(exec, type, message, lineNumber: -1, sourceID: -1, NULL); |
| 123 | exec->setException(error); |
| 124 | return error; |
| 125 | } |
| 126 | |
| 127 | JSObject* throwError(ExecState* exec, ErrorType type, const UString& message, int line, intptr_t sourceID, const UString& sourceURL) |
| 128 | { |
| 129 | JSObject* error = Error::create(exec, type, message, lineNumber: line, sourceID, sourceURL); |
| 130 | exec->setException(error); |
| 131 | return error; |
| 132 | } |
| 133 | |
| 134 | } // namespace JSC |
| 135 | |