1/*
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003-2019 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#pragma once
24
25#include "ErrorInstance.h"
26#include "ErrorType.h"
27#include "Exception.h"
28#include "InternalFunction.h"
29#include "JSObject.h"
30#include "ThrowScope.h"
31#include <stdint.h>
32
33
34namespace JSC {
35
36class ExecState;
37class VM;
38class JSGlobalObject;
39class JSObject;
40class SourceCode;
41class Structure;
42
43// ExecState wrappers.
44JSObject* createError(ExecState*, const String&, ErrorInstance::SourceAppender);
45JSObject* createEvalError(ExecState*, const String&, ErrorInstance::SourceAppender);
46JSObject* createRangeError(ExecState*, const String&, ErrorInstance::SourceAppender);
47JSObject* createRangeError(ExecState*, JSGlobalObject*, const String&, ErrorInstance::SourceAppender);
48JSObject* createReferenceError(ExecState*, const String&, ErrorInstance::SourceAppender);
49JSObject* createSyntaxError(ExecState*, const String&, ErrorInstance::SourceAppender);
50JSObject* createTypeError(ExecState*, const String&, ErrorInstance::SourceAppender, RuntimeType);
51JSObject* createNotEnoughArgumentsError(ExecState*, ErrorInstance::SourceAppender);
52JSObject* createURIError(ExecState*, const String&, ErrorInstance::SourceAppender);
53
54
55JS_EXPORT_PRIVATE JSObject* createError(ExecState*, const String&);
56JS_EXPORT_PRIVATE JSObject* createEvalError(ExecState*, const String&);
57JS_EXPORT_PRIVATE JSObject* createRangeError(ExecState*, const String&);
58JS_EXPORT_PRIVATE JSObject* createRangeError(ExecState*, JSGlobalObject*, const String&);
59JS_EXPORT_PRIVATE JSObject* createReferenceError(ExecState*, const String&);
60JS_EXPORT_PRIVATE JSObject* createSyntaxError(ExecState*, const String&);
61JS_EXPORT_PRIVATE JSObject* createTypeError(ExecState*);
62JS_EXPORT_PRIVATE JSObject* createTypeError(ExecState*, const String&);
63JS_EXPORT_PRIVATE JSObject* createNotEnoughArgumentsError(ExecState*);
64JS_EXPORT_PRIVATE JSObject* createURIError(ExecState*, const String&);
65JS_EXPORT_PRIVATE JSObject* createOutOfMemoryError(ExecState*);
66JS_EXPORT_PRIVATE JSObject* createOutOfMemoryError(ExecState*, const String&);
67
68JS_EXPORT_PRIVATE JSObject* createError(ExecState*, ErrorType, const String&);
69
70std::unique_ptr<Vector<StackFrame>> getStackTrace(ExecState*, VM&, JSObject*, bool useCurrentFrame);
71void getBytecodeOffset(ExecState*, VM&, Vector<StackFrame>*, CallFrame*&, unsigned& bytecodeOffset);
72bool getLineColumnAndSource(Vector<StackFrame>* stackTrace, unsigned& line, unsigned& column, String& sourceURL);
73bool addErrorInfo(VM&, Vector<StackFrame>*, JSObject*);
74JS_EXPORT_PRIVATE void addErrorInfo(ExecState*, JSObject*, bool);
75JSObject* addErrorInfo(ExecState*, JSObject* error, int line, const SourceCode&);
76
77// Methods to throw Errors.
78
79// Convenience wrappers, create an throw an exception with a default message.
80JS_EXPORT_PRIVATE Exception* throwConstructorCannotBeCalledAsFunctionTypeError(ExecState*, ThrowScope&, const char* constructorName);
81JS_EXPORT_PRIVATE Exception* throwTypeError(ExecState*, ThrowScope&);
82JS_EXPORT_PRIVATE Exception* throwTypeError(ExecState*, ThrowScope&, ASCIILiteral errorMessage);
83JS_EXPORT_PRIVATE Exception* throwTypeError(ExecState*, ThrowScope&, const String& errorMessage);
84JS_EXPORT_PRIVATE Exception* throwSyntaxError(ExecState*, ThrowScope&);
85JS_EXPORT_PRIVATE Exception* throwSyntaxError(ExecState*, ThrowScope&, const String& errorMessage);
86inline Exception* throwRangeError(ExecState* state, ThrowScope& scope, const String& errorMessage) { return throwException(state, scope, createRangeError(state, errorMessage)); }
87JS_EXPORT_PRIVATE JSValue throwDOMAttributeGetterTypeError(ExecState*, ThrowScope&, const ClassInfo*, PropertyName);
88
89// Convenience wrappers, wrap result as an EncodedJSValue.
90inline void throwVMError(ExecState* exec, ThrowScope& scope, Exception* exception) { throwException(exec, scope, exception); }
91inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope& scope, JSValue error) { return JSValue::encode(throwException(exec, scope, error)); }
92inline EncodedJSValue throwVMError(ExecState* exec, ThrowScope& scope, const char* errorMessage) { return JSValue::encode(throwException(exec, scope, createError(exec, errorMessage))); }
93inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope) { return JSValue::encode(throwTypeError(exec, scope)); }
94inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope, ASCIILiteral errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }
95inline EncodedJSValue throwVMTypeError(ExecState* exec, ThrowScope& scope, const String& errorMessage) { return JSValue::encode(throwTypeError(exec, scope, errorMessage)); }
96inline EncodedJSValue throwVMRangeError(ExecState* state, ThrowScope& scope, const String& errorMessage) { return JSValue::encode(throwRangeError(state, scope, errorMessage)); }
97inline EncodedJSValue throwVMDOMAttributeGetterTypeError(ExecState* state, ThrowScope& scope, const ClassInfo* classInfo, PropertyName propertyName) { return JSValue::encode(throwDOMAttributeGetterTypeError(state, scope, classInfo, propertyName)); }
98
99} // namespace JSC
100