ErrorConstructor.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/NativeFunction.h>
  9. namespace JS {
  10. class ErrorConstructor final : public NativeFunction {
  11. JS_OBJECT(ErrorConstructor, NativeFunction);
  12. GC_DECLARE_ALLOCATOR(ErrorConstructor);
  13. public:
  14. virtual void initialize(Realm&) override;
  15. virtual ~ErrorConstructor() override = default;
  16. virtual ThrowCompletionOr<Value> call() override;
  17. virtual ThrowCompletionOr<GC::Ref<Object>> construct(FunctionObject& new_target) override;
  18. private:
  19. explicit ErrorConstructor(Realm&);
  20. virtual bool has_constructor() const override { return true; }
  21. JS_DECLARE_NATIVE_FUNCTION(is_error);
  22. };
  23. #define DECLARE_NATIVE_ERROR_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName) \
  24. class ConstructorName final : public NativeFunction { \
  25. JS_OBJECT(ConstructorName, NativeFunction); \
  26. GC_DECLARE_ALLOCATOR(ConstructorName); \
  27. \
  28. public: \
  29. virtual void initialize(Realm&) override; \
  30. virtual ~ConstructorName() override; \
  31. virtual ThrowCompletionOr<Value> call() override; \
  32. virtual ThrowCompletionOr<GC::Ref<Object>> construct(FunctionObject& new_target) override; \
  33. \
  34. private: \
  35. explicit ConstructorName(Realm&); \
  36. \
  37. virtual bool has_constructor() const override \
  38. { \
  39. return true; \
  40. } \
  41. };
  42. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  43. DECLARE_NATIVE_ERROR_CONSTRUCTOR(ClassName, snake_name, PrototypeName, ConstructorName)
  44. JS_ENUMERATE_NATIVE_ERRORS
  45. #undef __JS_ENUMERATE
  46. }