use std::{
fmt::{self, Write},
sync::Arc,
};
use crate::{args::Signature, bytecode::Code, expressions::Identifier, intern::Interns, namespace::NamespaceId};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct Function {
pub name: Identifier,
pub signature: Signature,
pub namespace_size: usize,
pub free_var_enclosing_slots: Vec<NamespaceId>,
pub free_var_slots: Vec<NamespaceId>,
pub cell_var_slots: Vec<NamespaceId>,
pub cell_param_indices: Vec<Option<usize>>,
pub defaults_count: usize,
pub is_async: bool,
pub code: Arc<Code>,
}
impl Function {
#[expect(clippy::too_many_arguments)]
pub fn new(
name: Identifier,
signature: Signature,
namespace_size: usize,
free_var_enclosing_slots: Vec<NamespaceId>,
free_var_slots: Vec<NamespaceId>,
cell_var_slots: Vec<NamespaceId>,
cell_param_indices: Vec<Option<usize>>,
defaults_count: usize,
is_async: bool,
code: Code,
) -> Self {
Self {
name,
signature,
namespace_size,
free_var_enclosing_slots,
free_var_slots,
cell_var_slots,
cell_param_indices,
defaults_count,
is_async,
code: Arc::new(code),
}
}
pub fn py_repr_fmt<W: Write>(&self, f: &mut W, interns: &Interns, py_id: impl fmt::LowerHex) -> fmt::Result {
write!(
f,
"<function '{}' at 0x{:x}>",
interns.get_str(self.name.name_id),
py_id
)
}
}