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:
parent
bc7ed4524e
commit
e91ab0cb02
2 changed files with 56 additions and 0 deletions
|
@ -39,42 +39,63 @@ Console::Console(Interpreter& interpreter)
|
||||||
|
|
||||||
Value Console::debug()
|
Value Console::debug()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->debug();
|
||||||
|
|
||||||
dbg() << "debug: " << m_interpreter.join_arguments();
|
dbg() << "debug: " << m_interpreter.join_arguments();
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::error()
|
Value Console::error()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->error();
|
||||||
|
|
||||||
dbg() << "error: " << m_interpreter.join_arguments();
|
dbg() << "error: " << m_interpreter.join_arguments();
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::info()
|
Value Console::info()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->info();
|
||||||
|
|
||||||
dbg() << "info: " << m_interpreter.join_arguments();
|
dbg() << "info: " << m_interpreter.join_arguments();
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::log()
|
Value Console::log()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->log();
|
||||||
|
|
||||||
dbg() << "log: " << m_interpreter.join_arguments();
|
dbg() << "log: " << m_interpreter.join_arguments();
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::warn()
|
Value Console::warn()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->warn();
|
||||||
|
|
||||||
dbg() << "warn: " << m_interpreter.join_arguments();
|
dbg() << "warn: " << m_interpreter.join_arguments();
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::clear()
|
Value Console::clear()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->clear();
|
||||||
|
|
||||||
dbg() << "clear:";
|
dbg() << "clear:";
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value Console::trace()
|
Value Console::trace()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->trace();
|
||||||
|
|
||||||
StringBuilder message_text;
|
StringBuilder message_text;
|
||||||
message_text.append(m_interpreter.join_arguments());
|
message_text.append(m_interpreter.join_arguments());
|
||||||
|
|
||||||
|
@ -92,6 +113,9 @@ Value Console::trace()
|
||||||
|
|
||||||
Value Console::count()
|
Value Console::count()
|
||||||
{
|
{
|
||||||
|
if (m_client)
|
||||||
|
return m_client->count();
|
||||||
|
|
||||||
auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
|
auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
|
||||||
|
|
||||||
auto counter_value = counter_increment(label);
|
auto counter_value = counter_increment(label);
|
||||||
|
@ -102,6 +126,9 @@ Value Console::count()
|
||||||
|
|
||||||
Value Console::count_reset()
|
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";
|
auto label = m_interpreter.argument_count() ? m_interpreter.argument(0).to_string() : "default";
|
||||||
|
|
||||||
if (counter_reset(label))
|
if (counter_reset(label))
|
||||||
|
|
|
@ -33,6 +33,8 @@
|
||||||
|
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
|
class ConsoleClient;
|
||||||
|
|
||||||
class Console {
|
class Console {
|
||||||
AK_MAKE_NONCOPYABLE(Console);
|
AK_MAKE_NONCOPYABLE(Console);
|
||||||
AK_MAKE_NONMOVABLE(Console);
|
AK_MAKE_NONMOVABLE(Console);
|
||||||
|
@ -40,6 +42,8 @@ class Console {
|
||||||
public:
|
public:
|
||||||
Console(Interpreter&);
|
Console(Interpreter&);
|
||||||
|
|
||||||
|
void set_client(ConsoleClient& client) { m_client = &client; }
|
||||||
|
|
||||||
Interpreter& interpreter() { return m_interpreter; }
|
Interpreter& interpreter() { return m_interpreter; }
|
||||||
const Interpreter& interpreter() const { return m_interpreter; }
|
const Interpreter& interpreter() const { return m_interpreter; }
|
||||||
|
|
||||||
|
@ -64,8 +68,33 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Interpreter& m_interpreter;
|
Interpreter& m_interpreter;
|
||||||
|
ConsoleClient* m_client { nullptr };
|
||||||
|
|
||||||
HashMap<String, unsigned> m_counters;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue