ErrorConstructor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Error.h>
  7. #include <LibJS/Runtime/ErrorConstructor.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. namespace JS {
  10. ErrorConstructor::ErrorConstructor(GlobalObject& global_object)
  11. : NativeFunction(vm().names.Error, *global_object.function_prototype())
  12. {
  13. }
  14. void ErrorConstructor::initialize(GlobalObject& global_object)
  15. {
  16. auto& vm = this->vm();
  17. NativeFunction::initialize(global_object);
  18. // 20.5.2.1 Error.prototype, https://tc39.es/ecma262/#sec-error.prototype
  19. define_property(vm.names.prototype, global_object.error_prototype(), 0);
  20. define_property(vm.names.length, Value(1), Attribute::Configurable);
  21. }
  22. // 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
  23. Value ErrorConstructor::call()
  24. {
  25. return construct(*this);
  26. }
  27. // 20.5.1.1 Error ( message ), https://tc39.es/ecma262/#sec-error-message
  28. Value ErrorConstructor::construct(Function&)
  29. {
  30. auto& vm = this->vm();
  31. // FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%")
  32. auto* error = Error::create(global_object());
  33. u8 attr = Attribute::Writable | Attribute::Configurable;
  34. if (!vm.argument(0).is_undefined()) {
  35. auto message = vm.argument(0).to_string(global_object());
  36. if (vm.exception())
  37. return {};
  38. error->define_property(vm.names.message, js_string(vm, message), attr);
  39. }
  40. error->install_error_cause(vm.argument(1));
  41. if (vm.exception())
  42. return {};
  43. return error;
  44. }
  45. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  46. ConstructorName::ConstructorName(GlobalObject& global_object) \
  47. : NativeFunction(*static_cast<Object*>(global_object.error_constructor())) \
  48. { \
  49. } \
  50. \
  51. void ConstructorName::initialize(GlobalObject& global_object) \
  52. { \
  53. auto& vm = this->vm(); \
  54. NativeFunction::initialize(global_object); \
  55. \
  56. /* 20.5.6.2.1 NativeError.prototype, \
  57. https://tc39.es/ecma262/#sec-nativeerror.prototype */ \
  58. define_property(vm.names.prototype, global_object.snake_name##_prototype(), 0); \
  59. \
  60. define_property(vm.names.length, Value(1), Attribute::Configurable); \
  61. } \
  62. \
  63. ConstructorName::~ConstructorName() { } \
  64. \
  65. /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */ \
  66. Value ConstructorName::call() \
  67. { \
  68. return construct(*this); \
  69. } \
  70. \
  71. /* 20.5.6.1.1 NativeError ( message ), https://tc39.es/ecma262/#sec-nativeerror */ \
  72. Value ConstructorName::construct(Function&) \
  73. { \
  74. auto& vm = this->vm(); \
  75. /* FIXME: Use OrdinaryCreateFromConstructor( \
  76. * FIXME: newTarget, "%NativeError.prototype%"). */ \
  77. auto* error = ClassName::create(global_object()); \
  78. \
  79. u8 attr = Attribute::Writable | Attribute::Configurable; \
  80. \
  81. if (!vm.argument(0).is_undefined()) { \
  82. auto message = vm.argument(0).to_string(global_object()); \
  83. if (vm.exception()) \
  84. return {}; \
  85. error->define_property(vm.names.message, js_string(vm, message), attr); \
  86. } \
  87. \
  88. error->install_error_cause(vm.argument(1)); \
  89. if (vm.exception()) \
  90. return {}; \
  91. \
  92. return error; \
  93. }
  94. JS_ENUMERATE_NATIVE_ERRORS
  95. #undef __JS_ENUMERATE
  96. }