feat(plugin): Fix plugin BoxRef return and Box argument support

- Fixed deadlock in FileBox plugin copyFrom implementation (single lock)
- Added TLV Handle (tag=8) parsing in calls.rs for returned BoxRefs
- Improved plugin loader with config path consistency and detailed logging
- Fixed loader routing for proper Handle type_id/fini_method_id resolution
- Added detailed logging for TLV encoding/decoding in plugin_loader_v2

Test docs/examples/plugin_boxref_return.nyash now works correctly:
- cloneSelf() returns FileBox Handle properly
- copyFrom(Box) accepts plugin Box arguments
- Both FileBox instances close and fini correctly

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-08-21 00:41:26 +09:00
parent af32896574
commit cc2a820af7
274 changed files with 7244 additions and 4608 deletions

View File

@ -0,0 +1,82 @@
# Phase 9.78e: Dynamic Method Dispatch Implementation Summary
## 🎯 Overview
Phase 9.78e aimed to implement dynamic method dispatch through the `call_method` trait method to unify method calling across all Box types.
## ✅ Completed Tasks
### 1. **NyashBox Trait Enhancement**
- Added `call_method` to the NyashBox trait in `src/box_trait.rs`
- Default implementation returns `MethodNotFound` error
- Signature: `fn call_method(&mut self, method_name: &str, args: Vec<NyashValue>) -> Result<NyashValue, RuntimeError>`
### 2. **StringBox Implementation**
- Implemented `call_method` in `src/boxes/string_box.rs`
- Supports all StringBox methods:
- `type_name()`, `equals()`, `length()`
- `concat()`, `split()`, `toUpperCase()`, `toLowerCase()`
- `trim()`, `indexOf()`, `replace()`, `charAt()`, `substring()`
- Includes argument validation and proper error handling
### 3. **InstanceBox Implementation**
- Implemented `call_method` in `src/instance_v2.rs` with delegation pattern:
1. First checks user-defined methods
2. Delegates to inner box for builtin methods
3. Handles InstanceBox-specific methods (`getField`, `setField`, `hasMethod`)
- Enables transparent method calls on wrapped builtin boxes
### 4. **Error Type Updates**
- Added new RuntimeError variants:
- `MethodNotFound { method_name: String, type_name: String }`
- `FieldNotFound { field_name: String, class_name: String }`
### 5. **Interpreter Integration (Partial)**
- Added call_method integration in `src/interpreter/expressions/calls.rs`
- Implemented type conversion logic for Box<dyn NyashBox> to NyashValue
- Added debug output for tracking method dispatch flow
## 🚧 Challenges Encountered
### 1. **Type Conversion Complexity**
- Converting between `Box<dyn NyashBox>`, `Arc<Mutex<dyn NyashBox>>`, and `NyashValue`
- Current workaround: Direct type-based conversion for basic types
### 2. **Binary Compilation Issues**
- Several unrelated compilation errors in the binary prevent full testing
- Library builds successfully with call_method implementation
### 3. **Architecture Considerations**
- The current Box/NyashValue dual system creates friction
- Future consideration: Unified value representation system
## 📋 Test Results
- Basic StringBox creation and string operations work correctly
- Method calls currently fall back to legacy dispatch system
- Call_method infrastructure is in place but needs full integration
## 🔮 Next Steps
### Immediate Tasks:
1. Fix binary compilation errors to enable full testing
2. Complete NyashValue/Box type conversion helpers
3. Implement call_method for remaining builtin Box types
### Long-term Improvements:
1. Consider unified value representation to simplify type conversions
2. Optimize method dispatch performance
3. Add comprehensive test coverage for all Box types
## 💡 Key Insights
- The delegation pattern in InstanceBox successfully enables method calls on wrapped boxes
- Dynamic dispatch through call_method provides a clean abstraction for method calling
- Type conversion between the Box trait system and NyashValue remains a key challenge
## 📝 Code Locations
- Trait definition: `src/box_trait.rs:124-130`
- StringBox impl: `src/boxes/string_box.rs:160-313`
- InstanceBox impl: `src/instance_v2.rs:197-263`
- Interpreter integration: `src/interpreter/expressions/calls.rs:230-288`
---
*Phase 9.78e establishes the foundation for unified method dispatch across all Nyash Box types, with the core infrastructure successfully implemented and ready for expanded integration.*