diff --git a/boa_engine/src/environments/runtime/mod.rs b/boa_engine/src/environments/runtime/mod.rs index 1c2b284b87f..f873dfd95f0 100644 --- a/boa_engine/src/environments/runtime/mod.rs +++ b/boa_engine/src/environments/runtime/mod.rs @@ -672,7 +672,11 @@ impl Context<'_> { } } - /// Return the environment at the given index. Panics if the index is out of range. + /// Return the environment at the given index. + /// + /// # Panics + /// + /// Panics if the `index` is out of range. pub(crate) fn environment_expect(&self, index: u32) -> &Environment { self.vm .environments diff --git a/boa_engine/src/vm/code_block.rs b/boa_engine/src/vm/code_block.rs index 86461a758fb..0e4a000bb26 100644 --- a/boa_engine/src/vm/code_block.rs +++ b/boa_engine/src/vm/code_block.rs @@ -251,31 +251,49 @@ impl CodeBlock { .find(|(_, handler)| handler.contains(pc)) } + /// Get the [`JsString`] constant from the [`CodeBlock`]. + /// + /// # Panics + /// + /// If the type of the [`Constant`] is not [`Constant::String`]. + /// Or `index` is greater or equal to length of `constants`. pub(crate) fn constant_string_expect(&self, index: usize) -> JsString { - if let Constant::String(value) = &self.constants[index] { + if let Some(Constant::String(value)) = self.constants.get(index) { return value.clone(); } - panic!("there should be a string constant at index {index}") + panic!("expected string constant at index {index}") } + /// Get the function ([`Gc`]) constant from the [`CodeBlock`]. + /// + /// # Panics + /// + /// If the type of the [`Constant`] is not [`Constant::Function`]. + /// Or `index` is greater or equal to length of `constants`. pub(crate) fn constant_function_expect(&self, index: usize) -> Gc { - if let Constant::Function(value) = &self.constants[index] { + if let Some(Constant::Function(value)) = self.constants.get(index) { return value.clone(); } - panic!("there should be a function constant at index {index}") + panic!("expected function constant at index {index}") } + /// Get the [`CompileTimeEnvironment`] constant from the [`CodeBlock`]. + /// + /// # Panics + /// + /// If the type of the [`Constant`] is not [`Constant::CompileTimeEnvironment`]. + /// Or `index` is greater or equal to length of `constants`. pub(crate) fn constant_compile_time_environment_expect( &self, index: usize, ) -> Rc { - if let Constant::CompileTimeEnvironment(value) = &self.constants[index] { + if let Some(Constant::CompileTimeEnvironment(value)) = self.constants.get(index) { return value.clone(); } - panic!("there should be a compile time environment constant at index {index}") + panic!("expected compile time environment constant at index {index}") } }