ConsoleObject.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <LibJS/Console.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. put_native_function("countReset", count_reset);
  56. }
  57. ConsoleObject::~ConsoleObject()
  58. {
  59. }
  60. Value ConsoleObject::log(Interpreter& interpreter)
  61. {
  62. print_args(interpreter);
  63. return js_undefined();
  64. }
  65. Value ConsoleObject::debug(Interpreter& interpreter)
  66. {
  67. printf("\033[36;1m");
  68. print_args(interpreter);
  69. printf("\033[0m");
  70. return js_undefined();
  71. }
  72. Value ConsoleObject::info(Interpreter& interpreter)
  73. {
  74. print_args(interpreter);
  75. return js_undefined();
  76. }
  77. Value ConsoleObject::warn(Interpreter& interpreter)
  78. {
  79. printf("\033[33;1m");
  80. print_args(interpreter);
  81. printf("\033[0m");
  82. return js_undefined();
  83. }
  84. Value ConsoleObject::error(Interpreter& interpreter)
  85. {
  86. printf("\033[31;1m");
  87. print_args(interpreter);
  88. printf("\033[0m");
  89. return js_undefined();
  90. }
  91. Value ConsoleObject::trace(Interpreter& interpreter)
  92. {
  93. print_args(interpreter);
  94. auto call_stack = interpreter.call_stack();
  95. // -2 to skip the console.trace() call frame
  96. for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
  97. auto function_name = call_stack[i].function_name;
  98. if (String(function_name).is_empty())
  99. function_name = "<anonymous>";
  100. printf("%s\n", function_name.characters());
  101. }
  102. return js_undefined();
  103. }
  104. Value ConsoleObject::count(Interpreter& interpreter)
  105. {
  106. if (interpreter.argument_count())
  107. interpreter.console().count(interpreter.argument(0).to_string());
  108. else
  109. interpreter.console().count();
  110. return js_undefined();
  111. }
  112. Value ConsoleObject::count_reset(Interpreter& interpreter)
  113. {
  114. if (interpreter.argument_count())
  115. interpreter.console().count_reset(interpreter.argument(0).to_string());
  116. else
  117. interpreter.console().count_reset();
  118. return js_undefined();
  119. }
  120. }