Console.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <LibJS/Console.h>
  28. #include <LibJS/Interpreter.h>
  29. namespace JS {
  30. Console::Console(Interpreter& interpreter)
  31. : m_interpreter(interpreter)
  32. {
  33. }
  34. Value Console::debug()
  35. {
  36. if (m_client)
  37. return m_client->debug();
  38. return js_undefined();
  39. }
  40. Value Console::error()
  41. {
  42. if (m_client)
  43. return m_client->error();
  44. return js_undefined();
  45. }
  46. Value Console::info()
  47. {
  48. if (m_client)
  49. return m_client->info();
  50. return js_undefined();
  51. }
  52. Value Console::log()
  53. {
  54. if (m_client)
  55. return m_client->log();
  56. return js_undefined();
  57. }
  58. Value Console::warn()
  59. {
  60. if (m_client)
  61. return m_client->warn();
  62. return js_undefined();
  63. }
  64. Value Console::clear()
  65. {
  66. if (m_client)
  67. return m_client->clear();
  68. return js_undefined();
  69. }
  70. Value Console::trace()
  71. {
  72. if (m_client)
  73. return m_client->trace();
  74. return js_undefined();
  75. }
  76. Value Console::count()
  77. {
  78. if (m_client)
  79. return m_client->count();
  80. return js_undefined();
  81. }
  82. Value Console::count_reset()
  83. {
  84. if (m_client)
  85. return m_client->count_reset();
  86. return js_undefined();
  87. }
  88. unsigned Console::counter_increment(String label)
  89. {
  90. auto value = m_counters.get(label);
  91. if (!value.has_value()) {
  92. m_counters.set(label, 1);
  93. return 1;
  94. }
  95. auto new_value = value.value() + 1;
  96. m_counters.set(label, new_value);
  97. return new_value;
  98. }
  99. bool Console::counter_reset(String label)
  100. {
  101. if (!m_counters.contains(label))
  102. return false;
  103. m_counters.remove(label);
  104. return true;
  105. }
  106. Vector<String> ConsoleClient::get_trace() const
  107. {
  108. Vector<String> trace;
  109. auto call_stack = m_console.interpreter().call_stack();
  110. // -2 to skip the console.trace() call frame
  111. for (ssize_t i = call_stack.size() - 2; i >= 0; --i)
  112. trace.append(call_stack[i].function_name);
  113. return trace;
  114. }
  115. }