mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 15:28:11 +00:00
LibJS: Add console.{debug,info,warn,error}()
This commit is contained in:
parent
b1e8cc22bd
commit
dd7796515f
3 changed files with 56 additions and 7 deletions
5
Base/home/anon/js/console.js
Normal file
5
Base/home/anon/js/console.js
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
console.log("I am a generic log message");
|
||||||
|
console.debug("I am a debug log message");
|
||||||
|
console.info("I am an info log message");
|
||||||
|
console.warn("I am a warning log message");
|
||||||
|
console.error("I am an error log message");
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -32,9 +33,23 @@
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
static void print_args(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
|
||||||
|
printf("%s", interpreter.argument(i).to_string().characters());
|
||||||
|
if (i != interpreter.argument_count() - 1)
|
||||||
|
putchar(' ');
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
ConsoleObject::ConsoleObject()
|
ConsoleObject::ConsoleObject()
|
||||||
{
|
{
|
||||||
put_native_function("log", log);
|
put_native_function("log", log);
|
||||||
|
put_native_function("debug", debug);
|
||||||
|
put_native_function("info", info);
|
||||||
|
put_native_function("warn", warn);
|
||||||
|
put_native_function("error", error);
|
||||||
put_native_function("trace", trace);
|
put_native_function("trace", trace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,18 +59,43 @@ ConsoleObject::~ConsoleObject()
|
||||||
|
|
||||||
Value ConsoleObject::log(Interpreter& interpreter)
|
Value ConsoleObject::log(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
|
print_args(interpreter);
|
||||||
printf("%s", interpreter.argument(i).to_string().characters());
|
return js_undefined();
|
||||||
if (i != interpreter.argument_count() - 1)
|
|
||||||
putchar(' ');
|
|
||||||
}
|
}
|
||||||
putchar('\n');
|
|
||||||
|
Value ConsoleObject::debug(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
printf("\033[36;1m");
|
||||||
|
print_args(interpreter);
|
||||||
|
printf("\033[0m");
|
||||||
|
return js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value ConsoleObject::info(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
print_args(interpreter);
|
||||||
|
return js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value ConsoleObject::warn(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
printf("\033[33;1m");
|
||||||
|
print_args(interpreter);
|
||||||
|
printf("\033[0m");
|
||||||
|
return js_undefined();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value ConsoleObject::error(Interpreter& interpreter)
|
||||||
|
{
|
||||||
|
printf("\033[31;1m");
|
||||||
|
print_args(interpreter);
|
||||||
|
printf("\033[0m");
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value ConsoleObject::trace(Interpreter& interpreter)
|
Value ConsoleObject::trace(Interpreter& interpreter)
|
||||||
{
|
{
|
||||||
log(interpreter);
|
print_args(interpreter);
|
||||||
auto call_stack = interpreter.call_stack();
|
auto call_stack = interpreter.call_stack();
|
||||||
// -2 to skip the console.trace() call frame
|
// -2 to skip the console.trace() call frame
|
||||||
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
|
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
|
||||||
|
|
|
@ -39,6 +39,10 @@ private:
|
||||||
virtual const char* class_name() const override { return "ConsoleObject"; }
|
virtual const char* class_name() const override { return "ConsoleObject"; }
|
||||||
|
|
||||||
static Value log(Interpreter&);
|
static Value log(Interpreter&);
|
||||||
|
static Value debug(Interpreter&);
|
||||||
|
static Value info(Interpreter&);
|
||||||
|
static Value warn(Interpreter&);
|
||||||
|
static Value error(Interpreter&);
|
||||||
static Value trace(Interpreter&);
|
static Value trace(Interpreter&);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue