2020-03-24 13:37:39 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2023-04-12 22:47:15 +00:00
|
|
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
2020-03-24 13:37:39 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-24 13:37:39 +00:00
|
|
|
*/
|
|
|
|
|
2023-09-06 12:18:01 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2022-02-06 16:00:28 +00:00
|
|
|
#include <LibJS/AST.h>
|
2021-10-02 15:03:45 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2022-02-06 16:00:28 +00:00
|
|
|
#include <LibJS/Runtime/ExecutionContext.h>
|
2020-04-17 17:31:48 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2022-02-06 16:00:28 +00:00
|
|
|
#include <LibJS/SourceRange.h>
|
2020-03-24 13:37:39 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(Error);
|
2023-11-19 08:45:05 +00:00
|
|
|
|
2024-10-14 05:49:12 +00:00
|
|
|
static SourceRange dummy_source_range { SourceCode::create(String {}, String {}), {}, {} };
|
|
|
|
|
2023-05-28 06:28:43 +00:00
|
|
|
SourceRange const& TracebackFrame::source_range() const
|
|
|
|
{
|
2024-10-14 05:49:12 +00:00
|
|
|
if (!cached_source_range)
|
|
|
|
return dummy_source_range;
|
|
|
|
if (auto* unrealized = cached_source_range->source_range.get_pointer<UnrealizedSourceRange>()) {
|
2023-05-28 12:14:14 +00:00
|
|
|
auto source_range = [&] {
|
2024-10-14 05:49:12 +00:00
|
|
|
if (!unrealized->source_code)
|
|
|
|
return dummy_source_range;
|
2023-07-27 12:40:01 +00:00
|
|
|
return unrealized->realize();
|
2023-05-28 12:14:14 +00:00
|
|
|
}();
|
2024-10-14 05:49:12 +00:00
|
|
|
cached_source_range->source_range = move(source_range);
|
2023-05-28 06:28:43 +00:00
|
|
|
}
|
2024-10-14 05:49:12 +00:00
|
|
|
return cached_source_range->source_range.get<SourceRange>();
|
2023-05-28 06:28:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm)
|
2021-06-11 19:40:08 +00:00
|
|
|
{
|
2024-11-13 16:50:17 +00:00
|
|
|
return realm.create<Error>(realm.intrinsics().error_prototype());
|
2021-06-11 19:40:08 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm, String message)
|
2020-04-17 17:31:48 +00:00
|
|
|
{
|
2022-08-15 23:20:49 +00:00
|
|
|
auto& vm = realm.vm();
|
2022-12-13 20:49:50 +00:00
|
|
|
auto error = Error::create(realm);
|
2021-06-11 19:40:08 +00:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2023-02-16 19:09:11 +00:00
|
|
|
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr);
|
2021-04-11 22:08:28 +00:00
|
|
|
return error;
|
2020-04-17 17:31:48 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<Error> Error::create(Realm& realm, StringView message)
|
2023-02-16 19:09:11 +00:00
|
|
|
{
|
2023-09-06 12:18:01 +00:00
|
|
|
return create(realm, MUST(String::from_utf8(message)));
|
2023-02-16 19:09:11 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 22:08:28 +00:00
|
|
|
Error::Error(Object& prototype)
|
2022-12-14 11:17:58 +00:00
|
|
|
: Object(ConstructWithPrototypeTag::Tag, prototype)
|
2020-03-24 13:37:39 +00:00
|
|
|
{
|
2022-02-06 16:00:28 +00:00
|
|
|
populate_stack();
|
2020-03-24 13:37:39 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:34:06 +00:00
|
|
|
// 20.5.8.1 InstallErrorCause ( O, options ), https://tc39.es/ecma262/#sec-installerrorcause
|
2021-10-02 15:03:45 +00:00
|
|
|
ThrowCompletionOr<void> Error::install_error_cause(Value options)
|
2021-06-26 18:06:55 +00:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2021-10-02 15:03:45 +00:00
|
|
|
|
|
|
|
// 1. If Type(options) is Object and ? HasProperty(options, "cause") is true, then
|
2021-10-03 01:00:39 +00:00
|
|
|
if (options.is_object() && TRY(options.as_object().has_property(vm.names.cause))) {
|
2021-10-02 15:03:45 +00:00
|
|
|
// a. Let cause be ? Get(options, "cause").
|
2021-10-02 22:52:27 +00:00
|
|
|
auto cause = TRY(options.as_object().get(vm.names.cause));
|
2021-10-02 15:03:45 +00:00
|
|
|
|
2022-05-02 18:54:39 +00:00
|
|
|
// b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
|
|
|
|
create_non_enumerable_data_property_or_throw(vm.names.cause, cause);
|
2021-10-02 15:03:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:54:39 +00:00
|
|
|
// 2. Return unused.
|
2021-10-02 15:03:45 +00:00
|
|
|
return {};
|
2021-06-26 18:06:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 16:00:28 +00:00
|
|
|
void Error::populate_stack()
|
|
|
|
{
|
2023-10-31 23:39:28 +00:00
|
|
|
auto stack_trace = vm().stack_trace();
|
|
|
|
m_traceback.ensure_capacity(stack_trace.size());
|
|
|
|
for (auto& element : stack_trace) {
|
|
|
|
auto* context = element.execution_context;
|
2023-05-28 06:28:43 +00:00
|
|
|
TracebackFrame frame {
|
2023-12-16 14:19:34 +00:00
|
|
|
.function_name = context->function_name ? context->function_name->byte_string() : "",
|
2024-10-14 05:49:12 +00:00
|
|
|
.cached_source_range = element.source_range,
|
2023-05-28 06:28:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
m_traceback.append(move(frame));
|
2022-02-07 13:48:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-06 16:00:28 +00:00
|
|
|
|
2023-10-31 20:55:17 +00:00
|
|
|
String Error::stack_string(CompactTraceback compact) const
|
2022-02-07 13:48:09 +00:00
|
|
|
{
|
2023-11-02 15:11:33 +00:00
|
|
|
if (m_traceback.is_empty())
|
|
|
|
return {};
|
|
|
|
|
2023-09-06 12:18:01 +00:00
|
|
|
StringBuilder stack_string_builder;
|
2023-02-16 19:09:11 +00:00
|
|
|
|
2022-02-06 16:00:28 +00:00
|
|
|
// Note: We roughly follow V8's formatting
|
2023-10-31 20:55:17 +00:00
|
|
|
auto append_frame = [&](TracebackFrame const& frame) {
|
2022-02-07 13:48:09 +00:00
|
|
|
auto function_name = frame.function_name;
|
2023-05-28 06:28:43 +00:00
|
|
|
auto source_range = frame.source_range();
|
2022-02-07 13:48:09 +00:00
|
|
|
// Note: Since we don't know whether we have a valid SourceRange here we just check for some default values.
|
2023-05-28 06:28:43 +00:00
|
|
|
if (!source_range.filename().is_empty() || source_range.start.offset != 0 || source_range.end.offset != 0) {
|
2022-02-06 16:00:28 +00:00
|
|
|
|
2023-10-31 20:55:17 +00:00
|
|
|
if (function_name.is_empty())
|
2023-09-06 12:18:01 +00:00
|
|
|
stack_string_builder.appendff(" at {}:{}:{}\n", source_range.filename(), source_range.start.line, source_range.start.column);
|
2022-02-06 16:00:28 +00:00
|
|
|
else
|
2023-09-06 12:18:01 +00:00
|
|
|
stack_string_builder.appendff(" at {} ({}:{}:{})\n", function_name, source_range.filename(), source_range.start.line, source_range.start.column);
|
2022-02-06 16:00:28 +00:00
|
|
|
} else {
|
2023-09-06 12:18:01 +00:00
|
|
|
stack_string_builder.appendff(" at {}\n", function_name.is_empty() ? "<unknown>"sv : function_name.view());
|
2022-02-06 16:00:28 +00:00
|
|
|
}
|
2023-10-31 20:55:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto is_same_frame = [](TracebackFrame const& a, TracebackFrame const& b) {
|
|
|
|
if (a.function_name.is_empty() && b.function_name.is_empty()) {
|
|
|
|
auto source_range_a = a.source_range();
|
|
|
|
auto source_range_b = b.source_range();
|
|
|
|
return source_range_a.filename() == source_range_b.filename() && source_range_a.start.line == source_range_b.start.line;
|
|
|
|
}
|
|
|
|
return a.function_name == b.function_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: We don't want to capture the global execution context, so we omit the last frame
|
|
|
|
// Note: The error's name and message get prepended by ErrorPrototype::stack
|
|
|
|
// FIXME: We generate a stack-frame for the Errors constructor, other engines do not
|
|
|
|
unsigned repetitions = 0;
|
|
|
|
size_t used_frames = m_traceback.size() - 1;
|
|
|
|
for (size_t i = 0; i < used_frames; ++i) {
|
|
|
|
auto const& frame = m_traceback[i];
|
|
|
|
if (compact == CompactTraceback::Yes && i + 1 < used_frames) {
|
|
|
|
auto const& next_traceback_frame = m_traceback[i + 1];
|
|
|
|
if (is_same_frame(frame, next_traceback_frame)) {
|
|
|
|
repetitions++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (repetitions > 4) {
|
|
|
|
// If more than 5 (1 + >4) consecutive function calls with the same name, print
|
|
|
|
// the name only once and show the number of repetitions instead. This prevents
|
|
|
|
// printing ridiculously large call stacks of recursive functions.
|
|
|
|
append_frame(frame);
|
|
|
|
stack_string_builder.appendff(" {} more calls\n", repetitions);
|
|
|
|
} else {
|
|
|
|
for (size_t j = 0; j < repetitions + 1; j++)
|
|
|
|
append_frame(frame);
|
|
|
|
}
|
|
|
|
repetitions = 0;
|
2022-02-06 16:00:28 +00:00
|
|
|
}
|
2023-10-31 20:55:17 +00:00
|
|
|
for (size_t j = 0; j < repetitions; j++)
|
|
|
|
append_frame(m_traceback[used_frames - 1]);
|
2022-02-06 16:00:28 +00:00
|
|
|
|
2023-09-06 12:18:01 +00:00
|
|
|
return MUST(stack_string_builder.to_string());
|
2022-02-06 16:00:28 +00:00
|
|
|
}
|
|
|
|
|
2023-08-13 11:05:26 +00:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(ClassName); \
|
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm) \
|
2023-08-13 11:05:26 +00:00
|
|
|
{ \
|
2024-11-13 16:50:17 +00:00
|
|
|
return realm.create<ClassName>(realm.intrinsics().snake_name##_prototype()); \
|
2023-08-13 11:05:26 +00:00
|
|
|
} \
|
|
|
|
\
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm, String message) \
|
2023-08-13 11:05:26 +00:00
|
|
|
{ \
|
|
|
|
auto& vm = realm.vm(); \
|
|
|
|
auto error = ClassName::create(realm); \
|
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable; \
|
|
|
|
error->define_direct_property(vm.names.message, PrimitiveString::create(vm, move(message)), attr); \
|
|
|
|
return error; \
|
|
|
|
} \
|
|
|
|
\
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<ClassName> ClassName::create(Realm& realm, StringView message) \
|
2023-08-13 11:05:26 +00:00
|
|
|
{ \
|
2023-09-06 12:18:01 +00:00
|
|
|
return create(realm, MUST(String::from_utf8(message))); \
|
2023-08-13 11:05:26 +00:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
ClassName::ClassName(Object& prototype) \
|
|
|
|
: Error(prototype) \
|
|
|
|
{ \
|
2021-04-11 22:08:28 +00:00
|
|
|
}
|
2020-04-10 10:42:33 +00:00
|
|
|
|
2021-06-11 16:54:42 +00:00
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2020-04-10 12:06:52 +00:00
|
|
|
#undef __JS_ENUMERATE
|
2020-04-10 10:42:33 +00:00
|
|
|
|
2020-03-24 13:37:39 +00:00
|
|
|
}
|