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
|
|
|
|
|
|
2025-08-02 20:27:29 -03:00
|
|
|
#include <AK/FlyString.h>
|
2023-02-16 16:09:11 -03:00
|
|
|
#include <AK/String.h>
|
2025-08-07 20:31:52 -03:00
|
|
|
#include <AK/Utf16String.h>
|
2025-07-19 17:49:30 -03:00
|
|
|
#include <LibJS/Export.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 {
|
|
|
|
|
|
2025-07-19 17:49:30 -03:00
|
|
|
struct JS_API TracebackFrame {
|
2025-10-29 04:28:41 -03:00
|
|
|
Utf16String function_name;
|
2023-05-28 03:28:43 -03:00
|
|
|
[[nodiscard]] SourceRange const& source_range() const;
|
|
|
|
|
|
2025-11-30 06:21:50 -03:00
|
|
|
GC::Ptr<CachedSourceRange> cached_source_range;
|
2022-02-07 10:48:09 -03:00
|
|
|
};
|
|
|
|
|
|
2023-10-31 17:55:17 -03:00
|
|
|
enum CompactTraceback {
|
|
|
|
|
No,
|
|
|
|
|
Yes,
|
|
|
|
|
};
|
|
|
|
|
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API Error : public Object {
|
2020-06-21 10:14:02 -03:00
|
|
|
JS_OBJECT(Error, Object);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(Error);
|
2020-06-21 10:14:02 -03:00
|
|
|
|
2020-03-24 10:37:39 -03:00
|
|
|
public:
|
2024-11-14 12:01:23 -03:00
|
|
|
static GC::Ref<Error> create(Realm&);
|
2025-08-07 20:31:52 -03:00
|
|
|
static GC::Ref<Error> create(Realm&, Utf16String message);
|
2024-11-14 12:01:23 -03:00
|
|
|
static GC::Ref<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
|
|
|
|
2025-08-07 20:31:52 -03:00
|
|
|
void set_message(Utf16String);
|
2025-07-17 10:48:16 -03:00
|
|
|
|
2022-02-07 10:48:09 -03:00
|
|
|
Vector<TracebackFrame, 32> const& traceback() const { return m_traceback; }
|
|
|
|
|
|
2026-01-18 07:48:29 -03:00
|
|
|
void set_cached_string(GC::Ref<PrimitiveString> string) { m_cached_string = string; }
|
|
|
|
|
GC::Ptr<PrimitiveString> cached_string() const { return m_cached_string; }
|
|
|
|
|
|
2022-08-28 18:51:28 -03:00
|
|
|
protected:
|
|
|
|
|
explicit Error(Object& prototype);
|
|
|
|
|
|
2025-11-30 06:21:50 -03:00
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2022-02-06 13:00:28 -03:00
|
|
|
private:
|
2025-04-26 09:07:22 -03:00
|
|
|
virtual bool is_error_object() const final { return true; }
|
2025-03-25 05:34:06 -03:00
|
|
|
|
2022-02-06 13:00:28 -03:00
|
|
|
void populate_stack();
|
2022-02-07 10:48:09 -03:00
|
|
|
Vector<TracebackFrame, 32> m_traceback;
|
2026-01-18 07:48:29 -03:00
|
|
|
|
|
|
|
|
GC::Ptr<PrimitiveString> m_cached_string;
|
2020-03-24 10:37:39 -03:00
|
|
|
};
|
|
|
|
|
|
2025-03-25 05:34:06 -03:00
|
|
|
template<>
|
2025-04-26 09:07:22 -03:00
|
|
|
inline bool Object::fast_is<Error>() const { return is_error_object(); }
|
2025-03-25 05:34:06 -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) \
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API ClassName final : public Error { \
|
2023-09-06 09:18:01 -03:00
|
|
|
JS_OBJECT(ClassName, Error); \
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(ClassName); \
|
2023-09-06 09:18:01 -03:00
|
|
|
\
|
|
|
|
|
public: \
|
2024-11-14 12:01:23 -03:00
|
|
|
static GC::Ref<ClassName> create(Realm&); \
|
2025-08-07 20:31:52 -03:00
|
|
|
static GC::Ref<ClassName> create(Realm&, Utf16String message); \
|
2024-11-14 12:01:23 -03:00
|
|
|
static GC::Ref<ClassName> create(Realm&, StringView message); \
|
2023-09-06 09:18:01 -03:00
|
|
|
\
|
|
|
|
|
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
|
2025-05-13 08:06:33 -03:00
|
|
|
|
2020-03-24 10:37:39 -03:00
|
|
|
}
|