Error.cpp 8.1 KB

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