2020-05-23 19:53:09 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-23 19:53:09 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ConsoleWidget.h"
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2020-05-29 23:00:18 +03:00
|
|
|
#include <LibGUI/Button.h>
|
2020-05-23 19:53:09 +03:00
|
|
|
#include <LibGUI/TextBox.h>
|
2020-12-29 20:25:13 +03:00
|
|
|
#include <LibGfx/FontDatabase.h>
|
2020-05-23 19:53:09 +03:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-05-25 22:24:46 +03:00
|
|
|
#include <LibJS/MarkupGenerator.h>
|
2020-05-23 19:53:09 +03:00
|
|
|
#include <LibJS/Parser.h>
|
|
|
|
#include <LibJS/Runtime/Error.h>
|
2021-02-07 18:56:02 +03:00
|
|
|
#include <LibJS/SyntaxHighlighter.h>
|
2021-02-19 21:15:14 +03:00
|
|
|
#include <LibWeb/Bindings/DOMExceptionWrapper.h>
|
2020-05-23 19:53:09 +03:00
|
|
|
#include <LibWeb/DOM/DocumentType.h>
|
|
|
|
#include <LibWeb/DOM/Text.h>
|
|
|
|
#include <LibWeb/DOMTreeModel.h>
|
|
|
|
|
|
|
|
namespace Browser {
|
|
|
|
|
|
|
|
ConsoleWidget::ConsoleWidget()
|
|
|
|
{
|
|
|
|
set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
set_fill_with_background_color(true);
|
|
|
|
|
2020-10-23 09:31:26 +03:00
|
|
|
auto base_document = Web::DOM::Document::create();
|
2021-04-23 17:46:57 +03:00
|
|
|
base_document->append_child(adopt_ref(*new Web::DOM::DocumentType(base_document)));
|
2020-10-10 04:48:05 +03:00
|
|
|
auto html_element = base_document->create_element("html");
|
2020-05-23 19:53:09 +03:00
|
|
|
base_document->append_child(html_element);
|
2020-10-10 04:48:05 +03:00
|
|
|
auto head_element = base_document->create_element("head");
|
2020-05-23 19:53:09 +03:00
|
|
|
html_element->append_child(head_element);
|
2020-10-10 04:48:05 +03:00
|
|
|
auto body_element = base_document->create_element("body");
|
2020-05-23 19:53:09 +03:00
|
|
|
html_element->append_child(body_element);
|
2020-05-27 05:18:25 +03:00
|
|
|
m_output_container = body_element;
|
2020-05-23 19:53:09 +03:00
|
|
|
|
2020-08-17 16:58:29 +03:00
|
|
|
m_output_view = add<Web::InProcessWebView>();
|
2020-05-27 05:18:25 +03:00
|
|
|
m_output_view->set_document(base_document);
|
2020-05-23 19:53:09 +03:00
|
|
|
|
2020-05-29 23:00:18 +03:00
|
|
|
auto& bottom_container = add<GUI::Widget>();
|
|
|
|
bottom_container.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-30 03:23:32 +03:00
|
|
|
bottom_container.set_fixed_height(22);
|
2020-05-29 23:00:18 +03:00
|
|
|
|
|
|
|
m_input = bottom_container.add<GUI::TextBox>();
|
2021-02-07 18:56:02 +03:00
|
|
|
m_input->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
|
2020-05-23 19:53:09 +03:00
|
|
|
// FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
|
2020-12-29 20:25:13 +03:00
|
|
|
m_input->set_font(Gfx::FontDatabase::default_fixed_width_font());
|
2020-05-27 05:18:25 +03:00
|
|
|
m_input->set_history_enabled(true);
|
2020-05-23 19:53:09 +03:00
|
|
|
|
2020-05-27 05:18:25 +03:00
|
|
|
m_input->on_return_pressed = [this] {
|
|
|
|
auto js_source = m_input->text();
|
2020-05-25 04:35:46 +03:00
|
|
|
|
|
|
|
// FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
|
|
|
|
if (js_source.is_empty())
|
|
|
|
return;
|
|
|
|
|
2020-05-27 05:18:25 +03:00
|
|
|
m_input->add_current_text_to_history();
|
|
|
|
m_input->clear();
|
|
|
|
|
2020-05-23 19:53:09 +03:00
|
|
|
print_source_line(js_source);
|
|
|
|
|
2021-02-28 06:53:20 +03:00
|
|
|
if (on_js_input)
|
|
|
|
on_js_input(js_source);
|
|
|
|
|
|
|
|
// no interpreter being set means we are running in multi-process mode
|
|
|
|
if (!m_interpreter)
|
|
|
|
return;
|
|
|
|
|
2020-05-23 19:53:09 +03:00
|
|
|
auto parser = JS::Parser(JS::Lexer(js_source));
|
|
|
|
auto program = parser.parse_program();
|
|
|
|
|
2020-05-26 15:13:12 +03:00
|
|
|
StringBuilder output_html;
|
2020-05-23 19:53:09 +03:00
|
|
|
if (parser.has_errors()) {
|
|
|
|
auto error = parser.errors()[0];
|
2020-05-26 15:13:12 +03:00
|
|
|
auto hint = error.source_location_hint(js_source);
|
|
|
|
if (!hint.is_empty())
|
2020-10-04 13:39:02 +03:00
|
|
|
output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
|
2020-09-27 16:18:55 +03:00
|
|
|
m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
|
2020-05-23 19:53:09 +03:00
|
|
|
} else {
|
2020-09-18 10:49:51 +03:00
|
|
|
m_interpreter->run(m_interpreter->global_object(), *program);
|
2020-05-23 19:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_interpreter->exception()) {
|
2021-04-12 01:08:28 +03:00
|
|
|
auto* exception = m_interpreter->exception();
|
|
|
|
m_interpreter->vm().clear_exception();
|
2020-05-23 19:53:09 +03:00
|
|
|
output_html.append("Uncaught exception: ");
|
2021-04-12 01:08:28 +03:00
|
|
|
auto error = exception->value();
|
|
|
|
if (error.is_object())
|
|
|
|
output_html.append(JS::MarkupGenerator::html_from_error(error.as_object()));
|
|
|
|
else
|
|
|
|
output_html.append(JS::MarkupGenerator::html_from_value(error));
|
2020-05-23 19:53:09 +03:00
|
|
|
print_html(output_html.string_view());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-27 16:18:55 +03:00
|
|
|
print_html(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
|
2020-05-23 19:53:09 +03:00
|
|
|
};
|
2020-05-29 23:00:18 +03:00
|
|
|
|
2020-10-30 12:58:27 +03:00
|
|
|
set_focus_proxy(m_input);
|
|
|
|
|
2020-05-29 23:00:18 +03:00
|
|
|
auto& clear_button = bottom_container.add<GUI::Button>();
|
2020-12-30 03:23:32 +03:00
|
|
|
clear_button.set_fixed_size(22, 22);
|
2021-07-21 19:02:15 +03:00
|
|
|
clear_button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png"));
|
2020-05-29 23:00:18 +03:00
|
|
|
clear_button.set_tooltip("Clear the console output");
|
|
|
|
clear_button.on_click = [this](auto) {
|
|
|
|
clear_output();
|
|
|
|
};
|
2020-05-23 19:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleWidget::~ConsoleWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-28 06:53:20 +03:00
|
|
|
void ConsoleWidget::handle_js_console_output(const String& method, const String& line)
|
|
|
|
{
|
|
|
|
if (method == "html") {
|
|
|
|
print_html(line);
|
|
|
|
} else if (method == "clear") {
|
|
|
|
clear_output();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 19:53:09 +03:00
|
|
|
void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
|
|
|
|
{
|
|
|
|
if (m_interpreter.ptr() == interpreter.ptr())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_interpreter = interpreter;
|
2020-09-29 22:15:06 +03:00
|
|
|
m_console_client = make<BrowserConsoleClient>(interpreter->global_object().console(), *this);
|
|
|
|
interpreter->global_object().console().set_client(*m_console_client.ptr());
|
2020-05-23 19:53:09 +03:00
|
|
|
|
|
|
|
clear_output();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::print_source_line(const StringView& source)
|
|
|
|
{
|
|
|
|
StringBuilder html;
|
2020-05-25 04:35:46 +03:00
|
|
|
html.append("<span class=\"repl-indicator\">");
|
2020-05-23 19:53:09 +03:00
|
|
|
html.append("> ");
|
2020-05-25 04:35:46 +03:00
|
|
|
html.append("</span>");
|
|
|
|
|
2020-05-25 18:46:10 +03:00
|
|
|
html.append(JS::MarkupGenerator::html_from_source(source));
|
2020-05-23 19:53:09 +03:00
|
|
|
|
|
|
|
print_html(html.string_view());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::print_html(const StringView& line)
|
|
|
|
{
|
2020-10-10 04:48:05 +03:00
|
|
|
auto paragraph = m_output_container->document().create_element("p");
|
2020-05-23 19:53:09 +03:00
|
|
|
paragraph->set_inner_html(line);
|
|
|
|
|
2020-05-27 05:18:25 +03:00
|
|
|
m_output_container->append_child(paragraph);
|
|
|
|
m_output_container->document().invalidate_layout();
|
|
|
|
m_output_container->document().update_layout();
|
2020-05-25 04:35:46 +03:00
|
|
|
|
2020-05-27 05:18:25 +03:00
|
|
|
m_output_view->scroll_to_bottom();
|
2020-05-23 19:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleWidget::clear_output()
|
|
|
|
{
|
2020-05-29 23:00:18 +03:00
|
|
|
m_output_container->remove_all_children();
|
|
|
|
m_output_view->update();
|
2020-05-23 19:53:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|