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 "ExceptionHelpers.h"
30#include "FunctionPrototype.h"
31#include "JSArray.h"
32#include "JSFunction.h"
33#include "JSGlobalObject.h"
34#include "JSObject.h"
35#include "JSString.h"
36#include "NativeErrorConstructor.h"
37#include "JSCInlines.h"
38#include "SourceCode.h"
39
40#include <wtf/text/StringBuilder.h>
41
42namespace JSC {
43
44static const char* linePropertyName = "line";
45static const char* sourceURLPropertyName = "sourceURL";
46
47JSObject* createError(JSGlobalObject* globalObject, const String& message)
48{
49    ASSERT(!message.isEmpty());
50    return ErrorInstance::create(globalObject->vm(), globalObject->errorStructure(), message);
51}
52
53JSObject* createEvalError(JSGlobalObject* globalObject, const String& message)
54{
55    ASSERT(!message.isEmpty());
56    return ErrorInstance::create(globalObject->vm(), globalObject->evalErrorConstructor()->errorStructure(), message);
57}
58
59JSObject* createRangeError(JSGlobalObject* globalObject, const String& message)
60{
61    ASSERT(!message.isEmpty());
62    return ErrorInstance::create(globalObject->vm(), globalObject->rangeErrorConstructor()->errorStructure(), message);
63}
64
65JSObject* createReferenceError(JSGlobalObject* globalObject, const String& message)
66{
67    ASSERT(!message.isEmpty());
68    return ErrorInstance::create(globalObject->vm(), globalObject->referenceErrorConstructor()->errorStructure(), message);
69}
70
71JSObject* createSyntaxError(JSGlobalObject* globalObject, const String& message)
72{
73    ASSERT(!message.isEmpty());
74    return ErrorInstance::create(globalObject->vm(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
75}
76
77JSObject* createTypeError(JSGlobalObject* globalObject, const String& message)
78{
79    ASSERT(!message.isEmpty());
80    return ErrorInstance::create(globalObject->vm(), globalObject->typeErrorConstructor()->errorStructure(), message);
81}
82
83JSObject* createNotEnoughArgumentsError(JSGlobalObject* globalObject)
84{
85    return createTypeError(globalObject, ASCIILiteral("Not enough arguments"));
86}
87
88JSObject* createURIError(JSGlobalObject* globalObject, const String& message)
89{
90    ASSERT(!message.isEmpty());
91    return ErrorInstance::create(globalObject->vm(), globalObject->URIErrorConstructor()->errorStructure(), message);
92}
93
94JSObject* createError(ExecState* exec, const String& message)
95{
96    return createError(exec->lexicalGlobalObject(), message);
97}
98
99JSObject* createEvalError(ExecState* exec, const String& message)
100{
101    return createEvalError(exec->lexicalGlobalObject(), message);
102}
103
104JSObject* createRangeError(ExecState* exec, const String& message)
105{
106    return createRangeError(exec->lexicalGlobalObject(), message);
107}
108
109JSObject* createReferenceError(ExecState* exec, const String& message)
110{
111    return createReferenceError(exec->lexicalGlobalObject(), message);
112}
113
114JSObject* createSyntaxError(ExecState* exec, const String& message)
115{
116    return createSyntaxError(exec->lexicalGlobalObject(), message);
117}
118
119JSObject* createTypeError(ExecState* exec, const String& message)
120{
121    return createTypeError(exec->lexicalGlobalObject(), message);
122}
123
124JSObject* createNotEnoughArgumentsError(ExecState* exec)
125{
126    return createNotEnoughArgumentsError(exec->lexicalGlobalObject());
127}
128
129JSObject* createURIError(ExecState* exec, const String& message)
130{
131    return createURIError(exec->lexicalGlobalObject(), message);
132}
133
134JSObject* addErrorInfo(CallFrame* callFrame, JSObject* error, int line, const SourceCode& source)
135{
136    VM* vm = &callFrame->vm();
137    const String& sourceURL = source.provider()->url();
138
139    if (line != -1)
140        error->putDirect(*vm, Identifier(vm, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
141    if (!sourceURL.isNull())
142        error->putDirect(*vm, Identifier(vm, sourceURLPropertyName), jsString(vm, sourceURL), ReadOnly | DontDelete);
143    return error;
144}
145
146
147bool hasErrorInfo(ExecState* exec, JSObject* error)
148{
149    return error->hasProperty(exec, Identifier(exec, linePropertyName))
150        || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
151}
152
153JSObject* throwTypeError(ExecState* exec)
154{
155    return exec->vm().throwException(exec, createTypeError(exec, ASCIILiteral("Type error")));
156}
157
158JSObject* throwSyntaxError(ExecState* exec)
159{
160    return exec->vm().throwException(exec, createSyntaxError(exec, ASCIILiteral("Syntax error")));
161}
162
163const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) };
164
165void StrictModeTypeErrorFunction::destroy(JSCell* cell)
166{
167    static_cast<StrictModeTypeErrorFunction*>(cell)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();
168}
169
170} // namespace JSC
171