ErrorConstructor.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. JS_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. }
  24. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  25. ThrowCompletionOr<Value> ErrorConstructor::call()
  26. {
  27. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  28. return TRY(construct(*this));
  29. }
  30. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  31. ThrowCompletionOr<NonnullGCPtr<Object>> ErrorConstructor::construct(FunctionObject& new_target)
  32. {
  33. auto& vm = this->vm();
  34. auto message = vm.argument(0);
  35. auto options = vm.argument(1);
  36. // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
  37. auto error = TRY(ordinary_create_from_constructor<Error>(vm, new_target, &Intrinsics::error_prototype));
  38. // 3. If message is not undefined, then
  39. if (!message.is_undefined()) {
  40. // a. Let msg be ? ToString(message).
  41. auto msg = TRY(message.to_string(vm));
  42. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  43. error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg)));
  44. }
  45. // 4. Perform ? InstallErrorCause(O, options).
  46. TRY(error->install_error_cause(options));
  47. // 5. Return O.
  48. return error;
  49. }
  50. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  51. JS_DEFINE_ALLOCATOR(ConstructorName); \
  52. ConstructorName::ConstructorName(Realm& realm) \
  53. : NativeFunction(realm.vm().names.ClassName.as_string(), realm.intrinsics().error_constructor()) \
  54. { \
  55. } \
  56. \
  57. void ConstructorName::initialize(Realm& realm) \
  58. { \
  59. auto& vm = this->vm(); \
  60. Base::initialize(realm); \
  61. \
  62. /* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
  63. define_direct_property(vm.names.prototype, realm.intrinsics().snake_name##_prototype(), 0); \
  64. \
  65. define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
  66. } \
  67. \
  68. ConstructorName::~ConstructorName() = default; \
  69. \
  70. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  71. ThrowCompletionOr<Value> ConstructorName::call() \
  72. { \
  73. /* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
  74. return TRY(construct(*this)); \
  75. } \
  76. \
  77. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  78. ThrowCompletionOr<NonnullGCPtr<Object>> ConstructorName::construct(FunctionObject& new_target) \
  79. { \
  80. auto& vm = this->vm(); \
  81. \
  82. auto message = vm.argument(0); \
  83. auto options = vm.argument(1); \
  84. \
  85. /* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
  86. auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
  87. \
  88. /* 3. If message is not undefined, then */ \
  89. if (!message.is_undefined()) { \
  90. /* a. Let msg be ? ToString(message). */ \
  91. auto msg = TRY(message.to_string(vm)); \
  92. \
  93. /* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
  94. error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
  95. } \
  96. \
  97. /* 4. Perform ? InstallErrorCause(O, options). */ \
  98. TRY(error->install_error_cause(options)); \
  99. \
  100. /* 5. Return O. */ \
  101. return error; \
  102. }
  103. JS_ENUMERATE_NATIVE_ERRORS
  104. #undef __JS_ENUMERATE
  105. }