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 "ExceptionHelpers.h"
30 #include "FunctionPrototype.h"
32 #include "JSFunction.h"
33 #include "JSGlobalObject.h"
36 #include "NativeErrorConstructor.h"
37 #include "SourceCode.h"
39 #include <wtf/text/StringBuilder.h>
43 static const char* linePropertyName
= "line";
44 static const char* sourceURLPropertyName
= "sourceURL";
46 JSObject
* createError(JSGlobalObject
* globalObject
, const UString
& message
)
48 ASSERT(!message
.isEmpty());
49 return ErrorInstance::create(globalObject
->globalData(), globalObject
->errorStructure(), message
);
52 JSObject
* createEvalError(JSGlobalObject
* globalObject
, const UString
& message
)
54 ASSERT(!message
.isEmpty());
55 return ErrorInstance::create(globalObject
->globalData(), globalObject
->evalErrorConstructor()->errorStructure(), message
);
58 JSObject
* createRangeError(JSGlobalObject
* globalObject
, const UString
& message
)
60 ASSERT(!message
.isEmpty());
61 return ErrorInstance::create(globalObject
->globalData(), globalObject
->rangeErrorConstructor()->errorStructure(), message
);
64 JSObject
* createReferenceError(JSGlobalObject
* globalObject
, const UString
& message
)
66 ASSERT(!message
.isEmpty());
67 return ErrorInstance::create(globalObject
->globalData(), globalObject
->referenceErrorConstructor()->errorStructure(), message
);
70 JSObject
* createSyntaxError(JSGlobalObject
* globalObject
, const UString
& message
)
72 ASSERT(!message
.isEmpty());
73 return ErrorInstance::create(globalObject
->globalData(), globalObject
->syntaxErrorConstructor()->errorStructure(), message
);
76 JSObject
* createTypeError(JSGlobalObject
* globalObject
, const UString
& message
)
78 ASSERT(!message
.isEmpty());
79 return ErrorInstance::create(globalObject
->globalData(), globalObject
->typeErrorConstructor()->errorStructure(), message
);
82 JSObject
* createNotEnoughArgumentsError(JSGlobalObject
* globalObject
)
84 return createTypeError(globalObject
, "Not enough arguments");
87 JSObject
* createURIError(JSGlobalObject
* globalObject
, const UString
& message
)
89 ASSERT(!message
.isEmpty());
90 return ErrorInstance::create(globalObject
->globalData(), globalObject
->URIErrorConstructor()->errorStructure(), message
);
93 JSObject
* createError(ExecState
* exec
, const UString
& message
)
95 return createError(exec
->lexicalGlobalObject(), message
);
98 JSObject
* createEvalError(ExecState
* exec
, const UString
& message
)
100 return createEvalError(exec
->lexicalGlobalObject(), message
);
103 JSObject
* createRangeError(ExecState
* exec
, const UString
& message
)
105 return createRangeError(exec
->lexicalGlobalObject(), message
);
108 JSObject
* createReferenceError(ExecState
* exec
, const UString
& message
)
110 return createReferenceError(exec
->lexicalGlobalObject(), message
);
113 JSObject
* createSyntaxError(ExecState
* exec
, const UString
& message
)
115 return createSyntaxError(exec
->lexicalGlobalObject(), message
);
118 JSObject
* createTypeError(ExecState
* exec
, const UString
& message
)
120 return createTypeError(exec
->lexicalGlobalObject(), message
);
123 JSObject
* createNotEnoughArgumentsError(ExecState
* exec
)
125 return createNotEnoughArgumentsError(exec
->lexicalGlobalObject());
128 JSObject
* createURIError(ExecState
* exec
, const UString
& message
)
130 return createURIError(exec
->lexicalGlobalObject(), message
);
133 JSObject
* addErrorInfo(CallFrame
* callFrame
, JSObject
* error
, int line
, const SourceCode
& source
)
135 JSGlobalData
* globalData
= &callFrame
->globalData();
136 const UString
& sourceURL
= source
.provider()->url();
139 error
->putDirect(*globalData
, Identifier(globalData
, linePropertyName
), jsNumber(line
), ReadOnly
| DontDelete
);
140 if (!sourceURL
.isNull())
141 error
->putDirect(*globalData
, Identifier(globalData
, sourceURLPropertyName
), jsString(globalData
, sourceURL
), ReadOnly
| DontDelete
);
143 globalData
->interpreter
->addStackTraceIfNecessary(callFrame
, error
);
149 bool hasErrorInfo(ExecState
* exec
, JSObject
* error
)
151 return error
->hasProperty(exec
, Identifier(exec
, linePropertyName
))
152 || error
->hasProperty(exec
, Identifier(exec
, sourceURLPropertyName
));
155 JSValue
throwError(ExecState
* exec
, JSValue error
)
157 if (error
.isObject())
158 return throwError(exec
, asObject(error
));
159 exec
->globalData().exception
= error
;
163 JSObject
* throwError(ExecState
* exec
, JSObject
* error
)
165 Interpreter::addStackTraceIfNecessary(exec
, error
);
166 exec
->globalData().exception
= error
;
170 JSObject
* throwTypeError(ExecState
* exec
)
172 return throwError(exec
, createTypeError(exec
, "Type error"));
175 JSObject
* throwSyntaxError(ExecState
* exec
)
177 return throwError(exec
, createSyntaxError(exec
, "Syntax error"));
180 ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction
);
182 const ClassInfo
StrictModeTypeErrorFunction::s_info
= { "Function", &Base::s_info
, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction
) };
184 void StrictModeTypeErrorFunction::destroy(JSCell
* cell
)
186 jsCast
<StrictModeTypeErrorFunction
*>(cell
)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();