LibJS: Move Console from Interpreter to GlobalObject

Each JS global object has its own "console", so it makes more sense to
store it in GlobalObject.

We'll need some smartness later to bundle up console messages from all
the different frames that make up a page later, but this works for now.
This commit is contained in:
Andreas Kling 2020-09-29 21:15:06 +02:00
parent a9335eea1c
commit e4bda2e1e7
11 changed files with 55 additions and 50 deletions

View file

@ -30,7 +30,6 @@
#include <LibGUI/BoxLayout.h>
#include <LibGUI/JSSyntaxHighlighter.h>
#include <LibGUI/TextBox.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/DOM/DocumentType.h>
#include <LibWeb/DOM/ElementFactory.h>
#include <LibWeb/DOM/Text.h>
@ -41,7 +40,7 @@ namespace Browser {
JS::Value BrowserConsoleClient::log()
{
m_console_widget.print_html(interpreter().vm().join_arguments());
m_console_widget.print_html(vm().join_arguments());
return JS::js_undefined();
}
@ -50,7 +49,7 @@ JS::Value BrowserConsoleClient::info()
StringBuilder html;
html.append("<span class=\"info\">");
html.append("(i) ");
html.append(interpreter().vm().join_arguments());
html.append(vm().join_arguments());
html.append("</span>");
m_console_widget.print_html(html.string_view());
return JS::js_undefined();
@ -61,7 +60,7 @@ JS::Value BrowserConsoleClient::debug()
StringBuilder html;
html.append("<span class=\"debug\">");
html.append("(d) ");
html.append(interpreter().vm().join_arguments());
html.append(vm().join_arguments());
html.append("</span>");
m_console_widget.print_html(html.string_view());
return JS::js_undefined();
@ -72,7 +71,7 @@ JS::Value BrowserConsoleClient::warn()
StringBuilder html;
html.append("<span class=\"warn\">");
html.append("(w) ");
html.append(interpreter().vm().join_arguments());
html.append(vm().join_arguments());
html.append("</span>");
m_console_widget.print_html(html.string_view());
return JS::js_undefined();
@ -83,7 +82,7 @@ JS::Value BrowserConsoleClient::error()
StringBuilder html;
html.append("<span class=\"error\">");
html.append("(e) ");
html.append(interpreter().vm().join_arguments());
html.append(vm().join_arguments());
html.append("</span>");
m_console_widget.print_html(html.string_view());
return JS::js_undefined();
@ -98,7 +97,7 @@ JS::Value BrowserConsoleClient::clear()
JS::Value BrowserConsoleClient::trace()
{
StringBuilder html;
html.append(interpreter().vm().join_arguments());
html.append(vm().join_arguments());
auto trace = get_trace();
for (auto& function_name : trace) {
if (function_name.is_empty())
@ -111,7 +110,7 @@ JS::Value BrowserConsoleClient::trace()
JS::Value BrowserConsoleClient::count()
{
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default";
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
auto counter_value = m_console.counter_increment(label);
m_console_widget.print_html(String::format("%s: %u", label.characters(), counter_value));
return JS::js_undefined();
@ -119,7 +118,7 @@ JS::Value BrowserConsoleClient::count()
JS::Value BrowserConsoleClient::count_reset()
{
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default";
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
if (m_console.counter_reset(label)) {
m_console_widget.print_html(String::format("%s: 0", label.characters()));
} else {

View file

@ -129,8 +129,8 @@ void ConsoleWidget::set_interpreter(WeakPtr<JS::Interpreter> interpreter)
return;
m_interpreter = interpreter;
m_console_client = adopt_own(*new BrowserConsoleClient(interpreter->console(), *this));
interpreter->console().set_client(*m_console_client.ptr());
m_console_client = make<BrowserConsoleClient>(interpreter->global_object().console(), *this);
interpreter->global_object().console().set_client(*m_console_client.ptr());
clear_output();
}

View file

@ -26,12 +26,12 @@
*/
#include <LibJS/Console.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/GlobalObject.h>
namespace JS {
Console::Console(Interpreter& interpreter)
: m_interpreter(interpreter)
Console::Console(GlobalObject& global_object)
: m_global_object(global_object)
{
}
@ -120,10 +120,15 @@ bool Console::counter_reset(String label)
return true;
}
VM& ConsoleClient::vm()
{
return global_object().vm();
}
Vector<String> ConsoleClient::get_trace() const
{
Vector<String> trace;
auto& call_stack = m_console.interpreter().vm().call_stack();
auto& call_stack = m_console.global_object().vm().call_stack();
// -2 to skip the console.trace() call frame
for (ssize_t i = call_stack.size() - 2; i >= 0; --i)
trace.append(call_stack[i].function_name);

View file

@ -40,12 +40,12 @@ class Console {
AK_MAKE_NONMOVABLE(Console);
public:
Console(Interpreter&);
explicit Console(GlobalObject&);
void set_client(ConsoleClient& client) { m_client = &client; }
Interpreter& interpreter() { return m_interpreter; }
const Interpreter& interpreter() const { return m_interpreter; }
GlobalObject& global_object() { return m_global_object; }
const GlobalObject& global_object() const { return m_global_object; }
HashMap<String, unsigned>& counters() { return m_counters; }
const HashMap<String, unsigned>& counters() const { return m_counters; }
@ -67,7 +67,7 @@ public:
bool counter_reset(String label);
private:
Interpreter& m_interpreter;
GlobalObject& m_global_object;
ConsoleClient* m_client { nullptr };
HashMap<String, unsigned> m_counters;
@ -91,8 +91,10 @@ public:
virtual Value count_reset() = 0;
protected:
Interpreter& interpreter() { return m_console.interpreter(); }
const Interpreter& interpreter() const { return m_console.interpreter(); }
VM& vm();
GlobalObject& global_object() { return m_console.global_object(); }
const GlobalObject& global_object() const { return m_console.global_object(); }
Vector<String> get_trace() const;

View file

@ -98,6 +98,7 @@ class ASTNode;
class BigInt;
class BoundFunction;
class Cell;
class Console;
class DeferGC;
class Error;
class Exception;

View file

@ -54,7 +54,6 @@ NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(Globa
Interpreter::Interpreter(VM& vm)
: m_vm(vm)
, m_console(*this)
{
}

View file

@ -32,7 +32,6 @@
#include <AK/Vector.h>
#include <AK/Weakable.h>
#include <LibJS/AST.h>
#include <LibJS/Console.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/DeferGC.h>
#include <LibJS/Heap/Heap.h>
@ -87,9 +86,6 @@ public:
Heap& heap() { return vm().heap(); }
Exception* exception() { return vm().exception(); }
Console& console() { return m_console; }
const Console& console() const { return m_console; }
bool in_strict_mode() const
{
// FIXME: This implementation is bogus; strict mode is per-file or per-function, not per-block!
@ -121,8 +117,6 @@ private:
Handle<Object> m_global_object;
Console m_console;
Vector<ScopeFrame> m_scope_stack;
};

View file

@ -29,7 +29,6 @@
#include <AK/FlyString.h>
#include <AK/Function.h>
#include <LibJS/Console.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibJS/Runtime/GlobalObject.h>
@ -60,47 +59,47 @@ ConsoleObject::~ConsoleObject()
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log)
{
return vm.interpreter().console().log();
return global_object.console().log();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug)
{
return vm.interpreter().console().debug();
return global_object.console().debug();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info)
{
return vm.interpreter().console().info();
return global_object.console().info();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn)
{
return vm.interpreter().console().warn();
return global_object.console().warn();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error)
{
return vm.interpreter().console().error();
return global_object.console().error();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace)
{
return vm.interpreter().console().trace();
return global_object.console().trace();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count)
{
return vm.interpreter().console().count();
return global_object.console().count();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset)
{
return vm.interpreter().console().count_reset();
return global_object.console().count_reset();
}
JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear)
{
return vm.interpreter().console().clear();
return global_object.console().clear();
}
}

View file

@ -26,6 +26,7 @@
*/
#include <AK/LogStream.h>
#include <LibJS/Console.h>
#include <LibJS/Heap/DeferGC.h>
#include <LibJS/Runtime/ArrayConstructor.h>
#include <LibJS/Runtime/ArrayIteratorPrototype.h>
@ -68,6 +69,7 @@ namespace JS {
GlobalObject::GlobalObject()
: Object(GlobalObjectTag::Tag)
, m_console(make<Console>(*this))
{
}

View file

@ -41,6 +41,8 @@ public:
virtual ~GlobalObject() override;
Console& console() { return *m_console; }
Shape* empty_object_shape() { return m_empty_object_shape; }
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
@ -66,6 +68,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(is_finite);
JS_DECLARE_NATIVE_FUNCTION(parse_float);
NonnullOwnPtr<Console> m_console;
Shape* m_empty_object_shape { nullptr };
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \

View file

@ -470,32 +470,32 @@ public:
virtual JS::Value log() override
{
puts(interpreter().vm().join_arguments().characters());
puts(vm().join_arguments().characters());
return JS::js_undefined();
}
virtual JS::Value info() override
{
printf("(i) %s\n", interpreter().vm().join_arguments().characters());
printf("(i) %s\n", vm().join_arguments().characters());
return JS::js_undefined();
}
virtual JS::Value debug() override
{
printf("\033[36;1m");
puts(interpreter().vm().join_arguments().characters());
puts(vm().join_arguments().characters());
printf("\033[0m");
return JS::js_undefined();
}
virtual JS::Value warn() override
{
printf("\033[33;1m");
puts(interpreter().vm().join_arguments().characters());
puts(vm().join_arguments().characters());
printf("\033[0m");
return JS::js_undefined();
}
virtual JS::Value error() override
{
printf("\033[31;1m");
puts(interpreter().vm().join_arguments().characters());
puts(vm().join_arguments().characters());
printf("\033[0m");
return JS::js_undefined();
}
@ -507,7 +507,7 @@ public:
}
virtual JS::Value trace() override
{
puts(interpreter().vm().join_arguments().characters());
puts(vm().join_arguments().characters());
auto trace = get_trace();
for (auto& function_name : trace) {
if (function_name.is_empty())
@ -518,14 +518,14 @@ public:
}
virtual JS::Value count() override
{
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default";
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
auto counter_value = m_console.counter_increment(label);
printf("%s: %u\n", label.characters(), counter_value);
return JS::js_undefined();
}
virtual JS::Value count_reset() override
{
auto label = interpreter().argument_count() ? interpreter().argument(0).to_string_without_side_effects() : "default";
auto label = vm().argument_count() ? vm().argument(0).to_string_without_side_effects() : "default";
if (m_console.counter_reset(label)) {
printf("%s: 0\n", label.characters());
} else {
@ -564,8 +564,8 @@ int main(int argc, char** argv)
if (script_path == nullptr) {
s_print_last_result = true;
interpreter = JS::Interpreter::create<ReplObject>(*vm);
ReplConsoleClient console_client(interpreter->console());
interpreter->console().set_client(console_client);
ReplConsoleClient console_client(interpreter->global_object().console());
interpreter->global_object().console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
interpreter->vm().set_underscore_is_last_value(true);
@ -846,8 +846,8 @@ int main(int argc, char** argv)
repl(*interpreter);
} else {
interpreter = JS::Interpreter::create<JS::GlobalObject>(*vm);
ReplConsoleClient console_client(interpreter->console());
interpreter->console().set_client(console_client);
ReplConsoleClient console_client(interpreter->global_object().console());
interpreter->global_object().console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
signal(SIGINT, [](int) {