Error.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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(Realm& realm)
  15. {
  16. return realm.heap().allocate<Error>(realm, *realm.intrinsics().error_prototype());
  17. }
  18. Error* Error::create(Realm& realm, String const& message)
  19. {
  20. auto& vm = realm.vm();
  21. auto* error = Error::create(realm);
  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. create_non_enumerable_data_property_or_throw(vm.names.cause, cause);
  41. }
  42. // 2. Return unused.
  43. return {};
  44. }
  45. void Error::populate_stack()
  46. {
  47. static auto dummy_source_range = SourceRange { .code = SourceCode::create("", ""), .start = {}, .end = {} };
  48. auto& vm = this->vm();
  49. m_traceback.ensure_capacity(vm.execution_context_stack().size());
  50. for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
  51. auto* context = vm.execution_context_stack()[i];
  52. auto function_name = context->function_name;
  53. if (function_name.is_empty())
  54. function_name = "<unknown>"sv;
  55. m_traceback.empend(
  56. move(function_name),
  57. // We might not have an AST node associated with the execution context, e.g. in promise
  58. // reaction jobs (which aren't called anywhere from the source code).
  59. // They're not going to generate any _unhandled_ exceptions though, so a meaningless
  60. // source range is fine.
  61. context->current_node ? context->current_node->source_range() : dummy_source_range);
  62. }
  63. }
  64. String Error::stack_string() const
  65. {
  66. StringBuilder stack_string_builder;
  67. // Note: We roughly follow V8's formatting
  68. // Note: The error's name and message get prepended by ErrorPrototype::stack
  69. // Note: We don't want to capture the global execution context, so we omit the last frame
  70. // FIXME: We generate a stack-frame for the Errors constructor, other engines do not
  71. for (size_t i = 0; i < m_traceback.size() - 1; ++i) {
  72. auto const& frame = m_traceback[i];
  73. auto function_name = frame.function_name;
  74. // Note: Since we don't know whether we have a valid SourceRange here we just check for some default values.
  75. if (!frame.source_range.filename().is_null() || frame.source_range.start.offset != 0 || frame.source_range.end.offset != 0) {
  76. if (function_name == "<unknown>"sv)
  77. stack_string_builder.appendff(" at {}:{}:{}\n", frame.source_range.filename(), frame.source_range.start.line, frame.source_range.start.column);
  78. else
  79. stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, frame.source_range.filename(), frame.source_range.start.line, frame.source_range.start.column);
  80. } else {
  81. stack_string_builder.appendff(" at {}\n", function_name.is_empty() ? "<unknown>"sv : function_name.view());
  82. }
  83. }
  84. return stack_string_builder.build();
  85. }
  86. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  87. ClassName* ClassName::create(Realm& realm) \
  88. { \
  89. return realm.heap().allocate<ClassName>(realm, *realm.intrinsics().snake_name##_prototype()); \
  90. } \
  91. \
  92. ClassName* ClassName::create(Realm& realm, String const& message) \
  93. { \
  94. auto& vm = realm.vm(); \
  95. auto* error = ClassName::create(realm); \
  96. u8 attr = Attribute::Writable | Attribute::Configurable; \
  97. error->define_direct_property(vm.names.message, js_string(vm, message), attr); \
  98. return error; \
  99. } \
  100. \
  101. ClassName::ClassName(Object& prototype) \
  102. : Error(prototype) \
  103. { \
  104. }
  105. JS_ENUMERATE_NATIVE_ERRORS
  106. #undef __JS_ENUMERATE
  107. }