2020-03-24 14:37:39 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-12-13 20:49:50 +00:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2020-03-24 14:37:39 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 14:37:39 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-10 12:42:33 +02:00
|
|
|
#pragma once
|
|
|
|
|
2025-08-02 19:27:29 -04:00
|
|
|
#include <AK/FlyString.h>
|
2023-02-16 14:09:11 -05:00
|
|
|
#include <AK/String.h>
|
2025-08-07 19:31:52 -04:00
|
|
|
#include <AK/Utf16String.h>
|
2025-07-19 13:49:30 -07:00
|
|
|
#include <LibJS/Export.h>
|
2021-10-02 16:03:45 +01:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-24 14:37:39 +01:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2022-02-07 14:48:09 +01:00
|
|
|
#include <LibJS/SourceRange.h>
|
2020-03-24 14:37:39 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2025-07-19 13:49:30 -07:00
|
|
|
struct JS_API TracebackFrame {
|
2025-03-18 18:08:02 -05:00
|
|
|
FlyString function_name;
|
2023-05-28 08:28:43 +02:00
|
|
|
[[nodiscard]] SourceRange const& source_range() const;
|
|
|
|
|
2024-10-14 07:49:12 +02:00
|
|
|
RefPtr<CachedSourceRange> cached_source_range;
|
2022-02-07 14:48:09 +01:00
|
|
|
};
|
|
|
|
|
2023-10-31 21:55:17 +01:00
|
|
|
enum CompactTraceback {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
};
|
|
|
|
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API Error : public Object {
|
2020-06-21 15:14:02 +02:00
|
|
|
JS_OBJECT(Error, Object);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(Error);
|
2020-06-21 15:14:02 +02:00
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<Error> create(Realm&);
|
2025-08-07 19:31:52 -04:00
|
|
|
static GC::Ref<Error> create(Realm&, Utf16String message);
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<Error> create(Realm&, StringView message);
|
2020-04-17 19:31:48 +02:00
|
|
|
|
2021-04-12 00:08:28 +02:00
|
|
|
virtual ~Error() override = default;
|
2021-06-26 19:06:55 +01:00
|
|
|
|
2023-10-31 21:55:17 +01:00
|
|
|
[[nodiscard]] String stack_string(CompactTraceback compact = CompactTraceback::No) const;
|
2022-02-06 17:00:28 +01:00
|
|
|
|
2021-10-02 16:03:45 +01:00
|
|
|
ThrowCompletionOr<void> install_error_cause(Value options);
|
2022-02-06 17:00:28 +01:00
|
|
|
|
2025-08-07 19:31:52 -04:00
|
|
|
void set_message(Utf16String);
|
2025-07-17 09:48:16 -04:00
|
|
|
|
2022-02-07 14:48:09 +01:00
|
|
|
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
|
|
|
|
|
2022-08-28 23:51:28 +02:00
|
|
|
protected:
|
|
|
|
explicit Error(Object& prototype);
|
|
|
|
|
2022-02-06 17:00:28 +01:00
|
|
|
private:
|
2025-04-26 08:07:22 -04:00
|
|
|
virtual bool is_error_object() const final { return true; }
|
2025-03-25 08:34:06 +00:00
|
|
|
|
2022-02-06 17:00:28 +01:00
|
|
|
void populate_stack();
|
2022-02-07 14:48:09 +01:00
|
|
|
Vector<TracebackFrame, 32> m_traceback;
|
2020-03-24 14:37:39 +01:00
|
|
|
};
|
|
|
|
|
2025-03-25 08:34:06 +00:00
|
|
|
template<>
|
2025-04-26 08:07:22 -04:00
|
|
|
inline bool Object::fast_is<Error>() const { return is_error_object(); }
|
2025-03-25 08:34:06 +00:00
|
|
|
|
2021-06-11 17:54:42 +01: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 08:18:01 -04:00
|
|
|
#define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
|
2025-07-19 13:49:30 -07:00
|
|
|
class JS_API ClassName final : public Error { \
|
2023-09-06 08:18:01 -04:00
|
|
|
JS_OBJECT(ClassName, Error); \
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(ClassName); \
|
2023-09-06 08:18:01 -04:00
|
|
|
\
|
|
|
|
public: \
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<ClassName> create(Realm&); \
|
2025-08-07 19:31:52 -04:00
|
|
|
static GC::Ref<ClassName> create(Realm&, Utf16String message); \
|
2024-11-15 04:01:23 +13:00
|
|
|
static GC::Ref<ClassName> create(Realm&, StringView message); \
|
2023-09-06 08:18:01 -04:00
|
|
|
\
|
|
|
|
explicit ClassName(Object& prototype); \
|
|
|
|
virtual ~ClassName() override = default; \
|
2020-04-10 12:42:33 +02:00
|
|
|
};
|
|
|
|
|
2020-12-01 21:05:25 +01:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
2021-06-11 17:54:42 +01:00
|
|
|
DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName)
|
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2020-04-10 14:06:52 +02:00
|
|
|
#undef __JS_ENUMERATE
|
2025-05-13 07:06:33 -04:00
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|