ErrorConstructor.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/ErrorConstructor.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. GC_DEFINE_ALLOCATOR(ErrorConstructor);
  12. ErrorConstructor::ErrorConstructor(Realm& realm)
  13. : NativeFunction(realm.vm().names.Error.as_string(), realm.intrinsics().function_prototype())
  14. {
  15. }
  16. void ErrorConstructor::initialize(Realm& realm)
  17. {
  18. auto& vm = this->vm();
  19. Base::initialize(realm);
  20. // 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
  21. define_direct_property(vm.names.prototype, realm.intrinsics().error_prototype(), 0);
  22. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  23. u8 attr = Attribute::Writable | Attribute::Configurable;
  24. define_native_function(realm, vm.names.isError, is_error, 1, attr);
  25. }
  26. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  27. ThrowCompletionOr<Value> ErrorConstructor::call()
  28. {
  29. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  30. return TRY(construct(*this));
  31. }
  32. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  33. ThrowCompletionOr<GC::Ref<Object>> ErrorConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto message = vm.argument(0);
  37. auto options = vm.argument(1);
  38. // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
  39. auto error = TRY(ordinary_create_from_constructor<Error>(vm, new_target, &Intrinsics::error_prototype));
  40. // 3. If message is not undefined, then
  41. if (!message.is_undefined()) {
  42. // a. Let msg be ? ToString(message).
  43. auto msg = TRY(message.to_string(vm));
  44. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  45. error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg)));
  46. }
  47. // 4. Perform ? InstallErrorCause(O, options).
  48. TRY(error->install_error_cause(options));
  49. // 5. Return O.
  50. return error;
  51. }
  52. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  53. GC_DEFINE_ALLOCATOR(ConstructorName); \
  54. ConstructorName::ConstructorName(Realm& realm) \
  55. : NativeFunction(realm.vm().names.ClassName.as_string(), realm.intrinsics().error_constructor()) \
  56. { \
  57. } \
  58. \
  59. void ConstructorName::initialize(Realm& realm) \
  60. { \
  61. auto& vm = this->vm(); \
  62. Base::initialize(realm); \
  63. \
  64. /* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
  65. define_direct_property(vm.names.prototype, realm.intrinsics().snake_name##_prototype(), 0); \
  66. \
  67. define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
  68. } \
  69. \
  70. ConstructorName::~ConstructorName() = default; \
  71. \
  72. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  73. ThrowCompletionOr<Value> ConstructorName::call() \
  74. { \
  75. /* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
  76. return TRY(construct(*this)); \
  77. } \
  78. \
  79. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  80. ThrowCompletionOr<GC::Ref<Object>> ConstructorName::construct(FunctionObject& new_target) \
  81. { \
  82. auto& vm = this->vm(); \
  83. \
  84. auto message = vm.argument(0); \
  85. auto options = vm.argument(1); \
  86. \
  87. /* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
  88. auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
  89. \
  90. /* 3. If message is not undefined, then */ \
  91. if (!message.is_undefined()) { \
  92. /* a. Let msg be ? ToString(message). */ \
  93. auto msg = TRY(message.to_string(vm)); \
  94. \
  95. /* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
  96. error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
  97. } \
  98. \
  99. /* 4. Perform ? InstallErrorCause(O, options). */ \
  100. TRY(error->install_error_cause(options)); \
  101. \
  102. /* 5. Return O. */ \
  103. return error; \
  104. }
  105. JS_ENUMERATE_NATIVE_ERRORS
  106. #undef __JS_ENUMERATE
  107. // 20.5.2.1 Error.isError ( arg ), https://tc39.es/proposal-is-error/#sec-error.iserror
  108. JS_DEFINE_NATIVE_FUNCTION(ErrorConstructor::is_error)
  109. {
  110. auto arg = vm.argument(0);
  111. // 1. Return IsError(arg).
  112. return Value(arg.is_error());
  113. }
  114. }