Skip to content

Truly independent web browser

2.4 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibJS/EmbeddingABIConversions.h>
#include <LibJS/Runtime/ErrorData.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/ScriptAndModuleABIConversions.h>

namespace JS {

using namespace EmbeddingABI;

static JSErrorData const* error_data_to_abi(ErrorData const& error_data)
{
    return reinterpret_cast<JSErrorData const*>(&error_data);
}

SourceRange TracebackFrame::source_range() const
{
    if (!cached_source_range.has_value())
        return SourceRange { SourceCode::create({}, Utf16String {}), {} };
    return *cached_source_range;
}

Utf16String ErrorData::stack_string(CompactTraceback compact) const
{
    return owned_utf16_string_from_abi(js_error_data_stack_string(error_data_to_abi(*this), compact == CompactTraceback::Yes));
}

static Vector<TracebackFrame, 32> innermost_traceback_frames(JSErrorData const* error_data, size_t frame_count)
{
    Vector<TracebackFrame, 32> traceback;
    traceback.ensure_capacity(frame_count);
    for (size_t index = 0; index < frame_count; ++index) {
        JSTracebackFrame frame;
        js_error_data_traceback_frame(error_data, index, &frame);

        Optional<SourceRange> source_range;
        if (frame.has_source_range)
            source_range = SourceRange { source_code_from_abi(frame.source_code).release_nonnull(), { frame.line, frame.column } };
        traceback.unchecked_append({ Utf16String::from_utf16(utf16_view_from_abi(frame.function_name)), move(source_range) });
    }
    return traceback;
}

Vector<TracebackFrame, 32> ErrorData::traceback() const
{
    auto const* error_data = error_data_to_abi(*this);
    return innermost_traceback_frames(error_data, js_error_data_traceback_length(error_data));
}

Vector<TracebackFrame, 32> ErrorData::script_visible_traceback() const
{
    auto const* error_data = error_data_to_abi(*this);
    return innermost_traceback_frames(error_data, js_error_data_script_visible_traceback_length(error_data));
}

GC::Ref<ErrorDataCell> ErrorDataCell::capture(VM& vm)
{
    auto* cell = js_error_data_cell_capture(vm_to_abi(vm));
    VERIFY(cell);
    return cell_ref_from_abi<ErrorDataCell>(cell);
}

GC::Ref<ErrorDataCell> ErrorDataCell::capture_whole_stack(VM& vm)
{
    auto* cell = js_error_data_cell_capture_whole_stack(vm_to_abi(vm));
    VERIFY(cell);
    return cell_ref_from_abi<ErrorDataCell>(cell);
}

}