2020-03-24 10:37:39 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-12-13 17:49:50 -03:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2020-03-24 10:37:39 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 10:37:39 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-04-10 07:42:33 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-01-08 21:23:00 -03:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2023-02-16 16:09:11 -03:00
|
|
|
#include <AK/String.h>
|
2021-10-02 12:03:45 -03:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-24 10:37:39 -03:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2022-02-07 10:48:09 -03:00
|
|
|
#include <LibJS/SourceRange.h>
|
2020-03-24 10:37:39 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2022-02-07 10:48:09 -03:00
|
|
|
struct TracebackFrame {
|
2023-01-08 21:23:00 -03:00
|
|
|
DeprecatedFlyString function_name;
|
2023-05-28 03:28:43 -03:00
|
|
|
[[nodiscard]] SourceRange const& source_range() const;
|
|
|
|
|
|
2024-10-14 02:49:12 -03:00
|
|
|
RefPtr<CachedSourceRange> cached_source_range;
|
2022-02-07 10:48:09 -03:00
|
|
|
};
|
|
|
|
|
|
2023-10-31 17:55:17 -03:00
|
|
|
enum CompactTraceback {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-24 10:37:39 -03:00
|
|
|
class Error : public Object {
|
2020-06-21 10:14:02 -03:00
|
|
|
JS_OBJECT(Error, Object);
|
2023-11-19 05:45:05 -03:00
|
|
|
JS_DECLARE_ALLOCATOR(Error);
|
2020-06-21 10:14:02 -03:00
|
|
|
|
2020-03-24 10:37:39 -03:00
|
|
|
public:
|
2022-12-13 17:49:50 -03:00
|
|
|
static NonnullGCPtr<Error> create(Realm&);
|
2023-02-16 16:09:11 -03:00
|
|
|
static NonnullGCPtr<Error> create(Realm&, String message);
|
2023-09-06 09:18:01 -03:00
|
|
|
static NonnullGCPtr<Error> create(Realm&, StringView message);
|
2020-04-17 14:31:48 -03:00
|
|
|
|
2021-04-11 19:08:28 -03:00
|
|
|
virtual ~Error() override = default;
|
2021-06-26 15:06:55 -03:00
|
|
|
|
2023-10-31 17:55:17 -03:00
|
|
|
[[nodiscard]] String stack_string(CompactTraceback compact = CompactTraceback::No) const;
|
2022-02-06 13:00:28 -03:00
|
|
|
|
2021-10-02 12:03:45 -03:00
|
|
|
ThrowCompletionOr<void> install_error_cause(Value options);
|
2022-02-06 13:00:28 -03:00
|
|
|
|
2022-02-07 10:48:09 -03:00
|
|
|
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
|
|
|
|
|
|
2022-08-28 18:51:28 -03:00
|
|
|
protected:
|
|
|
|
|
explicit Error(Object& prototype);
|
|
|
|
|
|
2022-02-06 13:00:28 -03:00
|
|
|
private:
|
|
|
|
|
void populate_stack();
|
2022-02-07 10:48:09 -03:00
|
|
|
Vector<TracebackFrame, 32> m_traceback;
|
2020-03-24 10:37:39 -03:00
|
|
|
};
|
|
|
|
|
|
2021-06-11 13:54:42 -03:00
|
|
|
// NOTE: Making these inherit from Error is not required by the spec but
|
|
|
|
|
// our way of implementing the [[ErrorData]] internal slot, which is
|
|
|
|
|
// used in Object.prototype.toString().
|
2023-09-06 09:18:01 -03:00
|
|
|
#define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
|
class ClassName final : public Error { \
|
|
|
|
|
JS_OBJECT(ClassName, Error); \
|
2023-11-19 05:45:05 -03:00
|
|
|
JS_DECLARE_ALLOCATOR(ClassName); \
|
2023-09-06 09:18:01 -03:00
|
|
|
\
|
|
|
|
|
public: \
|
|
|
|
|
static NonnullGCPtr<ClassName> create(Realm&); \
|
|
|
|
|
static NonnullGCPtr<ClassName> create(Realm&, String message); \
|
|
|
|
|
static NonnullGCPtr<ClassName> create(Realm&, StringView message); \
|
|
|
|
|
\
|
|
|
|
|
explicit ClassName(Object& prototype); \
|
|
|
|
|
virtual ~ClassName() override = default; \
|
2020-04-10 07:42:33 -03:00
|
|
|
};
|
|
|
|
|
|
2020-12-01 17:05:25 -03:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
2021-06-11 13:54:42 -03:00
|
|
|
DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)
|
|
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2020-04-10 09:06:52 -03:00
|
|
|
#undef __JS_ENUMERATE
|
2020-03-24 10:37:39 -03:00
|
|
|
}
|