2020-03-24 14:37:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.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
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
#include <AK/DeprecatedFlyString.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 {
|
|
|
|
|
2022-02-07 14:48:09 +01:00
|
|
|
struct TracebackFrame {
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString function_name;
|
2022-02-07 14:48:09 +01:00
|
|
|
SourceRange source_range;
|
|
|
|
};
|
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
class Error : public Object {
|
2020-06-21 15:14:02 +02:00
|
|
|
JS_OBJECT(Error, Object);
|
|
|
|
|
2020-03-24 14:37:39 +01:00
|
|
|
public:
|
2022-12-13 20:49:50 +00:00
|
|
|
static NonnullGCPtr<Error> create(Realm&);
|
|
|
|
static NonnullGCPtr<Error> create(Realm&, DeprecatedString const& 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
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
[[nodiscard]] DeprecatedString stack_string() 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
|
|
|
|
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:
|
|
|
|
void populate_stack();
|
2022-02-07 14:48:09 +01:00
|
|
|
Vector<TracebackFrame, 32> m_traceback;
|
2020-03-24 14:37:39 +01: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().
|
2022-12-13 20:49:50 +00:00
|
|
|
#define DECLARE_NATIVE_ERROR(ClassName, snake_name, PrototypeName, ConstructorName) \
|
|
|
|
class ClassName final : public Error { \
|
|
|
|
JS_OBJECT(ClassName, Error); \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
static NonnullGCPtr<ClassName> create(Realm&); \
|
|
|
|
static NonnullGCPtr<ClassName> create(Realm&, DeprecatedString const& message); \
|
|
|
|
\
|
|
|
|
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
|
2020-03-24 14:37:39 +01:00
|
|
|
}
|