Error.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. namespace JS {
  10. Error* Error::create(GlobalObject& global_object)
  11. {
  12. return global_object.heap().allocate<Error>(global_object, *global_object.error_prototype());
  13. }
  14. Error* Error::create(GlobalObject& global_object, String const& message)
  15. {
  16. auto& vm = global_object.vm();
  17. auto* error = Error::create(global_object);
  18. u8 attr = Attribute::Writable | Attribute::Configurable;
  19. error->define_property(vm.names.message, js_string(vm, message), attr);
  20. return error;
  21. }
  22. Error::Error(Object& prototype)
  23. : Object(prototype)
  24. {
  25. }
  26. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  27. ClassName* ClassName::create(GlobalObject& global_object) \
  28. { \
  29. return global_object.heap().allocate<ClassName>(global_object, *global_object.snake_name##_prototype()); \
  30. } \
  31. \
  32. ClassName* ClassName::create(GlobalObject& global_object, String const& message) \
  33. { \
  34. auto& vm = global_object.vm(); \
  35. auto* error = ClassName::create(global_object); \
  36. u8 attr = Attribute::Writable | Attribute::Configurable; \
  37. error->define_property(vm.names.message, js_string(vm, message), attr); \
  38. return error; \
  39. } \
  40. \
  41. ClassName::ClassName(Object& prototype) \
  42. : Error(prototype) \
  43. { \
  44. }
  45. JS_ENUMERATE_NATIVE_ERRORS
  46. #undef __JS_ENUMERATE
  47. }