Files
hakorune/src/runtime/mod.rs

144 lines
5.7 KiB
Rust
Raw Normal View History

//! Nyashランタイムモジュール
//!
//! プラグインシステムとBox管理の中核
pub mod box_registry;
pub mod core_box_ids; // Phase 87: CoreBoxId/CoreMethodId 型安全enum
pub mod core_services; // Phase 91: CoreServices trait 定義
pub mod deprecations;
pub mod gc;
pub mod plugin_host; // Phase 91: PluginHost skeleton
pub mod ring0; // Phase 88: Ring0Context - OS API 抽象化レイヤー
pub mod runtime_profile; // Phase 109: RuntimeProfile enum (Default/NoFs)
pub mod gc_controller;
pub mod gc_mode;
pub mod gc_trace;
pub mod global_hooks;
pub mod leak_tracker;
pub mod nyash_runtime;
pub mod observe; // Lightweight observability flags (OOB etc.)
pub mod plugin_config;
pub mod plugin_ffi_common;
pub mod plugin_loader_unified;
pub mod plugin_loader_v2;
pub mod provider_lock;
pub mod provider_verify;
pub mod scheduler;
pub mod semantics;
pub mod unified_registry; // Deprecation warnings with warn-once guards
// pub mod plugin_box; // legacy - 古いPluginBox
// pub mod plugin_loader; // legacy - Host VTable使用
pub mod extern_registry; // ExternCall (env.*) 登録・診断用レジストリ
pub mod host_api; // C ABI: plugins -> host 逆呼び出しAPITLSでVMに橋渡し
pub mod host_handles; // C ABI(TLV) 向け HostHandle レジストリ(ユーザー/内蔵Box受け渡し
pub mod modules_registry;
pub mod type_box_abi; // Phase 12: Nyash ABI (vtable) 雛形
pub mod type_meta;
pub mod type_registry; // Phase 12: TypeId→TypeBox 解決(雛形) // env.modules minimal registry
#[cfg(test)]
mod tests;
pub use box_registry::{get_global_registry, BoxFactoryRegistry, BoxProvider};
pub use core_box_ids::{CoreBoxCategory, CoreBoxId, CoreMethodId}; // Phase 87: 型安全enum
pub use plugin_config::PluginConfig;
pub use ring0::{get_global_ring0, init_global_ring0, Ring0Context}; // Phase 88: Ring0 公開 API
pub use runtime_profile::RuntimeProfile; // Phase 109: RuntimeProfile enum
pub use plugin_host::CoreInitError; // Phase 92: CoreServices 初期化エラー
pub use plugin_loader_unified::{
get_global_plugin_host, init_global_plugin_host, MethodHandle, PluginBoxType, PluginHost,
PluginLibraryHandle,
};
pub use plugin_loader_v2::{get_global_loader_v2, init_global_loader_v2, PluginLoaderV2};
pub mod cache_versions;
pub use gc::{BarrierKind, GcHooks};
pub use nyash_runtime::{NyashRuntime, NyashRuntimeBuilder};
pub use scheduler::{Scheduler, SingleThreadScheduler};
pub use unified_registry::{
get_global_unified_registry, init_global_unified_registry, register_user_defined_factory,
};
// pub use plugin_box::PluginBox; // legacy
// Use unified plugin loader (formerly v2)
// pub use plugin_loader::{PluginLoaderV2 as PluginLoader, get_global_loader_v2 as get_global_loader}; // legacy
// Phase 95: CoreServices 用 global accessor
use std::sync::{Arc, OnceLock};
static GLOBAL_CORE_PLUGIN_HOST: OnceLock<Arc<plugin_host::PluginHost>> = OnceLock::new();
pub fn init_core_plugin_host(host: plugin_host::PluginHost) {
GLOBAL_CORE_PLUGIN_HOST
.set(Arc::new(host))
.expect("[Phase 95] CorePluginHost already initialized");
}
pub fn get_core_plugin_host() -> Arc<plugin_host::PluginHost> {
GLOBAL_CORE_PLUGIN_HOST
.get()
.expect("[Phase 95] CorePluginHost not initialized")
.clone()
}
feat(runtime): Phase 98 ConsoleService 代表パス拡張 - 7箇所置き換え完了 ## Phase 98 完了項目 - ✅ println!/eprintln! 7箇所 → ConsoleService 経由に移行 - ✅ console_println! マクロ追加(Graceful Degradation パターン) - ✅ try_get_core_plugin_host() 追加(安全なアクセサー) - ✅ 全テストPASS(core_services: 11, plugin_host: 7) ## 置き換え箇所(7箇所) **selfhost/child.rs** (3箇所): - spawn失敗エラー - タイムアウトメッセージ(stdout/stderr) **core_bridge.rs** (2箇所): - DUMP書き込みエラー - DUMP_MUT書き込みエラー **vm.rs** (1箇所): - RC(return code)出力 **selfhost/json.rs** (2箇所, オプション達成): - PyVM MIR JSON emit エラー - PyVM 使用ログ(verbose時) ## 技術的成果 **Graceful Degradation パターン確立**: - PluginHost 初期化前: eprintln! フォールバック - PluginHost 初期化後: ConsoleService 使用(Ring0直結) - Fail-Fast原則との整合性: 出力先選択のみ動的 **実装インフラ**: - src/runtime/mod.rs: console_println! マクロ & try_get_core_plugin_host() - 既存の get_core_plugin_host() は panic! 保持(Fail-Fast) ## 統計 - 置き換え完了: 7箇所(全体の約2%) - 残り候補: 約359箇所(Phase 99以降) - テスト: ビルド成功、全ユニットテストPASS ## ドキュメント - docs/development/current/main/core_boxes_design.md: Section 15 追加(128行) - 実装パターン、設計判断、テスト結果を完全記録 ## Phase 85-98 総括 - Phase 85-94: 構造設計 & 箱化モジュール化 - Phase 95.5: StringService/ConsoleService(Ring0直結型・純粋関数型) - Phase 96-96.5: ArrayService/MapService(downcast型)& コード整理 - Phase 97: IntegerService/BoolService(純粋関数型、#[allow(dead_code)] 根絶) - Phase 98: ConsoleService 実用拡大(7箇所)✅ 完了 次: Phase 99(CoreServices 完全統合、残り約359箇所の段階的移行) 🎊 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 11:04:58 +09:00
/// Phase 98: Safe accessor that returns None if not initialized
pub fn try_get_core_plugin_host() -> Option<Arc<plugin_host::PluginHost>> {
feat(runtime): Phase 98 ConsoleService 代表パス拡張 - 7箇所置き換え完了 ## Phase 98 完了項目 - ✅ println!/eprintln! 7箇所 → ConsoleService 経由に移行 - ✅ console_println! マクロ追加(Graceful Degradation パターン) - ✅ try_get_core_plugin_host() 追加(安全なアクセサー) - ✅ 全テストPASS(core_services: 11, plugin_host: 7) ## 置き換え箇所(7箇所) **selfhost/child.rs** (3箇所): - spawn失敗エラー - タイムアウトメッセージ(stdout/stderr) **core_bridge.rs** (2箇所): - DUMP書き込みエラー - DUMP_MUT書き込みエラー **vm.rs** (1箇所): - RC(return code)出力 **selfhost/json.rs** (2箇所, オプション達成): - PyVM MIR JSON emit エラー - PyVM 使用ログ(verbose時) ## 技術的成果 **Graceful Degradation パターン確立**: - PluginHost 初期化前: eprintln! フォールバック - PluginHost 初期化後: ConsoleService 使用(Ring0直結) - Fail-Fast原則との整合性: 出力先選択のみ動的 **実装インフラ**: - src/runtime/mod.rs: console_println! マクロ & try_get_core_plugin_host() - 既存の get_core_plugin_host() は panic! 保持(Fail-Fast) ## 統計 - 置き換え完了: 7箇所(全体の約2%) - 残り候補: 約359箇所(Phase 99以降) - テスト: ビルド成功、全ユニットテストPASS ## ドキュメント - docs/development/current/main/core_boxes_design.md: Section 15 追加(128行) - 実装パターン、設計判断、テスト結果を完全記録 ## Phase 85-98 総括 - Phase 85-94: 構造設計 & 箱化モジュール化 - Phase 95.5: StringService/ConsoleService(Ring0直結型・純粋関数型) - Phase 96-96.5: ArrayService/MapService(downcast型)& コード整理 - Phase 97: IntegerService/BoolService(純粋関数型、#[allow(dead_code)] 根絶) - Phase 98: ConsoleService 実用拡大(7箇所)✅ 完了 次: Phase 99(CoreServices 完全統合、残り約359箇所の段階的移行) 🎊 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 11:04:58 +09:00
GLOBAL_CORE_PLUGIN_HOST.get().cloned()
}
/// Phase 98: Helper macro to print using ConsoleService if available, otherwise eprintln
/// Phase 103: Updated to handle Option<Arc<dyn ConsoleService>>
feat(runtime): Phase 98 ConsoleService 代表パス拡張 - 7箇所置き換え完了 ## Phase 98 完了項目 - ✅ println!/eprintln! 7箇所 → ConsoleService 経由に移行 - ✅ console_println! マクロ追加(Graceful Degradation パターン) - ✅ try_get_core_plugin_host() 追加(安全なアクセサー) - ✅ 全テストPASS(core_services: 11, plugin_host: 7) ## 置き換え箇所(7箇所) **selfhost/child.rs** (3箇所): - spawn失敗エラー - タイムアウトメッセージ(stdout/stderr) **core_bridge.rs** (2箇所): - DUMP書き込みエラー - DUMP_MUT書き込みエラー **vm.rs** (1箇所): - RC(return code)出力 **selfhost/json.rs** (2箇所, オプション達成): - PyVM MIR JSON emit エラー - PyVM 使用ログ(verbose時) ## 技術的成果 **Graceful Degradation パターン確立**: - PluginHost 初期化前: eprintln! フォールバック - PluginHost 初期化後: ConsoleService 使用(Ring0直結) - Fail-Fast原則との整合性: 出力先選択のみ動的 **実装インフラ**: - src/runtime/mod.rs: console_println! マクロ & try_get_core_plugin_host() - 既存の get_core_plugin_host() は panic! 保持(Fail-Fast) ## 統計 - 置き換え完了: 7箇所(全体の約2%) - 残り候補: 約359箇所(Phase 99以降) - テスト: ビルド成功、全ユニットテストPASS ## ドキュメント - docs/development/current/main/core_boxes_design.md: Section 15 追加(128行) - 実装パターン、設計判断、テスト結果を完全記録 ## Phase 85-98 総括 - Phase 85-94: 構造設計 & 箱化モジュール化 - Phase 95.5: StringService/ConsoleService(Ring0直結型・純粋関数型) - Phase 96-96.5: ArrayService/MapService(downcast型)& コード整理 - Phase 97: IntegerService/BoolService(純粋関数型、#[allow(dead_code)] 根絶) - Phase 98: ConsoleService 実用拡大(7箇所)✅ 完了 次: Phase 99(CoreServices 完全統合、残り約359箇所の段階的移行) 🎊 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 11:04:58 +09:00
#[macro_export]
macro_rules! console_println {
($($arg:tt)*) => {
if let Some(host) = $crate::runtime::try_get_core_plugin_host() {
if let Some(ref console) = host.core.console {
console.println(&format!($($arg)*));
} else {
eprintln!($($arg)*);
}
feat(runtime): Phase 98 ConsoleService 代表パス拡張 - 7箇所置き換え完了 ## Phase 98 完了項目 - ✅ println!/eprintln! 7箇所 → ConsoleService 経由に移行 - ✅ console_println! マクロ追加(Graceful Degradation パターン) - ✅ try_get_core_plugin_host() 追加(安全なアクセサー) - ✅ 全テストPASS(core_services: 11, plugin_host: 7) ## 置き換え箇所(7箇所) **selfhost/child.rs** (3箇所): - spawn失敗エラー - タイムアウトメッセージ(stdout/stderr) **core_bridge.rs** (2箇所): - DUMP書き込みエラー - DUMP_MUT書き込みエラー **vm.rs** (1箇所): - RC(return code)出力 **selfhost/json.rs** (2箇所, オプション達成): - PyVM MIR JSON emit エラー - PyVM 使用ログ(verbose時) ## 技術的成果 **Graceful Degradation パターン確立**: - PluginHost 初期化前: eprintln! フォールバック - PluginHost 初期化後: ConsoleService 使用(Ring0直結) - Fail-Fast原則との整合性: 出力先選択のみ動的 **実装インフラ**: - src/runtime/mod.rs: console_println! マクロ & try_get_core_plugin_host() - 既存の get_core_plugin_host() は panic! 保持(Fail-Fast) ## 統計 - 置き換え完了: 7箇所(全体の約2%) - 残り候補: 約359箇所(Phase 99以降) - テスト: ビルド成功、全ユニットテストPASS ## ドキュメント - docs/development/current/main/core_boxes_design.md: Section 15 追加(128行) - 実装パターン、設計判断、テスト結果を完全記録 ## Phase 85-98 総括 - Phase 85-94: 構造設計 & 箱化モジュール化 - Phase 95.5: StringService/ConsoleService(Ring0直結型・純粋関数型) - Phase 96-96.5: ArrayService/MapService(downcast型)& コード整理 - Phase 97: IntegerService/BoolService(純粋関数型、#[allow(dead_code)] 根絶) - Phase 98: ConsoleService 実用拡大(7箇所)✅ 完了 次: Phase 99(CoreServices 完全統合、残り約359箇所の段階的移行) 🎊 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 11:04:58 +09:00
} else {
eprintln!($($arg)*);
}
};
}
/// Runtime 初期化Phase 95/109: profile-aware initialization
///
/// Phase 94: フォールバック削除 - 常に実際の Box を使用
/// Phase 95: global に登録して get_core_plugin_host() でアクセス可能に
/// Phase 109: RuntimeProfile に基づく条件付き初期化
///
refactor: Phase 109後のコード整理・改善(セットA/B/C完全実装) 全セット実装で総95行の純削減を達成(190削除, 95追加) ## Set A: Quick Wins (55行削減) - FileBox caps check を check_write_capability() ヘルパーに統一 - is_required_in() から冗長な local variable 削除 - 未使用の CoreServicesConfig::from_env() 削除 ## Set B: Architecture Refinement (65行削減) - provider_lock の責務を「登録」のみに限定(init_* メソッド削除) - PluginHost を initialization hub に統一 - profile-aware provider 初期化を一元化 - Default/NoFs の両 profile に対応 - FileBox::new() を Result-based に改善(Fail-Fast) - delete()/copy() デッドコード削除(実装なし) - PluginRegistry skeleton 削除(Phase 92 未実装プレースホルダ) ## Set C: Future-Proofing (+36行, 46追加/10削除) - RuntimeProfile ドキュメント大幅拡充 - 現在のプロファイル(Default, NoFs)の詳細説明 - 将来のプロファイル(TestMock, Sandbox, ReadOnly, Embedded)を明示 - PluginHost::new_skeleton() 削除 ## 設計改善 1. **責務分離の明確化**: - provider_lock: 登録のみ (set/get) - PluginHost: initialization hub (profile-aware setup) - initialize_runtime: env読み込みのみ 2. **Fail-Fast 原則の強化**: - FileBox provider missing → 即座にエラー(Default profile) - new() でパニック vs try_new() で Result 3. **将来への足がかり**: - Profile システムは拡張可能に設計 - TestMock/Sandbox/ReadOnly/Embedded への対応準備完了 テスト: - 既存テスト: 25/25 PASS ✅ - ビルド: SUCCESS ✅ ファイル変更: - src/boxes/file/mod.rs (-58) - src/runtime/core_box_ids.rs (-6) - src/runtime/mod.rs (-23) - src/runtime/plugin_host.rs (-90) - src/runtime/provider_lock.rs (-62) - src/runtime/runtime_profile.rs (+46) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 19:58:50 +09:00
/// # Responsibility Separation (Phase 109)
///
/// - **initialize_runtime**: 環境変数から profile を読む(唯一の env reader
refactor: Phase 109後のコード整理・改善(セットA/B/C完全実装) 全セット実装で総95行の純削減を達成(190削除, 95追加) ## Set A: Quick Wins (55行削減) - FileBox caps check を check_write_capability() ヘルパーに統一 - is_required_in() から冗長な local variable 削除 - 未使用の CoreServicesConfig::from_env() 削除 ## Set B: Architecture Refinement (65行削減) - provider_lock の責務を「登録」のみに限定(init_* メソッド削除) - PluginHost を initialization hub に統一 - profile-aware provider 初期化を一元化 - Default/NoFs の両 profile に対応 - FileBox::new() を Result-based に改善(Fail-Fast) - delete()/copy() デッドコード削除(実装なし) - PluginRegistry skeleton 削除(Phase 92 未実装プレースホルダ) ## Set C: Future-Proofing (+36行, 46追加/10削除) - RuntimeProfile ドキュメント大幅拡充 - 現在のプロファイル(Default, NoFs)の詳細説明 - 将来のプロファイル(TestMock, Sandbox, ReadOnly, Embedded)を明示 - PluginHost::new_skeleton() 削除 ## 設計改善 1. **責務分離の明確化**: - provider_lock: 登録のみ (set/get) - PluginHost: initialization hub (profile-aware setup) - initialize_runtime: env読み込みのみ 2. **Fail-Fast 原則の強化**: - FileBox provider missing → 即座にエラー(Default profile) - new() でパニック vs try_new() で Result 3. **将来への足がかり**: - Profile システムは拡張可能に設計 - TestMock/Sandbox/ReadOnly/Embedded への対応準備完了 テスト: - 既存テスト: 25/25 PASS ✅ - ビルド: SUCCESS ✅ ファイル変更: - src/boxes/file/mod.rs (-58) - src/runtime/core_box_ids.rs (-6) - src/runtime/mod.rs (-23) - src/runtime/plugin_host.rs (-90) - src/runtime/provider_lock.rs (-62) - src/runtime/runtime_profile.rs (+46) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 19:58:50 +09:00
/// - **PluginHost**: profile を引数として受け取り、provider 初期化を実行initialization hub
///
/// # Profile behavior
///
/// - **Default**: FileBox provider 必須Fail-Fast、全 core services 有効
refactor: Phase 109後のコード整理・改善(セットA/B/C完全実装) 全セット実装で総95行の純削減を達成(190削除, 95追加) ## Set A: Quick Wins (55行削減) - FileBox caps check を check_write_capability() ヘルパーに統一 - is_required_in() から冗長な local variable 削除 - 未使用の CoreServicesConfig::from_env() 削除 ## Set B: Architecture Refinement (65行削減) - provider_lock の責務を「登録」のみに限定(init_* メソッド削除) - PluginHost を initialization hub に統一 - profile-aware provider 初期化を一元化 - Default/NoFs の両 profile に対応 - FileBox::new() を Result-based に改善(Fail-Fast) - delete()/copy() デッドコード削除(実装なし) - PluginRegistry skeleton 削除(Phase 92 未実装プレースホルダ) ## Set C: Future-Proofing (+36行, 46追加/10削除) - RuntimeProfile ドキュメント大幅拡充 - 現在のプロファイル(Default, NoFs)の詳細説明 - 将来のプロファイル(TestMock, Sandbox, ReadOnly, Embedded)を明示 - PluginHost::new_skeleton() 削除 ## 設計改善 1. **責務分離の明確化**: - provider_lock: 登録のみ (set/get) - PluginHost: initialization hub (profile-aware setup) - initialize_runtime: env読み込みのみ 2. **Fail-Fast 原則の強化**: - FileBox provider missing → 即座にエラー(Default profile) - new() でパニック vs try_new() で Result 3. **将来への足がかり**: - Profile システムは拡張可能に設計 - TestMock/Sandbox/ReadOnly/Embedded への対応準備完了 テスト: - 既存テスト: 25/25 PASS ✅ - ビルド: SUCCESS ✅ ファイル変更: - src/boxes/file/mod.rs (-58) - src/runtime/core_box_ids.rs (-6) - src/runtime/mod.rs (-23) - src/runtime/plugin_host.rs (-90) - src/runtime/provider_lock.rs (-62) - src/runtime/runtime_profile.rs (+46) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 19:58:50 +09:00
/// - **NoFs**: FileBox provider optionalNoFsFileIo stub、core services のみ有効
pub fn initialize_runtime(ring0: std::sync::Arc<Ring0Context>) -> Result<(), CoreInitError> {
use crate::box_factory::UnifiedBoxRegistry;
use crate::box_factory::builtin::BuiltinBoxFactory;
// Phase 109: Read RuntimeProfile from environment (this layer only)
let profile = RuntimeProfile::from_env();
let mut registry = UnifiedBoxRegistry::with_env_policy();
// Phase 94: BuiltinBoxFactory を登録して core_required Boxes を提供
registry.register(std::sync::Arc::new(BuiltinBoxFactory::new()));
refactor: Phase 109後のコード整理・改善(セットA/B/C完全実装) 全セット実装で総95行の純削減を達成(190削除, 95追加) ## Set A: Quick Wins (55行削減) - FileBox caps check を check_write_capability() ヘルパーに統一 - is_required_in() から冗長な local variable 削除 - 未使用の CoreServicesConfig::from_env() 削除 ## Set B: Architecture Refinement (65行削減) - provider_lock の責務を「登録」のみに限定(init_* メソッド削除) - PluginHost を initialization hub に統一 - profile-aware provider 初期化を一元化 - Default/NoFs の両 profile に対応 - FileBox::new() を Result-based に改善(Fail-Fast) - delete()/copy() デッドコード削除(実装なし) - PluginRegistry skeleton 削除(Phase 92 未実装プレースホルダ) ## Set C: Future-Proofing (+36行, 46追加/10削除) - RuntimeProfile ドキュメント大幅拡充 - 現在のプロファイル(Default, NoFs)の詳細説明 - 将来のプロファイル(TestMock, Sandbox, ReadOnly, Embedded)を明示 - PluginHost::new_skeleton() 削除 ## 設計改善 1. **責務分離の明確化**: - provider_lock: 登録のみ (set/get) - PluginHost: initialization hub (profile-aware setup) - initialize_runtime: env読み込みのみ 2. **Fail-Fast 原則の強化**: - FileBox provider missing → 即座にエラー(Default profile) - new() でパニック vs try_new() で Result 3. **将来への足がかり**: - Profile システムは拡張可能に設計 - TestMock/Sandbox/ReadOnly/Embedded への対応準備完了 テスト: - 既存テスト: 25/25 PASS ✅ - ビルド: SUCCESS ✅ ファイル変更: - src/boxes/file/mod.rs (-58) - src/runtime/core_box_ids.rs (-6) - src/runtime/mod.rs (-23) - src/runtime/plugin_host.rs (-90) - src/runtime/provider_lock.rs (-62) - src/runtime/runtime_profile.rs (+46) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 19:58:50 +09:00
// Phase 109: PluginHost acts as initialization hub (handles FileBox provider)
let plugin_host = plugin_host::PluginHost::with_core_from_registry_optional(
ring0,
&registry,
plugin_host::CoreServicesConfig::all_enabled(),
&profile,
)?;
plugin_host.ensure_core_initialized();
// Phase 95: global に登録
init_core_plugin_host(plugin_host);
Ok(())
}