1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:58:11 +00:00

LibJS: Hoist variable declarations to the nearest relevant scope

"var" declarations are hoisted to the nearest function scope, while
"let" and "const" are hoisted to the nearest block scope.

This is done by the parser, which keeps two scope stacks, one stack
for the current var scope and one for the current let/const scope.

When the interpreter enters a scope, we walk all of the declarations
and insert them into the variable environment.

We don't support the temporal dead zone for let/const yet.
This commit is contained in:
Andreas Kling 2020-04-13 16:42:54 +02:00
parent b9415dc0e9
commit ac7459cb40
7 changed files with 158 additions and 21 deletions

View file

@ -412,8 +412,18 @@ void ASTNode::dump(int indent) const
void ScopeNode::dump(int indent) const
{
ASTNode::dump(indent);
for (auto& child : children())
child.dump(indent + 1);
if (!m_variables.is_empty()) {
print_indent(indent + 1);
printf("(Variables)\n");
for (auto& variable : m_variables)
variable.dump(indent + 2);
}
if (!m_children.is_empty()) {
print_indent(indent + 1);
printf("(Children)\n");
for (auto& child : children())
child.dump(indent + 2);
}
}
void BinaryExpression::dump(int indent) const
@ -578,7 +588,15 @@ void FunctionNode::dump(int indent, const char* class_name) const
print_indent(indent);
printf("%s '%s(%s)'\n", class_name, name().characters(), parameters_builder.build().characters());
body().dump(indent + 1);
if (!m_variables.is_empty()) {
print_indent(indent + 1);
printf("(Variables)\n");
}
for (auto& variable : m_variables)
variable.dump(indent + 2);
print_indent(indent + 1);
printf("(Body)\n");
body().dump(indent + 2);
}
void FunctionDeclaration::dump(int indent) const
@ -1163,4 +1181,9 @@ Value SequenceExpression::execute(Interpreter& interpreter) const
return last_value;
}
void ScopeNode::add_variables(NonnullRefPtrVector<VariableDeclaration> variables)
{
m_variables.append(move(variables));
}
}