Browse Source

Prevent `DefVar` opcode emit for global binding (#3453)

pull/3454/head
Haled Odat 1 year ago committed by GitHub
parent
commit
1e90f56ec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      boa_engine/src/bytecompiler/mod.rs
  2. 4
      boa_engine/src/vm/opcode/define/mod.rs

2
boa_engine/src/bytecompiler/mod.rs

@ -407,9 +407,11 @@ impl<'ctx, 'host> ByteCompiler<'ctx, 'host> {
match opcode { match opcode {
BindingOpcode::Var => { BindingOpcode::Var => {
let binding = self.variable_environment.get_identifier_reference(name); let binding = self.variable_environment.get_identifier_reference(name);
if !binding.locator().is_global() {
let index = self.get_or_insert_binding(binding.locator()); let index = self.get_or_insert_binding(binding.locator());
self.emit_with_varying_operand(Opcode::DefVar, index); self.emit_with_varying_operand(Opcode::DefVar, index);
} }
}
BindingOpcode::InitVar => match self.lexical_environment.set_mutable_binding(name) { BindingOpcode::InitVar => match self.lexical_environment.set_mutable_binding(name) {
Ok(binding) => { Ok(binding) => {
let index = self.get_or_insert_binding(binding); let index = self.get_or_insert_binding(binding);

4
boa_engine/src/vm/opcode/define/mod.rs

@ -22,15 +22,11 @@ impl DefVar {
// TODO: spec specifies to return `empty` on empty vars, but we're trying to initialize. // 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]; 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( context.vm.environments.put_value_if_uninitialized(
binding_locator.environment_index(), binding_locator.environment_index(),
binding_locator.binding_index(), binding_locator.binding_index(),
JsValue::undefined(), JsValue::undefined(),
); );
}
Ok(CompletionType::Normal) Ok(CompletionType::Normal)
} }
} }

Loading…
Cancel
Save