Browse Source

Make `typeof` throw when accessing uninitialized variables (#2902)

pull/2905/head
raskad 2 years ago committed by GitHub
parent
commit
990e4df947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      boa_engine/src/vm/opcode/get/name.rs

18
boa_engine/src/vm/opcode/get/name.rs

@ -1,7 +1,7 @@
use crate::{ use crate::{
error::JsNativeError, error::JsNativeError,
vm::{opcode::Operation, CompletionType}, vm::{opcode::Operation, CompletionType},
Context, JsResult, Context, JsResult, JsValue,
}; };
/// `GetName` implements the Opcode Operation for `Opcode::GetName` /// `GetName` implements the Opcode Operation for `Opcode::GetName`
@ -102,9 +102,23 @@ impl Operation for GetNameOrUndefined {
let mut binding_locator = context.vm.frame().code_block.bindings[index as usize]; let mut binding_locator = context.vm.frame().code_block.bindings[index as usize];
binding_locator.throw_mutate_immutable(context)?; binding_locator.throw_mutate_immutable(context)?;
let is_global = binding_locator.is_global();
context.find_runtime_binding(&mut binding_locator)?; context.find_runtime_binding(&mut binding_locator)?;
let value = context.get_binding(binding_locator)?.unwrap_or_default(); let value = if let Some(value) = context.get_binding(binding_locator)? {
value
} else if is_global {
JsValue::undefined()
} else {
let name = context
.interner()
.resolve_expect(binding_locator.name().sym())
.to_string();
return Err(JsNativeError::reference()
.with_message(format!("{name} is not defined"))
.into());
};
context.vm.push(value); context.vm.push(value);
Ok(CompletionType::Normal) Ok(CompletionType::Normal)

Loading…
Cancel
Save