ladybird/Libraries/LibJS/Console.h

136 lines
3.6 KiB
C
Raw Permalink Normal View History

/*
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
LibJS: Add ConsoleMessage concept A ConsoleMessage is a struct cointaining: * AK::String text; represents the text of the message sent to the console. * ConsoleMessageKind kind; represents the kind of JS `console` function from which the message was sent. Now, Javascript `console` functions only send a ConsoleMessage to the Interpreter's Console instead of printing text directly to stdout. The Console then stores the recived ConsoleMessage in Console::m_messages; the Console does not print to stdout by default. You can set Console::on_new_message to a void(ConsoleMessage&); this function will get call everytime a new message is added to the Console's messages and can be used, for example, to print ConsoleMessages to stdout or to color the output based on the kind of ConsoleMessage. In this patch, I also: * Re-implement all the previously implemented functions in the JavaScript ConsoleObject, as wrappers around Console functions that add new message to the Console. * Implement console.clear() like so: - m_messages get cleared; - a new_message with kind set ConsoleMessageKind::Clear gets added to m_messages, its text is an empty AK::String; * Give credit to linusg in Console.cpp since I used his console.trace() algorithm in Console::trace(). I think that having this abstration will help us in the implementation of a browser console or a JS debugger. We could also add more MetaData to ConsoleMessage, e.g. Object IDs of the arguments passed to console functions in order to make hyperlinks, Timestamps, ecc.; which could be interesting to see. This will also help in implementing a `/bin/js` option to make, for example, return a ConsoleMessageWrapper to console functions instead of undefined. This will be useful to make tests for functions like console.count() and console.countClear(). :^)
2020-05-01 15:34:43 +00:00
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/ElapsedTimer.h>
#include <LibGC/CellAllocator.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class ConsoleClient;
// https://console.spec.whatwg.org
class Console : public Cell {
GC_CELL(Console, Cell);
GC_DECLARE_ALLOCATOR(Console);
public:
virtual ~Console() override;
// These are not really levels, but that's the term used in the spec.
enum class LogLevel {
Assert,
Count,
CountReset,
Debug,
Dir,
DirXML,
Error,
Group,
GroupCollapsed,
Info,
Log,
TimeEnd,
TimeLog,
Table,
Trace,
Warn,
};
struct Group {
String label;
};
struct Trace {
String label;
Vector<String> stack;
};
void set_client(ConsoleClient& client) { m_client = &client; }
Realm& realm() const { return m_realm; }
GC::MarkedVector<Value> vm_arguments();
HashMap<String, unsigned>& counters() { return m_counters; }
HashMap<String, unsigned> const& counters() const { return m_counters; }
ThrowCompletionOr<Value> assert_();
Value clear();
ThrowCompletionOr<Value> debug();
ThrowCompletionOr<Value> error();
ThrowCompletionOr<Value> info();
ThrowCompletionOr<Value> log();
ThrowCompletionOr<Value> table();
ThrowCompletionOr<Value> trace();
ThrowCompletionOr<Value> warn();
ThrowCompletionOr<Value> dir();
ThrowCompletionOr<Value> count();
ThrowCompletionOr<Value> count_reset();
ThrowCompletionOr<Value> group();
ThrowCompletionOr<Value> group_collapsed();
ThrowCompletionOr<Value> group_end();
ThrowCompletionOr<Value> time();
ThrowCompletionOr<Value> time_log();
ThrowCompletionOr<Value> time_end();
LibJS: Add ConsoleMessage concept A ConsoleMessage is a struct cointaining: * AK::String text; represents the text of the message sent to the console. * ConsoleMessageKind kind; represents the kind of JS `console` function from which the message was sent. Now, Javascript `console` functions only send a ConsoleMessage to the Interpreter's Console instead of printing text directly to stdout. The Console then stores the recived ConsoleMessage in Console::m_messages; the Console does not print to stdout by default. You can set Console::on_new_message to a void(ConsoleMessage&); this function will get call everytime a new message is added to the Console's messages and can be used, for example, to print ConsoleMessages to stdout or to color the output based on the kind of ConsoleMessage. In this patch, I also: * Re-implement all the previously implemented functions in the JavaScript ConsoleObject, as wrappers around Console functions that add new message to the Console. * Implement console.clear() like so: - m_messages get cleared; - a new_message with kind set ConsoleMessageKind::Clear gets added to m_messages, its text is an empty AK::String; * Give credit to linusg in Console.cpp since I used his console.trace() algorithm in Console::trace(). I think that having this abstration will help us in the implementation of a browser console or a JS debugger. We could also add more MetaData to ConsoleMessage, e.g. Object IDs of the arguments passed to console functions in order to make hyperlinks, Timestamps, ecc.; which could be interesting to see. This will also help in implementing a `/bin/js` option to make, for example, return a ConsoleMessageWrapper to console functions instead of undefined. This will be useful to make tests for functions like console.count() and console.countClear(). :^)
2020-05-01 15:34:43 +00:00
void output_debug_message(LogLevel log_level, String const& output) const;
void report_exception(JS::Error const&, bool) const;
private:
explicit Console(Realm&);
virtual void visit_edges(Visitor&) override;
ThrowCompletionOr<String> value_vector_to_string(GC::MarkedVector<Value> const&);
GC::Ref<Realm> m_realm;
GC::Ptr<ConsoleClient> m_client;
HashMap<String, unsigned> m_counters;
HashMap<String, Core::ElapsedTimer> m_timer_table;
Vector<Group> m_group_stack;
};
class ConsoleClient : public Cell {
GC_CELL(ConsoleClient, Cell);
GC_DECLARE_ALLOCATOR(ConsoleClient);
public:
using PrinterArguments = Variant<Console::Group, Console::Trace, GC::MarkedVector<Value>>;
ThrowCompletionOr<Value> logger(Console::LogLevel log_level, GC::MarkedVector<Value> const& args);
ThrowCompletionOr<GC::MarkedVector<Value>> formatter(GC::MarkedVector<Value> const& args);
virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, PrinterArguments) = 0;
virtual void add_css_style_to_current_message(StringView) { }
virtual void report_exception(JS::Error const&, bool) { }
virtual void clear() = 0;
virtual void end_group() = 0;
ThrowCompletionOr<String> generically_format_values(GC::MarkedVector<Value> const&);
protected:
explicit ConsoleClient(Console&);
virtual ~ConsoleClient() override;
virtual void visit_edges(Visitor& visitor) override;
GC::Ref<Console> m_console;
};
}