2020-03-16 14:58:20 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2020-04-12 17:00:06 +01:00
|
|
|
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
|
2020-05-01 07:14:57 +02:00
|
|
|
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
|
2020-03-16 14:58:20 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-03-22 11:07:55 +01:00
|
|
|
#include <AK/FlyString.h>
|
2020-03-28 22:48:35 +01:00
|
|
|
#include <AK/Function.h>
|
2020-05-01 15:19:43 +02:00
|
|
|
#include <LibJS/Console.h>
|
2020-03-28 22:48:35 +01:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-03-16 14:58:20 +01:00
|
|
|
#include <LibJS/Runtime/ConsoleObject.h>
|
2020-04-18 13:18:06 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-16 14:58:20 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 17:11:11 +02:00
|
|
|
ConsoleObject::ConsoleObject(GlobalObject& global_object)
|
|
|
|
: Object(global_object.object_prototype())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-20 17:49:52 +02:00
|
|
|
void ConsoleObject::initialize(Interpreter& interpreter, GlobalObject& global_object)
|
2020-03-16 14:58:20 +01:00
|
|
|
{
|
2020-06-20 17:49:52 +02:00
|
|
|
Object::initialize(interpreter, global_object);
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
define_native_function("log", log);
|
|
|
|
define_native_function("debug", debug);
|
|
|
|
define_native_function("info", info);
|
|
|
|
define_native_function("warn", warn);
|
|
|
|
define_native_function("error", error);
|
|
|
|
define_native_function("trace", trace);
|
|
|
|
define_native_function("count", count);
|
|
|
|
define_native_function("countReset", count_reset);
|
|
|
|
define_native_function("clear", clear);
|
2020-03-16 14:58:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleObject::~ConsoleObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log)
|
2020-03-28 23:10:37 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().log();
|
2020-04-12 17:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug)
|
2020-04-12 17:00:06 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().debug();
|
2020-04-12 17:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info)
|
2020-04-12 17:00:06 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().info();
|
2020-04-12 17:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn)
|
2020-04-12 17:00:06 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().warn();
|
2020-04-12 17:00:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error)
|
2020-04-12 17:00:06 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().error();
|
2020-03-28 23:10:37 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace)
|
2020-04-11 12:57:13 +01:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().trace();
|
2020-04-11 12:57:13 +01:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count)
|
2020-05-01 07:14:57 +02:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().count();
|
2020-05-01 07:14:57 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset)
|
2020-05-01 08:59:05 +02:00
|
|
|
{
|
2020-05-04 13:20:25 +02:00
|
|
|
return interpreter.console().count_reset();
|
2020-05-01 08:59:05 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::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 13:20:25 +02:00
|
|
|
return interpreter.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-03-16 14:58:20 +01:00
|
|
|
}
|