tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / Error.cpp
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 "FunctionPrototype.h"
30 #include "JSFunction.h"
31 #include "JSGlobalObject.h"
32 #include "JSObject.h"
33 #include "JSString.h"
34 #include "NativeErrorConstructor.h"
35 #include "SourceCode.h"
36
37 namespace JSC {
38
39 static const char* linePropertyName = "line";
40 static const char* sourceIdPropertyName = "sourceId";
41 static const char* sourceURLPropertyName = "sourceURL";
42
43 JSObject* createError(JSGlobalObject* globalObject, const UString& message)
44 {
45     ASSERT(!message.isEmpty());
46     return ErrorInstance::create(globalObject->globalData(), globalObject->errorStructure(), message);
47 }
48
49 JSObject* createEvalError(JSGlobalObject* globalObject, const UString& message)
50 {
51     ASSERT(!message.isEmpty());
52     return ErrorInstance::create(globalObject->globalData(), globalObject->evalErrorConstructor()->errorStructure(), message);
53 }
54
55 JSObject* createRangeError(JSGlobalObject* globalObject, const UString& message)
56 {
57     ASSERT(!message.isEmpty());
58     return ErrorInstance::create(globalObject->globalData(), globalObject->rangeErrorConstructor()->errorStructure(), message);
59 }
60
61 JSObject* createReferenceError(JSGlobalObject* globalObject, const UString& message)
62 {
63     ASSERT(!message.isEmpty());
64     return ErrorInstance::create(globalObject->globalData(), globalObject->referenceErrorConstructor()->errorStructure(), message);
65 }
66
67 JSObject* createSyntaxError(JSGlobalObject* globalObject, const UString& message)
68 {
69     ASSERT(!message.isEmpty());
70     return ErrorInstance::create(globalObject->globalData(), globalObject->syntaxErrorConstructor()->errorStructure(), message);
71 }
72
73 JSObject* createTypeError(JSGlobalObject* globalObject, const UString& message)
74 {
75     ASSERT(!message.isEmpty());
76     return ErrorInstance::create(globalObject->globalData(), globalObject->typeErrorConstructor()->errorStructure(), message);
77 }
78
79 JSObject* createURIError(JSGlobalObject* globalObject, const UString& message)
80 {
81     ASSERT(!message.isEmpty());
82     return ErrorInstance::create(globalObject->globalData(), globalObject->URIErrorConstructor()->errorStructure(), message);
83 }
84
85 JSObject* createError(ExecState* exec, const UString& message)
86 {
87     return createError(exec->lexicalGlobalObject(), message);
88 }
89
90 JSObject* createEvalError(ExecState* exec, const UString& message)
91 {
92     return createEvalError(exec->lexicalGlobalObject(), message);
93 }
94
95 JSObject* createRangeError(ExecState* exec, const UString& message)
96 {
97     return createRangeError(exec->lexicalGlobalObject(), message);
98 }
99
100 JSObject* createReferenceError(ExecState* exec, const UString& message)
101 {
102     return createReferenceError(exec->lexicalGlobalObject(), message);
103 }
104
105 JSObject* createSyntaxError(ExecState* exec, const UString& message)
106 {
107     return createSyntaxError(exec->lexicalGlobalObject(), message);
108 }
109
110 JSObject* createTypeError(ExecState* exec, const UString& message)
111 {
112     return createTypeError(exec->lexicalGlobalObject(), message);
113 }
114
115 JSObject* createURIError(ExecState* exec, const UString& message)
116 {
117     return createURIError(exec->lexicalGlobalObject(), message);
118 }
119
120 JSObject* addErrorInfo(JSGlobalData* globalData, JSObject* error, int line, const SourceCode& source)
121 {
122     intptr_t sourceID = source.provider()->asID();
123     const UString& sourceURL = source.provider()->url();
124
125     if (line != -1)
126         error->putWithAttributes(globalData, Identifier(globalData, linePropertyName), jsNumber(line), ReadOnly | DontDelete);
127     if (sourceID != -1)
128         error->putWithAttributes(globalData, Identifier(globalData, sourceIdPropertyName), jsNumber((double)sourceID), ReadOnly | DontDelete);
129     if (!sourceURL.isNull())
130         error->putWithAttributes(globalData, Identifier(globalData, sourceURLPropertyName), jsString(globalData, sourceURL), ReadOnly | DontDelete);
131
132     return error;
133 }
134
135 JSObject* addErrorInfo(ExecState* exec, JSObject* error, int line, const SourceCode& source)
136 {
137     return addErrorInfo(&exec->globalData(), error, line, source);
138 }
139
140 bool hasErrorInfo(ExecState* exec, JSObject* error)
141 {
142     return error->hasProperty(exec, Identifier(exec, linePropertyName))
143         || error->hasProperty(exec, Identifier(exec, sourceIdPropertyName))
144         || error->hasProperty(exec, Identifier(exec, sourceURLPropertyName));
145 }
146
147 JSValue throwError(ExecState* exec, JSValue error)
148 {
149     exec->globalData().exception = error;
150     return error;
151 }
152
153 JSObject* throwError(ExecState* exec, JSObject* error)
154 {
155     exec->globalData().exception = error;
156     return error;
157 }
158
159 JSObject* throwTypeError(ExecState* exec)
160 {
161     return throwError(exec, createTypeError(exec, "Type error"));
162 }
163
164 JSObject* throwSyntaxError(ExecState* exec)
165 {
166     return throwError(exec, createSyntaxError(exec, "Syntax error"));
167 }
168
169 ASSERT_CLASS_FITS_IN_CELL(StrictModeTypeErrorFunction);
170
171 const ClassInfo StrictModeTypeErrorFunction::s_info = { "Function", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction) };
172
173 JSValue createTypeErrorFunction(ExecState* exec, const UString& message)
174 {
175     JSGlobalObject* globalObject = exec->lexicalGlobalObject();
176     return StrictModeTypeErrorFunction::create(exec, globalObject, globalObject->strictModeTypeErrorFunctionStructure(), message);
177 }
178
179 } // namespace JSC