EvaluateExpressionDialog.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "EvaluateExpressionDialog.h"
  7. #include "DebuggerGlobalJSObject.h"
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/TextBox.h>
  11. #include <LibGUI/Widget.h>
  12. #include <LibGfx/FontDatabase.h>
  13. #include <LibJS/Interpreter.h>
  14. #include <LibJS/MarkupGenerator.h>
  15. #include <LibJS/Parser.h>
  16. #include <LibJS/SyntaxHighlighter.h>
  17. #include <LibWeb/DOM/DocumentType.h>
  18. namespace HackStudio {
  19. static JS::VM& global_vm()
  20. {
  21. static RefPtr<JS::VM> vm;
  22. if (!vm)
  23. vm = JS::VM::create();
  24. return *vm;
  25. }
  26. EvaluateExpressionDialog::EvaluateExpressionDialog(Window* parent_window)
  27. : Dialog(parent_window)
  28. , m_interpreter(JS::Interpreter::create<DebuggerGlobalJSObject>(global_vm()))
  29. {
  30. set_title("Evaluate Expression");
  31. set_icon(parent_window->icon());
  32. build(parent_window);
  33. }
  34. void EvaluateExpressionDialog::build(Window* parent_window)
  35. {
  36. auto& widget = set_main_widget<GUI::Widget>();
  37. int width = max(parent_window->width() / 2, 150);
  38. int height = max(parent_window->height() * (2 / 3), 350);
  39. set_rect(x(), y(), width, height);
  40. widget.set_layout<GUI::VerticalBoxLayout>();
  41. widget.set_fill_with_background_color(true);
  42. widget.layout()->set_margins(6);
  43. widget.layout()->set_spacing(6);
  44. m_text_editor = widget.add<GUI::TextBox>();
  45. m_text_editor->set_fixed_height(19);
  46. m_text_editor->set_syntax_highlighter(make<JS::SyntaxHighlighter>());
  47. m_text_editor->set_font(Gfx::FontDatabase::default_fixed_width_font());
  48. m_text_editor->set_history_enabled(true);
  49. auto base_document = Web::DOM::Document::create();
  50. base_document->append_child(adopt_ref(*new Web::DOM::DocumentType(base_document)));
  51. auto html_element = base_document->create_element("html");
  52. base_document->append_child(html_element);
  53. auto head_element = base_document->create_element("head");
  54. html_element->append_child(head_element);
  55. auto body_element = base_document->create_element("body");
  56. html_element->append_child(body_element);
  57. m_output_container = body_element;
  58. m_output_view = widget.add<Web::InProcessWebView>();
  59. m_output_view->set_document(base_document);
  60. auto& button_container_outer = widget.add<GUI::Widget>();
  61. button_container_outer.set_fixed_height(20);
  62. button_container_outer.set_layout<GUI::VerticalBoxLayout>();
  63. auto& button_container_inner = button_container_outer.add<GUI::Widget>();
  64. button_container_inner.set_layout<GUI::HorizontalBoxLayout>();
  65. button_container_inner.layout()->set_spacing(6);
  66. button_container_inner.layout()->set_margins({ 4, 0, 4 });
  67. button_container_inner.layout()->add_spacer();
  68. m_evaluate_button = button_container_inner.add<GUI::Button>();
  69. m_evaluate_button->set_fixed_height(20);
  70. m_evaluate_button->set_text("Evaluate");
  71. m_evaluate_button->on_click = [this](auto) {
  72. handle_evaluation(m_text_editor->text());
  73. };
  74. m_close_button = button_container_inner.add<GUI::Button>();
  75. m_close_button->set_fixed_height(20);
  76. m_close_button->set_text("Close");
  77. m_close_button->on_click = [this](auto) {
  78. done(ExecOK);
  79. };
  80. m_text_editor->on_return_pressed = [this] {
  81. m_evaluate_button->click();
  82. };
  83. m_text_editor->on_escape_pressed = [this] {
  84. m_close_button->click();
  85. };
  86. m_text_editor->set_focus(true);
  87. }
  88. void EvaluateExpressionDialog::handle_evaluation(const String& expression)
  89. {
  90. m_output_container->remove_all_children();
  91. m_output_view->update();
  92. auto parser = JS::Parser(JS::Lexer(expression));
  93. auto program = parser.parse_program();
  94. StringBuilder output_html;
  95. if (parser.has_errors()) {
  96. auto error = parser.errors()[0];
  97. auto hint = error.source_location_hint(expression);
  98. if (!hint.is_empty())
  99. output_html.append(String::formatted("<pre>{}</pre>", escape_html_entities(hint)));
  100. m_interpreter->vm().throw_exception<JS::SyntaxError>(m_interpreter->global_object(), error.to_string());
  101. } else {
  102. m_interpreter->run(m_interpreter->global_object(), *program);
  103. }
  104. if (m_interpreter->exception()) {
  105. auto* exception = m_interpreter->exception();
  106. m_interpreter->vm().clear_exception();
  107. output_html.append("Uncaught exception: ");
  108. auto error = exception->value();
  109. if (error.is_object())
  110. output_html.append(JS::MarkupGenerator::html_from_error(error.as_object()));
  111. else
  112. output_html.append(JS::MarkupGenerator::html_from_value(error));
  113. set_output(output_html.string_view());
  114. return;
  115. }
  116. set_output(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
  117. }
  118. void EvaluateExpressionDialog::set_output(const StringView& html)
  119. {
  120. auto paragraph = m_output_container->document().create_element("p");
  121. paragraph->set_inner_html(html);
  122. m_output_container->append_child(paragraph);
  123. m_output_container->document().invalidate_layout();
  124. m_output_container->document().update_layout();
  125. }
  126. }