Console.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/MemoryStream.h>
  9. #include <LibJS/Console.h>
  10. #include <LibJS/Print.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/Completion.h>
  13. #include <LibJS/Runtime/StringConstructor.h>
  14. #include <LibJS/Runtime/Temporal/Duration.h>
  15. #include <LibJS/Runtime/ThrowableStringBuilder.h>
  16. namespace JS {
  17. Console::Console(Realm& realm)
  18. : m_realm(realm)
  19. {
  20. }
  21. // 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert
  22. ThrowCompletionOr<Value> Console::assert_()
  23. {
  24. auto& vm = realm().vm();
  25. // 1. If condition is true, return.
  26. auto condition = vm.argument(0).to_boolean();
  27. if (condition)
  28. return js_undefined();
  29. // 2. Let message be a string without any formatting specifiers indicating generically an assertion failure (such as "Assertion failed").
  30. auto message = MUST_OR_THROW_OOM(PrimitiveString::create(vm, "Assertion failed"sv));
  31. // NOTE: Assemble `data` from the function arguments.
  32. MarkedVector<Value> data { vm.heap() };
  33. if (vm.argument_count() > 1) {
  34. data.ensure_capacity(vm.argument_count() - 1);
  35. for (size_t i = 1; i < vm.argument_count(); ++i) {
  36. data.append(vm.argument(i));
  37. }
  38. }
  39. // 3. If data is empty, append message to data.
  40. if (data.is_empty()) {
  41. data.append(message);
  42. }
  43. // 4. Otherwise:
  44. else {
  45. // 1. Let first be data[0].
  46. auto& first = data[0];
  47. // 2. If Type(first) is not String, then prepend message to data.
  48. if (!first.is_string()) {
  49. data.prepend(message);
  50. }
  51. // 3. Otherwise:
  52. else {
  53. // 1. Let concat be the concatenation of message, U+003A (:), U+0020 SPACE, and first.
  54. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", TRY(message->utf8_string()), MUST(first.to_string(vm))));
  55. // 2. Set data[0] to concat.
  56. data[0] = PrimitiveString::create(vm, move(concat));
  57. }
  58. }
  59. // 5. Perform Logger("assert", data).
  60. if (m_client)
  61. TRY(m_client->logger(LogLevel::Assert, data));
  62. return js_undefined();
  63. }
  64. // 1.1.2. clear(), https://console.spec.whatwg.org/#clear
  65. Value Console::clear()
  66. {
  67. // 1. Empty the appropriate group stack.
  68. m_group_stack.clear();
  69. // 2. If possible for the environment, clear the console. (Otherwise, do nothing.)
  70. if (m_client)
  71. m_client->clear();
  72. return js_undefined();
  73. }
  74. // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug
  75. ThrowCompletionOr<Value> Console::debug()
  76. {
  77. // 1. Perform Logger("debug", data).
  78. if (m_client) {
  79. auto data = vm_arguments();
  80. return m_client->logger(LogLevel::Debug, data);
  81. }
  82. return js_undefined();
  83. }
  84. // 1.1.4. error(...data), https://console.spec.whatwg.org/#error
  85. ThrowCompletionOr<Value> Console::error()
  86. {
  87. // 1. Perform Logger("error", data).
  88. if (m_client) {
  89. auto data = vm_arguments();
  90. return m_client->logger(LogLevel::Error, data);
  91. }
  92. return js_undefined();
  93. }
  94. // 1.1.5. info(...data), https://console.spec.whatwg.org/#info
  95. ThrowCompletionOr<Value> Console::info()
  96. {
  97. // 1. Perform Logger("info", data).
  98. if (m_client) {
  99. auto data = vm_arguments();
  100. return m_client->logger(LogLevel::Info, data);
  101. }
  102. return js_undefined();
  103. }
  104. // 1.1.6. log(...data), https://console.spec.whatwg.org/#log
  105. ThrowCompletionOr<Value> Console::log()
  106. {
  107. // 1. Perform Logger("log", data).
  108. if (m_client) {
  109. auto data = vm_arguments();
  110. return m_client->logger(LogLevel::Log, data);
  111. }
  112. return js_undefined();
  113. }
  114. // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace
  115. ThrowCompletionOr<Value> Console::trace()
  116. {
  117. if (!m_client)
  118. return js_undefined();
  119. auto& vm = realm().vm();
  120. // 1. Let trace be some implementation-specific, potentially-interactive representation of the callstack from where this function was called.
  121. Console::Trace trace;
  122. auto& execution_context_stack = vm.execution_context_stack();
  123. // NOTE: -2 to skip the console.trace() execution context
  124. for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i) {
  125. auto const& function_name = execution_context_stack[i]->function_name;
  126. trace.stack.append(function_name.is_empty()
  127. ? TRY_OR_THROW_OOM(vm, "<anonymous>"_string)
  128. : TRY_OR_THROW_OOM(vm, String::from_deprecated_string(function_name)));
  129. }
  130. // 2. Optionally, let formattedData be the result of Formatter(data), and incorporate formattedData as a label for trace.
  131. if (vm.argument_count() > 0) {
  132. auto data = vm_arguments();
  133. auto formatted_data = TRY(m_client->formatter(data));
  134. trace.label = TRY(value_vector_to_string(formatted_data));
  135. }
  136. // 3. Perform Printer("trace", « trace »).
  137. return m_client->printer(Console::LogLevel::Trace, trace);
  138. }
  139. // 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn
  140. ThrowCompletionOr<Value> Console::warn()
  141. {
  142. // 1. Perform Logger("warn", data).
  143. if (m_client) {
  144. auto data = vm_arguments();
  145. return m_client->logger(LogLevel::Warn, data);
  146. }
  147. return js_undefined();
  148. }
  149. // 1.1.10. dir(item, options), https://console.spec.whatwg.org/#dir
  150. ThrowCompletionOr<Value> Console::dir()
  151. {
  152. auto& vm = realm().vm();
  153. // 1. Let object be item with generic JavaScript object formatting applied.
  154. // NOTE: Generic formatting is performed by ConsoleClient::printer().
  155. auto object = vm.argument(0);
  156. // 2. Perform Printer("dir", « object », options).
  157. if (m_client) {
  158. MarkedVector<Value> printer_arguments { vm.heap() };
  159. TRY_OR_THROW_OOM(vm, printer_arguments.try_append(object));
  160. return m_client->printer(LogLevel::Dir, move(printer_arguments));
  161. }
  162. return js_undefined();
  163. }
  164. static ThrowCompletionOr<String> label_or_fallback(VM& vm, StringView fallback)
  165. {
  166. return vm.argument_count() > 0
  167. ? vm.argument(0).to_string(vm)
  168. : TRY_OR_THROW_OOM(vm, String::from_utf8(fallback));
  169. }
  170. // 1.2.1. count(label), https://console.spec.whatwg.org/#count
  171. ThrowCompletionOr<Value> Console::count()
  172. {
  173. auto& vm = realm().vm();
  174. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-count
  175. auto label = TRY(label_or_fallback(vm, "default"sv));
  176. // 1. Let map be the associated count map.
  177. auto& map = m_counters;
  178. // 2. If map[label] exists, set map[label] to map[label] + 1.
  179. if (auto found = map.find(label); found != map.end()) {
  180. map.set(label, found->value + 1);
  181. }
  182. // 3. Otherwise, set map[label] to 1.
  183. else {
  184. map.set(label, 1);
  185. }
  186. // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and ToString(map[label]).
  187. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, map.get(label).value()));
  188. // 5. Perform Logger("count", « concat »).
  189. MarkedVector<Value> concat_as_vector { vm.heap() };
  190. concat_as_vector.append(PrimitiveString::create(vm, move(concat)));
  191. if (m_client)
  192. TRY(m_client->logger(LogLevel::Count, concat_as_vector));
  193. return js_undefined();
  194. }
  195. // 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset
  196. ThrowCompletionOr<Value> Console::count_reset()
  197. {
  198. auto& vm = realm().vm();
  199. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-countreset
  200. auto label = TRY(label_or_fallback(vm, "default"sv));
  201. // 1. Let map be the associated count map.
  202. auto& map = m_counters;
  203. // 2. If map[label] exists, set map[label] to 0.
  204. if (auto found = map.find(label); found != map.end()) {
  205. map.set(label, 0);
  206. }
  207. // 3. Otherwise:
  208. else {
  209. // 1. Let message be a string without any formatting specifiers indicating generically
  210. // that the given label does not have an associated count.
  211. auto message = TRY_OR_THROW_OOM(vm, String::formatted("\"{}\" doesn't have a count", label));
  212. // 2. Perform Logger("countReset", « message »);
  213. MarkedVector<Value> message_as_vector { vm.heap() };
  214. message_as_vector.append(PrimitiveString::create(vm, move(message)));
  215. if (m_client)
  216. TRY(m_client->logger(LogLevel::CountReset, message_as_vector));
  217. }
  218. return js_undefined();
  219. }
  220. // 1.3.1. group(...data), https://console.spec.whatwg.org/#group
  221. ThrowCompletionOr<Value> Console::group()
  222. {
  223. auto& vm = realm().vm();
  224. // 1. Let group be a new group.
  225. Group group;
  226. // 2. If data is not empty, let groupLabel be the result of Formatter(data).
  227. String group_label {};
  228. auto data = vm_arguments();
  229. if (!data.is_empty()) {
  230. auto formatted_data = TRY(m_client->formatter(data));
  231. group_label = TRY(value_vector_to_string(formatted_data));
  232. }
  233. // ... Otherwise, let groupLabel be an implementation-chosen label representing a group.
  234. else {
  235. group_label = TRY_OR_THROW_OOM(vm, "Group"_string);
  236. }
  237. // 3. Incorporate groupLabel as a label for group.
  238. group.label = group_label;
  239. // 4. Optionally, if the environment supports interactive groups, group should be expanded by default.
  240. // NOTE: This is handled in Printer.
  241. // 5. Perform Printer("group", « group »).
  242. if (m_client)
  243. TRY(m_client->printer(LogLevel::Group, group));
  244. // 6. Push group onto the appropriate group stack.
  245. m_group_stack.append(group);
  246. return js_undefined();
  247. }
  248. // 1.3.2. groupCollapsed(...data), https://console.spec.whatwg.org/#groupcollapsed
  249. ThrowCompletionOr<Value> Console::group_collapsed()
  250. {
  251. auto& vm = realm().vm();
  252. // 1. Let group be a new group.
  253. Group group;
  254. // 2. If data is not empty, let groupLabel be the result of Formatter(data).
  255. String group_label {};
  256. auto data = vm_arguments();
  257. if (!data.is_empty()) {
  258. auto formatted_data = TRY(m_client->formatter(data));
  259. group_label = TRY(value_vector_to_string(formatted_data));
  260. }
  261. // ... Otherwise, let groupLabel be an implementation-chosen label representing a group.
  262. else {
  263. group_label = TRY_OR_THROW_OOM(vm, "Group"_string);
  264. }
  265. // 3. Incorporate groupLabel as a label for group.
  266. group.label = group_label;
  267. // 4. Optionally, if the environment supports interactive groups, group should be collapsed by default.
  268. // NOTE: This is handled in Printer.
  269. // 5. Perform Printer("groupCollapsed", « group »).
  270. if (m_client)
  271. TRY(m_client->printer(LogLevel::GroupCollapsed, group));
  272. // 6. Push group onto the appropriate group stack.
  273. m_group_stack.append(group);
  274. return js_undefined();
  275. }
  276. // 1.3.3. groupEnd(), https://console.spec.whatwg.org/#groupend
  277. ThrowCompletionOr<Value> Console::group_end()
  278. {
  279. if (m_group_stack.is_empty())
  280. return js_undefined();
  281. // 1. Pop the last group from the group stack.
  282. m_group_stack.take_last();
  283. if (m_client)
  284. m_client->end_group();
  285. return js_undefined();
  286. }
  287. // 1.4.1. time(label), https://console.spec.whatwg.org/#time
  288. ThrowCompletionOr<Value> Console::time()
  289. {
  290. auto& vm = realm().vm();
  291. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-time
  292. auto label = TRY(label_or_fallback(vm, "default"sv));
  293. // 1. If the associated timer table contains an entry with key label, return, optionally reporting
  294. // a warning to the console indicating that a timer with label `label` has already been started.
  295. if (m_timer_table.contains(label)) {
  296. if (m_client) {
  297. MarkedVector<Value> timer_already_exists_warning_message_as_vector { vm.heap() };
  298. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' already exists.", label));
  299. timer_already_exists_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  300. TRY(m_client->printer(LogLevel::Warn, move(timer_already_exists_warning_message_as_vector)));
  301. }
  302. return js_undefined();
  303. }
  304. // 2. Otherwise, set the value of the entry with key label in the associated timer table to the current time.
  305. m_timer_table.set(label, Core::ElapsedTimer::start_new());
  306. return js_undefined();
  307. }
  308. // 1.4.2. timeLog(label, ...data), https://console.spec.whatwg.org/#timelog
  309. ThrowCompletionOr<Value> Console::time_log()
  310. {
  311. auto& vm = realm().vm();
  312. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timelog
  313. auto label = TRY(label_or_fallback(vm, "default"sv));
  314. // 1. Let timerTable be the associated timer table.
  315. // 2. Let startTime be timerTable[label].
  316. auto maybe_start_time = m_timer_table.find(label);
  317. // NOTE: Warn if the timer doesn't exist. Not part of the spec yet, but discussed here: https://github.com/whatwg/console/issues/134
  318. if (maybe_start_time == m_timer_table.end()) {
  319. if (m_client) {
  320. MarkedVector<Value> timer_does_not_exist_warning_message_as_vector { vm.heap() };
  321. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' does not exist.", label));
  322. timer_does_not_exist_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  323. TRY(m_client->printer(LogLevel::Warn, move(timer_does_not_exist_warning_message_as_vector)));
  324. }
  325. return js_undefined();
  326. }
  327. auto start_time = maybe_start_time->value;
  328. // 3. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format.
  329. auto duration = TRY(format_time_since(start_time));
  330. // 4. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration.
  331. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration));
  332. // 5. Prepend concat to data.
  333. MarkedVector<Value> data { vm.heap() };
  334. data.ensure_capacity(vm.argument_count());
  335. data.append(PrimitiveString::create(vm, move(concat)));
  336. for (size_t i = 1; i < vm.argument_count(); ++i)
  337. data.append(vm.argument(i));
  338. // 6. Perform Printer("timeLog", data).
  339. if (m_client)
  340. TRY(m_client->printer(LogLevel::TimeLog, move(data)));
  341. return js_undefined();
  342. }
  343. // 1.4.3. timeEnd(label), https://console.spec.whatwg.org/#timeend
  344. ThrowCompletionOr<Value> Console::time_end()
  345. {
  346. auto& vm = realm().vm();
  347. // NOTE: "default" is the default value in the IDL. https://console.spec.whatwg.org/#ref-for-timeend
  348. auto label = TRY(label_or_fallback(vm, "default"sv));
  349. // 1. Let timerTable be the associated timer table.
  350. // 2. Let startTime be timerTable[label].
  351. auto maybe_start_time = m_timer_table.find(label);
  352. // NOTE: Warn if the timer doesn't exist. Not part of the spec yet, but discussed here: https://github.com/whatwg/console/issues/134
  353. if (maybe_start_time == m_timer_table.end()) {
  354. if (m_client) {
  355. MarkedVector<Value> timer_does_not_exist_warning_message_as_vector { vm.heap() };
  356. auto message = TRY_OR_THROW_OOM(vm, String::formatted("Timer '{}' does not exist.", label));
  357. timer_does_not_exist_warning_message_as_vector.append(PrimitiveString::create(vm, move(message)));
  358. TRY(m_client->printer(LogLevel::Warn, move(timer_does_not_exist_warning_message_as_vector)));
  359. }
  360. return js_undefined();
  361. }
  362. auto start_time = maybe_start_time->value;
  363. // 3. Remove timerTable[label].
  364. m_timer_table.remove(label);
  365. // 4. Let duration be a string representing the difference between the current time and startTime, in an implementation-defined format.
  366. auto duration = TRY(format_time_since(start_time));
  367. // 5. Let concat be the concatenation of label, U+003A (:), U+0020 SPACE, and duration.
  368. auto concat = TRY_OR_THROW_OOM(vm, String::formatted("{}: {}", label, duration));
  369. // 6. Perform Printer("timeEnd", « concat »).
  370. if (m_client) {
  371. MarkedVector<Value> concat_as_vector { vm.heap() };
  372. concat_as_vector.append(PrimitiveString::create(vm, move(concat)));
  373. TRY(m_client->printer(LogLevel::TimeEnd, move(concat_as_vector)));
  374. }
  375. return js_undefined();
  376. }
  377. MarkedVector<Value> Console::vm_arguments()
  378. {
  379. auto& vm = realm().vm();
  380. MarkedVector<Value> arguments { vm.heap() };
  381. arguments.ensure_capacity(vm.argument_count());
  382. for (size_t i = 0; i < vm.argument_count(); ++i) {
  383. arguments.append(vm.argument(i));
  384. }
  385. return arguments;
  386. }
  387. void Console::output_debug_message(LogLevel log_level, String const& output) const
  388. {
  389. switch (log_level) {
  390. case Console::LogLevel::Debug:
  391. dbgln("\033[32;1m(js debug)\033[0m {}", output);
  392. break;
  393. case Console::LogLevel::Error:
  394. dbgln("\033[32;1m(js error)\033[0m {}", output);
  395. break;
  396. case Console::LogLevel::Info:
  397. dbgln("\033[32;1m(js info)\033[0m {}", output);
  398. break;
  399. case Console::LogLevel::Log:
  400. dbgln("\033[32;1m(js log)\033[0m {}", output);
  401. break;
  402. case Console::LogLevel::Warn:
  403. dbgln("\033[32;1m(js warn)\033[0m {}", output);
  404. break;
  405. default:
  406. dbgln("\033[32;1m(js)\033[0m {}", output);
  407. break;
  408. }
  409. }
  410. void Console::report_exception(JS::Error const& exception, bool in_promise) const
  411. {
  412. if (m_client)
  413. m_client->report_exception(exception, in_promise);
  414. }
  415. ThrowCompletionOr<String> Console::value_vector_to_string(MarkedVector<Value> const& values)
  416. {
  417. auto& vm = realm().vm();
  418. ThrowableStringBuilder builder(vm);
  419. for (auto const& item : values) {
  420. if (!builder.is_empty())
  421. MUST_OR_THROW_OOM(builder.append(' '));
  422. MUST_OR_THROW_OOM(builder.append(TRY(item.to_string(vm))));
  423. }
  424. return builder.to_string();
  425. }
  426. ThrowCompletionOr<String> Console::format_time_since(Core::ElapsedTimer timer)
  427. {
  428. auto& vm = realm().vm();
  429. auto elapsed_ms = timer.elapsed_time().to_milliseconds();
  430. auto duration = TRY(Temporal::balance_duration(vm, 0, 0, 0, 0, elapsed_ms, 0, "0"_sbigint, "year"sv));
  431. auto append = [&](ThrowableStringBuilder& builder, auto format, auto number) -> ThrowCompletionOr<void> {
  432. if (!builder.is_empty())
  433. MUST_OR_THROW_OOM(builder.append(' '));
  434. MUST_OR_THROW_OOM(builder.appendff(format, number));
  435. return {};
  436. };
  437. ThrowableStringBuilder builder(vm);
  438. if (duration.days > 0)
  439. MUST_OR_THROW_OOM(append(builder, "{:.0} day(s)"sv, duration.days));
  440. if (duration.hours > 0)
  441. MUST_OR_THROW_OOM(append(builder, "{:.0} hour(s)"sv, duration.hours));
  442. if (duration.minutes > 0)
  443. MUST_OR_THROW_OOM(append(builder, "{:.0} minute(s)"sv, duration.minutes));
  444. if (duration.seconds > 0 || duration.milliseconds > 0) {
  445. double combined_seconds = duration.seconds + (0.001 * duration.milliseconds);
  446. MUST_OR_THROW_OOM(append(builder, "{:.3} seconds"sv, combined_seconds));
  447. }
  448. return builder.to_string();
  449. }
  450. // 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger
  451. ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, MarkedVector<Value> const& args)
  452. {
  453. auto& vm = m_console.realm().vm();
  454. // 1. If args is empty, return.
  455. if (args.is_empty())
  456. return js_undefined();
  457. // 2. Let first be args[0].
  458. auto first = args[0];
  459. // 3. Let rest be all elements following first in args.
  460. size_t rest_size = args.size() - 1;
  461. // 4. If rest is empty, perform Printer(logLevel, « first ») and return.
  462. if (rest_size == 0) {
  463. MarkedVector<Value> first_as_vector { vm.heap() };
  464. first_as_vector.append(first);
  465. return printer(log_level, move(first_as_vector));
  466. }
  467. // 5. Otherwise, perform Printer(logLevel, Formatter(args)).
  468. else {
  469. auto formatted = TRY(formatter(args));
  470. TRY(printer(log_level, formatted));
  471. }
  472. // 6. Return undefined.
  473. return js_undefined();
  474. }
  475. // 2.2. Formatter(args), https://console.spec.whatwg.org/#formatter
  476. ThrowCompletionOr<MarkedVector<Value>> ConsoleClient::formatter(MarkedVector<Value> const& args)
  477. {
  478. auto& realm = m_console.realm();
  479. auto& vm = realm.vm();
  480. // 1. If args’s size is 1, return args.
  481. if (args.size() == 1)
  482. return args;
  483. // 2. Let target be the first element of args.
  484. auto target = (!args.is_empty()) ? TRY(args.first().to_string(vm)) : String {};
  485. // 3. Let current be the second element of args.
  486. auto current = (args.size() > 1) ? args[1] : js_undefined();
  487. // 4. Find the first possible format specifier specifier, from the left to the right in target.
  488. auto find_specifier = [](StringView target) -> Optional<StringView> {
  489. size_t start_index = 0;
  490. while (start_index < target.length()) {
  491. auto maybe_index = target.find('%');
  492. if (!maybe_index.has_value())
  493. return {};
  494. auto index = maybe_index.value();
  495. if (index + 1 >= target.length())
  496. return {};
  497. switch (target[index + 1]) {
  498. case 'c':
  499. case 'd':
  500. case 'f':
  501. case 'i':
  502. case 'o':
  503. case 'O':
  504. case 's':
  505. return target.substring_view(index, 2);
  506. }
  507. start_index = index + 1;
  508. }
  509. return {};
  510. };
  511. auto maybe_specifier = find_specifier(target);
  512. // 5. If no format specifier was found, return args.
  513. if (!maybe_specifier.has_value()) {
  514. return args;
  515. }
  516. // 6. Otherwise:
  517. else {
  518. auto specifier = maybe_specifier.release_value();
  519. Optional<Value> converted;
  520. // 1. If specifier is %s, let converted be the result of Call(%String%, undefined, « current »).
  521. if (specifier == "%s"sv) {
  522. converted = TRY(call(vm, *realm.intrinsics().string_constructor(), js_undefined(), current));
  523. }
  524. // 2. If specifier is %d or %i:
  525. else if (specifier.is_one_of("%d"sv, "%i"sv)) {
  526. // 1. If Type(current) is Symbol, let converted be NaN
  527. if (current.is_symbol()) {
  528. converted = js_nan();
  529. }
  530. // 2. Otherwise, let converted be the result of Call(%parseInt%, undefined, « current, 10 »).
  531. else {
  532. converted = TRY(call(vm, *realm.intrinsics().parse_int_function(), js_undefined(), current, Value { 10 }));
  533. }
  534. }
  535. // 3. If specifier is %f:
  536. else if (specifier == "%f"sv) {
  537. // 1. If Type(current) is Symbol, let converted be NaN
  538. if (current.is_symbol()) {
  539. converted = js_nan();
  540. }
  541. // 2. Otherwise, let converted be the result of Call(% parseFloat %, undefined, « current »).
  542. else {
  543. converted = TRY(call(vm, *realm.intrinsics().parse_float_function(), js_undefined(), current));
  544. }
  545. }
  546. // 4. If specifier is %o, optionally let converted be current with optimally useful formatting applied.
  547. else if (specifier == "%o"sv) {
  548. // TODO: "Optimally-useful formatting"
  549. converted = current;
  550. }
  551. // 5. If specifier is %O, optionally let converted be current with generic JavaScript object formatting applied.
  552. else if (specifier == "%O"sv) {
  553. // TODO: "generic JavaScript object formatting"
  554. converted = current;
  555. }
  556. // 6. TODO: process %c
  557. else if (specifier == "%c"sv) {
  558. // NOTE: This has no spec yet. `%c` specifiers treat the argument as CSS styling for the log message.
  559. add_css_style_to_current_message(TRY(current.to_string(vm)));
  560. converted = PrimitiveString::create(vm, String {});
  561. }
  562. // 7. If any of the previous steps set converted, replace specifier in target with converted.
  563. if (converted.has_value())
  564. target = TRY_OR_THROW_OOM(vm, target.replace(specifier, TRY(converted->to_string(vm)), ReplaceMode::FirstOnly));
  565. }
  566. // 7. Let result be a list containing target together with the elements of args starting from the third onward.
  567. MarkedVector<Value> result { vm.heap() };
  568. result.ensure_capacity(args.size() - 1);
  569. result.empend(PrimitiveString::create(vm, move(target)));
  570. for (size_t i = 2; i < args.size(); ++i)
  571. result.unchecked_append(args[i]);
  572. // 8. Return Formatter(result).
  573. return formatter(result);
  574. }
  575. ThrowCompletionOr<String> ConsoleClient::generically_format_values(MarkedVector<Value> const& values)
  576. {
  577. AllocatingMemoryStream stream;
  578. auto& vm = m_console.realm().vm();
  579. PrintContext ctx { vm, stream, true };
  580. bool first = true;
  581. for (auto const& value : values) {
  582. if (!first)
  583. TRY_OR_THROW_OOM(vm, stream.write_until_depleted(" "sv.bytes()));
  584. TRY_OR_THROW_OOM(vm, JS::print(value, ctx));
  585. first = false;
  586. }
  587. // FIXME: Is it possible we could end up serializing objects to invalid UTF-8?
  588. return TRY_OR_THROW_OOM(vm, String::from_stream(stream, stream.used_buffer_size()));
  589. }
  590. }