ladybird/Userland/DevTools/HackStudio/Debugger/EvaluateExpressionDialog.cpp

148 lines
5 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "EvaluateExpressionDialog.h"
#include "DebuggerGlobalJSObject.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Widget.h>
#include <LibGfx/FontDatabase.h>
#include <LibJS/Interpreter.h>
#include <LibJS/MarkupGenerator.h>
#include <LibJS/Parser.h>
#include <LibJS/SyntaxHighlighter.h>
#include <LibWeb/DOM/DocumentType.h>
namespace HackStudio {
static JS::VM& global_vm()
{
static RefPtr<JS::VM> vm;
if (!vm)
vm = JS::VM::create();
return *vm;
}
EvaluateExpressionDialog::EvaluateExpressionDialog(Window* parent_window)
: Dialog(parent_window)
, m_interpreter(JS::Interpreter::create<DebuggerGlobalJSObject>(global_vm()))
{
set_title("Evaluate Expression");
set_icon(parent_window->icon());
build(parent_window);
}
void EvaluateExpressionDialog::build(Window* parent_window)
{
auto& widget = set_main_widget<GUI::Widget>();
int width = max(parent_window->width() / 2, 150);
int height = max(parent_window->height() * (2 / 3), 350);
set_rect(x(), y(), width, height);
widget.set_layout<GUI::VerticalBoxLayout>();
widget.set_fill_with_background_color(true);
widget.layout()->set_margins(6);
widget.layout()->set_spacing(6);
m_text_editor = widget.add<GUI::TextBox>();
m_text_editor->set_fixed_height(19);
m_text_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
m_text_editor->set_font(Gfx::FontDatabase::default_fixed_width_font());
m_text_editor->set_history_enabled(true);
auto base_document = Web::DOM::Document::create();
base_document->append_child(adopt_ref(*new Web::DOM::DocumentType(base_document)));
auto html_element = base_document->create_element("html").release_value();
base_document->append_child(html_element);
auto head_element = base_document->create_element("head").release_value();
html_element->append_child(head_element);
auto body_element = base_document->create_element("body").release_value();
html_element->append_child(body_element);
m_output_container = body_element;
m_output_view = widget.add<Web::InProcessWebView>();
m_output_view->set_document(base_document);
auto& button_container_outer = widget.add<GUI::Widget>();
button_container_outer.set_fixed_height(20);
button_container_outer.set_layout<GUI::VerticalBoxLayout>();
auto& button_container_inner = button_container_outer.add<GUI::Widget>();
button_container_inner.set_layout<GUI::HorizontalBoxLayout>();
button_container_inner.layout()->set_spacing(6);
button_container_inner.layout()->set_margins({ 4, 0, 4 });
button_container_inner.layout()->add_spacer();
m_evaluate_button = button_container_inner.add<GUI::Button>();
m_evaluate_button->set_fixed_height(20);
m_evaluate_button->set_text("Evaluate");
m_evaluate_button->on_click = [this](auto) {
handle_evaluation(m_text_editor->text());
};
m_close_button = button_container_inner.add<GUI::Button>();
m_close_button->set_fixed_height(20);
m_close_button->set_text("Close");
m_close_button->on_click = [this](auto) {
done(ExecOK);
};
m_text_editor->on_return_pressed = [this] {
m_evaluate_button->click();
};
m_text_editor->on_escape_pressed = [this] {
m_close_button->click();
};
m_text_editor->set_focus(true);
}
2022-04-01 14:58:27 -03:00
void EvaluateExpressionDialog::handle_evaluation(String const& expression)
{
m_output_container->remove_all_children();
m_output_view->update();
LibJS: Refactor interpreter to use Script and Source Text Modules This also refactors interpreter creation to follow InitializeHostDefinedRealm, but I couldn't fit it in the title :^) This allows us to follow the spec much more closely rather than being completely ad-hoc with just the parse node instead of having all the surrounding data such as the realm of the parse node. The interpreter creation refactor creates the global execution context once and doesn't take it off the stack. This allows LibWeb to take the global execution context and manually handle it, following the HTML spec. The HTML spec calls this the "realm execution context" of the environment settings object. It also allows us to specify the globalThis type, as it can be different from the global object type. For example, on the web, Window global objects use a WindowProxy global this value to enforce the same origin policy on operations like [[GetOwnProperty]]. Finally, it allows us to directly call Program::execute in perform_eval and perform_shadow_realm_eval as this moves global_declaration_instantiation into Interpreter::run (ScriptEvaluation) as per the spec. Note that this doesn't evalulate Source Text Modules yet or refactor the bytecode interpreter, that's work for future us :^) This patch was originally build by Luke for the environment settings object change but was also needed for modules. So I (davidot) have modified it with the new completion changes and setup for that. Co-authored-by: davidot <davidot@serenityos.org>
2022-01-16 09:16:04 -03:00
auto script_or_error = JS::Script::parse(expression, m_interpreter->realm());
StringBuilder output_html;
auto result = JS::ThrowCompletionOr<JS::Value> { JS::js_undefined() };
LibJS: Refactor interpreter to use Script and Source Text Modules This also refactors interpreter creation to follow InitializeHostDefinedRealm, but I couldn't fit it in the title :^) This allows us to follow the spec much more closely rather than being completely ad-hoc with just the parse node instead of having all the surrounding data such as the realm of the parse node. The interpreter creation refactor creates the global execution context once and doesn't take it off the stack. This allows LibWeb to take the global execution context and manually handle it, following the HTML spec. The HTML spec calls this the "realm execution context" of the environment settings object. It also allows us to specify the globalThis type, as it can be different from the global object type. For example, on the web, Window global objects use a WindowProxy global this value to enforce the same origin policy on operations like [[GetOwnProperty]]. Finally, it allows us to directly call Program::execute in perform_eval and perform_shadow_realm_eval as this moves global_declaration_instantiation into Interpreter::run (ScriptEvaluation) as per the spec. Note that this doesn't evalulate Source Text Modules yet or refactor the bytecode interpreter, that's work for future us :^) This patch was originally build by Luke for the environment settings object change but was also needed for modules. So I (davidot) have modified it with the new completion changes and setup for that. Co-authored-by: davidot <davidot@serenityos.org>
2022-01-16 09:16:04 -03:00
if (script_or_error.is_error()) {
auto error = script_or_error.error()[0];
auto hint = error.source_location_hint(expression);
if (!hint.is_empty())
output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
result = m_interpreter->vm().throw_completion<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
} else {
LibJS: Refactor interpreter to use Script and Source Text Modules This also refactors interpreter creation to follow InitializeHostDefinedRealm, but I couldn't fit it in the title :^) This allows us to follow the spec much more closely rather than being completely ad-hoc with just the parse node instead of having all the surrounding data such as the realm of the parse node. The interpreter creation refactor creates the global execution context once and doesn't take it off the stack. This allows LibWeb to take the global execution context and manually handle it, following the HTML spec. The HTML spec calls this the "realm execution context" of the environment settings object. It also allows us to specify the globalThis type, as it can be different from the global object type. For example, on the web, Window global objects use a WindowProxy global this value to enforce the same origin policy on operations like [[GetOwnProperty]]. Finally, it allows us to directly call Program::execute in perform_eval and perform_shadow_realm_eval as this moves global_declaration_instantiation into Interpreter::run (ScriptEvaluation) as per the spec. Note that this doesn't evalulate Source Text Modules yet or refactor the bytecode interpreter, that's work for future us :^) This patch was originally build by Luke for the environment settings object change but was also needed for modules. So I (davidot) have modified it with the new completion changes and setup for that. Co-authored-by: davidot <davidot@serenityos.org>
2022-01-16 09:16:04 -03:00
result = m_interpreter->run(script_or_error.value());
}
if (result.is_error()) {
output_html.append("Uncaught exception: ");
auto error = *result.throw_completion().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));
set_output(output_html.string_view());
return;
}
set_output(JS::MarkupGenerator::html_from_value(result.value()));
}
void EvaluateExpressionDialog::set_output(StringView html)
{
auto paragraph = m_output_container->document().create_element("p").release_value();
paragraph->set_inner_html(html);
m_output_container->append_child(paragraph);
}
}