1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2003-2017 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Samuel Weinig <[email protected]>
5 * Copyright (C) 2009 Google, Inc. All rights reserved.
6 * Copyright (C) 2012 Ericsson AB. All rights reserved.
7 * Copyright (C) 2013 Michael Pruett <[email protected]>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#pragma once
25
26#include "ExceptionOr.h"
27#include <JavaScriptCore/ThrowScope.h>
28
29namespace JSC {
30class CatchScope;
31}
32
33namespace WebCore {
34
35class CachedScript;
36class DeferredPromise;
37class JSDOMGlobalObject;
38
39struct ExceptionDetails {
40 String message;
41 int lineNumber { 0 };
42 int columnNumber { 0 };
43 String sourceURL;
44};
45
46void throwAttributeTypeError(JSC::ExecState&, JSC::ThrowScope&, const char* interfaceName, const char* attributeName, const char* expectedType);
47WEBCORE_EXPORT bool throwSetterTypeError(JSC::ExecState&, JSC::ThrowScope&, const char* interfaceName, const char* attributeName);
48
49void throwDataCloneError(JSC::ExecState&, JSC::ThrowScope&);
50void throwDOMSyntaxError(JSC::ExecState&, JSC::ThrowScope&, ASCIILiteral); // Not the same as a JavaScript syntax error.
51void throwInvalidStateError(JSC::ExecState&, JSC::ThrowScope&, ASCIILiteral);
52WEBCORE_EXPORT void throwNonFiniteTypeError(JSC::ExecState&, JSC::ThrowScope&);
53void throwNotSupportedError(JSC::ExecState&, JSC::ThrowScope&, ASCIILiteral);
54void throwSecurityError(JSC::ExecState&, JSC::ThrowScope&, const String& message);
55WEBCORE_EXPORT void throwSequenceTypeError(JSC::ExecState&, JSC::ThrowScope&);
56
57WEBCORE_EXPORT JSC::EncodedJSValue throwArgumentMustBeEnumError(JSC::ExecState&, JSC::ThrowScope&, unsigned argumentIndex, const char* argumentName, const char* functionInterfaceName, const char* functionName, const char* expectedValues);
58WEBCORE_EXPORT JSC::EncodedJSValue throwArgumentMustBeFunctionError(JSC::ExecState&, JSC::ThrowScope&, unsigned argumentIndex, const char* argumentName, const char* functionInterfaceName, const char* functionName);
59WEBCORE_EXPORT JSC::EncodedJSValue throwArgumentTypeError(JSC::ExecState&, JSC::ThrowScope&, unsigned argumentIndex, const char* argumentName, const char* functionInterfaceName, const char* functionName, const char* expectedType);
60WEBCORE_EXPORT JSC::EncodedJSValue throwRequiredMemberTypeError(JSC::ExecState&, JSC::ThrowScope&, const char* memberName, const char* dictionaryName, const char* expectedType);
61JSC::EncodedJSValue throwConstructorScriptExecutionContextUnavailableError(JSC::ExecState&, JSC::ThrowScope&, const char* interfaceName);
62
63String makeGetterTypeErrorMessage(const char* interfaceName, const char* attributeName);
64String makeThisTypeErrorMessage(const char* interfaceName, const char* attributeName);
65
66WEBCORE_EXPORT JSC::EncodedJSValue throwGetterTypeError(JSC::ExecState&, JSC::ThrowScope&, const char* interfaceName, const char* attributeName);
67WEBCORE_EXPORT JSC::EncodedJSValue throwThisTypeError(JSC::ExecState&, JSC::ThrowScope&, const char* interfaceName, const char* functionName);
68
69WEBCORE_EXPORT JSC::EncodedJSValue rejectPromiseWithGetterTypeError(JSC::ExecState&, const char* interfaceName, const char* attributeName);
70WEBCORE_EXPORT JSC::EncodedJSValue rejectPromiseWithThisTypeError(DeferredPromise&, const char* interfaceName, const char* operationName);
71WEBCORE_EXPORT JSC::EncodedJSValue rejectPromiseWithThisTypeError(JSC::ExecState&, const char* interfaceName, const char* operationName);
72
73String retrieveErrorMessage(JSC::ExecState&, JSC::VM&, JSC::JSValue exception, JSC::CatchScope&);
74WEBCORE_EXPORT void reportException(JSC::ExecState*, JSC::JSValue exception, CachedScript* = nullptr);
75WEBCORE_EXPORT void reportException(JSC::ExecState*, JSC::Exception*, CachedScript* = nullptr, ExceptionDetails* = nullptr);
76void reportCurrentException(JSC::ExecState*);
77
78JSC::JSValue createDOMException(JSC::ExecState&, Exception&&);
79JSC::JSValue createDOMException(JSC::ExecState*, ExceptionCode, const String& = emptyString());
80
81// Convert a DOM implementation exception into a JavaScript exception in the execution state.
82WEBCORE_EXPORT void propagateExceptionSlowPath(JSC::ExecState&, JSC::ThrowScope&, Exception&&);
83
84ALWAYS_INLINE void propagateException(JSC::ExecState& state, JSC::ThrowScope& throwScope, Exception&& exception)
85{
86 if (throwScope.exception())
87 return;
88 propagateExceptionSlowPath(state, throwScope, WTFMove(exception));
89}
90
91inline void propagateException(JSC::ExecState& state, JSC::ThrowScope& throwScope, ExceptionOr<void>&& value)
92{
93 if (UNLIKELY(value.hasException()))
94 propagateException(state, throwScope, value.releaseException());
95}
96
97} // namespace WebCore
98