use crate::{
property::PropertyDescriptor,
vm::{opcode::Operation, CompletionType},
Context, JsNativeError, JsResult,
};
#[derive(Debug, Clone, Copy)]
pub(crate) struct DefineOwnPropertyByName;
impl Operation for DefineOwnPropertyByName {
const NAME: &'static str = "DefineOwnPropertyByName";
const INSTRUCTION: &'static str = "INST - DefineOwnPropertyByName";
fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let index = context.vm.read::<u32>();
let value = context.vm.pop();
let object = context.vm.pop();
let object = if let Some(object) = object.as_object() {
object.clone()
} else {
object.to_object(context)?
};
let name = context.vm.frame().code_block.names[index as usize].clone();
object.__define_own_property__(
&name.into(),
PropertyDescriptor::builder()
.value(value)
.writable(true)
.enumerable(true)
.configurable(true)
.build(),
context,
)?;
Ok(CompletionType::Normal)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct DefineOwnPropertyByValue;
impl Operation for DefineOwnPropertyByValue {
const NAME: &'static str = "DefineOwnPropertyByValue";
const INSTRUCTION: &'static str = "INST - DefineOwnPropertyByValue";
fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
let value = context.vm.pop();
let key = context.vm.pop();
let object = context.vm.pop();
let object = if let Some(object) = object.as_object() {
object.clone()
} else {
object.to_object(context)?
};
let key = key.to_property_key(context)?;
let success = object.__define_own_property__(
&key,
PropertyDescriptor::builder()
.value(value)
.writable(true)
.enumerable(true)
.configurable(true)
.build(),
context,
)?;
if !success {
return Err(JsNativeError::typ()
.with_message("failed to defined own property")
.into());
}
Ok(CompletionType::Normal)
}
}