1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 16:55:09 +00:00

LibJS: Implement ConsoleClient

Now, you can optionally specify a ConsoleClient, to customise the
behaviour of the LibJS Console.

To customise the console, create a new ConsoleClient class that inherits
from JS::ConsoleClient and override all the abstract methods.

When Console::log() is called, if Console has a ConsoleClient,
ConsoleClient::log() is called instead.

These abstract methods are Value(void) functions: you can return a Value
which will be returned by the JavaScript function which calls that
method, in JavaScript.
This commit is contained in:
Emanuele Torre 2020-05-04 15:57:05 +02:00 committed by Andreas Kling
parent bc7ed4524e
commit e91ab0cb02
2 changed files with 56 additions and 0 deletions

View file

@ -39,42 +39,63 @@ Console::Console(Interpreter& interpreter)
Value Console::debug()
{
if (m_client)
return m_client->debug();
dbg() << "debug: " << m_interpreter.join_arguments();
return js_undefined();
}
Value Console::error()
{
if (m_client)
return m_client->error();
dbg() << "error: " << m_interpreter.join_arguments();
return js_undefined();
}
Value Console::info()
{
if (m_client)
return m_client->info();
dbg() << "info: " << m_interpreter.join_arguments();
return js_undefined();
}
Value Console::log()
{
if (m_client)
return m_client->log();
dbg() << "log: " << m_interpreter.join_arguments();
return js_undefined();
}
Value Console::warn()
{
if (m_client)
return m_client->warn();
dbg() << "warn: " << m_interpreter.join_arguments();
return js_undefined();
}
Value Console::clear()
{
if (m_client)
return m_client->clear();
dbg() << "clear:";
return js_undefined();
}
Value Console::trace()
{
if (m_client)
return m_client->trace();
StringBuilder message_text;
message_text.append(m_interpreter.join_arguments());
@ -92,6 +113,9 @@ Value Console::trace()
Value Console::count()
{
if (m_client)
return m_client->count();
auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
auto counter_value = counter_increment(label);
@ -102,6 +126,9 @@ Value Console::count()
Value Console::count_reset()
{
if (m_client)
return m_client->count_reset();
auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
if (counter_reset(label))

View file

@ -33,6 +33,8 @@
namespace JS {
class ConsoleClient;
class Console {
AK_MAKE_NONCOPYABLE(Console);
AK_MAKE_NONMOVABLE(Console);
@ -40,6 +42,8 @@ class Console {
public:
Console(Interpreter&);
void set_client(ConsoleClient& client) { m_client = &client; }
Interpreter& interpreter() { return m_interpreter; }
const Interpreter& interpreter() const { return m_interpreter; }
@ -64,8 +68,33 @@ public:
private:
Interpreter& m_interpreter;
ConsoleClient* m_client { nullptr };
HashMap<String, unsigned> m_counters;
};
class ConsoleClient {
public:
ConsoleClient(Console& console)
: m_console(console)
{
}
virtual Value debug() = 0;
virtual Value error() = 0;
virtual Value info() = 0;
virtual Value log() = 0;
virtual Value warn() = 0;
virtual Value clear() = 0;
virtual Value trace() = 0;
virtual Value count() = 0;
virtual Value count_reset() = 0;
protected:
Interpreter& interpreter() { return m_console.interpreter(); }
const Interpreter& interpreter() const { return m_console.interpreter(); }
Console& m_console;
};
}