1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +00:00

LibJS: Move scope stack from VM back to Interpreter

Okay, my vision here is improving. Interpreter should be a thing that
executes an AST. The scope stack is irrelevant to the VM proper,
so we can move that to the Interpreter. Same with execute_statement().
This commit is contained in:
Andreas Kling 2020-09-27 16:56:58 +02:00
parent 6861c619c6
commit be31805e8b
6 changed files with 124 additions and 119 deletions

View file

@ -130,7 +130,7 @@ Value ScriptFunction::call(Interpreter& interpreter)
arguments.append({ parameter.name, value });
interpreter.current_environment()->set(parameter.name, { value, DeclarationKind::Var });
}
return interpreter.vm().execute_statement(global_object(), m_body, arguments, ScopeType::Function);
return interpreter.execute_statement(global_object(), m_body, arguments, ScopeType::Function);
}
Value ScriptFunction::construct(Interpreter& interpreter, Function&)

View file

@ -132,70 +132,6 @@ Symbol* VM::get_global_symbol(const String& description)
return new_global_symbol;
}
bool VM::in_strict_mode() const
{
if (m_scope_stack.is_empty())
return true;
return m_scope_stack.last().scope_node->in_strict_mode();
}
void VM::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type, GlobalObject& global_object)
{
for (auto& declaration : scope_node.functions()) {
auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_environment());
set_variable(declaration.name(), function, global_object);
}
if (scope_type == ScopeType::Function) {
m_scope_stack.append({ scope_type, scope_node, false });
return;
}
HashMap<FlyString, Variable> scope_variables_with_declaration_kind;
scope_variables_with_declaration_kind.ensure_capacity(16);
for (auto& declaration : scope_node.variables()) {
for (auto& declarator : declaration.declarations()) {
if (scope_node.is_program()) {
global_object.put(declarator.id().string(), js_undefined());
if (exception())
return;
} else {
scope_variables_with_declaration_kind.set(declarator.id().string(), { js_undefined(), declaration.declaration_kind() });
}
}
}
for (auto& argument : arguments) {
scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var });
}
bool pushed_lexical_environment = false;
if (!scope_variables_with_declaration_kind.is_empty()) {
auto* block_lexical_environment = heap().allocate<LexicalEnvironment>(global_object, move(scope_variables_with_declaration_kind), current_environment());
m_call_stack.last().environment = block_lexical_environment;
pushed_lexical_environment = true;
}
m_scope_stack.append({ scope_type, scope_node, pushed_lexical_environment });
}
void VM::exit_scope(const ScopeNode& scope_node)
{
while (!m_scope_stack.is_empty()) {
auto popped_scope = m_scope_stack.take_last();
if (popped_scope.pushed_environment)
m_call_stack.last().environment = m_call_stack.last().environment->parent();
if (popped_scope.scope_node.ptr() == &scope_node)
break;
}
// If we unwind all the way, just reset m_unwind_until so that future "return" doesn't break.
if (m_scope_stack.is_empty())
m_unwind_until = ScopeType::None;
}
void VM::set_variable(const FlyString& name, Value value, GlobalObject& global_object, bool first_assignment)
{
if (m_call_stack.size()) {
@ -249,36 +185,6 @@ Reference VM::get_reference(const FlyString& name)
return { Reference::GlobalVariable, name };
}
Value VM::execute_statement(GlobalObject& global_object, const Statement& statement, ArgumentVector arguments, ScopeType scope_type)
{
if (!statement.is_scope_node())
return statement.execute(interpreter(), global_object);
auto& block = static_cast<const ScopeNode&>(statement);
enter_scope(block, move(arguments), scope_type, global_object);
if (block.children().is_empty())
m_last_value = js_undefined();
for (auto& node : block.children()) {
m_last_value = node.execute(interpreter(), global_object);
if (should_unwind()) {
if (!block.label().is_null() && should_unwind_until(ScopeType::Breakable, block.label()))
stop_unwind();
break;
}
}
bool did_return = m_unwind_until == ScopeType::Function;
if (m_unwind_until == scope_type)
m_unwind_until = ScopeType::None;
exit_scope(block);
return did_return ? m_last_value : js_undefined();
}
Value VM::construct(Function& function, Function& new_target, Optional<MarkedValueList> arguments, GlobalObject& global_object)
{
auto& call_frame = push_call_frame();

View file

@ -112,7 +112,8 @@ public:
return m_call_stack.last();
}
void pop_call_frame() { m_call_stack.take_last(); }
const CallFrame& call_frame() { return m_call_stack.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; }
@ -153,6 +154,7 @@ public:
}
Value last_value() const { return m_last_value; }
void set_last_value(Badge<Interpreter>, Value value) { m_last_value = value; }
bool underscore_is_last_value() const { return m_underscore_is_last_value; }
void set_underscore_is_last_value(bool b) { m_underscore_is_last_value = b; }
@ -171,14 +173,13 @@ public:
}
bool should_unwind() const { return m_unwind_until != ScopeType::None; }
ScopeType unwind_until() const { return m_unwind_until; }
Value get_variable(const FlyString& name, GlobalObject&);
void set_variable(const FlyString& name, Value, GlobalObject&, bool first_assignment = false);
Reference get_reference(const FlyString& name);
void enter_scope(const ScopeNode&, ArgumentVector, ScopeType, GlobalObject&);
void exit_scope(const ScopeNode&);
template<typename T, typename... Args>
void throw_exception(GlobalObject& global_object, Args&&... args)
{
@ -197,8 +198,6 @@ public:
return throw_exception(global_object, T::create(global_object, String::format(type.message(), forward<Args>(args)...)));
}
Value execute_statement(GlobalObject&, const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block);
Value construct(Function&, Function& new_target, Optional<MarkedValueList> arguments, GlobalObject&);
String join_arguments() const;
@ -215,7 +214,6 @@ private:
Heap m_heap;
Vector<Interpreter*> m_interpreters;
Vector<ScopeFrame> m_scope_stack;
Vector<CallFrame> m_call_stack;
Value m_last_value;