mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 22:07:34 +00:00
LibJS/Bytecode: Define named functions as a variable inside their scope
This allows you to recurse into a named function that is stored in a variable. For example, this would previously print "wrong" instead of "right": ```js function g() { console.log("wrong") } f = function g(i) { if (i !== 1) g(1); else console.log("right"); } f() ```
This commit is contained in:
parent
9ad807d08b
commit
125a71d36d
1 changed files with 16 additions and 0 deletions
|
@ -1010,7 +1010,23 @@ Bytecode::CodeGenerationErrorOr<void> FunctionDeclaration::generate_bytecode(Byt
|
||||||
|
|
||||||
Bytecode::CodeGenerationErrorOr<void> FunctionExpression::generate_bytecode(Bytecode::Generator& generator) const
|
Bytecode::CodeGenerationErrorOr<void> FunctionExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||||
{
|
{
|
||||||
|
bool has_name = !name().is_empty();
|
||||||
|
Optional<Bytecode::IdentifierTableIndex> name_identifier;
|
||||||
|
|
||||||
|
if (has_name) {
|
||||||
|
generator.begin_variable_scope(Bytecode::Generator::BindingMode::Lexical);
|
||||||
|
|
||||||
|
name_identifier = generator.intern_identifier(name());
|
||||||
|
generator.emit<Bytecode::Op::CreateVariable>(*name_identifier, Bytecode::Op::EnvironmentMode::Lexical, true);
|
||||||
|
}
|
||||||
|
|
||||||
generator.emit<Bytecode::Op::NewFunction>(*this);
|
generator.emit<Bytecode::Op::NewFunction>(*this);
|
||||||
|
|
||||||
|
if (has_name) {
|
||||||
|
generator.emit<Bytecode::Op::SetVariable>(*name_identifier, Bytecode::Op::SetVariable::InitializationMode::Initialize, Bytecode::Op::EnvironmentMode::Lexical);
|
||||||
|
generator.end_variable_scope();
|
||||||
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue