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
}
}

View File

@ -11,6 +11,7 @@ use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::fmt;
use crate::box_trait::NyashBox;
use crate::instance_v2::InstanceBox;
use lazy_static::lazy_static;
lazy_static! {
@ -67,7 +68,7 @@ impl BoxFinalizer {
if !is_finalized(*box_id) {
// fini()メソッドを呼び出す(存在する場合)
if let Some(instance) = nyash_box.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
if let Some(instance) = nyash_box.as_any().downcast_ref::<InstanceBox>() {
let _ = instance.fini();
}
mark_as_finalized(*box_id);

View File

@ -220,6 +220,13 @@ impl NyashInterpreter {
pub fn new() -> Self {
let shared = SharedState::new();
// Register user-defined box factory with unified registry
use crate::box_factory::user_defined::UserDefinedBoxFactory;
use crate::runtime::register_user_defined_factory;
let factory = UserDefinedBoxFactory::new(shared.clone());
register_user_defined_factory(Arc::new(factory));
Self {
shared,
local_vars: HashMap::new(),
@ -387,7 +394,7 @@ impl NyashInterpreter {
} else {
eprintln!("🔍 DEBUG: '{}' not found in statics MapBox", name);
}
} else if let Some(instance) = statics_namespace.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
} else if let Some(instance) = statics_namespace.as_any().downcast_ref::<InstanceBox>() {
eprintln!("🔍 DEBUG: statics is an InstanceBox, looking for '{}'", name);
if let Some(static_box) = instance.get_field(name) {
eprintln!("🔍 DEBUG: Found '{}' in statics namespace", name);

View File

@ -8,8 +8,20 @@ use crate::box_trait::{NyashBox, IntegerBox, BoolBox, CompareBox};
use crate::boxes::StringBox; // 🔧 統一レジストリと一致させる
use crate::boxes::FloatBox;
use crate::interpreter::core::{NyashInterpreter, RuntimeError};
use crate::instance_v2::InstanceBox;
// Local helper functions to bypass import issues
/// InstanceBoxでラップされている場合、内部のBoxを取得する
/// シンプルなヘルパー関数で型地獄を回避
fn unwrap_instance(boxed: &dyn NyashBox) -> &dyn NyashBox {
if let Some(instance) = boxed.as_any().downcast_ref::<InstanceBox>() {
if let Some(ref inner) = instance.inner_content {
return inner.as_ref();
}
}
boxed
}
pub(super) fn try_add_operation(left: &dyn NyashBox, right: &dyn NyashBox) -> Option<Box<dyn NyashBox>> {
// 🔍 デバッグ出力追加
eprintln!("🔍 try_add_operation: left={}, right={}", left.type_name(), right.type_name());

View File

@ -36,6 +36,44 @@ impl NyashInterpreter {
match registry_lock.create_box(class, &args) {
Ok(box_instance) => {
eprintln!("🏭 Unified registry created: {}", class);
// Check if this is a user-defined box that needs constructor execution
if let Some(_instance_box) = box_instance.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
// This is a user-defined box, we need to execute its constructor
eprintln!("🔍 User-defined box detected, executing constructor");
// Check if we have a box declaration for this class
let (box_decl_opt, constructor_opt) = {
let box_decls = self.shared.box_declarations.read().unwrap();
if let Some(box_decl) = box_decls.get(class) {
// Find the appropriate constructor
let constructor_name = format!("init/{}", arguments.len());
let constructor = box_decl.constructors.get(&constructor_name).cloned();
(Some(box_decl.clone()), constructor)
} else {
(None, None)
}
};
if let Some(box_decl) = box_decl_opt {
if let Some(constructor) = constructor_opt {
// Execute the constructor
let instance_arc: SharedNyashBox = Arc::from(box_instance);
drop(registry_lock); // Release lock before executing constructor
self.execute_constructor(&instance_arc, &constructor, arguments, &box_decl)?;
return Ok((*instance_arc).clone_box());
} else if arguments.is_empty() {
// No constructor needed for zero arguments
return Ok(box_instance);
} else {
return Err(RuntimeError::InvalidOperation {
message: format!("No constructor found for {} with {} arguments", class, arguments.len()),
});
}
}
}
// Not a user-defined box or no constructor needed
return Ok(box_instance);
},
Err(e) => {

View File

@ -15,6 +15,7 @@
use super::*;
use crate::boxes::SoundBox;
use crate::method_box::MethodBox;
use crate::instance_v2::InstanceBox;
impl NyashInterpreter {
/// SoundBoxのメソッド呼び出しを実行
@ -159,7 +160,7 @@ impl NyashInterpreter {
let instance = instance_arc.lock().unwrap();
// InstanceBoxにダウンキャスト
if let Some(instance_box) = instance.as_any().downcast_ref::<crate::instance_v2::InstanceBox>() {
if let Some(instance_box) = instance.as_any().downcast_ref::<InstanceBox>() {
// メソッドを取得
let method_ast = instance_box.get_method(&method_box.method_name)
.ok_or(RuntimeError::InvalidOperation {