2025-08-20 18:57:10 +09:00
|
|
|
//! Minimal NyashRuntime skeleton shared by interpreter and VM
|
|
|
|
|
//!
|
|
|
|
|
//! Focused on dependency inversion: core models + runtime services,
|
|
|
|
|
//! while execution strategies live in interpreter/VM layers.
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
|
|
|
|
|
2025-09-04 03:41:02 +09:00
|
|
|
use crate::box_factory::builtin::BuiltinBoxFactory;
|
2025-08-20 18:57:10 +09:00
|
|
|
#[cfg(feature = "plugins")]
|
|
|
|
|
use crate::box_factory::plugin::PluginBoxFactory;
|
2025-09-17 07:43:07 +09:00
|
|
|
use crate::box_factory::{BoxFactory, UnifiedBoxRegistry};
|
|
|
|
|
use crate::core::model::BoxDeclaration;
|
2025-08-20 18:57:10 +09:00
|
|
|
|
|
|
|
|
/// Core runtime container for executing Nyash programs
|
|
|
|
|
pub struct NyashRuntime {
|
|
|
|
|
/// Unified registry that can create any Box type
|
|
|
|
|
pub box_registry: Arc<Mutex<UnifiedBoxRegistry>>,
|
|
|
|
|
/// User-defined box declarations collected from source
|
|
|
|
|
pub box_declarations: Arc<RwLock<HashMap<String, BoxDeclaration>>>,
|
2025-08-27 17:06:46 +09:00
|
|
|
/// GC hooks (switchable runtime). Default is no-op.
|
|
|
|
|
pub gc: Arc<dyn crate::runtime::gc::GcHooks>,
|
|
|
|
|
/// Optional scheduler (single-thread by default is fine)
|
2025-09-17 07:43:07 +09:00
|
|
|
pub scheduler: Option<Arc<dyn crate::runtime::scheduler::Scheduler>>,
|
2025-08-20 18:57:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NyashRuntime {
|
|
|
|
|
/// Create a new runtime with defaults
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
box_registry: create_default_registry(),
|
|
|
|
|
box_declarations: Arc::new(RwLock::new(HashMap::new())),
|
2025-08-27 17:06:46 +09:00
|
|
|
gc: Arc::new(crate::runtime::gc::NullGc),
|
2025-09-17 07:43:07 +09:00
|
|
|
scheduler: Some(Arc::new(
|
|
|
|
|
crate::runtime::scheduler::SingleThreadScheduler::new(),
|
|
|
|
|
)),
|
2025-08-20 18:57:10 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builder for NyashRuntime allowing DI without globals (future-proof)
|
|
|
|
|
pub struct NyashRuntimeBuilder {
|
|
|
|
|
box_registry: Option<Arc<Mutex<UnifiedBoxRegistry>>>,
|
|
|
|
|
box_declarations: Option<Arc<RwLock<HashMap<String, BoxDeclaration>>>>,
|
2025-09-17 07:43:07 +09:00
|
|
|
gc: Option<Arc<dyn crate::runtime::gc::GcHooks>>,
|
2025-08-27 17:06:46 +09:00
|
|
|
scheduler: Option<Arc<dyn crate::runtime::scheduler::Scheduler>>,
|
2025-08-20 18:57:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NyashRuntimeBuilder {
|
|
|
|
|
pub fn new() -> Self {
|
2025-09-17 07:43:07 +09:00
|
|
|
Self {
|
|
|
|
|
box_registry: None,
|
|
|
|
|
box_declarations: None,
|
|
|
|
|
gc: None,
|
|
|
|
|
scheduler: None,
|
|
|
|
|
}
|
2025-08-20 18:57:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Inject a BoxFactory implementation directly into a private registry
|
|
|
|
|
pub fn with_factory(mut self, factory: Arc<dyn BoxFactory>) -> Self {
|
2025-09-17 07:43:07 +09:00
|
|
|
let registry = self
|
|
|
|
|
.box_registry
|
|
|
|
|
.take()
|
|
|
|
|
.unwrap_or_else(|| create_default_registry());
|
2025-08-20 18:57:10 +09:00
|
|
|
if let Ok(mut reg) = registry.lock() {
|
|
|
|
|
reg.register(factory);
|
|
|
|
|
}
|
|
|
|
|
self.box_registry = Some(registry);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn with_box_declarations(
|
|
|
|
|
mut self,
|
|
|
|
|
decls: Arc<RwLock<HashMap<String, BoxDeclaration>>>,
|
|
|
|
|
) -> Self {
|
|
|
|
|
self.box_declarations = Some(decls);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn build(self) -> NyashRuntime {
|
2025-09-17 07:43:07 +09:00
|
|
|
let registry = self
|
|
|
|
|
.box_registry
|
|
|
|
|
.unwrap_or_else(|| create_default_registry());
|
2025-08-21 14:28:24 +09:00
|
|
|
|
2025-08-20 18:57:10 +09:00
|
|
|
NyashRuntime {
|
2025-08-21 14:28:24 +09:00
|
|
|
box_registry: registry,
|
2025-09-17 07:43:07 +09:00
|
|
|
box_declarations: self
|
|
|
|
|
.box_declarations
|
|
|
|
|
.unwrap_or_else(|| Arc::new(RwLock::new(HashMap::new()))),
|
|
|
|
|
gc: self
|
|
|
|
|
.gc
|
|
|
|
|
.unwrap_or_else(|| Arc::new(crate::runtime::gc::NullGc)),
|
|
|
|
|
scheduler: Some(self.scheduler.unwrap_or_else(|| {
|
|
|
|
|
Arc::new(crate::runtime::scheduler::SingleThreadScheduler::new())
|
|
|
|
|
})),
|
2025-08-20 18:57:10 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_default_registry() -> Arc<Mutex<UnifiedBoxRegistry>> {
|
|
|
|
|
let mut registry = UnifiedBoxRegistry::new();
|
🎉 Phase 11.8/12.7: MIR Core-13 完全実装 + 糖衣構文ドキュメント更新
主要な変更:
- MIR Core-13命令セット確定(Load/Store削除の革命的設計)
- Const, BinOp, Compare(値・計算)
- Jump, Branch, Return, Phi(制御)
- Call, BoxCall, ExternCall(呼び出し)
- TypeOp, Safepoint, Barrier(メタ)
- Phase 12.7糖衣構文ドキュメント整理(超圧縮重視、可逆変換保証)
- MIRビルダーのモジュール分割完了
- vtableテストスイート拡充
- AI協調開発ツール追加(並列リファクタリング支援)
詳細:
- src/mir/instruction_introspection.rs: core13_instruction_names()追加
- MIRビルダー分割: decls.rs, exprs_*.rs, fields.rs
- plugin_loader_v2: errors.rs, host_bridge.rs分離
- 論文用データ: mir13-final.md作成
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-04 11:34:15 +09:00
|
|
|
// Default: enable builtins unless explicitly building with feature "plugins-only"
|
|
|
|
|
#[cfg(not(feature = "plugins-only"))]
|
2025-09-04 03:41:02 +09:00
|
|
|
{
|
|
|
|
|
registry.register(Arc::new(BuiltinBoxFactory::new()));
|
|
|
|
|
}
|
2025-08-20 18:57:10 +09:00
|
|
|
#[cfg(feature = "plugins")]
|
|
|
|
|
{
|
|
|
|
|
registry.register(Arc::new(PluginBoxFactory::new()));
|
|
|
|
|
}
|
|
|
|
|
Arc::new(Mutex::new(registry))
|
|
|
|
|
}
|
2025-08-21 14:28:24 +09:00
|
|
|
|
|
|
|
|
impl NyashRuntimeBuilder {
|
2025-08-27 17:06:46 +09:00
|
|
|
/// Inject custom GC hooks (switchable runtime). Default is no-op.
|
|
|
|
|
pub fn with_gc_hooks(mut self, gc: Arc<dyn crate::runtime::gc::GcHooks>) -> Self {
|
|
|
|
|
self.gc = Some(gc);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience: use CountingGc for development metrics
|
|
|
|
|
pub fn with_counting_gc(mut self) -> Self {
|
|
|
|
|
let gc = Arc::new(crate::runtime::gc::CountingGc::new());
|
|
|
|
|
self.gc = Some(gc);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Inject a custom scheduler implementation
|
|
|
|
|
pub fn with_scheduler(mut self, sched: Arc<dyn crate::runtime::scheduler::Scheduler>) -> Self {
|
|
|
|
|
self.scheduler = Some(sched);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convenience: use SingleThreadScheduler
|
|
|
|
|
pub fn with_single_thread_scheduler(mut self) -> Self {
|
2025-09-17 07:43:07 +09:00
|
|
|
self.scheduler = Some(Arc::new(
|
|
|
|
|
crate::runtime::scheduler::SingleThreadScheduler::new(),
|
|
|
|
|
));
|
2025-08-27 17:06:46 +09:00
|
|
|
self
|
|
|
|
|
}
|
2025-08-21 14:28:24 +09:00
|
|
|
}
|