Error.cpp 8.9 KB

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