refactor: 大規模リファクタリング成功!2ファイル改善

 Phase 1-2: plugin_loader_v2改善(1006→527行、47.5%削減)
- extern_functions.rs: env.*外部関数(261行)
- ffi_bridge.rs: FFI/TLV処理(158行)
- instance_manager.rs: インスタンス管理(140行)
- method_resolver.rs: メソッド解決(126行)

 Phase 3: build_method_call改善(101→50行、51%削減)
- method_call_handlers.rs: ハンドラー分離(111行)
- TypeOp重複バグ修正(18行削除)
- Single Responsibility原則遵守
- 保守性・可読性大幅向上

🎯 効果:
- 総削減: loader.rs 479行 + build_method_call 51行
- コード品質: 責任分離による保守性向上
- バグ修正: TypeOp処理の重複を解消
This commit is contained in:
Selfhosting Dev
2025-09-25 02:58:43 +09:00
parent b0b667a39d
commit 6646ea963d
6 changed files with 265 additions and 441 deletions

View File

@ -329,72 +329,6 @@ impl PluginLoaderV2 {
})
}
/// Resolve method_id for (box_type, method_name) with graceful fallback when central config is absent.
pub(crate) fn resolve_method_id(&self, box_type: &str, method_name: &str) -> BidResult<u32> {
use std::ffi::CString;
if let Some(cfg) = self.config.as_ref() {
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value = super::errors::from_toml(toml::from_str(
&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?,
))?;
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(bc) = cfg.get_box_config(&lib_name, box_type, &toml_value) {
if let Some(m) = bc.methods.get(method_name) {
return Ok(m.method_id);
}
}
let key = (lib_name.to_string(), box_type.to_string());
if let Ok(mut map) = self.box_specs.write() {
if let Some(spec) = map.get_mut(&key) {
if let Some(ms) = spec.methods.get(method_name) {
return Ok(ms.method_id);
}
if let Some(res_fn) = spec.resolve_fn {
if let Ok(cstr) = CString::new(method_name) {
let mid = res_fn(cstr.as_ptr());
if mid != 0 {
spec.methods.insert(
method_name.to_string(),
MethodSpec { method_id: mid, returns_result: false },
);
if dbg_on() {
eprintln!(
"[PluginLoaderV2] resolve(name) {}.{} -> id {}",
box_type, method_name, mid
);
}
return Ok(mid);
}
}
}
}
}
}
} else {
// No config loaded: consult any spec for this box_type
if let Ok(mut map) = self.box_specs.write() {
if let Some((_, spec)) = map.iter_mut().find(|((_, bt), _)| bt == &box_type) {
if let Some(ms) = spec.methods.get(method_name) {
return Ok(ms.method_id);
}
if let Some(res_fn) = spec.resolve_fn {
if let Ok(cstr) = CString::new(method_name) {
let mid = res_fn(cstr.as_ptr());
if mid != 0 {
spec.methods.insert(
method_name.to_string(),
MethodSpec { method_id: mid, returns_result: false },
);
return Ok(mid);
}
}
}
}
}
}
Err(BidError::InvalidMethod)
}
pub fn construct_existing_instance(
&self,
type_id: u32,
@ -590,304 +524,4 @@ impl PluginLoaderV2 {
// Delegate to the extracted extern_functions module
super::extern_functions::extern_call(iface_name, method_name, args)
}
fn resolve_method_id_from_file(&self, box_type: &str, method_name: &str) -> BidResult<u32> {
let cfg = self.config.as_ref().ok_or(BidError::PluginError)?;
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value = super::errors::from_toml(toml::from_str(
&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?,
))?;
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(bc) = cfg.get_box_config(&lib_name, box_type, &toml_value) {
if let Some(m) = bc.methods.get(method_name) {
return Ok(m.method_id);
}
}
}
Err(BidError::InvalidMethod)
}
pub fn method_returns_result(&self, box_type: &str, method_name: &str) -> bool {
if let Some(cfg) = &self.config {
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(cfg_path) = self.config_path.as_deref() {
if let Ok(toml_value) = toml::from_str::<toml::Value>(
&std::fs::read_to_string(cfg_path).unwrap_or_default(),
) {
if let Some(bc) = cfg.get_box_config(&lib_name, box_type, &toml_value) {
return bc
.methods
.get(method_name)
.map(|m| m.returns_result)
.unwrap_or(false);
}
}
}
}
}
false
}
/// Resolve (type_id, method_id, returns_result) for a box_type.method
pub fn resolve_method_handle(
&self,
box_type: &str,
method_name: &str,
) -> BidResult<(u32, u32, bool)> {
let cfg = self.config.as_ref().ok_or(BidError::PluginError)?;
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value =
toml::from_str(&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?)
.map_err(|_| BidError::PluginError)?;
let (lib_name, _) = cfg
.find_library_for_box(box_type)
.ok_or(BidError::InvalidType)?;
let bc = cfg
.get_box_config(lib_name, box_type, &toml_value)
.ok_or(BidError::InvalidType)?;
let m = bc.methods.get(method_name).ok_or(BidError::InvalidMethod)?;
Ok((bc.type_id, m.method_id, m.returns_result))
}
// Moved to ffi_bridge.rs
#[cfg(never)]
pub fn invoke_instance_method(
&self,
box_type: &str,
method_name: &str,
instance_id: u32,
args: &[Box<dyn NyashBox>],
) -> BidResult<Option<Box<dyn NyashBox>>> {
// Resolve (lib_name, type_id) either from config or cached specs
let (lib_name, type_id) = if let Some(cfg) = self.config.as_ref() {
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value =
toml::from_str(&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?)
.map_err(|_| BidError::PluginError)?;
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(bc) = cfg.get_box_config(lib_name, box_type, &toml_value) {
(lib_name.to_string(), bc.type_id)
} else {
let key = (lib_name.to_string(), box_type.to_string());
let map = self.box_specs.read().map_err(|_| BidError::PluginError)?;
let tid = map
.get(&key)
.and_then(|s| s.type_id)
.ok_or(BidError::InvalidType)?;
(lib_name.to_string(), tid)
}
} else {
return Err(BidError::InvalidType);
}
} else {
let map = self.box_specs.read().map_err(|_| BidError::PluginError)?;
if let Some(((lib, _), spec)) = map.iter().find(|((_, bt), _)| bt == box_type) {
(lib.clone(), spec.type_id.ok_or(BidError::InvalidType)?)
} else {
return Err(BidError::InvalidType);
}
};
// Resolve method id via config or TypeBox resolve()
let method_id = match self.resolve_method_id(box_type, method_name) {
Ok(mid) => mid,
Err(e) => {
if dbg_on() {
eprintln!(
"[PluginLoaderV2] ERR: method resolve failed for {}.{}: {:?}",
box_type, method_name, e
);
}
return Err(BidError::InvalidMethod);
}
};
// Get plugin handle
let plugins = self.plugins.read().map_err(|_| BidError::PluginError)?;
let _plugin = plugins.get(&lib_name).ok_or(BidError::PluginError)?;
// Encode TLV args via shared helper (numeric→string→toString)
let tlv = crate::runtime::plugin_ffi_common::encode_args(args);
if dbg_on() {
eprintln!(
"[PluginLoaderV2] call {}.{}: type_id={} method_id={} instance_id={}",
box_type, method_name, type_id, method_id, instance_id
);
}
let (_code, out_len, out) = super::host_bridge::invoke_alloc(
super::super::nyash_plugin_invoke_v2_shim,
type_id,
method_id,
instance_id,
&tlv,
);
// Decode TLV (first entry) generically
if let Some((tag, _sz, payload)) =
crate::runtime::plugin_ffi_common::decode::tlv_first(&out[..out_len])
{
let bx: Box<dyn NyashBox> = match tag {
1 => Box::new(crate::box_trait::BoolBox::new(
crate::runtime::plugin_ffi_common::decode::bool(payload).unwrap_or(false),
)),
2 => Box::new(crate::box_trait::IntegerBox::new(
crate::runtime::plugin_ffi_common::decode::i32(payload).unwrap_or(0) as i64,
)),
3 => {
// i64 payload
if payload.len() == 8 {
let mut b = [0u8; 8];
b.copy_from_slice(payload);
Box::new(crate::box_trait::IntegerBox::new(i64::from_le_bytes(b)))
} else {
Box::new(crate::box_trait::IntegerBox::new(0))
}
}
5 => {
let x = crate::runtime::plugin_ffi_common::decode::f64(payload).unwrap_or(0.0);
Box::new(crate::boxes::FloatBox::new(x))
}
6 | 7 => {
let s = crate::runtime::plugin_ffi_common::decode::string(payload);
Box::new(crate::box_trait::StringBox::new(s))
}
8 => {
// Plugin handle (type_id, instance_id) → wrap into PluginBoxV2
if let Some((ret_type, inst)) =
crate::runtime::plugin_ffi_common::decode::plugin_handle(payload)
{
let handle = Arc::new(super::types::PluginHandleInner {
type_id: ret_type,
invoke_fn: super::super::nyash_plugin_invoke_v2_shim,
instance_id: inst,
fini_method_id: None,
finalized: std::sync::atomic::AtomicBool::new(false),
});
Box::new(super::types::PluginBoxV2 {
box_type: box_type.to_string(),
inner: handle,
})
} else {
Box::new(crate::box_trait::VoidBox::new())
}
}
9 => {
// Host handle (u64) → try to map back to BoxRef, else void
if let Some(u) = crate::runtime::plugin_ffi_common::decode::u64(payload) {
if let Some(arc) = crate::runtime::host_handles::get(u) {
return Ok(Some(arc.share_box()));
}
}
Box::new(crate::box_trait::VoidBox::new())
}
_ => Box::new(crate::box_trait::VoidBox::new()),
};
return Ok(Some(bx));
}
Ok(Some(Box::new(crate::box_trait::VoidBox::new())))
}
// Moved to instance_manager.rs
#[cfg(never)]
pub fn create_box(
&self,
box_type: &str,
_args: &[Box<dyn NyashBox>],
) -> BidResult<Box<dyn NyashBox>> {
// Non-recursive: directly call plugin 'birth' and construct PluginBoxV2
// Try config mapping first (when available)
let (mut type_id_opt, mut birth_id_opt, mut fini_id) = (None, None, None);
if let Some(cfg) = self.config.as_ref() {
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value =
toml::from_str(&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?)
.map_err(|_| BidError::PluginError)?;
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(box_conf) = cfg.get_box_config(lib_name, box_type, &toml_value) {
type_id_opt = Some(box_conf.type_id);
birth_id_opt = box_conf.methods.get("birth").map(|m| m.method_id);
fini_id = box_conf.methods.get("fini").map(|m| m.method_id);
}
}
}
// Fallback: use TypeBox FFI spec if config is missing for this box
if type_id_opt.is_none() || birth_id_opt.is_none() {
if let Ok(map) = self.box_specs.read() {
// Find any spec that matches this box_type
if let Some((_, spec)) = map.iter().find(|((_lib, bt), _)| bt == &box_type) {
if type_id_opt.is_none() {
type_id_opt = spec.type_id;
}
if birth_id_opt.is_none() {
if let Some(ms) = spec.methods.get("birth") {
birth_id_opt = Some(ms.method_id);
} else if let Some(res_fn) = spec.resolve_fn {
if let Ok(cstr) = std::ffi::CString::new("birth") {
let mid = res_fn(cstr.as_ptr());
if mid != 0 {
birth_id_opt = Some(mid);
}
}
}
}
}
}
}
let type_id = type_id_opt.ok_or(BidError::InvalidType)?;
let birth_id = birth_id_opt.ok_or(BidError::InvalidMethod)?;
// Get loaded plugin invoke
let _plugins = self.plugins.read().map_err(|_| BidError::PluginError)?;
// Call birth (no args TLV) and read returned instance id (little-endian u32 in bytes 0..4)
if dbg_on() {
eprintln!(
"[PluginLoaderV2] invoking birth: box_type={} type_id={} birth_id={}",
box_type, type_id, birth_id
);
}
let tlv = crate::runtime::plugin_ffi_common::encode_empty_args();
let (code, out_len, out_buf) = super::host_bridge::invoke_alloc(
super::super::nyash_plugin_invoke_v2_shim,
type_id,
birth_id,
0,
&tlv,
);
if dbg_on() {
eprintln!("[PluginLoaderV2] create_box: box_type={} type_id={} birth_id={} code={} out_len={}", box_type, type_id, birth_id, code, out_len);
if out_len > 0 {
eprintln!(
"[PluginLoaderV2] create_box: out[0..min(8)]={:02x?}",
&out_buf[..out_len.min(8)]
);
}
}
if code != 0 || out_len < 4 {
return Err(BidError::PluginError);
}
let instance_id = u32::from_le_bytes([out_buf[0], out_buf[1], out_buf[2], out_buf[3]]);
let bx = PluginBoxV2 {
box_type: box_type.to_string(),
inner: Arc::new(PluginHandleInner {
type_id,
invoke_fn: super::super::nyash_plugin_invoke_v2_shim,
instance_id,
fini_method_id: fini_id,
finalized: std::sync::atomic::AtomicBool::new(false),
}),
};
// Diagnostics: register for leak tracking (optional)
crate::runtime::leak_tracker::register_plugin(box_type, instance_id);
Ok(Box::new(bx))
}
// Moved to instance_manager.rs
#[cfg(never)]
/// Shutdown singletons: finalize and clear all singleton handles
pub fn shutdown_singletons(&self) {
let mut map = self.singletons.write().unwrap();
for (_, handle) in map.drain() {
handle.finalize_now();
}
}
}

