ConsoleWidget.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2020, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ConsoleWidget.h"
  27. #include <AK/StringBuilder.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/JSSyntaxHighlighter.h>
  30. #include <LibGUI/TextBox.h>
  31. #include <LibJS/Interpreter.h>
  32. #include <LibJS/Parser.h>
  33. #include <LibJS/Runtime/Array.h>
  34. #include <LibJS/Runtime/Date.h>
  35. #include <LibJS/Runtime/Error.h>
  36. #include <LibWeb/DOM/DocumentType.h>
  37. #include <LibWeb/DOM/ElementFactory.h>
  38. #include <LibWeb/DOM/HTMLBodyElement.h>
  39. #include <LibWeb/DOM/Text.h>
  40. #include <LibWeb/DOMTreeModel.h>
  41. namespace Browser {
  42. ConsoleWidget::ConsoleWidget()
  43. {
  44. set_layout<GUI::VerticalBoxLayout>();
  45. set_fill_with_background_color(true);
  46. auto base_document = adopt(*new Web::Document);
  47. base_document->append_child(adopt(*new Web::DocumentType(base_document)));
  48. auto html_element = create_element(base_document, "html");
  49. base_document->append_child(html_element);
  50. auto head_element = create_element(base_document, "head");
  51. html_element->append_child(head_element);
  52. auto style_element = create_element(base_document, "style");
  53. style_element->append_child(adopt(*new Web::Text(base_document, "div { font-family: Csilla; font-weight: lighter; }")));
  54. head_element->append_child(style_element);
  55. auto body_element = create_element(base_document, "body");
  56. html_element->append_child(body_element);
  57. m_console_output_container = body_element;
  58. m_console_output_view = add<Web::HtmlView>();
  59. m_console_output_view->set_document(base_document);
  60. m_console_input = add<GUI::TextBox>();
  61. m_console_input->set_syntax_highlighter(make<GUI::JSSyntaxHighlighter>());
  62. m_console_input->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  63. m_console_input->set_preferred_size(0, 22);
  64. // FIXME: Syntax Highlighting breaks the cursor's position on non fixed-width fonts.
  65. m_console_input->set_font(Gfx::Font::default_fixed_width_font());
  66. m_console_input->on_return_pressed = [this] {
  67. auto js_source = m_console_input->text();
  68. m_console_input->clear();
  69. print_source_line(js_source);
  70. auto parser = JS::Parser(JS::Lexer(js_source));
  71. auto program = parser.parse_program();
  72. if (parser.has_errors()) {
  73. auto error = parser.errors()[0];
  74. m_interpreter->throw_exception<JS::SyntaxError>(error.to_string());
  75. } else {
  76. m_interpreter->run(*program);
  77. }
  78. if (m_interpreter->exception()) {
  79. StringBuilder output_html;
  80. output_html.append("Uncaught exception: ");
  81. print_value(m_interpreter->exception()->value(), output_html);
  82. print_html(output_html.string_view());
  83. m_interpreter->clear_exception();
  84. return;
  85. }
  86. StringBuilder output_html;
  87. print_value(m_interpreter->last_value(), output_html);
  88. print_html(output_html.string_view());
  89. };
  90. }
  91. ConsoleWidget::~ConsoleWidget()
  92. {
  93. }
  94. void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
  95. {
  96. if (m_interpreter.ptr() == interpreter.ptr())
  97. return;
  98. m_interpreter = interpreter;
  99. m_console_client = adopt_own(*new BrowserConsoleClient(interpreter->console(), *this));
  100. interpreter->console().set_client(*m_console_client.ptr());
  101. clear_output();
  102. }
  103. void ConsoleWidget::print_source_line(const StringView& source)
  104. {
  105. StringBuilder html;
  106. html.append("&gt; ");
  107. // FIXME: Support output highlighting
  108. html.append(source);
  109. print_html(html.string_view());
  110. }
  111. void ConsoleWidget::print_value(JS::Value value, StringBuilder& output_html, HashTable<JS::Object*> seen_objects)
  112. {
  113. // FIXME: Support output highlighting
  114. if (value.is_empty()) {
  115. output_html.append("&lt;empty&gt;");
  116. return;
  117. }
  118. if (value.is_object()) {
  119. if (seen_objects.contains(&value.as_object())) {
  120. // FIXME: Maybe we should only do this for circular references,
  121. // not for all reoccurring objects.
  122. output_html.appendf("&lt;already printed Object %p&gt;", &value.as_object());
  123. return;
  124. }
  125. seen_objects.set(&value.as_object());
  126. }
  127. if (value.is_array())
  128. return print_array(static_cast<const JS::Array&>(value.as_object()), output_html, seen_objects);
  129. if (value.is_object()) {
  130. auto& object = value.as_object();
  131. if (object.is_function())
  132. return print_function(object, output_html, seen_objects);
  133. if (object.is_date())
  134. return print_date(object, output_html, seen_objects);
  135. if (object.is_error())
  136. return print_error(object, output_html, seen_objects);
  137. return print_object(object, output_html, seen_objects);
  138. }
  139. if (value.is_string())
  140. output_html.append('"');
  141. output_html.append(value.to_string_without_side_effects());
  142. if (value.is_string())
  143. output_html.append('"');
  144. }
  145. void ConsoleWidget::print_array(const JS::Array& array, StringBuilder& html_output, HashTable<JS::Object*>& seen_objects)
  146. {
  147. html_output.append("[ ");
  148. for (size_t i = 0; i < array.elements().size(); ++i) {
  149. print_value(array.elements()[i], html_output, seen_objects);
  150. if (i != array.elements().size() - 1)
  151. html_output.append(", ");
  152. }
  153. html_output.append(" ]");
  154. }
  155. void ConsoleWidget::print_object(const JS::Object& object, StringBuilder& html_output, HashTable<JS::Object*>& seen_objects)
  156. {
  157. html_output.append("{ ");
  158. for (size_t i = 0; i < object.elements().size(); ++i) {
  159. if (object.elements()[i].is_empty())
  160. continue;
  161. html_output.appendf("\"m%zu\": ", i);
  162. print_value(object.elements()[i], html_output, seen_objects);
  163. if (i != object.elements().size() - 1)
  164. html_output.append(", ");
  165. }
  166. if (!object.elements().is_empty() && object.shape().property_count())
  167. html_output.append(", ");
  168. size_t index = 0;
  169. for (auto& it : object.shape().property_table_ordered()) {
  170. html_output.appendf("\"%s\": ", it.key.characters());
  171. print_value(object.get_direct(it.value.offset), html_output, seen_objects);
  172. if (index != object.shape().property_count() - 1)
  173. html_output.append(", ");
  174. ++index;
  175. }
  176. html_output.append(" }");
  177. }
  178. void ConsoleWidget::print_function(const JS::Object& function, StringBuilder& html_output, HashTable<JS::Object*>&)
  179. {
  180. html_output.appendf("[%s]", function.class_name());
  181. }
  182. void ConsoleWidget::print_date(const JS::Object& date, StringBuilder& html_output, HashTable<JS::Object*>&)
  183. {
  184. html_output.appendf("Date %s", static_cast<const JS::Date&>(date).string().characters());
  185. }
  186. void ConsoleWidget::print_error(const JS::Object& object, StringBuilder& html_output, HashTable<JS::Object*>&)
  187. {
  188. auto& error = static_cast<const JS::Error&>(object);
  189. html_output.appendf("[%s]", error.name().characters());
  190. if (!error.message().is_empty())
  191. html_output.appendf(": %s", error.message().characters());
  192. }
  193. void ConsoleWidget::print_html(const StringView& line)
  194. {
  195. auto paragraph = create_element(m_console_output_container->document(), "p");
  196. paragraph->set_inner_html(line);
  197. m_console_output_container->append_child(paragraph);
  198. m_console_output_container->set_needs_style_update(true);
  199. m_console_output_container->document().schedule_style_update();
  200. m_console_output_container->document().invalidate_layout();
  201. }
  202. void ConsoleWidget::clear_output()
  203. {
  204. const_cast<Web::HTMLBodyElement*>(m_console_output_view->document()->body())->remove_all_children();
  205. }
  206. }