Console.cpp 19 KB

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