feat: Implement Phase 9.78e instance_v2 migration with unified registry

Major achievements:
-  UserDefinedBoxFactory implementation with unified registry integration
-  Constructor execution for user-defined boxes (Person init working)
-  Import path fixes across interpreter modules
-  unwrap_instance helper function for InstanceBox operator support

Technical details:
- Modified UnifiedBoxRegistry to handle empty box_types() factories
- Implemented constructor execution in execute_new for InstanceBox
- Added unwrap_instance helper to handle InstanceBox wrapping in operators
- Updated CURRENT_TASK.md with detailed progress tracking

Next: Fix 4 operator functions to complete InstanceBox operator support

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-20 00:21:20 +09:00
parent 5a50cf6415
commit e5e381aa83
9 changed files with 274 additions and 70 deletions

View File

@ -90,10 +90,22 @@ impl UnifiedBoxRegistry {
}
drop(cache);
// Fallback: linear search through all factories
// Linear search through all factories
for factory in &self.factories {
if factory.box_types().contains(&name) && factory.is_available() {
return factory.create_box(name, args);
if !factory.is_available() {
continue;
}
// For factories that advertise types, check if they support this type
let box_types = factory.box_types();
if !box_types.is_empty() && !box_types.contains(&name) {
continue;
}
// Try to create the box (factories with empty box_types() will always be tried)
match factory.create_box(name, args) {
Ok(boxed) => return Ok(boxed),
Err(_) => continue, // Try next factory
}
}

View File

@ -7,19 +7,18 @@
use super::BoxFactory;
use crate::box_trait::NyashBox;
use crate::interpreter::RuntimeError;
use crate::interpreter::{RuntimeError, SharedState};
use crate::instance_v2::InstanceBox;
/// Factory for user-defined Box types
pub struct UserDefinedBoxFactory {
// TODO: This will need access to the interpreter context
// to look up box declarations and execute constructors
// For now, this is a placeholder
shared_state: SharedState,
}
impl UserDefinedBoxFactory {
pub fn new() -> Self {
pub fn new(shared_state: SharedState) -> Self {
Self {
// TODO: Initialize with interpreter reference
shared_state,
}
}
}
@ -27,28 +26,39 @@ impl UserDefinedBoxFactory {
impl BoxFactory for UserDefinedBoxFactory {
fn create_box(
&self,
_name: &str,
name: &str,
_args: &[Box<dyn NyashBox>],
) -> Result<Box<dyn NyashBox>, RuntimeError> {
// TODO: Implementation will be moved from objects.rs
// This will:
// 1. Look up box declaration
// 2. Create InstanceBox with fields and methods
// 3. Execute birth constructor if present
// 4. Return the instance
// Look up box declaration
let box_decl = {
let box_decls = self.shared_state.box_declarations.read().unwrap();
box_decls.get(name).cloned()
};
Err(RuntimeError::InvalidOperation {
message: "User-defined Box factory not yet implemented".to_string(),
})
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))
}
fn box_types(&self) -> Vec<&str> {
// TODO: Return list of registered user-defined Box types
// Can't return borrowed strings from temporary RwLock guard
// For now, return empty - this method isn't critical
vec![]
}
fn is_available(&self) -> bool {
// TODO: Check if interpreter context is available
false
// Always available when SharedState is present
true
}
}