ConsoleObject.cpp 3.4 KB

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