1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

LibJS: Allow the choice of a scope of declaration for a variable (#1408)

Previously, we were assuming all declared variables were bound to a
block scope, now, with the addition of declaration types, we can bind
a variable to a block scope using `let`, or a function scope (the scope
of the inner-most enclosing function of a `var` declaration) using
`var`.
This commit is contained in:
0xtechnobabble 2020-03-11 21:09:20 +02:00 committed by GitHub
parent 542108421e
commit df40c85f80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 16 deletions

View file

@ -56,7 +56,7 @@ Value CallExpression::execute(Interpreter& interpreter) const
auto* callee_object = callee.as_object();
ASSERT(callee_object->is_function());
auto& function = static_cast<Function&>(*callee_object);
return interpreter.run(function.body());
return interpreter.run(function.body(), ScopeType::Function);
}
Value ReturnStatement::execute(Interpreter& interpreter) const
@ -379,17 +379,30 @@ void AssignmentExpression::dump(int indent) const
Value VariableDeclaration::execute(Interpreter& interpreter) const
{
interpreter.declare_variable(name().string());
interpreter.declare_variable(name().string(), m_declaration_type);
if (m_initializer) {
auto initalizer_result = m_initializer->execute(interpreter);
interpreter.set_variable(name().string(), initalizer_result);
}
return js_undefined();
}
void VariableDeclaration::dump(int indent) const
{
const char* op_string = nullptr;
switch (m_declaration_type) {
case DeclarationType::Let:
op_string = "Let";
break;
case DeclarationType::Var:
op_string = "Var";
break;
}
ASTNode::dump(indent);
print_indent(indent + 1);
printf("%s\n", op_string);
m_name->dump(indent + 1);
if (m_initializer)
m_initializer->dump(indent + 1);