Error.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, 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/Runtime/ThrowableStringBuilder.h>
  13. #include <LibJS/SourceRange.h>
  14. namespace JS {
  15. SourceRange const& TracebackFrame::source_range() const
  16. {
  17. if (auto* unrealized = source_range_storage.get_pointer<UnrealizedSourceRange>()) {
  18. auto source_range = [&] {
  19. if (!unrealized->source_code) {
  20. static auto dummy_source_code = SourceCode::create(String {}, String {});
  21. return SourceRange { dummy_source_code, {}, {} };
  22. }
  23. return unrealized->source_code->range_from_offsets(unrealized->start_offset, unrealized->end_offset);
  24. }();
  25. source_range_storage = move(source_range);
  26. }
  27. return source_range_storage.get<SourceRange>();
  28. }
  29. NonnullGCPtr<Error> Error::create(Realm& realm)
  30. {
  31. return realm.heap().allocate<Error>(realm, realm.intrinsics().error_prototype()).release_allocated_value_but_fixme_should_propagate_errors();
  32. }
  33. NonnullGCPtr<Error> Error::create(Realm& realm, String message)
  34. {
  35. auto& vm = realm.vm();
  36. auto error = Error::create(realm);
  37. u8 attr = Attribute::Writable | Attribute::Configurable;
  38. error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
  39. return error;
  40. }
  41. ThrowCompletionOr<NonnullGCPtr<Error>> Error::create(Realm& realm, StringView message)
  42. {
  43. return create(realm, TRY_OR_THROW_OOM(realm.vm(), String::from_utf8(message)));
  44. }
  45. Error::Error(Object& prototype)
  46. : Object(ConstructWithPrototypeTag::Tag, prototype)
  47. {
  48. populate_stack();
  49. }
  50. // 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/ecma262/#sec-installerrorcause
  51. ThrowCompletionOr<void> Error::install_error_cause(Value options)
  52. {
  53. auto& vm = this->vm();
  54. // 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
  55. if (options.is_object() && TRY(options.as_object().has_property(vm.names.cause))) {
  56. // a. Let cause be ? Get(options, "cause").
  57. auto cause = TRY(options.as_object().get(vm.names.cause));
  58. // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
  59. create_non_enumerable_data_property_or_throw(vm.names.cause, cause);
  60. }
  61. // 2. Return unused.
  62. return {};
  63. }
  64. void Error::populate_stack()
  65. {
  66. auto& vm = this->vm();
  67. m_traceback.ensure_capacity(vm.execution_context_stack().size());
  68. for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
  69. auto context = vm.execution_context_stack()[i];
  70. auto function_name = context->function_name;
  71. if (function_name.is_empty())
  72. function_name = "<unknown>"sv;
  73. TracebackFrame frame {
  74. .function_name = move(function_name),
  75. .source_range_storage = TracebackFrame::UnrealizedSourceRange {},
  76. };
  77. // We might not have an AST node associated with the execution context, e.g. in promise
  78. // reaction jobs (which aren't called anywhere from the source code).
  79. // They're not going to generate any _unhandled_ exceptions though, so a meaningless
  80. // source range is fine.
  81. if (context->current_node) {
  82. auto* unrealized = frame.source_range_storage.get_pointer<TracebackFrame::UnrealizedSourceRange>();
  83. unrealized->source_code = context->current_node->source_code();
  84. unrealized->start_offset = context->current_node->start_offset();
  85. unrealized->end_offset = context->current_node->end_offset();
  86. }
  87. m_traceback.append(move(frame));
  88. }
  89. }
  90. ThrowCompletionOr<String> Error::stack_string(VM& vm) const
  91. {
  92. ThrowableStringBuilder stack_string_builder(vm);
  93. // Note: We roughly follow V8's formatting
  94. // Note: The error's name and message get prepended by ErrorPrototype::stack
  95. // Note: We don't want to capture the global execution context, so we omit the last frame
  96. // FIXME: We generate a stack-frame for the Errors constructor, other engines do not
  97. for (size_t i = 0; i < m_traceback.size() - 1; ++i) {
  98. auto const& frame = m_traceback[i];
  99. auto function_name = frame.function_name;
  100. auto source_range = frame.source_range();
  101. // Note: Since we don't know whether we have a valid SourceRange here we just check for some default values.
  102. if (!source_range.filename().is_empty() || source_range.start.offset != 0 || source_range.end.offset != 0) {
  103. if (function_name == "<unknown>"sv)
  104. MUST_OR_THROW_OOM(stack_string_builder.appendff(" at {}:{}:{}\n", source_range.filename(), source_range.start.line, source_range.start.column));
  105. else
  106. MUST_OR_THROW_OOM(stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, source_range.filename(), source_range.start.line, source_range.start.column));
  107. } else {
  108. MUST_OR_THROW_OOM(stack_string_builder.appendff(" at {}\n", function_name.is_empty() ? "<unknown>"sv : function_name.view()));
  109. }
  110. }
  111. return stack_string_builder.to_string();
  112. }
  113. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  114. NonnullGCPtr<ClassName> ClassName::create(Realm& realm) \
  115. { \
  116. return realm.heap().allocate<ClassName>(realm, realm.intrinsics().snake_name##_prototype()).release_allocated_value_but_fixme_should_propagate_errors(); \
  117. } \
  118. \
  119. NonnullGCPtr<ClassName> ClassName::create(Realm& realm, String message) \
  120. { \
  121. auto& vm = realm.vm(); \
  122. auto error = ClassName::create(realm); \
  123. u8 attr = Attribute::Writable | Attribute::Configurable; \
  124. error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr); \
  125. return error; \
  126. } \
  127. \
  128. ThrowCompletionOr<NonnullGCPtr<ClassName>> ClassName::create(Realm& realm, StringView message) \
  129. { \
  130. return create(realm, TRY_OR_THROW_OOM(realm.vm(), String::from_utf8(message))); \
  131. } \
  132. \
  133. ClassName::ClassName(Object& prototype) \
  134. : Error(prototype) \
  135. { \
  136. }
  137. JS_ENUMERATE_NATIVE_ERRORS
  138. #undef __JS_ENUMERATE
  139. }