ErrorConstructor.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020-2021, 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(GlobalObject& global_object)
  12. : NativeFunction(vm().names.Error.as_string(), *global_object.function_prototype())
  13. {
  14. }
  15. void ErrorConstructor::initialize(GlobalObject& global_object)
  16. {
  17. auto& vm = this->vm();
  18. NativeFunction::initialize(global_object);
  19. // 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
  20. define_property(vm.names.prototype, global_object.error_prototype(), 0);
  21. define_property(vm.names.length, Value(1), Attribute::Configurable);
  22. }
  23. // 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
  24. Value ErrorConstructor::call()
  25. {
  26. return construct(*this);
  27. }
  28. // 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
  29. Value ErrorConstructor::construct(FunctionObject& new_target)
  30. {
  31. auto& vm = this->vm();
  32. auto& global_object = this->global_object();
  33. auto* error = ordinary_create_from_constructor<Error>(global_object, new_target, &GlobalObject::error_prototype);
  34. if (vm.exception())
  35. return {};
  36. u8 attr = Attribute::Writable | Attribute::Configurable;
  37. if (!vm.argument(0).is_undefined()) {
  38. auto message = vm.argument(0).to_string(global_object);
  39. if (vm.exception())
  40. return {};
  41. error->define_property(vm.names.message, js_string(vm, message), attr);
  42. }
  43. error->install_error_cause(vm.argument(1));
  44. if (vm.exception())
  45. return {};
  46. return error;
  47. }
  48. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  49. ConstructorName::ConstructorName(GlobalObject& global_object) \
  50. : NativeFunction(*static_cast<Object*>(global_object.error_constructor())) \
  51. { \
  52. } \
  53. \
  54. void ConstructorName::initialize(GlobalObject& global_object) \
  55. { \
  56. auto& vm = this->vm(); \
  57. NativeFunction::initialize(global_object); \
  58. \
  59. /* 20.5.6.2.1 NativeError.prototype, \
  60. https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
  61. define_property(vm.names.prototype, global_object.snake_name##_prototype(), 0); \
  62. \
  63. define_property(vm.names.length, Value(1), Attribute::Configurable); \
  64. } \
  65. \
  66. ConstructorName::~ConstructorName() { } \
  67. \
  68. /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */ \
  69. Value ConstructorName::call() \
  70. { \
  71. return construct(*this); \
  72. } \
  73. \
  74. /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */ \
  75. Value ConstructorName::construct(FunctionObject& new_target) \
  76. { \
  77. auto& vm = this->vm(); \
  78. auto& global_object = this->global_object(); \
  79. \
  80. auto* error = ordinary_create_from_constructor<ClassName>( \
  81. global_object, new_target, &GlobalObject::snake_name##_prototype); \
  82. if (vm.exception()) \
  83. return {}; \
  84. \
  85. u8 attr = Attribute::Writable | Attribute::Configurable; \
  86. \
  87. if (!vm.argument(0).is_undefined()) { \
  88. auto message = vm.argument(0).to_string(global_object); \
  89. if (vm.exception()) \
  90. return {}; \
  91. error->define_property(vm.names.message, js_string(vm, message), attr); \
  92. } \
  93. \
  94. error->install_error_cause(vm.argument(1)); \
  95. if (vm.exception()) \
  96. return {}; \
  97. \
  98. return error; \
  99. }
  100. JS_ENUMERATE_NATIVE_ERRORS
  101. #undef __JS_ENUMERATE
  102. }