1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
* 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, 6, 6, 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");
base_document->append_child(html_element);
auto head_element = base_document->create_element("head");
html_element->append_child(head_element);
auto body_element = base_document->create_element("body");
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, 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);
}
void EvaluateExpressionDialog::handle_evaluation(const String& expression)
{
m_output_container->remove_all_children();
m_output_view->update();
auto parser = JS::Parser(JS::Lexer(expression));
auto program = parser.parse_program();
StringBuilder output_html;
if (parser.has_errors()) {
auto error = parser.errors()[0];
auto hint = error.source_location_hint(expression);
if (!hint.is_empty())
output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
} else {
m_interpreter->run(m_interpreter->global_object(), *program);
}
if (m_interpreter->exception()) {
auto* exception = m_interpreter->exception();
m_interpreter->vm().clear_exception();
output_html.append("Uncaught exception: ");
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));
set_output(output_html.string_view());
return;
}
set_output(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
}
void EvaluateExpressionDialog::set_output(const StringView& html)
{
auto paragraph = m_output_container->document().create_element("p");
paragraph->set_inner_html(html);
m_output_container->append_child(paragraph);
m_output_container->document().invalidate_layout();
m_output_container->document().update_layout();
}
}
|