From 062d6af16edffa180eba55f6ff2c774c04242963 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 13 Apr 2020 16:45:51 +0200 Subject: [PATCH] LibJS: Remove Interpreter::declare_variable() Since declarations are now hoisted and handled on scope entry, the job of a VariableDeclaration becomes to actually initialize variables. As such, we can remove the part where we insert variables into the nearest relevant scope. Less work == more speed! :^) --- Libraries/LibJS/AST.cpp | 1 - Libraries/LibJS/Interpreter.cpp | 21 --------------------- Libraries/LibJS/Interpreter.h | 1 - 3 files changed, 23 deletions(-) diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 67fab0a34c7..0b1ff217c91 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -828,7 +828,6 @@ void UpdateExpression::dump(int indent) const Value VariableDeclaration::execute(Interpreter& interpreter) const { for (auto& declarator : m_declarations) { - interpreter.declare_variable(declarator.id().string(), m_declaration_kind); if (auto* init = declarator.init()) { auto initalizer_result = init->execute(interpreter); if (interpreter.exception()) diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 5bd64042529..d79b57f2dbb 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -122,27 +122,6 @@ void Interpreter::exit_scope(const ScopeNode& scope_node) m_unwind_until = ScopeType::None; } -void Interpreter::declare_variable(const FlyString& name, DeclarationKind declaration_kind) -{ - switch (declaration_kind) { - case DeclarationKind::Var: - for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { - auto& scope = m_scope_stack.at(i); - if (scope.type == ScopeType::Function) { - scope.variables.set(move(name), { js_undefined(), declaration_kind }); - return; - } - } - - global_object().put(move(name), js_undefined()); - break; - case DeclarationKind::Let: - case DeclarationKind::Const: - m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_kind }); - break; - } -} - void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment) { for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index c5427fe1358..b25df3352a2 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -96,7 +96,6 @@ public: Optional get_variable(const FlyString& name); void set_variable(const FlyString& name, Value, bool first_assignment = false); - void declare_variable(const FlyString& name, DeclarationKind); void gather_roots(Badge, HashTable&);