From 1603623772c689665904880f437e402d11e1cd53 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 21 Mar 2021 12:18:56 +0100 Subject: [PATCH] LibJS: Move AST node stack from VM to Interpreter --- Userland/Libraries/LibJS/AST.cpp | 2 +- Userland/Libraries/LibJS/Interpreter.cpp | 4 ++-- Userland/Libraries/LibJS/Interpreter.h | 7 +++++++ Userland/Libraries/LibJS/Runtime/Exception.cpp | 16 ++++++++++------ Userland/Libraries/LibJS/Runtime/Object.cpp | 7 +++++-- Userland/Libraries/LibJS/Runtime/VM.cpp | 6 ++++-- Userland/Libraries/LibJS/Runtime/VM.h | 6 ------ 7 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index f3268b0c48..58d98faae4 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -235,7 +235,7 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj } } - vm.call_frame().current_node = vm.current_node(); + vm.call_frame().current_node = interpreter.current_node(); Object* new_object = nullptr; Value result; if (is(*this)) { diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp index e773fbd34f..5750ecb129 100644 --- a/Userland/Libraries/LibJS/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Interpreter.cpp @@ -146,12 +146,12 @@ void Interpreter::exit_scope(const ScopeNode& scope_node) void Interpreter::enter_node(const ASTNode& node) { vm().call_frame().current_node = &node; - vm().push_ast_node(node); + push_ast_node(node); } void Interpreter::exit_node(const ASTNode&) { - vm().pop_ast_node(); + pop_ast_node(); } void Interpreter::push_scope(ScopeFrame frame) diff --git a/Userland/Libraries/LibJS/Interpreter.h b/Userland/Libraries/LibJS/Interpreter.h index 5efb1428da..a2b801cefa 100644 --- a/Userland/Libraries/LibJS/Interpreter.h +++ b/Userland/Libraries/LibJS/Interpreter.h @@ -80,6 +80,12 @@ public: void enter_node(const ASTNode&); void exit_node(const ASTNode&); + void push_ast_node(const ASTNode& node) { m_ast_nodes.append(&node); } + void pop_ast_node() { m_ast_nodes.take_last(); } + + const ASTNode* current_node() const { return !m_ast_nodes.is_empty() ? m_ast_nodes.last() : nullptr; } + const Vector& node_stack() const { return m_ast_nodes; } + Value execute_statement(GlobalObject&, const Statement&, ScopeType = ScopeType::Block); private: @@ -88,6 +94,7 @@ private: void push_scope(ScopeFrame frame); Vector m_scope_stack; + Vector m_ast_nodes; NonnullRefPtr m_vm; diff --git a/Userland/Libraries/LibJS/Runtime/Exception.cpp b/Userland/Libraries/LibJS/Runtime/Exception.cpp index b7769581a3..f8f0f0c4c8 100644 --- a/Userland/Libraries/LibJS/Runtime/Exception.cpp +++ b/Userland/Libraries/LibJS/Runtime/Exception.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -34,7 +35,8 @@ namespace JS { Exception::Exception(Value value) : m_value(value) { - auto& call_stack = vm().call_stack(); + auto& vm = this->vm(); + auto& call_stack = vm.call_stack(); for (ssize_t i = call_stack.size() - 1; i >= 0; --i) { String function_name = call_stack[i]->function_name; if (function_name.is_empty()) @@ -42,11 +44,13 @@ Exception::Exception(Value value) m_trace.append(function_name); } - auto& node_stack = vm().node_stack(); - for (ssize_t i = node_stack.size() - 1; i >= 0; --i) { - auto* node = node_stack[i]; - VERIFY(node); - m_source_ranges.append(node->source_range()); + if (auto* interpreter = vm.interpreter_if_exists()) { + auto& node_stack = interpreter->node_stack(); + for (ssize_t i = node_stack.size() - 1; i >= 0; --i) { + auto* node = node_stack[i]; + VERIFY(node); + m_source_ranges.append(node->source_range()); + } } } diff --git a/Userland/Libraries/LibJS/Runtime/Object.cpp b/Userland/Libraries/LibJS/Runtime/Object.cpp index f828219e76..91b58f6717 100644 --- a/Userland/Libraries/LibJS/Runtime/Object.cpp +++ b/Userland/Libraries/LibJS/Runtime/Object.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -901,7 +902,8 @@ Value Object::call_native_property_getter(NativeProperty& property, Value this_v { auto& vm = this->vm(); CallFrame call_frame; - call_frame.current_node = property.vm().current_node(); + if (auto* interpreter = vm.interpreter_if_exists()) + call_frame.current_node = interpreter->current_node(); call_frame.is_strict_mode = vm.in_strict_mode(); call_frame.this_value = this_value; vm.push_call_frame(call_frame, global_object()); @@ -916,7 +918,8 @@ void Object::call_native_property_setter(NativeProperty& property, Value this_va { auto& vm = this->vm(); CallFrame call_frame; - call_frame.current_node = property.vm().current_node(); + if (auto* interpreter = vm.interpreter_if_exists()) + call_frame.current_node = interpreter->current_node(); call_frame.is_strict_mode = vm.in_strict_mode(); call_frame.this_value = this_value; vm.push_call_frame(call_frame, global_object()); diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 894524f20d..38e4a395fc 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -213,7 +213,8 @@ Value VM::construct(Function& function, Function& new_target, Optionalcurrent_node(); call_frame.is_strict_mode = function.is_strict_mode(); push_call_frame(call_frame, function.global_object()); @@ -338,7 +339,8 @@ Value VM::call_internal(Function& function, Value this_value, Optionalcurrent_node(); call_frame.is_strict_mode = function.is_strict_mode(); call_frame.function_name = function.name(); call_frame.this_value = function.bound_this().value_or(this_value); diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index 1dca17e66f..025d83e25e 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -128,15 +128,10 @@ public: void pop_call_frame() { m_call_stack.take_last(); } - void push_ast_node(const ASTNode& node) { m_ast_nodes.append(&node); } - void pop_ast_node() { m_ast_nodes.take_last(); } - CallFrame& call_frame() { return *m_call_stack.last(); } const CallFrame& call_frame() const { return *m_call_stack.last(); } const Vector& call_stack() const { return m_call_stack; } Vector& call_stack() { return m_call_stack; } - const ASTNode* current_node() const { return !m_ast_nodes.is_empty() ? m_ast_nodes.last() : nullptr; } - const Vector& node_stack() const { return m_ast_nodes; } const ScopeObject* current_scope() const { return call_frame().scope; } ScopeObject* current_scope() { return call_frame().scope; } @@ -256,7 +251,6 @@ private: Vector m_interpreters; Vector m_call_stack; - Vector m_ast_nodes; Value m_last_value; ScopeType m_unwind_until { ScopeType::None };