1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:37:35 +00:00

LibJS: Move AST node stack from VM to Interpreter

This commit is contained in:
Andreas Kling 2021-03-21 12:18:56 +01:00
parent b3b8c01ebf
commit 1603623772
7 changed files with 29 additions and 19 deletions

View file

@ -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<NewExpression>(*this)) {

View file

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

View file

@ -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<const ASTNode*>& 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<ScopeFrame> m_scope_stack;
Vector<const ASTNode*> m_ast_nodes;
NonnullRefPtr<VM> m_vm;

View file

@ -26,6 +26,7 @@
#include <AK/String.h>
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Exception.h>
#include <LibJS/Runtime/VM.h>
@ -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,12 +44,14 @@ Exception::Exception(Value value)
m_trace.append(function_name);
}
auto& node_stack = vm().node_stack();
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());
}
}
}
Exception::~Exception()

View file

@ -28,6 +28,7 @@
#include <AK/String.h>
#include <AK/TemporaryChange.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Accessor.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
@ -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());

View file

@ -213,7 +213,8 @@ Value VM::construct(Function& function, Function& new_target, Optional<MarkedVal
{
CallFrame call_frame;
call_frame.callee = &function;
call_frame.current_node = current_node();
if (auto* interpreter = interpreter_if_exists())
call_frame.current_node = interpreter->current_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, Optional<MarkedVal
CallFrame call_frame;
call_frame.callee = &function;
call_frame.current_node = current_node();
if (auto* interpreter = interpreter_if_exists())
call_frame.current_node = interpreter->current_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);

View file

@ -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<CallFrame*>& call_stack() const { return m_call_stack; }
Vector<CallFrame*>& call_stack() { return m_call_stack; }
const ASTNode* current_node() const { return !m_ast_nodes.is_empty() ? m_ast_nodes.last() : nullptr; }
const Vector<const ASTNode*>& 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<Interpreter*> m_interpreters;
Vector<CallFrame*> m_call_stack;
Vector<const ASTNode*> m_ast_nodes;
Value m_last_value;
ScopeType m_unwind_until { ScopeType::None };