1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +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

@ -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;
};
}