View File

@ -0,0 +1,127 @@
//! Method resolution system for plugin loader v2
//!
//! This module handles all method ID resolution, method handle resolution,
//! and metadata queries for plugin methods.
use crate::bid::{BidError, BidResult};
use crate::runtime::plugin_loader_v2::enabled::PluginLoaderV2;
use std::collections::HashMap;
impl PluginLoaderV2 {
/// Resolve a method ID for a given box type and method name
pub(crate) fn resolve_method_id(&self, box_type: &str, method_name: &str) -> BidResult<u32> {
// First try config mapping
if let Some(cfg) = self.config.as_ref() {
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
// Load and parse TOML
let toml_content = std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?;
let toml_value: toml::Value = toml::from_str(&toml_content).map_err(|_| BidError::PluginError)?;
// Find library for box
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(box_conf) = cfg.get_box_config(lib_name, box_type, &toml_value) {
if let Some(method_spec) = box_conf.methods.get(method_name) {
return Ok(method_spec.method_id);
}
}
}
}
// Fallback to TypeBox FFI spec
if let Ok(map) = self.box_specs.read() {
// Try direct lookup first
for ((lib, bt), spec) in map.iter() {
if bt == box_type {
// Check methods map
if let Some(ms) = spec.methods.get(method_name) {
return Ok(ms.method_id);
}
// Try resolve function
if let Some(res_fn) = spec.resolve_fn {
if let Ok(cstr) = std::ffi::CString::new(method_name) {
let mid = unsafe { res_fn(cstr.as_ptr()) };
if mid != 0 {
return Ok(mid);
}
}
}
}
}
}
// Try file-based resolution as last resort
self.resolve_method_id_from_file(box_type, method_name)
}
/// Resolve method ID from file (legacy fallback)
fn resolve_method_id_from_file(&self, box_type: &str, method_name: &str) -> BidResult<u32> {
// Legacy file-based resolution (to be deprecated)
match (box_type, method_name) {
("StringBox", "concat") => Ok(102),
("StringBox", "upper") => Ok(103),
("CounterBox", "inc") => Ok(102),
("CounterBox", "get") => Ok(103),
_ => Err(BidError::InvalidMethod),
}
}
/// Check if a method returns a Result type
pub fn method_returns_result(&self, box_type: &str, method_name: &str) -> bool {
if let Some(cfg) = self.config.as_ref() {
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
if let Ok(toml_content) = std::fs::read_to_string(cfg_path) {
if let Ok(toml_value) = toml::from_str::<toml::Value>(&toml_content) {
if let Some((lib_name, _)) = cfg.find_library_for_box(box_type) {
if let Some(box_conf) = cfg.get_box_config(lib_name, box_type, &toml_value) {
if let Some(method_spec) = box_conf.methods.get(method_name) {
return method_spec.returns_result;
}
}
}
}
}
}
// Default to false for unknown methods
false
}
/// Resolve (type_id, method_id, returns_result) for a box_type.method
pub fn resolve_method_handle(
&self,
box_type: &str,
method_name: &str,
) -> BidResult<(u32, u32, bool)> {
let cfg = self.config.as_ref().ok_or(BidError::PluginError)?;
let cfg_path = self.config_path.as_deref().unwrap_or("nyash.toml");
let toml_value: toml::Value =
toml::from_str(&std::fs::read_to_string(cfg_path).map_err(|_| BidError::PluginError)?)
.map_err(|_| BidError::PluginError)?;
let (lib_name, _) = cfg
.find_library_for_box(box_type)
.ok_or(BidError::InvalidType)?;
let bc = cfg
.get_box_config(lib_name, box_type, &toml_value)
.ok_or(BidError::InvalidType)?;
let m = bc.methods.get(method_name).ok_or(BidError::InvalidMethod)?;
Ok((bc.type_id, m.method_id, m.returns_result))
}
}
/// Helper functions for method resolution
pub(super) fn is_special_method(method_name: &str) -> bool {
matches!(method_name, "birth" | "fini" | "toString")
}
/// Get default method IDs for special methods
pub(super) fn get_special_method_id(method_name: &str) -> Option<u32> {
match method_name {
"birth" => Some(1),
"toString" => Some(100),
"fini" => Some(999),
_ => None,
}
}

View File

@ -5,6 +5,7 @@ mod globals;
mod host_bridge;
mod instance_manager;
mod loader;
mod method_resolver;
mod types;
pub use globals::{get_global_loader_v2, init_global_loader_v2, shutdown_plugins_v2};