ErrorConstructor.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2020-2022, 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. ErrorConstructor::ErrorConstructor(Realm& realm)
  12. : NativeFunction(vm().names.Error.as_string(), *realm.global_object().function_prototype())
  13. {
  14. }
  15. void ErrorConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(realm);
  19. // 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
  20. define_direct_property(vm.names.prototype, realm.global_object().error_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  24. ThrowCompletionOr<Value> ErrorConstructor::call()
  25. {
  26. // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
  27. return TRY(construct(*this));
  28. }
  29. // 20.5.1.1 Error ( message [ , options ] ), https://tc39.es/ecma262/#sec-error-message
  30. ThrowCompletionOr<Object*> ErrorConstructor::construct(FunctionObject& new_target)
  31. {
  32. auto& vm = this->vm();
  33. auto& global_object = this->global_object();
  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>(global_object, new_target, &GlobalObject::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(global_object));
  42. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg).
  43. error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(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. ConstructorName::ConstructorName(Realm& realm) \
  52. : NativeFunction(vm().names.ClassName.as_string(), *static_cast<Object*>(realm.global_object().error_constructor())) \
  53. { \
  54. } \
  55. \
  56. void ConstructorName::initialize(Realm& realm) \
  57. { \
  58. auto& vm = this->vm(); \
  59. NativeFunction::initialize(realm); \
  60. \
  61. /* 20.5.6.2.1 NativeError.prototype, https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
  62. define_direct_property(vm.names.prototype, realm.global_object().snake_name##_prototype(), 0); \
  63. \
  64. define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
  65. } \
  66. \
  67. ConstructorName::~ConstructorName() = default; \
  68. \
  69. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  70. ThrowCompletionOr<Value> ConstructorName::call() \
  71. { \
  72. /* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
  73. return TRY(construct(*this)); \
  74. } \
  75. \
  76. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  77. ThrowCompletionOr<Object*> ConstructorName::construct(FunctionObject& new_target) \
  78. { \
  79. auto& vm = this->vm(); \
  80. auto& global_object = this->global_object(); \
  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>(global_object, new_target, &GlobalObject::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(global_object)); \
  92. \
  93. /* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
  94. error->create_non_enumerable_data_property_or_throw(vm.names.message, js_string(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. }