2025-08-19 16:43:13 +09:00
|
|
|
/*!
|
|
|
|
|
* Global Unified Box Registry
|
|
|
|
|
*
|
|
|
|
|
* Manages the global instance of UnifiedBoxRegistry
|
|
|
|
|
* Integrates all Box creation sources (builtin, user-defined, plugin)
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-20 05:57:18 +09:00
|
|
|
use crate::box_factory::{UnifiedBoxRegistry, builtin::BuiltinBoxFactory};
|
|
|
|
|
#[cfg(feature = "plugins")]
|
|
|
|
|
use crate::box_factory::plugin::PluginBoxFactory;
|
2025-08-19 16:43:13 +09:00
|
|
|
use std::sync::{Arc, Mutex, OnceLock};
|
|
|
|
|
|
|
|
|
|
/// Global registry instance
|
|
|
|
|
static GLOBAL_REGISTRY: OnceLock<Arc<Mutex<UnifiedBoxRegistry>>> = OnceLock::new();
|
|
|
|
|
|
|
|
|
|
/// Initialize the global unified registry
|
|
|
|
|
pub fn init_global_unified_registry() {
|
|
|
|
|
GLOBAL_REGISTRY.get_or_init(|| {
|
|
|
|
|
let mut registry = UnifiedBoxRegistry::new();
|
|
|
|
|
|
2025-08-30 01:33:52 +09:00
|
|
|
// Register built-in Box factory (highest priority) unless disabled
|
|
|
|
|
let disable_builtins = std::env::var("NYASH_DISABLE_BUILTINS").ok().as_deref() == Some("1");
|
|
|
|
|
if !disable_builtins {
|
|
|
|
|
registry.register(Arc::new(BuiltinBoxFactory::new()));
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("[UnifiedRegistry] Builtin boxes disabled via NYASH_DISABLE_BUILTINS=1");
|
|
|
|
|
}
|
2025-08-19 16:43:13 +09:00
|
|
|
|
|
|
|
|
// Register plugin Box factory (lowest priority)
|
2025-08-20 05:57:18 +09:00
|
|
|
#[cfg(feature = "plugins")]
|
|
|
|
|
{
|
|
|
|
|
registry.register(Arc::new(PluginBoxFactory::new()));
|
|
|
|
|
}
|
2025-08-19 16:43:13 +09:00
|
|
|
|
|
|
|
|
// TODO: User-defined Box factory will be registered by interpreter
|
|
|
|
|
|
|
|
|
|
Arc::new(Mutex::new(registry))
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the global unified registry
|
|
|
|
|
pub fn get_global_unified_registry() -> Arc<Mutex<UnifiedBoxRegistry>> {
|
|
|
|
|
init_global_unified_registry();
|
|
|
|
|
GLOBAL_REGISTRY.get().unwrap().clone()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Register a user-defined Box factory (called by interpreter)
|
2025-08-19 16:56:44 +09:00
|
|
|
pub fn register_user_defined_factory(factory: Arc<dyn crate::box_factory::BoxFactory>) {
|
2025-08-19 16:43:13 +09:00
|
|
|
let registry = get_global_unified_registry();
|
|
|
|
|
let mut registry_lock = registry.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
// Insert at position 1 (after builtin, before plugin)
|
|
|
|
|
// This maintains priority: builtin > user > plugin
|
|
|
|
|
if registry_lock.factories.len() >= 2 {
|
|
|
|
|
registry_lock.factories.insert(1, factory);
|
|
|
|
|
} else {
|
|
|
|
|
registry_lock.register(factory);
|
|
|
|
|
}
|
2025-08-20 05:57:18 +09:00
|
|
|
}
|