2025-08-19 16:43:13 +09:00
|
|
|
/*!
|
|
|
|
|
* User-Defined Box Factory
|
|
|
|
|
*
|
|
|
|
|
* Handles creation of user-defined Box types through InstanceBox
|
|
|
|
|
* Manages inheritance, fields, methods, and birth/fini lifecycle
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use super::BoxFactory;
|
|
|
|
|
use crate::box_trait::NyashBox;
|
2025-08-20 00:21:20 +09:00
|
|
|
use crate::interpreter::{RuntimeError, SharedState};
|
|
|
|
|
use crate::instance_v2::InstanceBox;
|
2025-08-19 16:43:13 +09:00
|
|
|
|
|
|
|
|
/// Factory for user-defined Box types
|
|
|
|
|
pub struct UserDefinedBoxFactory {
|
2025-08-20 00:21:20 +09:00
|
|
|
shared_state: SharedState,
|
2025-08-19 16:43:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl UserDefinedBoxFactory {
|
2025-08-20 00:21:20 +09:00
|
|
|
pub fn new(shared_state: SharedState) -> Self {
|
2025-08-19 16:43:13 +09:00
|
|
|
Self {
|
2025-08-20 00:21:20 +09:00
|
|
|
shared_state,
|
2025-08-19 16:43:13 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BoxFactory for UserDefinedBoxFactory {
|
|
|
|
|
fn create_box(
|
|
|
|
|
&self,
|
2025-08-20 00:21:20 +09:00
|
|
|
name: &str,
|
2025-08-19 16:43:13 +09:00
|
|
|
_args: &[Box<dyn NyashBox>],
|
|
|
|
|
) -> Result<Box<dyn NyashBox>, RuntimeError> {
|
2025-08-20 00:21:20 +09:00
|
|
|
// Look up box declaration
|
|
|
|
|
let box_decl = {
|
|
|
|
|
let box_decls = self.shared_state.box_declarations.read().unwrap();
|
|
|
|
|
box_decls.get(name).cloned()
|
|
|
|
|
};
|
2025-08-19 16:43:13 +09:00
|
|
|
|
2025-08-20 00:21:20 +09:00
|
|
|
let box_decl = box_decl.ok_or_else(|| RuntimeError::InvalidOperation {
|
|
|
|
|
message: format!("Unknown Box type: {}", name),
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// Create InstanceBox with fields and methods
|
|
|
|
|
let instance = InstanceBox::from_declaration(
|
|
|
|
|
name.to_string(),
|
|
|
|
|
box_decl.fields.clone(),
|
|
|
|
|
box_decl.methods.clone(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TODO: Execute birth/init constructor with args
|
|
|
|
|
// For now, just return the instance
|
|
|
|
|
Ok(Box::new(instance))
|
2025-08-19 16:43:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn box_types(&self) -> Vec<&str> {
|
2025-08-20 00:21:20 +09:00
|
|
|
// Can't return borrowed strings from temporary RwLock guard
|
|
|
|
|
// For now, return empty - this method isn't critical
|
2025-08-19 16:43:13 +09:00
|
|
|
vec![]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_available(&self) -> bool {
|
2025-08-20 00:21:20 +09:00
|
|
|
// Always available when SharedState is present
|
|
|
|
|
true
|
2025-08-19 16:43:13 +09:00
|
|
|
}
|
|
|
|
|
}
|