ConsoleWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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, create_document_style())));
  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. // FIXME: An is_blank check to check if there is only whitespace would probably be preferable.
  69. if (js_source.is_empty())
  70. return;
  71. m_console_input->clear();
  72. print_source_line(js_source);
  73. auto parser = JS::Parser(JS::Lexer(js_source));
  74. auto program = parser.parse_program();
  75. if (parser.has_errors()) {
  76. auto error = parser.errors()[0];
  77. m_interpreter->throw_exception<JS::SyntaxError>(error.to_string());
  78. } else {
  79. m_interpreter->run(*program);
  80. }
  81. if (m_interpreter->exception()) {
  82. StringBuilder output_html;
  83. output_html.append("Uncaught exception: ");
  84. print_value(m_interpreter->exception()->value(), output_html);
  85. print_html(output_html.string_view());
  86. m_interpreter->clear_exception();
  87. return;
  88. }
  89. StringBuilder output_html;
  90. print_value(m_interpreter->last_value(), output_html);
  91. print_html(output_html.string_view());
  92. };
  93. }
  94. ConsoleWidget::~ConsoleWidget()
  95. {
  96. }
  97. void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
  98. {
  99. if (m_interpreter.ptr() == interpreter.ptr())
  100. return;
  101. m_interpreter = interpreter;
  102. m_console_client = adopt_own(*new BrowserConsoleClient(interpreter->console(), *this));
  103. interpreter->console().set_client(*m_console_client.ptr());
  104. clear_output();
  105. }
  106. String ConsoleWidget::create_document_style()
  107. {
  108. StringBuilder style;
  109. auto palette = this->palette();
  110. auto add_class_and_color = [&](const StringView& class_name, Color color, bool bold = false) {
  111. style.append(".");
  112. style.append(class_name);
  113. style.append(" { color: ");
  114. style.append(color.to_string_without_alpha());
  115. style.append(";");
  116. if (bold) {
  117. style.append("font-weight: bold;");
  118. }
  119. style.append(" } ");
  120. };
  121. add_class_and_color("js-string", palette.syntax_string());
  122. add_class_and_color("js-number", palette.syntax_number());
  123. add_class_and_color("js-boolean", palette.syntax_keyword(), true);
  124. add_class_and_color("js-null", palette.syntax_keyword(), true);
  125. add_class_and_color("js-undefined", palette.syntax_keyword(), true);
  126. add_class_and_color("js-array-open", palette.syntax_punctuation());
  127. add_class_and_color("js-array-close", palette.syntax_punctuation());
  128. add_class_and_color("js-array-element-separator", palette.syntax_punctuation());
  129. add_class_and_color("js-object-open", palette.syntax_punctuation());
  130. add_class_and_color("js-object-close", palette.syntax_punctuation());
  131. add_class_and_color("js-object-element-separator", palette.syntax_punctuation());
  132. add_class_and_color("js-object-element-index", palette.syntax_number());
  133. add_class_and_color("js-object-element-key", palette.syntax_string());
  134. // FIXME: Add to palette?
  135. add_class_and_color("js-error-name", Color::Red, true);
  136. return style.to_string();
  137. }
  138. void ConsoleWidget::print_source_line(const StringView& source)
  139. {
  140. StringBuilder html;
  141. html.append("<span class=\"repl-indicator\">");
  142. html.append("&gt; ");
  143. html.append("</span>");
  144. // FIXME: Support output source highlighting
  145. html.append(source);
  146. print_html(html.string_view());
  147. }
  148. void ConsoleWidget::print_value(JS::Value value, StringBuilder& output_html, HashTable<JS::Object*> seen_objects)
  149. {
  150. // FIXME: Support output highlighting
  151. if (value.is_empty()) {
  152. output_html.append("<span class=\"empty-object\">");
  153. output_html.append("&lt;empty&gt;");
  154. output_html.append("</span>");
  155. return;
  156. }
  157. if (value.is_object()) {
  158. if (seen_objects.contains(&value.as_object())) {
  159. // FIXME: Maybe we should only do this for circular references,
  160. // not for all reoccurring objects.
  161. output_html.append("<span class=\"already-printed\">");
  162. output_html.appendf("&lt;already printed Object %p&gt;", &value.as_object());
  163. output_html.append("</span>");
  164. return;
  165. }
  166. seen_objects.set(&value.as_object());
  167. }
  168. if (value.is_array())
  169. return print_array(static_cast<const JS::Array&>(value.as_object()), output_html, seen_objects);
  170. if (value.is_object()) {
  171. auto& object = value.as_object();
  172. if (object.is_function())
  173. return print_function(object, output_html, seen_objects);
  174. if (object.is_date())
  175. return print_date(object, output_html, seen_objects);
  176. if (object.is_error())
  177. return print_error(object, output_html, seen_objects);
  178. return print_object(object, output_html, seen_objects);
  179. }
  180. if (value.is_string())
  181. output_html.append("<span class=\"js-string\">");
  182. else if (value.is_number())
  183. output_html.append("<span class=\"js-number\">");
  184. else if (value.is_boolean())
  185. output_html.append("<span class=\"js-boolean\">");
  186. else if (value.is_null())
  187. output_html.append("<span class=\"js-null\">");
  188. else if (value.is_undefined())
  189. output_html.append("<span class=\"js-undefined\">");
  190. if (value.is_string())
  191. output_html.append('"');
  192. output_html.append(value.to_string_without_side_effects());
  193. if (value.is_string())
  194. output_html.append('"');
  195. output_html.append("</span>");
  196. }
  197. void ConsoleWidget::print_array(const JS::Array& array, StringBuilder& html_output, HashTable<JS::Object*>& seen_objects)
  198. {
  199. html_output.append("<span class=\"js-array-open\">");
  200. html_output.append("[ ");
  201. html_output.append("</span>");
  202. for (size_t i = 0; i < array.elements().size(); ++i) {
  203. print_value(array.elements()[i], html_output, seen_objects);
  204. if (i != array.elements().size() - 1) {
  205. html_output.append("<span class=\"js-array-element-separator\">");
  206. html_output.append(", ");
  207. html_output.append("</span>");
  208. }
  209. }
  210. html_output.append("<span class=\"js-array-close\">");
  211. html_output.append(" ]");
  212. html_output.append("</span>");
  213. }
  214. void ConsoleWidget::print_object(const JS::Object& object, StringBuilder& html_output, HashTable<JS::Object*>& seen_objects)
  215. {
  216. html_output.append("<span class=\"js-object-open\">");
  217. html_output.append("{ ");
  218. html_output.append("</span>");
  219. for (size_t i = 0; i < object.elements().size(); ++i) {
  220. if (object.elements()[i].is_empty())
  221. continue;
  222. html_output.append("<span class=\"js-object-element-index\">");
  223. html_output.appendf("%zu", i);
  224. html_output.append("</span>");
  225. html_output.append(": ");
  226. print_value(object.elements()[i], html_output, seen_objects);
  227. if (i != object.elements().size() - 1) {
  228. html_output.append("<span class=\"js-object-element-separator\">");
  229. html_output.append(", ");
  230. html_output.append("</span>");
  231. }
  232. }
  233. if (!object.elements().is_empty() && object.shape().property_count()) {
  234. html_output.append("<span class=\"js-object-element-separator\">");
  235. html_output.append(", ");
  236. html_output.append("</span>");
  237. }
  238. size_t index = 0;
  239. for (auto& it : object.shape().property_table_ordered()) {
  240. html_output.append("<span class=\"js-object-element-key\">");
  241. html_output.appendf("\"%s\"", it.key.characters());
  242. html_output.append("</span>");
  243. html_output.append(": ");
  244. print_value(object.get_direct(it.value.offset), html_output, seen_objects);
  245. if (index != object.shape().property_count() - 1) {
  246. html_output.append("<span class=\"js-object-element-separator\">");
  247. html_output.append(", ");
  248. html_output.append("</span>");
  249. }
  250. ++index;
  251. }
  252. html_output.append("<span class=\"js-object-close\">");
  253. html_output.append(" }");
  254. html_output.append("</span>");
  255. }
  256. void ConsoleWidget::print_function(const JS::Object& function, StringBuilder& html_output, HashTable<JS::Object*>&)
  257. {
  258. html_output.append("<span class=\"js-function\">");
  259. html_output.appendf("[%s]", function.class_name());
  260. html_output.append("</span>");
  261. }
  262. void ConsoleWidget::print_date(const JS::Object& date, StringBuilder& html_output, HashTable<JS::Object*>&)
  263. {
  264. html_output.append("<span class=\"js-date\">");
  265. html_output.appendf("Date %s", static_cast<const JS::Date&>(date).string().characters());
  266. html_output.append("</span>");
  267. }
  268. void ConsoleWidget::print_error(const JS::Object& object, StringBuilder& html_output, HashTable<JS::Object*>&)
  269. {
  270. auto& error = static_cast<const JS::Error&>(object);
  271. html_output.append("<span class=\"js-error-name\">");
  272. html_output.appendf("[%s]", error.name().characters());
  273. html_output.append("</span>");
  274. if (!error.message().is_empty()) {
  275. html_output.append("<span class=\"js-error-message\">");
  276. html_output.appendf(": %s", error.message().characters());
  277. html_output.append("</span>");
  278. }
  279. }
  280. void ConsoleWidget::print_html(const StringView& line)
  281. {
  282. auto paragraph = create_element(m_console_output_container->document(), "p");
  283. paragraph->set_inner_html(line);
  284. m_console_output_container->append_child(paragraph);
  285. m_console_output_container->document().invalidate_layout();
  286. m_console_output_container->document().update_layout();
  287. m_console_output_view->scroll_to_bottom();
  288. }
  289. void ConsoleWidget::clear_output()
  290. {
  291. const_cast<Web::HTMLBodyElement*>(m_console_output_view->document()->body())->remove_all_children();
  292. }
  293. }