ConsoleObject.cpp 4.2 KB

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