ConsoleObject.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  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 <AK/FlyString.h>
  28. #include <AK/Function.h>
  29. #include <LibJS/Interpreter.h>
  30. #include <LibJS/Runtime/ConsoleObject.h>
  31. #include <stdio.h>
  32. namespace JS {
  33. static void print_args(Interpreter& interpreter)
  34. {
  35. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  36. printf("%s", interpreter.argument(i).to_string().characters());
  37. if (i != interpreter.argument_count() - 1)
  38. putchar(' ');
  39. }
  40. putchar('\n');
  41. }
  42. ConsoleObject::ConsoleObject()
  43. {
  44. put_native_function("log", log);
  45. put_native_function("debug", debug);
  46. put_native_function("info", info);
  47. put_native_function("warn", warn);
  48. put_native_function("error", error);
  49. put_native_function("trace", trace);
  50. }
  51. ConsoleObject::~ConsoleObject()
  52. {
  53. }
  54. Value ConsoleObject::log(Interpreter& interpreter)
  55. {
  56. print_args(interpreter);
  57. return js_undefined();
  58. }
  59. Value ConsoleObject::debug(Interpreter& interpreter)
  60. {
  61. printf("\033[36;1m");
  62. print_args(interpreter);
  63. printf("\033[0m");
  64. return js_undefined();
  65. }
  66. Value ConsoleObject::info(Interpreter& interpreter)
  67. {
  68. print_args(interpreter);
  69. return js_undefined();
  70. }
  71. Value ConsoleObject::warn(Interpreter& interpreter)
  72. {
  73. printf("\033[33;1m");
  74. print_args(interpreter);
  75. printf("\033[0m");
  76. return js_undefined();
  77. }
  78. Value ConsoleObject::error(Interpreter& interpreter)
  79. {
  80. printf("\033[31;1m");
  81. print_args(interpreter);
  82. printf("\033[0m");
  83. return js_undefined();
  84. }
  85. Value ConsoleObject::trace(Interpreter& interpreter)
  86. {
  87. print_args(interpreter);
  88. auto call_stack = interpreter.call_stack();
  89. // -2 to skip the console.trace() call frame
  90. for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
  91. auto function_name = call_stack[i].function_name;
  92. if (String(function_name).is_empty())
  93. function_name = "<anonymous>";
  94. printf("%s\n", function_name.characters());
  95. }
  96. return js_undefined();
  97. }
  98. }