use crate::{
error::JsNativeError,
object::internal_methods::InternalMethodContext,
vm::{opcode::Operation, CompletionType},
Context, JsResult,
};
#[derive(Debug, Clone, Copy)]
pub(crate) struct DeletePropertyByName;
impl DeletePropertyByName {
fn operation(context: &mut Context, index: usize) -> JsResult<CompletionType> {
let value = context.vm.pop();
let object = value.to_object(context)?;
let code_block = context.vm.frame().code_block();
let key = code_block.constant_string(index).into();
let strict = code_block.strict();
let result = object.__delete__(&key, &mut InternalMethodContext::new(context))?;
if !result && strict {
return Err(JsNativeError::typ()
.with_message("Cannot delete property")
.into());
}
context.vm.push(result);
Ok(CompletionType::Normal)
}
}
impl Operation for DeletePropertyByName {
const NAME: &'static str = "DeletePropertyByName";
const INSTRUCTION: &'static str = "INST - DeletePropertyByName";
const COST: u8 = 3;
fn execute(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u8>() as usize;
Self::operation(context, index)
}
fn execute_with_u16_operands(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u16>() as usize;
Self::operation(context, index)
}
fn execute_with_u32_operands(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u32>() as usize;
Self::operation(context, index)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct DeletePropertyByValue;
impl Operation for DeletePropertyByValue {
const NAME: &'static str = "DeletePropertyByValue";
const INSTRUCTION: &'static str = "INST - DeletePropertyByValue";
const COST: u8 = 3;
fn execute(context: &mut Context) -> JsResult<CompletionType> {
let key_value = context.vm.pop();
let value = context.vm.pop();
let object = value.to_object(context)?;
let property_key = key_value.to_property_key(context)?;
let result = object.__delete__(&property_key, &mut InternalMethodContext::new(context))?;
if !result && context.vm.frame().code_block().strict() {
return Err(JsNativeError::typ()
.with_message("Cannot delete property")
.into());
}
context.vm.push(result);
Ok(CompletionType::Normal)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct DeleteName;
impl DeleteName {
fn operation(context: &mut Context, index: usize) -> JsResult<CompletionType> {
let mut binding_locator = context.vm.frame().code_block.bindings[index].clone();
context.find_runtime_binding(&mut binding_locator)?;
let deleted = context.delete_binding(&binding_locator)?;
context.vm.push(deleted);
Ok(CompletionType::Normal)
}
}
impl Operation for DeleteName {
const NAME: &'static str = "DeleteName";
const INSTRUCTION: &'static str = "INST - DeleteName";
const COST: u8 = 3;
fn execute(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u8>();
Self::operation(context, index as usize)
}
fn execute_with_u16_operands(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u16>() as usize;
Self::operation(context, index)
}
fn execute_with_u32_operands(context: &mut Context) -> JsResult<CompletionType> {
let index = context.vm.read::<u32>();
Self::operation(context, index as usize)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct DeleteSuperThrow;
impl Operation for DeleteSuperThrow {
const NAME: &'static str = "DeleteSuperThrow";
const INSTRUCTION: &'static str = "INST - DeleteSuperThrow";
const COST: u8 = 2;
fn execute(_: &mut Context) -> JsResult<CompletionType> {
Err(JsNativeError::reference()
.with_message("cannot delete a property of `super`")
.into())
}
}