2020-05-01 14:40:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
|
2021-04-22 22:51:19 +02:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2021-12-10 12:26:25 +00:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-05-01 14:40:43 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-01 14:40:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Console.h>
|
2020-09-29 21:15:06 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-05-01 14:40:43 +02:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-09-29 21:15:06 +02:00
|
|
|
Console::Console(GlobalObject& global_object)
|
|
|
|
: m_global_object(global_object)
|
2020-05-01 14:40:43 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-21 21:11:49 +01:00
|
|
|
VM& Console::vm()
|
|
|
|
{
|
|
|
|
return m_global_object.vm();
|
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug
|
|
|
|
ThrowCompletionOr<Value> Console::debug()
|
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 17:34:43 +02:00
|
|
|
{
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1. Perform Logger("debug", data).
|
|
|
|
if (m_client) {
|
|
|
|
auto data = vm_arguments();
|
|
|
|
return m_client->logger(LogLevel::Debug, data);
|
|
|
|
}
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1.1.4. error(...data), https://console.spec.whatwg.org/#error
|
|
|
|
ThrowCompletionOr<Value> Console::error()
|
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 17:34:43 +02:00
|
|
|
{
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1. Perform Logger("error", data).
|
|
|
|
if (m_client) {
|
|
|
|
auto data = vm_arguments();
|
|
|
|
return m_client->logger(LogLevel::Error, data);
|
|
|
|
}
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1.1.5. info(...data), https://console.spec.whatwg.org/#info
|
|
|
|
ThrowCompletionOr<Value> Console::info()
|
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 17:34:43 +02:00
|
|
|
{
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1. Perform Logger("info", data).
|
|
|
|
if (m_client) {
|
|
|
|
auto data = vm_arguments();
|
|
|
|
return m_client->logger(LogLevel::Info, data);
|
|
|
|
}
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1.1.6. log(...data), https://console.spec.whatwg.org/#log
|
|
|
|
ThrowCompletionOr<Value> Console::log()
|
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 17:34:43 +02:00
|
|
|
{
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1. Perform Logger("log", data).
|
|
|
|
if (m_client) {
|
|
|
|
auto data = vm_arguments();
|
|
|
|
return m_client->logger(LogLevel::Log, data);
|
|
|
|
}
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn
|
|
|
|
ThrowCompletionOr<Value> Console::warn()
|
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 17:34:43 +02:00
|
|
|
{
|
2021-12-10 12:26:25 +00:00
|
|
|
// 1. Perform Logger("warn", data).
|
|
|
|
if (m_client) {
|
|
|
|
auto data = vm_arguments();
|
|
|
|
return m_client->logger(LogLevel::Warn, data);
|
|
|
|
}
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:25 +02:00
|
|
|
Value Console::clear()
|
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 17:34:43 +02:00
|
|
|
{
|
2020-05-04 15:57:05 +02:00
|
|
|
if (m_client)
|
|
|
|
return m_client->clear();
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:25 +02:00
|
|
|
Value Console::trace()
|
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 17:34:43 +02:00
|
|
|
{
|
2020-05-04 15:57:05 +02:00
|
|
|
if (m_client)
|
|
|
|
return m_client->trace();
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
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 17:34:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:25 +02:00
|
|
|
Value Console::count()
|
2020-05-01 15:19:43 +02:00
|
|
|
{
|
2020-05-04 15:57:05 +02:00
|
|
|
if (m_client)
|
|
|
|
return m_client->count();
|
2020-05-04 13:20:25 +02:00
|
|
|
return js_undefined();
|
2020-05-01 15:19:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:20:25 +02:00
|
|
|
Value Console::count_reset()
|
2020-05-01 15:19:43 +02:00
|
|
|
{
|
2020-05-04 15:57:05 +02:00
|
|
|
if (m_client)
|
|
|
|
return m_client->count_reset();
|
2020-05-04 14:18:15 +02:00
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:08:14 +02:00
|
|
|
Value Console::assert_()
|
|
|
|
{
|
|
|
|
if (m_client)
|
|
|
|
return m_client->assert_();
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:18:15 +02:00
|
|
|
unsigned Console::counter_increment(String label)
|
|
|
|
{
|
|
|
|
auto value = m_counters.get(label);
|
|
|
|
if (!value.has_value()) {
|
|
|
|
m_counters.set(label, 1);
|
|
|
|
return 1;
|
2020-05-04 12:02:16 +02:00
|
|
|
}
|
2020-05-01 15:19:43 +02:00
|
|
|
|
2020-05-04 14:18:15 +02:00
|
|
|
auto new_value = value.value() + 1;
|
|
|
|
m_counters.set(label, new_value);
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::counter_reset(String label)
|
|
|
|
{
|
|
|
|
if (!m_counters.contains(label))
|
|
|
|
return false;
|
|
|
|
|
2020-05-01 15:19:43 +02:00
|
|
|
m_counters.remove(label);
|
2020-05-04 14:18:15 +02:00
|
|
|
return true;
|
2020-05-01 15:19:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
Vector<Value> Console::vm_arguments()
|
|
|
|
{
|
|
|
|
Vector<Value> arguments;
|
|
|
|
arguments.ensure_capacity(vm().argument_count());
|
|
|
|
for (size_t i = 0; i < vm().argument_count(); ++i) {
|
|
|
|
arguments.append(vm().argument(i));
|
|
|
|
}
|
|
|
|
return arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::output_debug_message([[maybe_unused]] LogLevel log_level, [[maybe_unused]] String output) const
|
|
|
|
{
|
|
|
|
#ifdef __serenity__
|
|
|
|
switch (log_level) {
|
|
|
|
case JS::Console::LogLevel::Debug:
|
|
|
|
dbgln("\033[32;1m(js debug)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
case JS::Console::LogLevel::Error:
|
|
|
|
dbgln("\033[32;1m(js error)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
case JS::Console::LogLevel::Info:
|
|
|
|
dbgln("\033[32;1m(js info)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
case JS::Console::LogLevel::Log:
|
|
|
|
dbgln("\033[32;1m(js log)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
case JS::Console::LogLevel::Warn:
|
|
|
|
dbgln("\033[32;1m(js warn)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dbgln("\033[32;1m(js)\033[0m {}", output);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-29 21:15:06 +02:00
|
|
|
VM& ConsoleClient::vm()
|
|
|
|
{
|
|
|
|
return global_object().vm();
|
|
|
|
}
|
|
|
|
|
2020-06-02 13:54:26 +01:00
|
|
|
Vector<String> ConsoleClient::get_trace() const
|
|
|
|
{
|
|
|
|
Vector<String> trace;
|
2021-06-24 19:17:45 +02:00
|
|
|
auto& execution_context_stack = m_console.global_object().vm().execution_context_stack();
|
|
|
|
// NOTE: -2 to skip the console.trace() execution context
|
|
|
|
for (ssize_t i = execution_context_stack.size() - 2; i >= 0; --i)
|
|
|
|
trace.append(execution_context_stack[i]->function_name);
|
2020-06-02 13:54:26 +01:00
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2021-12-10 12:26:25 +00:00
|
|
|
// 2.1. Logger(logLevel, args), https://console.spec.whatwg.org/#logger
|
|
|
|
ThrowCompletionOr<Value> ConsoleClient::logger(Console::LogLevel log_level, Vector<Value>& args)
|
|
|
|
{
|
|
|
|
auto& global_object = this->global_object();
|
|
|
|
|
|
|
|
// 1. If args is empty, return.
|
|
|
|
if (args.is_empty())
|
|
|
|
return js_undefined();
|
|
|
|
|
|
|
|
// 2. Let first be args[0].
|
|
|
|
auto first = args[0];
|
|
|
|
|
|
|
|
// 3. Let rest be all elements following first in args.
|
|
|
|
size_t rest_size = args.size() - 1;
|
|
|
|
|
|
|
|
// 4. If rest is empty, perform Printer(logLevel, « first ») and return.
|
|
|
|
if (rest_size == 0) {
|
|
|
|
auto first_as_vector = Vector { first };
|
|
|
|
return printer(log_level, first_as_vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. If first does not contain any format specifiers, perform Printer(logLevel, args).
|
|
|
|
if (!TRY(first.to_string(global_object)).contains('%')) {
|
|
|
|
TRY(printer(log_level, args));
|
|
|
|
} else {
|
|
|
|
// 6. Otherwise, perform Printer(logLevel, Formatter(args)).
|
|
|
|
auto formatted = TRY(formatter(args));
|
|
|
|
TRY(printer(log_level, formatted));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 7. Return undefined.
|
|
|
|
return js_undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2.2. Formatter(args), https://console.spec.whatwg.org/#formatter
|
|
|
|
ThrowCompletionOr<Vector<Value>> ConsoleClient::formatter(Vector<Value>& args)
|
|
|
|
{
|
|
|
|
// TODO: Actually implement formatting
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:40:43 +02:00
|
|
|
}
|