Console.cpp 22 KB

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