ConsoleObject.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/StringBuilder.h>
  31. #include <LibJS/Console.h>
  32. #include <LibJS/Interpreter.h>
  33. #include <LibJS/Runtime/ConsoleObject.h>
  34. #include <LibJS/Runtime/GlobalObject.h>
  35. namespace JS {
  36. static String join_args(Interpreter& interpreter)
  37. {
  38. StringBuilder joined_arguments;
  39. for (size_t i = 0; i < interpreter.argument_count(); ++i) {
  40. joined_arguments.append(interpreter.argument(i).to_string().characters());
  41. if (i != interpreter.argument_count() - 1)
  42. joined_arguments.append(' ');
  43. }
  44. return joined_arguments.build();
  45. }
  46. ConsoleObject::ConsoleObject()
  47. : Object(interpreter().global_object().object_prototype())
  48. {
  49. put_native_function("log", log);
  50. put_native_function("debug", debug);
  51. put_native_function("info", info);
  52. put_native_function("warn", warn);
  53. put_native_function("error", error);
  54. put_native_function("trace", trace);
  55. put_native_function("count", count);
  56. put_native_function("countReset", count_reset);
  57. put_native_function("clear", clear);
  58. }
  59. ConsoleObject::~ConsoleObject()
  60. {
  61. }
  62. Value ConsoleObject::log(Interpreter& interpreter)
  63. {
  64. interpreter.console().log(join_args(interpreter));
  65. return js_undefined();
  66. }
  67. Value ConsoleObject::debug(Interpreter& interpreter)
  68. {
  69. interpreter.console().debug(join_args(interpreter));
  70. return js_undefined();
  71. }
  72. Value ConsoleObject::info(Interpreter& interpreter)
  73. {
  74. interpreter.console().info(join_args(interpreter));
  75. return js_undefined();
  76. }
  77. Value ConsoleObject::warn(Interpreter& interpreter)
  78. {
  79. interpreter.console().warn(join_args(interpreter));
  80. return js_undefined();
  81. }
  82. Value ConsoleObject::error(Interpreter& interpreter)
  83. {
  84. interpreter.console().error(join_args(interpreter));
  85. return js_undefined();
  86. }
  87. Value ConsoleObject::trace(Interpreter& interpreter)
  88. {
  89. interpreter.console().trace(join_args(interpreter));
  90. return js_undefined();
  91. }
  92. Value ConsoleObject::count(Interpreter& interpreter)
  93. {
  94. if (interpreter.argument_count())
  95. interpreter.console().count(interpreter.argument(0).to_string());
  96. else
  97. interpreter.console().count();
  98. return js_undefined();
  99. }
  100. Value ConsoleObject::count_reset(Interpreter& interpreter)
  101. {
  102. if (interpreter.argument_count())
  103. interpreter.console().count_reset(interpreter.argument(0).to_string());
  104. else
  105. interpreter.console().count_reset();
  106. return js_undefined();
  107. }
  108. Value ConsoleObject::clear(Interpreter& interpreter)
  109. {
  110. interpreter.console().clear();
  111. return js_undefined();
  112. }
  113. }