Skip to content

Commit

Permalink
LibJS: Remove Interpreter::declare_variable()
Browse files Browse the repository at this point in the history
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! :^)
  • Loading branch information
awesomekling committed Apr 13, 2020
1 parent ac7459c commit 062d6af
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 23 deletions.
1 change: 0 additions & 1 deletion Libraries/LibJS/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
21 changes: 0 additions & 21 deletions Libraries/LibJS/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion Libraries/LibJS/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Interpreter {

Optional<Value> 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<Heap>, HashTable<Cell*>&);

Expand Down

0 comments on commit 062d6af

Please sign in to comment.