ErrorConstructor.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(realm.vm().names.Error.as_string(), *realm.intrinsics().function_prototype())
  13. {
  14. }
  15. ThrowCompletionOr<void> ErrorConstructor::initialize(Realm& realm)
  16. {
  17. auto& vm = this->vm();
  18. MUST_OR_THROW_OOM(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.intrinsics().error_prototype(), 0);
  21. define_direct_property(vm.names.length, Value(1), Attribute::Configurable);
  22. return {};
  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. ConstructorName::ConstructorName(Realm& realm) \
  52. : NativeFunction(realm.vm().names.ClassName.as_string(), *static_cast<Object*>(realm.intrinsics().error_constructor())) \
  53. { \
  54. } \
  55. \
  56. ThrowCompletionOr<void> ConstructorName::initialize(Realm& realm) \
  57. { \
  58. auto& vm = this->vm(); \
  59. MUST_OR_THROW_OOM(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.intrinsics().snake_name##_prototype(), 0); \
  63. \
  64. define_direct_property(vm.names.length, Value(1), Attribute::Configurable); \
  65. \
  66. return {}; \
  67. } \
  68. \
  69. ConstructorName::~ConstructorName() = default; \
  70. \
  71. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  72. ThrowCompletionOr<Value> ConstructorName::call() \
  73. { \
  74. /* 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget. */ \
  75. return TRY(construct(*this)); \
  76. } \
  77. \
  78. /* 20.5.6.1.1 NativeError ( message [ , options ] ), https://tc39.es/ecma262/#sec-nativeerror */ \
  79. ThrowCompletionOr<NonnullGCPtr<Object>> ConstructorName::construct(FunctionObject& new_target) \
  80. { \
  81. auto& vm = this->vm(); \
  82. \
  83. auto message = vm.argument(0); \
  84. auto options = vm.argument(1); \
  85. \
  86. /* 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »). */ \
  87. auto error = TRY(ordinary_create_from_constructor<ClassName>(vm, new_target, &Intrinsics::snake_name##_prototype)); \
  88. \
  89. /* 3. If message is not undefined, then */ \
  90. if (!message.is_undefined()) { \
  91. /* a. Let msg be ? ToString(message). */ \
  92. auto msg = TRY(message.to_string(vm)); \
  93. \
  94. /* b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "message", msg). */ \
  95. error->create_non_enumerable_data_property_or_throw(vm.names.message, PrimitiveString::create(vm, move(msg))); \
  96. } \
  97. \
  98. /* 4. Perform ? InstallErrorCause(O, options). */ \
  99. TRY(error->install_error_cause(options)); \
  100. \
  101. /* 5. Return O. */ \
  102. return error; \
  103. }
  104. JS_ENUMERATE_NATIVE_ERRORS
  105. #undef __JS_ENUMERATE
  106. }