Skip to content

Commit

Permalink
Prevent DefVar opcode emit for global binding (boa-dev#3453)
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored and ahaoboy committed Nov 15, 2023
1 parent e0438b0 commit 36ba2bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
6 changes: 4 additions & 2 deletions boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
match opcode {
BindingOpcode::Var => {
let binding = self.variable_environment.get_identifier_reference(name);
let index = self.get_or_insert_binding(binding.locator());
self.emit_with_varying_operand(Opcode::DefVar, index);
if !binding.locator().is_global() {
let index = self.get_or_insert_binding(binding.locator());
self.emit_with_varying_operand(Opcode::DefVar, index);
}
}
BindingOpcode::InitVar => match self.lexical_environment.set_mutable_binding(name) {
Ok(binding) => {
Expand Down
14 changes: 5 additions & 9 deletions boa_engine/src/vm/opcode/define/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ impl DefVar {
// TODO: spec specifies to return `empty` on empty vars, but we're trying to initialize.
let binding_locator = context.vm.frame().code_block.bindings[index];

if binding_locator.is_global() {
// already initialized at compile time
} else {
context.vm.environments.put_value_if_uninitialized(
binding_locator.environment_index(),
binding_locator.binding_index(),
JsValue::undefined(),
);
}
context.vm.environments.put_value_if_uninitialized(
binding_locator.environment_index(),
binding_locator.binding_index(),
JsValue::undefined(),
);
Ok(CompletionType::Normal)
}
}
Expand Down

0 comments on commit 36ba2bf

Please sign in to comment.