use super::VaryingOperand;
use crate::{
Context, JsObject, JsValue,
module::ModuleKind,
vm::{ActiveRunnable, opcode::Operation},
};
use std::unreachable;
#[derive(Debug, Clone, Copy)]
pub(crate) struct NewTarget;
impl NewTarget {
#[inline(always)]
pub(super) fn operation(dst: VaryingOperand, context: &mut Context) {
let new_target = if let Some(new_target) = context
.vm
.environments
.get_this_environment()
.as_function()
.and_then(|env| env.slots().new_target().cloned())
{
new_target.into()
} else {
JsValue::undefined()
};
context.vm.set_register(dst.into(), new_target);
}
}
impl Operation for NewTarget {
const NAME: &'static str = "NewTarget";
const INSTRUCTION: &'static str = "INST - NewTarget";
const COST: u8 = 2;
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct ImportMeta;
impl ImportMeta {
#[inline(always)]
pub(super) fn operation(dst: VaryingOperand, context: &mut Context) {
let Some(ActiveRunnable::Module(module)) = context.get_active_script_or_module() else {
unreachable!("2. Assert: module is a Source Text Module Record.");
};
let ModuleKind::SourceText(src) = module.kind() else {
unreachable!("2. Assert: module is a Source Text Module Record.");
};
let import_meta = src
.import_meta()
.borrow_mut()
.get_or_insert_with(|| {
let import_meta = JsObject::with_null_proto();
context
.module_loader()
.init_import_meta(&import_meta, &module, context);
import_meta
})
.clone();
context.vm.set_register(dst.into(), import_meta.into());
}
}
impl Operation for ImportMeta {
const NAME: &'static str = "ImportMeta";
const INSTRUCTION: &'static str = "INST - ImportMeta";
const COST: u8 = 6;
}