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 "JSCInlines.h"
38 #include "SourceCode.h"
40 #include <wtf/text/StringBuilder.h>
44 static const char* linePropertyName
= "line";
45 static const char* sourceURLPropertyName
= "sourceURL";
47 JSObject
* createError(JSGlobalObject
* globalObject
, const String
& message
)
49 ASSERT(!message
.isEmpty());
50 return ErrorInstance::create(globalObject
->vm(), globalObject
->errorStructure(), message
);
53 JSObject
* createEvalError(JSGlobalObject
* globalObject
, const String
& message
)
55 ASSERT(!message
.isEmpty());
56 return ErrorInstance::create(globalObject
->vm(), globalObject
->evalErrorConstructor()->errorStructure(), message
);
59 JSObject
* createRangeError(JSGlobalObject
* globalObject
, const String
& message
)
61 ASSERT(!message
.isEmpty());
62 return ErrorInstance::create(globalObject
->vm(), globalObject
->rangeErrorConstructor()->errorStructure(), message
);
65 JSObject
* createReferenceError(JSGlobalObject
* globalObject
, const String
& message
)
67 ASSERT(!message
.isEmpty());
68 return ErrorInstance::create(globalObject
->vm(), globalObject
->referenceErrorConstructor()->errorStructure(), message
);
71 JSObject
* createSyntaxError(JSGlobalObject
* globalObject
, const String
& message
)
73 ASSERT(!message
.isEmpty());
74 return ErrorInstance::create(globalObject
->vm(), globalObject
->syntaxErrorConstructor()->errorStructure(), message
);
77 JSObject
* createTypeError(JSGlobalObject
* globalObject
, const String
& message
)
79 ASSERT(!message
.isEmpty());
80 return ErrorInstance::create(globalObject
->vm(), globalObject
->typeErrorConstructor()->errorStructure(), message
);
83 JSObject
* createNotEnoughArgumentsError(JSGlobalObject
* globalObject
)
85 return createTypeError(globalObject
, ASCIILiteral("Not enough arguments"));
88 JSObject
* createURIError(JSGlobalObject
* globalObject
, const String
& message
)
90 ASSERT(!message
.isEmpty());
91 return ErrorInstance::create(globalObject
->vm(), globalObject
->URIErrorConstructor()->errorStructure(), message
);
94 JSObject
* createError(ExecState
* exec
, const String
& message
)
96 return createError(exec
->lexicalGlobalObject(), message
);
99 JSObject
* createEvalError(ExecState
* exec
, const String
& message
)
101 return createEvalError(exec
->lexicalGlobalObject(), message
);
104 JSObject
* createRangeError(ExecState
* exec
, const String
& message
)
106 return createRangeError(exec
->lexicalGlobalObject(), message
);
109 JSObject
* createReferenceError(ExecState
* exec
, const String
& message
)
111 return createReferenceError(exec
->lexicalGlobalObject(), message
);
114 JSObject
* createSyntaxError(ExecState
* exec
, const String
& message
)
116 return createSyntaxError(exec
->lexicalGlobalObject(), message
);
119 JSObject
* createTypeError(ExecState
* exec
, const String
& message
)
121 return createTypeError(exec
->lexicalGlobalObject(), message
);
124 JSObject
* createNotEnoughArgumentsError(ExecState
* exec
)
126 return createNotEnoughArgumentsError(exec
->lexicalGlobalObject());
129 JSObject
* createURIError(ExecState
* exec
, const String
& message
)
131 return createURIError(exec
->lexicalGlobalObject(), message
);
134 JSObject
* addErrorInfo(CallFrame
* callFrame
, JSObject
* error
, int line
, const SourceCode
& source
)
136 VM
* vm
= &callFrame
->vm();
137 const String
& sourceURL
= source
.provider()->url();
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
);
147 bool hasErrorInfo(ExecState
* exec
, JSObject
* error
)
149 return error
->hasProperty(exec
, Identifier(exec
, linePropertyName
))
150 || error
->hasProperty(exec
, Identifier(exec
, sourceURLPropertyName
));
153 JSObject
* throwTypeError(ExecState
* exec
)
155 return exec
->vm().throwException(exec
, createTypeError(exec
, ASCIILiteral("Type error")));
158 JSObject
* throwSyntaxError(ExecState
* exec
)
160 return exec
->vm().throwException(exec
, createSyntaxError(exec
, ASCIILiteral("Syntax error")));
163 const ClassInfo
StrictModeTypeErrorFunction::s_info
= { "Function", &Base::s_info
, 0, 0, CREATE_METHOD_TABLE(StrictModeTypeErrorFunction
) };
165 void StrictModeTypeErrorFunction::destroy(JSCell
* cell
)
167 static_cast<StrictModeTypeErrorFunction
*>(cell
)->StrictModeTypeErrorFunction::~StrictModeTypeErrorFunction();