summaryrefslogtreecommitdiff
path: root/Libraries/LibJS
diff options
context:
space:
mode:
authorEmanuele Torre <torreemanuele6@gmail.com>2020-05-04 15:57:05 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-05 09:15:16 +0200
commite91ab0cb02eaedfd6f08a40e05a4d4ccd8c96418 (patch)
tree6a93f7b5a2b8fffdca34d5b219edf503175702e3 /Libraries/LibJS
parentbc7ed4524e9d302521b78bc8578df7379dc8e0c3 (diff)
downloadserenity-e91ab0cb02eaedfd6f08a40e05a4d4ccd8c96418.zip
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.
Diffstat (limited to 'Libraries/LibJS')
-rw-r--r--Libraries/LibJS/Console.cpp27
-rw-r--r--Libraries/LibJS/Console.h29
2 files changed, 56 insertions, 0 deletions
diff --git a/Libraries/LibJS/Console.cpp b/Libraries/LibJS/Console.cpp
index 55038f13aa..8f82078055 100644
--- a/Libraries/LibJS/Console.cpp
+++ b/Libraries/LibJS/Console.cpp
@@ -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))
diff --git a/Libraries/LibJS/Console.h b/Libraries/LibJS/Console.h
index 39001be722..beb59031ce 100644
--- a/Libraries/LibJS/Console.h
+++ b/Libraries/LibJS/Console.h
@@ -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;
+};
+
}