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

@ -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 {