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)
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.
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.
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.
27 #include "ConstructData.h"
28 #include "ErrorConstructor.h"
29 #include "JSFunction.h"
30 #include "JSGlobalObject.h"
33 #include "NativeErrorConstructor.h"
34 #include "SourceCode.h"
38 static const char* linePropertyName = "line";
39 static const char* sourceIdPropertyName = "sourceId";
40 static const char* sourceURLPropertyName = "sourceURL";
42 JSObject* createError(JSGlobalObject* globalObject, const UString& message)
44 ASSERT(!message.isEmpty());
45 return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message);
48 JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
50 ASSERT(!message.isEmpty());
51 return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
54 JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
56 ASSERT(!message.isEmpty());
57 return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
60 JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
62 ASSERT(!message.isEmpty());
63 return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
66 JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
68 ASSERT(!message.isEmpty());
69 return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
72 JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
74 ASSERT(!message.isEmpty());
75 return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
78 JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
80 ASSERT(!message.isEmpty());
81 return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
84 JSObject* createError(ExecState* exec, const UString& message)
86 return createError(exec->lexicalGlobalObject(), message);
89 JSObject* createEvalError(ExecState* exec, const UString& message)
91 return createEvalError(exec->lexicalGlobalObject(), message);
94 JSObject* createRangeError(ExecState* exec, const UString& message)
96 return createRangeError(exec->lexicalGlobalObject(), message);
99 JSObject* createReferenceError(ExecState* exec, const UString& message)
101 return createReferenceError(exec->lexicalGlobalObject(), message);
104 JSObject* createSyntaxError(ExecState* exec, const UString& message)
106 return createSyntaxError(exec->lexicalGlobalObject(), message);
109 JSObject* createTypeError(ExecState* exec, const UString& message)
111 return createTypeError(exec->lexicalGlobalObject(), message);
114 JSObject* createURIError(ExecState* exec, const UString& message)
116 return createURIError(exec->lexicalGlobalObject(), message);
119 JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
121 intptr_t sourceID = source.provider()->asID();
122 const UString& sourceURL = source.provider()->url();
125 error->putWithAttributes(globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
127 error->putWithAttributes(globalData, Identifier(globalData, sourceIdPropertyName), jsNumber((double)sourceID), ReadOnly | DontDelete);
128 if (!sourceURL.isNull())
129 error->putWithAttributes(globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
134 JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source)
136 return addErrorInfo(&exec->globalData(), error, line, source);
139 bool hasErrorInfo(ExecState* exec, JSObject* error)
141 return error->hasProperty(exec, Identifier(exec, linePropertyName))
142 || error->hasProperty(exec, Identifier(exec, sourceIdPropertyName))
143 || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
146 JSValue throwError(ExecState* exec, JSValue error)
148 exec->globalData().exception = error;
152 JSObject* throwError(ExecState* exec, JSObject* error)
154 exec->globalData().exception = error;
158 JSObject* throwTypeError(ExecState* exec)
160 return throwError(exec, createTypeError(exec, "Type error"));
163 JSObject* throwSyntaxError(ExecState* exec)
165 return throwError(exec, createSyntaxError(exec, "Syntax error"));
168 class StrictModeTypeErrorFunction : public InternalFunction {
170 StrictModeTypeErrorFunction(JSGlobalObject* globalObject, Structure* structure, const UString& message)
171 : InternalFunction(globalObject, structure)
177 typedef InternalFunction Base;
179 static StrictModeTypeErrorFunction* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, const UString& message)
181 StrictModeTypeErrorFunction* function = new (allocateCell<StrictModeTypeErrorFunction>(*exec->heap())) StrictModeTypeErrorFunction(globalObject, structure, message);
182 function->finishCreation(exec->globalData(), exec->globalData().propertyNames->emptyIdentifier);
186 static EncodedJSValue JSC_HOST_CALL constructThrowTypeError(ExecState* exec)
188 throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
189 return JSValue::encode(jsNull());
192 ConstructType getConstructData(ConstructData& constructData)
194 constructData.native.function = constructThrowTypeError;
195 return ConstructTypeHost;
198 static EncodedJSValue JSC_HOST_CALL callThrowTypeError(ExecState* exec)
200 throwTypeError(exec, static_cast<StrictModeTypeErrorFunction*>(exec->callee())->m_message);
201 return JSValue::encode(jsNull());
204 CallType getCallData(CallData& callData)
206 callData.native.function = callThrowTypeError;
214 ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction);
216 JSValue createTypeErrorFunction(ExecState* exec, const UString& message)
218 return StrictModeTypeErrorFunction::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->internalFunctionStructure(), message);