summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2022-03-13 12:43:45 +0330
committerAndreas Kling <kling@serenityos.org>2022-03-13 17:50:21 +0100
commit57386ca8398e38ad21a6b91edef13b8024fc5510 (patch)
treee743ebfef6c3ec338aee359fad0165fe0a0cddaf /Userland/Libraries
parentba9c4959d660d80250654c81a97c89bfcb2faad9 (diff)
downloadserenity-57386ca8398e38ad21a6b91edef13b8024fc5510.zip
LibJS: Initialize 'var' bindings to undefined on declaration
This is what CreateGlobalVarBinding does, so do the same thing in BC.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp7
1 files changed, 5 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
index 0e00457160..a952ded98f 100644
--- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp
@@ -176,6 +176,8 @@ Bytecode::CodeGenerationErrorOr<void> ScopeNode::generate_bytecode(Bytecode::Gen
if (!declared_function_names.contains(function_name) && !declared_var_names.contains(function_name)) {
// i. Perform ? env.CreateGlobalVarBinding(F, false).
generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Var, false);
+ generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
+ generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Initialize, Bytecode::Op::EnvironmentMode::Var);
// ii. Append F to declaredFunctionOrVarNames.
declared_function_names.set(function_name);
@@ -848,7 +850,7 @@ Bytecode::CodeGenerationErrorOr<void> FunctionDeclaration::generate_bytecode(Byt
if (m_is_hoisted) {
auto index = generator.intern_identifier(name());
generator.emit<Bytecode::Op::GetVariable>(index);
- generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Initialize, Bytecode::Op::EnvironmentMode::Var);
+ generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Set, Bytecode::Op::EnvironmentMode::Var);
}
return {};
}
@@ -1127,10 +1129,11 @@ Bytecode::CodeGenerationErrorOr<void> VariableDeclaration::generate_bytecode(Byt
generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
auto initialization_mode = is_lexical_declaration() ? Bytecode::Op::SetVariable::InitializationMode::Initialize : Bytecode::Op::SetVariable::InitializationMode::Set;
+ auto environment_mode = is_lexical_declaration() ? Bytecode::Op::EnvironmentMode::Lexical : Bytecode::Op::EnvironmentMode::Var;
TRY(declarator.target().visit(
[&](NonnullRefPtr<Identifier> const& id) -> Bytecode::CodeGenerationErrorOr<void> {
- generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(id->string()), initialization_mode);
+ generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(id->string()), initialization_mode, environment_mode);
return {};
},
[&](NonnullRefPtr<BindingPattern> const& pattern) -> Bytecode::CodeGenerationErrorOr<void> {