1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 10:08:10 +00:00

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

@ -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) \