Error.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/AST.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/Error.h>
  10. #include <LibJS/Runtime/ExecutionContext.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/SourceRange.h>
  13. namespace JS {
  14. Error* Error::create(GlobalObject& global_object)
  15. {
  16. return global_object.heap().allocate<Error>(global_object, *global_object.error_prototype());
  17. }
  18. Error* Error::create(GlobalObject& global_object, String const& message)
  19. {
  20. auto& vm = global_object.vm();
  21. auto* error = Error::create(global_object);
  22. u8 attr = Attribute::Writable | Attribute::Configurable;
  23. error->define_direct_property(vm.names.message, js_string(vm, message), attr);
  24. return error;
  25. }
  26. Error::Error(Object& prototype)
  27. : Object(prototype)
  28. {
  29. populate_stack();
  30. }
  31. // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/ecma262/#sec-installerrorcause
  32. ThrowCompletionOr<void> Error::install_error_cause(Value options)
  33. {
  34. auto& vm = this->vm();
  35. // 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
  36. if (options.is_object() && TRY(options.as_object().has_property(vm.names.cause))) {
  37. // a. Let cause be ? Get(options, "cause").
  38. auto cause = TRY(options.as_object().get(vm.names.cause));
  39. // b. Perform ! CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
  40. MUST(create_non_enumerable_data_property_or_throw(vm.names.cause, cause));
  41. }
  42. // 2. Return NormalCompletion(undefined).
  43. return {};
  44. }
  45. void Error::populate_stack()
  46. {
  47. auto& vm = this->vm();
  48. m_traceback.ensure_capacity(vm.execution_context_stack().size());
  49. for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
  50. auto* context = vm.execution_context_stack()[i];
  51. auto function_name = context->function_name;
  52. if (function_name.is_empty())
  53. function_name = "<unknown>"sv;
  54. m_traceback.empend(
  55. move(function_name),
  56. // We might not have an AST node associated with the execution context, e.g. in promise
  57. // reaction jobs (which aren't called anywhere from the source code).
  58. // They're not going to generate any _unhandled_ exceptions though, so a meaningless
  59. // source range is fine.
  60. context->current_node ? context->current_node->source_range() : SourceRange {});
  61. }
  62. }
  63. String Error::stack_string() const
  64. {
  65. StringBuilder stack_string_builder;
  66. // Note: We roughly follow V8's formatting
  67. // Note: The error's name and message get prepended by ErrorPrototype::stack
  68. // Note: We don't want to capture the global execution context, so we omit the last frame
  69. // FIXME: We generate a stack-frame for the Errors constructor, other engines do not
  70. for (size_t i = 0; i < m_traceback.size() - 1; ++i) {
  71. auto const& frame = m_traceback[i];
  72. auto function_name = frame.function_name;
  73. // Note: Since we don't know whether we have a valid SourceRange here we just check for some default values.
  74. if (!frame.source_range.filename.is_null() || frame.source_range.start.offset != 0 || frame.source_range.end.offset != 0) {
  75. if (function_name == "<unknown>"sv)
  76. stack_string_builder.appendff(" at {}:{}:{}\n", frame.source_range.filename, frame.source_range.start.line, frame.source_range.start.column);
  77. else
  78. stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, frame.source_range.filename, frame.source_range.start.line, frame.source_range.start.column);
  79. } else {
  80. stack_string_builder.appendff(" at {}\n", function_name.is_empty() ? "<unknown>"sv : function_name.view());
  81. }
  82. }
  83. return stack_string_builder.build();
  84. }
  85. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  86. ClassName* ClassName::create(GlobalObject& global_object) \
  87. { \
  88. return global_object.heap().allocate<ClassName>(global_object, *global_object.snake_name##_prototype()); \
  89. } \
  90. \
  91. ClassName* ClassName::create(GlobalObject& global_object, String const& message) \
  92. { \
  93. auto& vm = global_object.vm(); \
  94. auto* error = ClassName::create(global_object); \
  95. u8 attr = Attribute::Writable | Attribute::Configurable; \
  96. error->define_direct_property(vm.names.message, js_string(vm, message), attr); \
  97. return error; \
  98. } \
  99. \
  100. ClassName::ClassName(Object& prototype) \
  101. : Error(prototype) \
  102. { \
  103. }
  104. JS_ENUMERATE_NATIVE_ERRORS
  105. #undef __JS_ENUMERATE
  106. }