use crate::{
    vm::{opcode::Operation, ShouldExit},
    Context, JsResult,
};

/// `PushLiteral` implements the Opcode Operation for `Opcode::PushLiteral`
///
/// Operation:
///  - Push literal value on the stack.
#[derive(Debug, Clone, Copy)]
pub(crate) struct PushLiteral;

impl Operation for PushLiteral {
    const NAME: &'static str = "PushLiteral";
    const INSTRUCTION: &'static str = "INST - PushLiteral";

    fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
        let index = context.vm.read::<u32>() as usize;
        let value = context.vm.frame().code_block.literals[index].clone();
        context.vm.push(value);
        Ok(ShouldExit::False)
    }
}
