ConsoleObject.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. : Object(interpreter().object_prototype())
  44. {
  45. put_native_function("log", log);
  46. put_native_function("debug", debug);
  47. put_native_function("info", info);
  48. put_native_function("warn", warn);
  49. put_native_function("error", error);
  50. put_native_function("trace", trace);
  51. }
  52. ConsoleObject::~ConsoleObject()
  53. {
  54. }
  55. Value ConsoleObject::log(Interpreter& interpreter)
  56. {
  57. print_args(interpreter);
  58. return js_undefined();
  59. }
  60. Value ConsoleObject::debug(Interpreter& interpreter)
  61. {
  62. printf("\033[36;1m");
  63. print_args(interpreter);
  64. printf("\033[0m");
  65. return js_undefined();
  66. }
  67. Value ConsoleObject::info(Interpreter& interpreter)
  68. {
  69. print_args(interpreter);
  70. return js_undefined();
  71. }
  72. Value ConsoleObject::warn(Interpreter& interpreter)
  73. {
  74. printf("\033[33;1m");
  75. print_args(interpreter);
  76. printf("\033[0m");
  77. return js_undefined();
  78. }
  79. Value ConsoleObject::error(Interpreter& interpreter)
  80. {
  81. printf("\033[31;1m");
  82. print_args(interpreter);
  83. printf("\033[0m");
  84. return js_undefined();
  85. }
  86. Value ConsoleObject::trace(Interpreter& interpreter)
  87. {
  88. print_args(interpreter);
  89. auto call_stack = interpreter.call_stack();
  90. // -2 to skip the console.trace() call frame
  91. for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
  92. auto function_name = call_stack[i].function_name;
  93. if (String(function_name).is_empty())
  94. function_name = "<anonymous>";
  95. printf("%s\n", function_name.characters());
  96. }
  97. return js_undefined();
  98. }
  99. }