From 08510a0c8084211f6e4a622db963dcef9c0c1586 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 21 Jun 2021 23:47:44 +0200 Subject: [PATCH] LibJS: Rename VM::current_scope() => current_environment_record() And rename some related functions that wrapped this as well. --- Userland/Libraries/LibJS/AST.cpp | 8 ++++---- Userland/Libraries/LibJS/Bytecode/Op.cpp | 4 ++-- Userland/Libraries/LibJS/Interpreter.cpp | 8 ++++---- Userland/Libraries/LibJS/Interpreter.h | 5 +++-- .../Runtime/GeneratorFunctionConstructor.cpp | 2 +- .../LibJS/Runtime/ScriptFunction.cpp | 2 +- Userland/Libraries/LibJS/Runtime/VM.cpp | 20 +++++++++---------- Userland/Libraries/LibJS/Runtime/VM.h | 6 +++--- 8 files changed, 28 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index e6d02bde7c..45df4ec2ac 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -100,7 +100,7 @@ Value FunctionDeclaration::execute(Interpreter& interpreter, GlobalObject&) cons Value FunctionExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const { InterpreterNodeScope node_scope { interpreter, *this }; - return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_scope(), kind(), is_strict_mode() || interpreter.vm().in_strict_mode(), is_arrow_function()); + return ScriptFunction::create(global_object, name(), body(), parameters(), function_length(), interpreter.current_environment_record(), kind(), is_strict_mode() || interpreter.vm().in_strict_mode(), is_arrow_function()); } Value ExpressionStatement::execute(Interpreter& interpreter, GlobalObject& global_object) const @@ -131,7 +131,7 @@ CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interprete Object* this_value = nullptr; if (is(member_expression.object())) { - auto super_base = interpreter.current_environment()->get_super_base(); + auto super_base = interpreter.current_declarative_environment_record()->get_super_base(); if (super_base.is_nullish()) { vm.throw_exception(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, super_base.to_string_without_side_effects()); return {}; @@ -229,7 +229,7 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj // FIXME: This is merely a band-aid to make super() inside catch {} work (which constructs // a new DeclarativeEnvironmentRecord without current function). Implement GetSuperConstructor() // and subsequently GetThisEnvironment() instead. - auto* function_environment = interpreter.current_environment(); + auto* function_environment = interpreter.current_declarative_environment_record(); if (!function_environment->current_function()) function_environment = static_cast(function_environment->outer_environment()); @@ -853,7 +853,7 @@ Value ClassDeclaration::execute(Interpreter& interpreter, GlobalObject& global_o if (interpreter.exception()) return {}; - interpreter.current_scope()->put_into_environment_record(m_class_expression->name(), { class_constructor, DeclarationKind::Let }); + interpreter.current_environment_record()->put_into_environment_record(m_class_expression->name(), { class_constructor, DeclarationKind::Let }); return {}; } diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp index 7274641c56..23cef0d119 100644 --- a/Userland/Libraries/LibJS/Bytecode/Op.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp @@ -312,7 +312,7 @@ void Call::execute_impl(Bytecode::Interpreter& interpreter) const void NewFunction::execute_impl(Bytecode::Interpreter& interpreter) const { auto& vm = interpreter.vm(); - interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_scope(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function()); + interpreter.accumulator() = ScriptFunction::create(interpreter.global_object(), m_function_node.name(), m_function_node.body(), m_function_node.parameters(), m_function_node.function_length(), vm.current_environment_record(), m_function_node.kind(), m_function_node.is_strict_mode(), m_function_node.is_arrow_function()); } void Return::execute_impl(Bytecode::Interpreter& interpreter) const @@ -386,7 +386,7 @@ void PushDeclarativeEnvironmentRecord::execute_impl(Bytecode::Interpreter& inter HashMap resolved_variables; for (auto& it : m_variables) resolved_variables.set(interpreter.current_executable().get_string(it.key), it.value); - auto* environment_record = interpreter.vm().heap().allocate(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_scope()); + auto* environment_record = interpreter.vm().heap().allocate(interpreter.global_object(), move(resolved_variables), interpreter.vm().current_environment_record()); interpreter.vm().call_frame().environment_record = environment_record; } diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp index 0e44d25ae7..bc65abdf3c 100644 --- a/Userland/Libraries/LibJS/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Interpreter.cpp @@ -83,7 +83,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, { ScopeGuard guard([&] { for (auto& declaration : scope_node.functions()) { - auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_scope(), declaration.kind(), declaration.is_strict_mode()); + auto* function = ScriptFunction::create(global_object, declaration.name(), declaration.body(), declaration.parameters(), declaration.function_length(), current_environment_record(), declaration.kind(), declaration.is_strict_mode()); vm().set_variable(declaration.name(), function, global_object); } }); @@ -91,7 +91,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, if (scope_type == ScopeType::Function) { push_scope({ scope_type, scope_node, false }); for (auto& declaration : scope_node.functions()) - current_scope()->put_into_environment_record(declaration.name(), { js_undefined(), DeclarationKind::Var }); + current_environment_record()->put_into_environment_record(declaration.name(), { js_undefined(), DeclarationKind::Var }); return; } @@ -131,7 +131,7 @@ void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, bool pushed_environment_record = false; if (!scope_variables_with_declaration_kind.is_empty()) { - auto* environment_record = heap().allocate(global_object, move(scope_variables_with_declaration_kind), current_scope()); + auto* environment_record = heap().allocate(global_object, move(scope_variables_with_declaration_kind), current_environment_record()); vm().call_frame().environment_record = environment_record; pushed_environment_record = true; } @@ -193,7 +193,7 @@ Value Interpreter::execute_statement(GlobalObject& global_object, const Statemen return last_value; } -DeclarativeEnvironmentRecord* Interpreter::current_environment() +DeclarativeEnvironmentRecord* Interpreter::current_declarative_environment_record() { VERIFY(is(vm().call_frame().environment_record)); return static_cast(vm().call_frame().environment_record); diff --git a/Userland/Libraries/LibJS/Interpreter.h b/Userland/Libraries/LibJS/Interpreter.h index 4407952814..1a0adf0495 100644 --- a/Userland/Libraries/LibJS/Interpreter.h +++ b/Userland/Libraries/LibJS/Interpreter.h @@ -56,8 +56,9 @@ public: ALWAYS_INLINE Heap& heap() { return vm().heap(); } ALWAYS_INLINE Exception* exception() { return vm().exception(); } - EnvironmentRecord* current_scope() { return vm().current_scope(); } - DeclarativeEnvironmentRecord* current_environment(); + EnvironmentRecord* current_environment_record() { return vm().current_environment_record(); } + + DeclarativeEnvironmentRecord* current_declarative_environment_record(); void enter_scope(const ScopeNode&, ScopeType, GlobalObject&); void exit_scope(const ScopeNode&); diff --git a/Userland/Libraries/LibJS/Runtime/GeneratorFunctionConstructor.cpp b/Userland/Libraries/LibJS/Runtime/GeneratorFunctionConstructor.cpp index 4107eaae6a..b2d5264d94 100644 --- a/Userland/Libraries/LibJS/Runtime/GeneratorFunctionConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/GeneratorFunctionConstructor.cpp @@ -62,7 +62,7 @@ Value GeneratorFunctionConstructor::construct(Function& new_target) block.dump(executable); } - return ScriptFunction::create(global_object(), function->name(), function->body(), function->parameters(), function->function_length(), vm().current_scope(), FunctionKind::Generator, function->is_strict_mode(), false); + return ScriptFunction::create(global_object(), function->name(), function->body(), function->parameters(), function->function_length(), vm().current_environment_record(), FunctionKind::Generator, function->is_strict_mode(), false); } } diff --git a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp index fccf957d50..bb2678673d 100644 --- a/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp +++ b/Userland/Libraries/LibJS/Runtime/ScriptFunction.cpp @@ -166,7 +166,7 @@ Value ScriptFunction::execute_function_body() if (i >= call_frame_args.size()) call_frame_args.resize(i + 1); call_frame_args[i] = argument_value; - vm.assign(param, argument_value, global_object(), true, vm.current_scope()); + vm.assign(param, argument_value, global_object(), true, vm.current_environment_record()); }); if (vm.exception()) diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index 4092a1150d..71026c58f0 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -135,7 +135,7 @@ void VM::set_variable(const FlyString& name, Value value, GlobalObject& global_o { Optional possible_match; if (!specific_scope && m_call_stack.size()) { - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { possible_match = environment_record->get_from_environment_record(name); if (possible_match.has_value()) { specific_scope = environment_record; @@ -167,7 +167,7 @@ bool VM::delete_variable(FlyString const& name) EnvironmentRecord* specific_scope = nullptr; Optional possible_match; if (!m_call_stack.is_empty()) { - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { possible_match = environment_record->get_from_environment_record(name); if (possible_match.has_value()) { specific_scope = environment_record; @@ -363,7 +363,7 @@ Value VM::get_variable(const FlyString& name, GlobalObject& global_object) // a function parameter, or by a local var declaration, we use that. // Otherwise, we return a lazily constructed Array with all the argument values. // FIXME: Do something much more spec-compliant. - auto possible_match = current_scope()->get_from_environment_record(name); + auto possible_match = current_environment_record()->get_from_environment_record(name); if (possible_match.has_value()) return possible_match.value().value; if (!call_frame().arguments_object) { @@ -376,7 +376,7 @@ Value VM::get_variable(const FlyString& name, GlobalObject& global_object) return call_frame().arguments_object; } - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { auto possible_match = environment_record->get_from_environment_record(name); if (exception()) return {}; @@ -393,7 +393,7 @@ Value VM::get_variable(const FlyString& name, GlobalObject& global_object) Reference VM::get_reference(const FlyString& name) { if (m_call_stack.size()) { - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { if (is(environment_record)) break; auto possible_match = environment_record->get_from_environment_record(name); @@ -460,8 +460,8 @@ Value VM::construct(Function& function, Function& new_target, Optional(current_scope())); - static_cast(current_scope())->replace_this_binding(result); + VERIFY(is(current_environment_record())); + static_cast(current_environment_record())->replace_this_binding(result); } auto prototype = new_target.get(names.prototype); if (exception()) @@ -508,7 +508,7 @@ Value VM::resolve_this_binding(GlobalObject& global_object) const const EnvironmentRecord* VM::find_this_scope() const { // We will always return because the Global environment will always be reached, which has a |this| binding. - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { if (environment_record->has_this_binding()) return environment_record; } @@ -622,9 +622,9 @@ void VM::dump_backtrace() const dbgln("-> {}", m_call_stack[i]->function_name); } -void VM::dump_scope_chain() const +void VM::dump_environment_record_chain() const { - for (auto* environment_record = current_scope(); environment_record; environment_record = environment_record->outer_environment()) { + for (auto* environment_record = current_environment_record(); environment_record; environment_record = environment_record->outer_environment()) { dbgln("+> {} ({:p})", environment_record->class_name(), environment_record); if (is(*environment_record)) { auto& declarative_environment_record = static_cast(*environment_record); diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index fb47a002fb..8defd28d5a 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -72,7 +72,7 @@ public: void clear_exception() { m_exception = nullptr; } void dump_backtrace() const; - void dump_scope_chain() const; + void dump_environment_record_chain() const; class InterpreterExecutionScope { public: @@ -122,8 +122,8 @@ public: const Vector& call_stack() const { return m_call_stack; } Vector& call_stack() { return m_call_stack; } - const EnvironmentRecord* current_scope() const { return call_frame().environment_record; } - EnvironmentRecord* current_scope() { return call_frame().environment_record; } + EnvironmentRecord const* current_environment_record() const { return call_frame().environment_record; } + EnvironmentRecord* current_environment_record() { return call_frame().environment_record; } bool in_strict_mode() const;