ErrorConstructor.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. define_property(vm.names.prototype, global_object.error_prototype(), 0);
  19. define_property(vm.names.length, Value(1), Attribute::Configurable);
  20. }
  21. Value ErrorConstructor::call()
  22. {
  23. return construct(*this);
  24. }
  25. Value ErrorConstructor::construct(Function&)
  26. {
  27. auto& vm = this->vm();
  28. // FIXME: Use OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%")
  29. auto* error = Error::create(global_object());
  30. u8 attr = Attribute::Writable | Attribute::Configurable;
  31. if (!vm.argument(0).is_undefined()) {
  32. auto message = vm.argument(0).to_string(global_object());
  33. if (vm.exception())
  34. return {};
  35. error->define_property(vm.names.message, js_string(vm, message), attr);
  36. }
  37. error->install_error_cause(vm.argument(1));
  38. if (vm.exception())
  39. return {};
  40. return error;
  41. }
  42. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  43. ConstructorName::ConstructorName(GlobalObject& global_object) \
  44. : NativeFunction(*static_cast<Object*>(global_object.error_constructor())) \
  45. { \
  46. } \
  47. \
  48. void ConstructorName::initialize(GlobalObject& global_object) \
  49. { \
  50. auto& vm = this->vm(); \
  51. NativeFunction::initialize(global_object); \
  52. define_property(vm.names.prototype, global_object.snake_name##_prototype(), 0); \
  53. define_property(vm.names.length, Value(1), Attribute::Configurable); \
  54. } \
  55. \
  56. ConstructorName::~ConstructorName() { } \
  57. \
  58. Value ConstructorName::call() \
  59. { \
  60. return construct(*this); \
  61. } \
  62. \
  63. Value ConstructorName::construct(Function&) \
  64. { \
  65. auto& vm = this->vm(); \
  66. /* FIXME: Use OrdinaryCreateFromConstructor( \
  67. * FIXME: newTarget, "%NativeError.prototype%"). */ \
  68. auto* error = ClassName::create(global_object()); \
  69. \
  70. u8 attr = Attribute::Writable | Attribute::Configurable; \
  71. \
  72. if (!vm.argument(0).is_undefined()) { \
  73. auto message = vm.argument(0).to_string(global_object()); \
  74. if (vm.exception()) \
  75. return {}; \
  76. error->define_property(vm.names.message, js_string(vm, message), attr); \
  77. } \
  78. \
  79. error->install_error_cause(vm.argument(1)); \
  80. if (vm.exception()) \
  81. return {}; \
  82. \
  83. return error; \
  84. }
  85. JS_ENUMERATE_NATIVE_ERRORS
  86. #undef __JS_ENUMERATE
  87. }