mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:48:10 +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:
parent
542108421e
commit
df40c85f80
6 changed files with 85 additions and 16 deletions
|
@ -92,10 +92,12 @@ void build_program(JS::Program& program)
|
|||
auto block = make<JS::BlockStatement>();
|
||||
block->append<JS::VariableDeclaration>(
|
||||
make<JS::Identifier>("a"),
|
||||
make<JS::Literal>(JS::Value(5)));
|
||||
make<JS::Literal>(JS::Value(5)),
|
||||
JS::DeclarationType::Var);
|
||||
block->append<JS::VariableDeclaration>(
|
||||
make<JS::Identifier>("b"),
|
||||
make<JS::Literal>(JS::Value(7)));
|
||||
make<JS::Literal>(JS::Value(7)),
|
||||
JS::DeclarationType::Var);
|
||||
|
||||
block->append<JS::ReturnStatement>(
|
||||
make<JS::BinaryExpression>(
|
||||
|
@ -120,13 +122,38 @@ void build_program(JS::Program& program)
|
|||
auto block = make<JS::BlockStatement>();
|
||||
block->append<JS::VariableDeclaration>(
|
||||
make<JS::Identifier>("x"),
|
||||
make<JS::ObjectExpression>());
|
||||
make<JS::ObjectExpression>(),
|
||||
JS::DeclarationType::Var);
|
||||
block->append<JS::CallExpression>("$gc");
|
||||
|
||||
program.append<JS::FunctionDeclaration>("foo", move(block));
|
||||
program.append<JS::CallExpression>("foo");
|
||||
}
|
||||
#elif PROGRAM == 4
|
||||
void build_program(JS::Program& program)
|
||||
{
|
||||
// function foo() {
|
||||
// function bar() {
|
||||
// var y = 6;
|
||||
// }
|
||||
//
|
||||
// bar()
|
||||
// return y;
|
||||
// }
|
||||
// foo(); //I should return `undefined` because y is bound to the inner-most enclosing function, i.e the nested one (bar()), therefore, it's undefined in the scope of foo()
|
||||
|
||||
auto block_bar = make<JS::BlockStatement>();
|
||||
block_bar->append<JS::VariableDeclaration>(make<JS::Identifier>("y"), make<JS::Literal>(JS::Value(6)), JS::DeclarationType::Var);
|
||||
|
||||
auto block_foo = make<JS::BlockStatement>();
|
||||
block_foo->append<JS::FunctionDeclaration>("bar", move(block_bar));
|
||||
block_foo->append<JS::CallExpression>("bar");
|
||||
block_foo->append<JS::ReturnStatement>(make<JS::Identifier>("y"));
|
||||
|
||||
program.append<JS::FunctionDeclaration>("foo", move(block_foo));
|
||||
program.append<JS::CallExpression>("foo");
|
||||
}
|
||||
#elif PROGRAM == 5
|
||||
void build_program(JS::Program& program, JS::Heap& heap)
|
||||
{
|
||||
// "hello friends".length
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue