Console.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
  3. * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2024, Gasim Gasimzada <gasim@gasimzada.net>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/MemoryStream.h>
  10. #include <AK/NumberFormat.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibJS/Console.h>
  13. #include <LibJS/Print.h>
  14. #include <LibJS/Runtime/AbstractOperations.h>
  15. #include <LibJS/Runtime/Array.h>
  16. #include <LibJS/Runtime/Completion.h>
  17. #include <LibJS/Runtime/StringConstructor.h>
  18. #include <LibJS/Runtime/ValueInlines.h>
  19. namespace JS {
  20. GC_DEFINE_ALLOCATOR(Console);
  21. GC_DEFINE_ALLOCATOR(ConsoleClient);
  22. Console::Console(Realm& realm)
  23. : m_realm(realm)
  24. {
  25. }
  26. Console::~Console() = default;
  27. void Console::visit_edges(Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_realm);
  31. visitor.visit(m_client);
  32. }
  33. // 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert
  34. ThrowCompletionOr<Value> Console::assert_()
  35. {
  36. auto& vm = realm().vm();
  37. // 1. If condition is true, return.
  38. auto condition = vm.argument(0).to_boolean();
  39. if (condition)
  40. return js_undefined();
  41. // 2. Let message be a string without any formatting specifiers indicating generically an assertion failure (such as "Assertion failed").
  42. auto message = PrimitiveString::create(vm, "Assertion failed"_string);
  43. // NOTE: Assemble `data` from the function arguments.
  44. GC::RootVector<Value> data { vm.heap() };
  45. if (vm.argument_count() > 1) {
  46. data.ensure_capacity(vm.argument_count() - 1);
  47. for (size_t i = 1; i < vm.argument_count(); ++i) {
  48. data.append(vm.argument(i));
  49. }
  50. }
  51. // 3. If data is empty, append message to data.
  52. if (data.is_empty()) {
  53. data.append(message);
  54. }
  55. // 4. Otherwise:
  56. else {
  57. // 1. Let first be data[0].
  58. auto& first = data[0];
  59. // 2. If first is not a String, then prepend message to data.
  60. if (!first.is_string()) {
  61. data.prepend(message);
  62. }
  63. // 3. Otherwise:
  64. else {
  65. // 1. Let concat be the concatenation of message, U+003A (:), U+0020 SPACE, and first.
  66. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", message->utf8_string(), MUST(first.to_string(vm))));
  67. // 2. Set data[0] to concat.
  68. data[0] = PrimitiveString::create(vm, move(concat));
  69. }
  70. }
  71. // 5. Perform Logger("assert", data).
  72. if (m_client)
  73. TRY(m_client->logger(LogLevel::Assert, data));
  74. return js_undefined();
  75. }
  76. // 1.1.2. clear(), https://console.spec.whatwg.org/#clear
  77. Value Console::clear()
  78. {
  79. // 1. Empty the appropriate group stack.
  80. m_group_stack.clear();
  81. // 2. If possible for the environment, clear the console. (Otherwise, do nothing.)
  82. if (m_client)
  83. m_client->clear();
  84. return js_undefined();
  85. }
  86. // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug
  87. ThrowCompletionOr<Value> Console::debug()
  88. {
  89. // 1. Perform Logger("debug", data).
  90. if (m_client) {
  91. auto data = vm_arguments();
  92. return m_client->logger(LogLevel::Debug, data);
  93. }
  94. return js_undefined();
  95. }
  96. // 1.1.4. error(...data), https://console.spec.whatwg.org/#error
  97. ThrowCompletionOr<Value> Console::error()
  98. {
  99. // 1. Perform Logger("error", data).
  100. if (m_client) {
  101. auto data = vm_arguments();
  102. return m_client->logger(LogLevel::Error, data);
  103. }
  104. return js_undefined();
  105. }
  106. // 1.1.5. info(...data), https://console.spec.whatwg.org/#info
  107. ThrowCompletionOr<Value> Console::info()
  108. {
  109. // 1. Perform Logger("info", data).
  110. if (m_client) {
  111. auto data = vm_arguments();
  112. return m_client->logger(LogLevel::Info, data);
  113. }
  114. return js_undefined();
  115. }
  116. // 1.1.6. log(...data), https://console.spec.whatwg.org/#log
  117. ThrowCompletionOr<Value> Console::log()
  118. {
  119. // 1. Perform Logger("log", data).
  120. if (m_client) {
  121. auto data = vm_arguments();
  122. return m_client->logger(LogLevel::Log, data);
  123. }
  124. return js_undefined();
  125. }
  126. // To [create table row] given tabularDataItem, rowIndex, list finalColumns, and optional list properties, perform the following steps:
  127. static ThrowCompletionOr<GC::Ref<Object>> create_table_row(Realm& realm, Value row_index, Value tabular_data_item, GC::RootVector<Value>& final_columns, HashMap<PropertyKey, bool>& visited_columns, HashMap<PropertyKey, bool>& properties)
  128. {
  129. auto& vm = realm.vm();
  130. auto add_column = [&](PropertyKey const& column_name) -> Optional<Completion> {
  131. // In order to not iterate over the final_columns to find if a column is
  132. // already in the list, an additional hash map is used to identify
  133. // if a column is already visited without needing to loop through the whole
  134. // array.
  135. if (!visited_columns.contains(column_name)) {
  136. visited_columns.set(column_name, true);
  137. if (column_name.is_string()) {
  138. final_columns.append(PrimitiveString::create(vm, column_name.as_string()));
  139. } else if (column_name.is_symbol()) {
  140. final_columns.append(column_name.as_symbol());
  141. } else if (column_name.is_number()) {
  142. final_columns.append(Value(column_name.as_number()));
  143. }
  144. }
  145. return {};
  146. };
  147. // 1. Let `row` be a new map
  148. auto row = Object::create(realm, nullptr);
  149. // 2. Set `row["(index)"]` to `rowIndex`
  150. {
  151. auto key = PropertyKey { "(index)", PropertyKey::StringMayBeNumber::No };
  152. TRY(row->set(key, row_index, Object::ShouldThrowExceptions::No));
  153. add_column(key);
  154. }
  155. // 3. If `tabularDataItem` is a list, then:
  156. if (TRY(tabular_data_item.is_array(vm))) {
  157. auto& array = tabular_data_item.as_array();
  158. // 3.1. Let `indices` be get the indices of `tabularDataItem`
  159. auto& indices = array.indexed_properties();
  160. // 3.2. For each `index` of `indices`
  161. for (auto const& prop : indices) {
  162. PropertyKey key(prop.index());
  163. // 3.2.1. Let `value` be `tabularDataItem[index]`
  164. Value value = TRY(array.get(key));
  165. // 3.2.2. If `properties` is not empty and `properties` does not contain `index`, continue
  166. if (properties.size() > 0 && !properties.contains(key)) {
  167. continue;
  168. }
  169. // 3.2.3. Set `row[index]` to `value`
  170. TRY(row->set(key, value, Object::ShouldThrowExceptions::No));
  171. // 3.2.4. If `finalColumns` does not contain `index`, append `index` to `finalColumns`
  172. add_column(key);
  173. }
  174. }
  175. // 4. Otherwise, if `tabularDataItem` is a map, then:
  176. else if (tabular_data_item.is_object()) {
  177. auto& object = tabular_data_item.as_object();
  178. // 4.1. For each `key` -> `value` of `tabularDataItem`
  179. object.enumerate_object_properties([&](Value key_v) -> Optional<Completion> {
  180. auto key = TRY(PropertyKey::from_value(vm, key_v));
  181. // 4.1.1. If `properties` is not empty and `properties` does not contain `key`, continue
  182. if (properties.size() > 0 && !properties.contains(key)) {
  183. return {};
  184. }
  185. // 4.1.2. Set `row[key]` to `value`
  186. TRY(row->set(key, TRY(object.get(key)), Object::ShouldThrowExceptions::No));
  187. // 4.1.3. If `finalColumns` does not contain `key`, append `key` to `finalColumns`
  188. add_column(key);
  189. return {};
  190. });
  191. }
  192. // 5. Otherwise,
  193. else {
  194. // 5.1. Set `row["Value"]` to `tabularDataItem`
  195. TRY(row->set(vm.names.Value, tabular_data_item, Object::ShouldThrowExceptions::No));
  196. // 5.2. If `finalColumns` does not contain "Value", append "Value" to `finalColumns`
  197. add_column(vm.names.Value);
  198. }
  199. // 6. Return row
  200. return row;
  201. }
  202. // 1.1.7. table(tabularData, properties), https://console.spec.whatwg.org/#table, WIP
  203. ThrowCompletionOr<Value> Console::table()
  204. {
  205. if (!m_client) {
  206. return js_undefined();
  207. }
  208. auto& vm = realm().vm();
  209. if (vm.argument_count() > 0) {
  210. auto tabular_data = vm.argument(0);
  211. auto properties_arg = vm.argument(1);
  212. HashMap<PropertyKey, bool> properties;
  213. if (TRY(properties_arg.is_array(vm))) {
  214. auto& properties_array = properties_arg.as_array().indexed_properties();
  215. auto* properties_storage = properties_array.storage();
  216. for (auto const& col : properties_array) {
  217. auto col_name = properties_storage->get(col.index()).value().value;
  218. properties.set(TRY(PropertyKey::from_value(vm, col_name)), true);
  219. }
  220. }
  221. // 1. Let `finalRows` be the new list, initially empty
  222. GC::RootVector<Value> final_rows(vm.heap());
  223. // 2. Let `finalColumns` be the new list, initially empty
  224. GC::RootVector<Value> final_columns(vm.heap());
  225. HashMap<PropertyKey, bool> visited_columns;
  226. // 3. If `tabularData` is a list, then:
  227. if (TRY(tabular_data.is_array(vm))) {
  228. auto& array = tabular_data.as_array();
  229. // 3.1. Let `indices` be get the indices of `tabularData`
  230. auto& indices = array.indexed_properties();
  231. // 3.2. For each `index` of `indices`
  232. for (auto const& prop : indices) {
  233. PropertyKey index(prop.index());
  234. // 3.2.1. Let `value` be `tabularData[index]`
  235. Value value = TRY(array.get(index));
  236. // 3.2.2. Perform create table row with `value`, `key`, `finalColumns`, and `properties` that returns `row`
  237. auto row = TRY(create_table_row(realm(), Value(index.as_number()), value, final_columns, visited_columns, properties));
  238. // 3.2.3. Append `row` to `finalRows`
  239. final_rows.append(row);
  240. }
  241. }
  242. // 4. Otherwise, if `tabularData` is a map, then:
  243. else if (tabular_data.is_object()) {
  244. auto& object = tabular_data.as_object();
  245. // 4.1. For each `key` -> `value` of `tabularData`
  246. object.enumerate_object_properties([&](Value key) -> Optional<Completion> {
  247. auto index = TRY(PropertyKey::from_value(vm, key));
  248. auto value = TRY(object.get(index));
  249. // 4.1.1. Perform create table row with `key`, `value`, `finalColumns`, and `properties` that returns `row`
  250. auto row = TRY(create_table_row(realm(), key, value, final_columns, visited_columns, properties));
  251. // 4.1.2. Append `row` to `finalRows`
  252. final_rows.append(row);
  253. return {};
  254. });
  255. }
  256. // 5. If `finalRows` is not empty, then:
  257. if (final_rows.size() > 0) {
  258. auto table_rows = Array::create_from(realm(), final_rows);
  259. auto table_cols = Array::create_from(realm(), final_columns);
  260. // 5.1. Let `finalData` to be a new map:
  261. auto final_data = Object::create(realm(), nullptr);
  262. // 5.2. Set `finalData["rows"]` to `finalRows`
  263. TRY(final_data->set(vm.names.rows, table_rows, Object::ShouldThrowExceptions::No));
  264. // 5.3. Set finalData["columns"] to finalColumns
  265. TRY(final_data->set(vm.names.columns, table_cols, Object::ShouldThrowExceptions::No));
  266. // 5.4. Perform `Printer("table", finalData)`
  267. GC::RootVector<Value> args(vm.heap());
  268. args.append(Value(final_data));
  269. return m_client->printer(LogLevel::Table, args);
  270. }
  271. }
  272. // 6. Otherwise, perform `Printer("log", tabularData)`
  273. return m_client->printer(LogLevel::Log, vm_arguments());
  274. }
  275. // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace
  276. ThrowCompletionOr<Value> Console::trace()
  277. {
  278. if (!m_client)
  279. return js_undefined();
  280. auto& vm = realm().vm();
  281. // 1. Let trace be some implementation-defined, potentially-interactive representation of the callstack from where this function was called.
  282. Console::Trace trace;
  283. auto& execution_context_stack = vm.execution_context_stack();
  284. // NOTE: -2 to skip the console.trace() execution context
  285. for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i) {
  286. auto const& function_name = execution_context_stack[i]->function_name;
  287. trace.stack.append((!function_name || function_name->is_empty())
  288. ? "<anonymous>"_string
  289. : function_name->utf8_string());
  290. }
  291. // 2. Optionally, let formattedData be the result of Formatter(data), and incorporate formattedData as a label for trace.
  292. if (vm.argument_count() > 0) {
  293. auto data = vm_arguments();
  294. auto formatted_data = TRY(m_client->formatter(data));
  295. trace.label = TRY(value_vector_to_string(formatted_data));
  296. }
  297. // 3. Perform Printer("trace", « trace »).
  298. return m_client->printer(Console::LogLevel::Trace, trace);
  299. }
  300. // 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn
  301. ThrowCompletionOr<Value> Console::warn()
  302. {
  303. // 1. Perform Logger("warn", data).
  304. if (m_client) {
  305. auto data = vm_arguments();
  306. return m_client->logger(LogLevel::Warn, data);
  307. }
  308. return js_undefined();
  309. }
  310. // 1.1.10. dir(item, options), https://console.spec.whatwg.org/#dir
  311. ThrowCompletionOr<Value> Console::dir()
  312. {
  313. auto& vm = realm().vm();
  314. // 1. Let object be item with generic JavaScript object formatting applied.
  315. // NOTE: Generic formatting is performed by ConsoleClient::printer().
  316. auto object = vm.argument(0);
  317. // 2. Perform Printer("dir", « object », options).
  318. if (m_client) {
  319. GC::RootVector<Value> printer_arguments { vm.heap() };
  320. TRY_OR_THROW_OOM(vm, printer_arguments.try_append(object));
  321. return m_client->printer(LogLevel::Dir, move(printer_arguments));
  322. }
  323. return js_undefined();
  324. }
  325. static ThrowCompletionOr<String> label_or_fallback(VM& vm, StringView fallback)
  326. {
  327. return vm.argument_count() > 0 && !vm.argument(0).is_undefined()
  328. ? vm.argument(0).to_string(vm)
  329. : TRY_OR_THROW_OOM(vm, String::from_utf8(fallback));
  330. }
  331. // 1.2.1. count(label), https://console.spec.whatwg.org/#count
  332. ThrowCompletionOr<Value> Console::count()
  333. {
  334. auto& vm = realm().vm();
  335. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-count
  336. auto label = TRY(label_or_fallback(vm, "default"sv));
  337. // 1. Let map be the associated count map.
  338. auto& map = m_counters;
  339. // 2. If map[label] exists, set map[label] to map[label] + 1.
  340. if (auto found = map.find(label); found != map.end()) {
  341. map.set(label, found->value + 1);
  342. }
  343. // 3. Otherwise, set map[label] to 1.
  344. else {
  345. map.set(label, 1);
  346. }
  347. // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and ToString(map[label]).
  348. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, map.get(label).value()));
  349. // 5. Perform Logger("count", « concat »).
  350. GC::RootVector<Value> concat_as_vector { vm.heap() };
  351. concat_as_vector.append(PrimitiveString::create(vm, move(concat)));
  352. if (m_client)
  353. TRY(m_client->logger(LogLevel::Count, concat_as_vector));
  354. return js_undefined();
  355. }
  356. // 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset
  357. ThrowCompletionOr<Value> Console::count_reset()
  358. {
  359. auto& vm = realm().vm();
  360. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-countreset
  361. auto label = TRY(label_or_fallback(vm, "default"sv));
  362. // 1. Let map be the associated count map.
  363. auto& map = m_counters;
  364. // 2. If map[label] exists, set map[label] to 0.
  365. if (auto found = map.find(label); found != map.end()) {
  366. map.set(label, 0);
  367. }
  368. // 3. Otherwise:
  369. else {
  370. // 1. Let message be a string without any formatting specifiers indicating generically
  371. // that the given label does not have an associated count.
  372. auto message = TRY_OR_THROW_OOM(vm, String::formatted("\"{}\" doesn't have a count", label));
  373. // 2. Perform Logger("countReset", « message »);
  374. GC::RootVector<Value> message_as_vector { vm.heap() };
  375. message_as_vector.append(PrimitiveString::create(vm, move(message)));
  376. if (m_client)
  377. TRY(m_client->logger(LogLevel::CountReset, message_as_vector));
  378. }
  379. return js_undefined();
  380. }
  381. // 1.3.1. group(...data), https://console.spec.whatwg.org/#group
  382. ThrowCompletionOr<Value> Console::group()
  383. {
  384. // 1. Let group be a new group.
  385. Group group;
  386. // 2. If data is not empty, let groupLabel be the result of Formatter(data).
  387. String group_label {};
  388. auto data = vm_arguments();
  389. if (!data.is_empty()) {
  390. auto formatted_data = TRY(m_client->formatter(data));
  391. group_label = TRY(value_vector_to_string(formatted_data));
  392. }
  393. // ... Otherwise, let groupLabel be an implementation-chosen label representing a group.
  394. else {
  395. group_label = "Group"_string;
  396. }
  397. // 3. Incorporate groupLabel as a label for group.
  398. group.label = group_label;
  399. // 4. Optionally, if the environment supports interactive groups, group should be expanded by default.
  400. // NOTE: This is handled in Printer.
  401. // 5. Perform Printer("group", « group »).
  402. if (m_client)
  403. TRY(m_client->printer(LogLevel::Group, group));
  404. // 6. Push group onto the appropriate group stack.
  405. m_group_stack.append(group);
  406. return js_undefined();
  407. }
  408. // 1.3.2. groupCollapsed(...data), https://console.spec.whatwg.org/#groupcollapsed
  409. ThrowCompletionOr<Value> Console::group_collapsed()
  410. {
  411. // 1. Let group be a new group.
  412. Group group;
  413. // 2. If data is not empty, let groupLabel be the result of Formatter(data).
  414. String group_label {};
  415. auto data = vm_arguments();
  416. if (!data.is_empty()) {
  417. auto formatted_data = TRY(m_client->formatter(data));
  418. group_label = TRY(value_vector_to_string(formatted_data));
  419. }
  420. // ... Otherwise, let groupLabel be an implementation-chosen label representing a group.
  421. else {
  422. group_label = "Group"_string;
  423. }
  424. // 3. Incorporate groupLabel as a label for group.
  425. group.label = group_label;
  426. // 4. Optionally, if the environment supports interactive groups, group should be collapsed by default.
  427. // NOTE: This is handled in Printer.
  428. // 5. Perform Printer("groupCollapsed", « group »).
  429. if (m_client)
  430. TRY(m_client->printer(LogLevel::GroupCollapsed, group));
  431. // 6. Push group onto the appropriate group stack.
  432. m_group_stack.append(group);
  433. return js_undefined();
  434. }
  435. // 1.3.3. groupEnd(), https://console.spec.whatwg.org/#groupend
  436. ThrowCompletionOr<Value> Console::group_end()
  437. {
  438. if (m_group_stack.is_empty())
  439. return js_undefined();
  440. // 1. Pop the last group from the group stack.
  441. m_group_stack.take_last();
  442. if (m_client)
  443. m_client->end_group();
  444. return js_undefined();
  445. }
  446. // 1.4.1. time(label), https://console.spec.whatwg.org/#time
  447. ThrowCompletionOr<Value> Console::time()
  448. {
  449. auto& vm = realm().vm();
  450. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-time
  451. auto label = TRY(label_or_fallback(vm, "default"sv));
  452. // 1. If the associated timer table contains an entry with key label, return, optionally reporting
  453. // a warning to the console indicating that a timer with label `label` has already been started.
  454. if (m_timer_table.contains(label)) {
  455. if (m_client) {
  456. GC::RootVector<Value> timer_already_exists_warning_message_as_vector { vm.heap() };
  457. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' already exists.", label));
  458. timer_already_exists_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  459. TRY(m_client->printer(LogLevel::Warn, move(timer_already_exists_warning_message_as_vector)));
  460. }
  461. return js_undefined();
  462. }
  463. // 2. Otherwise, set the value of the entry with key label in the associated timer table to the current time.
  464. m_timer_table.set(label, Core::ElapsedTimer::start_new());
  465. return js_undefined();
  466. }
  467. // 1.4.2. timeLog(label, ...data), https://console.spec.whatwg.org/#timelog
  468. ThrowCompletionOr<Value> Console::time_log()
  469. {
  470. auto& vm = realm().vm();
  471. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timelog
  472. auto label = TRY(label_or_fallback(vm, "default"sv));
  473. // 1. Let timerTable be the associated timer table.
  474. // 2. Let startTime be timerTable[label].
  475. auto maybe_start_time = m_timer_table.find(label);
  476. // NOTE: Warn if the timer doesn't exist. Not part of the spec yet, but discussed here: https://github.com/whatwg/console/issues/134
  477. if (maybe_start_time == m_timer_table.end()) {
  478. if (m_client) {
  479. GC::RootVector<Value> timer_does_not_exist_warning_message_as_vector { vm.heap() };
  480. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' does not exist.", label));
  481. timer_does_not_exist_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  482. TRY(m_client->printer(LogLevel::Warn, move(timer_does_not_exist_warning_message_as_vector)));
  483. }
  484. return js_undefined();
  485. }
  486. auto start_time = maybe_start_time->value;
  487. // 3. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format.
  488. auto duration = AK::human_readable_time(start_time.elapsed_time());
  489. // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration.
  490. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration));
  491. // 5. Prepend concat to data.
  492. GC::RootVector<Value> data { vm.heap() };
  493. data.ensure_capacity(vm.argument_count());
  494. data.append(PrimitiveString::create(vm, move(concat)));
  495. for (size_t i = 1; i < vm.argument_count(); ++i)
  496. data.append(vm.argument(i));
  497. // 6. Perform Printer("timeLog", data).
  498. if (m_client)
  499. TRY(m_client->printer(LogLevel::TimeLog, move(data)));
  500. return js_undefined();
  501. }
  502. // 1.4.3. timeEnd(label), https://console.spec.whatwg.org/#timeend
  503. ThrowCompletionOr<Value> Console::time_end()
  504. {
  505. auto& vm = realm().vm();
  506. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timeend
  507. auto label = TRY(label_or_fallback(vm, "default"sv));
  508. // 1. Let timerTable be the associated timer table.
  509. // 2. Let startTime be timerTable[label].
  510. auto maybe_start_time = m_timer_table.find(label);
  511. // NOTE: Warn if the timer doesn't exist. Not part of the spec yet, but discussed here: https://github.com/whatwg/console/issues/134
  512. if (maybe_start_time == m_timer_table.end()) {
  513. if (m_client) {
  514. GC::RootVector<Value> timer_does_not_exist_warning_message_as_vector { vm.heap() };
  515. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' does not exist.", label));
  516. timer_does_not_exist_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  517. TRY(m_client->printer(LogLevel::Warn, move(timer_does_not_exist_warning_message_as_vector)));
  518. }
  519. return js_undefined();
  520. }
  521. auto start_time = maybe_start_time->value;
  522. // 3. Remove timerTable[label].
  523. m_timer_table.remove(label);
  524. // 4. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format.
  525. auto duration = AK::human_readable_time(start_time.elapsed_time());
  526. // 5. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration.
  527. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration));
  528. // 6. Perform Printer("timeEnd", « concat »).
  529. if (m_client) {
  530. GC::RootVector<Value> concat_as_vector { vm.heap() };
  531. concat_as_vector.append(PrimitiveString::create(vm, move(concat)));
  532. TRY(m_client->printer(LogLevel::TimeEnd, move(concat_as_vector)));
  533. }
  534. return js_undefined();
  535. }
  536. GC::RootVector<Value> Console::vm_arguments()
  537. {
  538. auto& vm = realm().vm();
  539. GC::RootVector<Value> arguments { vm.heap() };
  540. arguments.ensure_capacity(vm.argument_count());
  541. for (size_t i = 0; i < vm.argument_count(); ++i) {
  542. arguments.append(vm.argument(i));
  543. }
  544. return arguments;
  545. }
  546. void Console::output_debug_message(LogLevel log_level, StringView output) const
  547. {
  548. switch (log_level) {
  549. case Console::LogLevel::Debug:
  550. dbgln("\033[32;1m(js debug)\033[0m {}", output);
  551. break;
  552. case Console::LogLevel::Error:
  553. dbgln("\033[32;1m(js error)\033[0m {}", output);
  554. break;
  555. case Console::LogLevel::Info:
  556. dbgln("\033[32;1m(js info)\033[0m {}", output);
  557. break;
  558. case Console::LogLevel::Log:
  559. dbgln("\033[32;1m(js log)\033[0m {}", output);
  560. break;
  561. case Console::LogLevel::Warn:
  562. dbgln("\033[32;1m(js warn)\033[0m {}", output);
  563. break;
  564. default:
  565. dbgln("\033[32;1m(js)\033[0m {}", output);
  566. break;
  567. }
  568. }
  569. void Console::report_exception(JS::Error const& exception, bool in_promise) const
  570. {
  571. if (m_client)
  572. m_client->report_exception(exception, in_promise);
  573. }
  574. ThrowCompletionOr<String> Console::value_vector_to_string(GC::RootVector<Value> const& values)
  575. {
  576. auto& vm = realm().vm();
  577. StringBuilder builder;
  578. for (auto const& item : values) {
  579. if (!builder.is_empty())
  580. builder.append(' ');
  581. builder.append(TRY(item.to_string(vm)));
  582. }
  583. return MUST(builder.to_string());
  584. }
  585. ConsoleClient::ConsoleClient(Console& console)
  586. : m_console(console)
  587. {
  588. }
  589. ConsoleClient::~ConsoleClient() = default;
  590. void ConsoleClient::visit_edges(Visitor& visitor)
  591. {
  592. Base::visit_edges(visitor);
  593. visitor.visit(m_console);
  594. }
  595. // 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger
  596. ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, GC::RootVector<Value> const& args)
  597. {
  598. auto& vm = m_console->realm().vm();
  599. // 1. If args is empty, return.
  600. if (args.is_empty())
  601. return js_undefined();
  602. // 2. Let first be args[0].
  603. auto first = args[0];
  604. // 3. Let rest be all elements following first in args.
  605. size_t rest_size = args.size() - 1;
  606. // 4. If rest is empty, perform Printer(logLevel, « first ») and return.
  607. if (rest_size == 0) {
  608. GC::RootVector<Value> first_as_vector { vm.heap() };
  609. first_as_vector.append(first);
  610. return printer(log_level, move(first_as_vector));
  611. }
  612. // 5. Otherwise, perform Printer(logLevel, Formatter(args)).
  613. else {
  614. auto formatted = TRY(formatter(args));
  615. TRY(printer(log_level, formatted));
  616. }
  617. // 6. Return undefined.
  618. return js_undefined();
  619. }
  620. // 2.2. Formatter(args), https://console.spec.whatwg.org/#formatter
  621. ThrowCompletionOr<GC::RootVector<Value>> ConsoleClient::formatter(GC::RootVector<Value> const& args)
  622. {
  623. auto& realm = m_console->realm();
  624. auto& vm = realm.vm();
  625. // 1. If args’s size is 1, return args.
  626. if (args.size() == 1)
  627. return args;
  628. // 2. Let target be the first element of args.
  629. auto target = (!args.is_empty()) ? TRY(args.first().to_string(vm)) : String {};
  630. // 3. Let current be the second element of args.
  631. auto current = (args.size() > 1) ? args[1] : js_undefined();
  632. // 4. Find the first possible format specifier specifier, from the left to the right in target.
  633. auto find_specifier = [](StringView target) -> Optional<StringView> {
  634. size_t start_index = 0;
  635. while (start_index < target.length()) {
  636. auto maybe_index = target.find('%', start_index);
  637. if (!maybe_index.has_value())
  638. return {};
  639. auto index = maybe_index.value();
  640. if (index + 1 >= target.length())
  641. return {};
  642. switch (target[index + 1]) {
  643. case 'c':
  644. case 'd':
  645. case 'f':
  646. case 'i':
  647. case 'o':
  648. case 'O':
  649. case 's':
  650. return target.substring_view(index, 2);
  651. }
  652. start_index = index + 1;
  653. }
  654. return {};
  655. };
  656. auto maybe_specifier = find_specifier(target);
  657. // 5. If no format specifier was found, return args.
  658. if (!maybe_specifier.has_value()) {
  659. return args;
  660. }
  661. // 6. Otherwise:
  662. else {
  663. auto specifier = maybe_specifier.release_value();
  664. Optional<Value> converted;
  665. // 1. If specifier is %s, let converted be the result of Call(%String%, undefined, « current »).
  666. if (specifier == "%s"sv) {
  667. converted = TRY(call(vm, *realm.intrinsics().string_constructor(), js_undefined(), current));
  668. }
  669. // 2. If specifier is %d or %i:
  670. else if (specifier.is_one_of("%d"sv, "%i"sv)) {
  671. // 1. If current is a Symbol, let converted be NaN
  672. if (current.is_symbol()) {
  673. converted = js_nan();
  674. }
  675. // 2. Otherwise, let converted be the result of Call(%parseInt%, undefined, « current, 10 »).
  676. else {
  677. converted = TRY(call(vm, *realm.intrinsics().parse_int_function(), js_undefined(), current, Value { 10 }));
  678. }
  679. }
  680. // 3. If specifier is %f:
  681. else if (specifier == "%f"sv) {
  682. // 1. If current is a Symbol, let converted be NaN
  683. if (current.is_symbol()) {
  684. converted = js_nan();
  685. }
  686. // 2. Otherwise, let converted be the result of Call(% parseFloat %, undefined, « current »).
  687. else {
  688. converted = TRY(call(vm, *realm.intrinsics().parse_float_function(), js_undefined(), current));
  689. }
  690. }
  691. // 4. If specifier is %o, optionally let converted be current with optimally useful formatting applied.
  692. else if (specifier == "%o"sv) {
  693. // TODO: "Optimally-useful formatting"
  694. converted = current;
  695. }
  696. // 5. If specifier is %O, optionally let converted be current with generic JavaScript object formatting applied.
  697. else if (specifier == "%O"sv) {
  698. // TODO: "generic JavaScript object formatting"
  699. converted = current;
  700. }
  701. // 6. TODO: process %c
  702. else if (specifier == "%c"sv) {
  703. // NOTE: This has no spec yet. `%c` specifiers treat the argument as CSS styling for the log message.
  704. add_css_style_to_current_message(TRY(current.to_string(vm)));
  705. converted = PrimitiveString::create(vm, String {});
  706. }
  707. // 7. If any of the previous steps set converted, replace specifier in target with converted.
  708. if (converted.has_value())
  709. target = TRY_OR_THROW_OOM(vm, target.replace(specifier, TRY(converted->to_string(vm)), ReplaceMode::FirstOnly));
  710. }
  711. // 7. Let result be a list containing target together with the elements of args starting from the third onward.
  712. GC::RootVector<Value> result { vm.heap() };
  713. result.ensure_capacity(args.size() - 1);
  714. result.empend(PrimitiveString::create(vm, move(target)));
  715. for (size_t i = 2; i < args.size(); ++i)
  716. result.unchecked_append(args[i]);
  717. // 8. Return Formatter(result).
  718. return formatter(result);
  719. }
  720. ThrowCompletionOr<String> ConsoleClient::generically_format_values(GC::RootVector<Value> const& values)
  721. {
  722. AllocatingMemoryStream stream;
  723. auto& vm = m_console->realm().vm();
  724. PrintContext ctx { vm, stream, true };
  725. bool first = true;
  726. for (auto const& value : values) {
  727. if (!first)
  728. TRY_OR_THROW_OOM(vm, stream.write_until_depleted(" "sv.bytes()));
  729. TRY_OR_THROW_OOM(vm, JS::print(value, ctx));
  730. first = false;
  731. }
  732. // FIXME: Is it possible we could end up serializing objects to invalid UTF-8?
  733. return TRY_OR_THROW_OOM(vm, String::from_stream(stream, stream.used_buffer_size()));
  734. }
  735. }