Skip to content

Commit

Permalink
Apply review
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Oct 26, 2023
1 parent 1d9cf6c commit a2a8472
Show file tree
Hide file tree
Showing 22 changed files with 81 additions and 147 deletions.
6 changes: 5 additions & 1 deletion boa_engine/src/environments/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/module/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ impl SourceTextModule {

// deferred initialization of function exports
for (index, locator, kind) in functions {
let code = codeblock.constant_function_expect(index as usize);
let code = codeblock.constant_function(index as usize);

let function = if kind.is_generator() {
create_generator_function_object(code, kind.is_async(), None, context)
Expand Down
12 changes: 6 additions & 6 deletions boa_engine/src/object/internal_methods/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) fn function_call(
let index = context
.vm
.environments
.push_lexical(code.constant_compile_time_environment_expect(last_env));
.push_lexical(code.constant_compile_time_environment(last_env));
context
.vm
.environments
Expand All @@ -106,7 +106,7 @@ pub(crate) fn function_call(
}

context.vm.environments.push_function(
code.constant_compile_time_environment_expect(last_env),
code.constant_compile_time_environment(last_env),
FunctionSlots::new(this, function_object.clone(), None),
);

Expand All @@ -115,7 +115,7 @@ pub(crate) fn function_call(
context
.vm
.environments
.push_lexical(code.constant_compile_time_environment_expect(last_env));
.push_lexical(code.constant_compile_time_environment(last_env));
}

// Taken from: `FunctionDeclarationInstantiation` abstract function.
Expand Down Expand Up @@ -225,7 +225,7 @@ fn function_construct(
let index = context
.vm
.environments
.push_lexical(code.constant_compile_time_environment_expect(last_env));
.push_lexical(code.constant_compile_time_environment(last_env));
context
.vm
.environments
Expand All @@ -234,7 +234,7 @@ fn function_construct(
}

context.vm.environments.push_function(
code.constant_compile_time_environment_expect(last_env),
code.constant_compile_time_environment(last_env),
FunctionSlots::new(
this.clone().map_or(ThisBindingStatus::Uninitialized, |o| {
ThisBindingStatus::Initialized(o.into())
Expand All @@ -254,7 +254,7 @@ fn function_construct(
context
.vm
.environments
.push_lexical(code.constant_compile_time_environment_expect(last_env));
.push_lexical(code.constant_compile_time_environment(last_env));
}

// Taken from: `FunctionDeclarationInstantiation` abstract function.
Expand Down
54 changes: 34 additions & 20 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,31 +251,49 @@ impl CodeBlock {
.find(|(_, handler)| handler.contains(pc))
}

pub(crate) fn constant_string_expect(&self, index: usize) -> JsString {
if let Constant::String(value) = &self.constants[index] {
/// 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(&self, index: usize) -> JsString {
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}")
}

pub(crate) fn constant_function_expect(&self, index: usize) -> Gc<Self> {
if let Constant::Function(value) = &self.constants[index] {
/// Get the function ([`Gc<CodeBlock>`]) 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(&self, index: usize) -> Gc<Self> {
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}")
}

pub(crate) fn constant_compile_time_environment_expect(
/// 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(
&self,
index: usize,
) -> Rc<CompileTimeEnvironment> {
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}")
}
}

Expand Down Expand Up @@ -351,10 +369,10 @@ impl CodeBlock {
flags_index: flag_index,
} => {
let pattern = self
.constant_string_expect(source_index.value() as usize)
.constant_string(source_index.value() as usize)
.to_std_string_escaped();
let flags = self
.constant_string_expect(flag_index.value() as usize)
.constant_string(flag_index.value() as usize)
.to_std_string_escaped();
format!("/{pattern}/{flags}")
}
Expand Down Expand Up @@ -408,10 +426,8 @@ impl CodeBlock {
let index = index.value() as usize;
format!(
"{index:04}: '{}' (length: {}), method: {method}",
self.constant_function_expect(index)
.name()
.to_std_string_escaped(),
self.constant_function_expect(index).length
self.constant_function(index).name().to_std_string_escaped(),
self.constant_function(index).length
)
}
Instruction::GetArrowFunction { index }
Expand All @@ -421,10 +437,8 @@ impl CodeBlock {
let index = index.value() as usize;
format!(
"{index:04}: '{}' (length: {})",
self.constant_function_expect(index)
.name()
.to_std_string_escaped(),
self.constant_function_expect(index).length
self.constant_function(index).name().to_std_string_escaped(),
self.constant_function(index).length
)
}
Instruction::DefVar { index }
Expand Down Expand Up @@ -469,7 +483,7 @@ impl CodeBlock {
format!(
"{:04}: '{}'",
index.value(),
self.constant_string_expect(index.value() as usize)
self.constant_string(index.value() as usize)
.to_std_string_escaped(),
)
}
Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/opcode/binary_ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ pub(crate) struct InPrivate;

impl InPrivate {
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let name = context
.vm
.frame()
.code_block()
.constant_string_expect(index);
let name = context.vm.frame().code_block().constant_string(index);
let rhs = context.vm.pop();

let Some(rhs) = rhs.as_object() else {
Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/opcode/control_flow/throw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ pub(crate) struct ThrowNewTypeError;

impl ThrowNewTypeError {
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let msg = context
.vm
.frame()
.code_block()
.constant_string_expect(index);
let msg = context.vm.frame().code_block().constant_string(index);
let msg = msg
.to_std_string()
.expect("throw message must be an ASCII string");
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/define/class/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl DefineClassStaticGetterByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down Expand Up @@ -90,7 +90,7 @@ impl DefineClassGetterByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/define/class/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl DefineClassStaticMethodByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down Expand Up @@ -86,7 +86,7 @@ impl DefineClassMethodByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/vm/opcode/define/class/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl DefineClassStaticSetterByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down Expand Up @@ -91,7 +91,7 @@ impl DefineClassSetterByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
{
let function_object = function
Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/opcode/define/own_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ impl DefineOwnPropertyByName {
} else {
object.to_object(context)?
};
let name = context
.vm
.frame()
.code_block()
.constant_string_expect(index);
let name = context.vm.frame().code_block().constant_string(index);
object.__define_own_property__(
&name.into(),
PropertyDescriptor::builder()
Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/vm/opcode/delete/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl DeletePropertyByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
let result = object.__delete__(&key, context)?;
if !result && context.vm.frame().code_block.strict() {
Expand Down
24 changes: 4 additions & 20 deletions boa_engine/src/vm/opcode/get/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub(crate) struct GetArrowFunction;
impl GetArrowFunction {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, false, true, false, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down Expand Up @@ -55,11 +51,7 @@ pub(crate) struct GetAsyncArrowFunction;
impl GetAsyncArrowFunction {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, true, true, false, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down Expand Up @@ -101,11 +93,7 @@ impl GetFunction {
index: usize,
method: bool,
) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, false, false, method, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down Expand Up @@ -150,11 +138,7 @@ impl GetFunctionAsync {
index: usize,
method: bool,
) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_function_object_fast(code, true, false, method, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down
12 changes: 2 additions & 10 deletions boa_engine/src/vm/opcode/get/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ pub(crate) struct GetGenerator;
impl GetGenerator {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_generator_function_object(code, false, None, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down Expand Up @@ -55,11 +51,7 @@ pub(crate) struct GetGeneratorAsync;
impl GetGeneratorAsync {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let code = context
.vm
.frame()
.code_block()
.constant_function_expect(index);
let code = context.vm.frame().code_block().constant_function(index);
let function = create_generator_function_object(code, true, None, context);
context.vm.push(function);
Ok(CompletionType::Normal)
Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/opcode/get/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ pub(crate) struct GetPrivateField;

impl GetPrivateField {
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let name = context
.vm
.frame()
.code_block()
.constant_string_expect(index);
let name = context.vm.frame().code_block().constant_string(index);
let value = context.vm.pop();
let base_obj = value.to_object(context)?;

Expand Down
2 changes: 1 addition & 1 deletion boa_engine/src/vm/opcode/get/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl GetPropertyByName {
.vm
.frame()
.code_block()
.constant_string_expect(index)
.constant_string(index)
.into();
let result = object.__get__(&key, receiver, context)?;

Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/opcode/push/class/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ pub(crate) struct PushClassFieldPrivate;
impl PushClassFieldPrivate {
#[allow(clippy::unnecessary_wraps)]
fn operation(context: &mut Context<'_>, index: usize) -> JsResult<CompletionType> {
let name = context
.vm
.frame()
.code_block()
.constant_string_expect(index);
let name = context.vm.frame().code_block().constant_string(index);
let field_function_value = context.vm.pop();
let class_value = context.vm.pop();

Expand Down

0 comments on commit a2a8472

Please sign in to comment.