feat(phase95): CoreServices実用化 - Console/String Service実装完了

Phase 95完全達成 - Ring1-Core層の実際のService実装

### 実装成果
-  ConsoleService::println/print 実装
-  StringService::len 実装(UTF-8文字数対応)
-  global accessor 実装(get_core_plugin_host)
-  代表パス切り替え(selfhost.rs)
-  テスト13/13 PASS(100%)

### 変更ファイル
- src/runtime/core_services.rs: Service API定義、Adapter実装、テスト追加(+79行)
- src/runtime/mod.rs: global accessor実装(+18行)
- src/runtime/plugin_host.rs: Debug impl追加(+3行)
- src/runner/selfhost.rs: ConsoleService経由に切り替え(+5行)
- docs/development/current/main/core_boxes_design.md: Phase 95文書化(+118行)

### 技術的成果
- Ring0 → Ring1-Core → 実行パス の三層構造確立
- 型安全なService経由アクセス実現
- UTF-8完全対応(文字数カウント)
- global accessorパターン統一(Ring0と同じOnceLock)

### Bug修正
- PluginHost Debug impl追加
- PluginHost.optional型修正(Send + Sync追加)
- CoreServices Debug impl実装

### 次のステップ
Phase 95.5: Ring0統合とAdapter整理(#[allow(dead_code)]削除)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-12-03 09:57:38 +09:00
parent f4144a22a6
commit e74694d08b
6 changed files with 231 additions and 17 deletions

View File

@ -1,6 +1,6 @@
# Core Boxes 設計ドキュメントPhase 8794 完了版)
# Core Boxes 設計ドキュメントPhase 8795 完了版)
Phase 87 で実装された CoreBoxId/CoreMethodId と、Phase 9194 で統合された CoreServices/PluginHost/Adapter の仕様。
Phase 87 で実装された CoreBoxId/CoreMethodId と、Phase 9195 で統合された CoreServices/PluginHost/Adapter/Service API の仕様。
**目的**: Box名・メソッド名のハードコードを型安全な enum に箱化することで、以下を実現:
- ✅ コンパイル時検証(タイポ撲滅)
@ -12,6 +12,7 @@ Phase 87 で実装された CoreBoxId/CoreMethodId と、Phase 9194 で統合
- ✅ Phase 87: CoreBoxId/CoreMethodId 実装
- ✅ Phase 91: CoreServices/PluginHost skeleton
- ✅ Phase 94: Box → Service Adapter 実装と Dummy 削除
- ✅ Phase 95: CoreServices 実用化Console/String+ global accessor
---
@ -549,3 +550,121 @@ NYASH_USE_PLUGIN_HOST=1 ./target/release/nyash apps/tests/selfhost_min.hako
2. `src/runtime/core_services.rs`: CoreServices::dummy() ヘルパー
3. `src/runtime/mod.rs`: initialize_runtime() 実装(環境変数制御)
4. `src/runner/selfhost.rs`: PluginHost 初期化追加
---
## 11. Phase 95: CoreServices 実用化2025-12-03
### 11.1 実装成果
- ✅ **ConsoleService API**: `println(msg)`, `print(msg)` 実装
- ✅ **StringService API**: `len(s) -> i64` 実装
- ✅ **ConsoleBoxAdapter**: 実際に println! を呼び出す実装
- ✅ **StringBoxAdapter**: UTF-8 文字数カウント実装
- ✅ **global accessor**: `get_core_plugin_host()` 実装
- ✅ **代表パス切り替え**: `src/runner/selfhost.rs` で ConsoleService 使用
### 11.2 Service API 定義
```rust
// ConsoleService: 最優先で実装
pub trait ConsoleService: Send + Sync {
fn println(&self, msg: &str);
fn print(&self, msg: &str);
}
// StringService: 2番目に実装
pub trait StringService: Send + Sync {
fn len(&self, s: &str) -> i64; // UTF-8 文字数
}
// ArrayService, MapService: Phase 96 で実装予定
pub trait ArrayService: Send + Sync { }
pub trait MapService: Send + Sync { }
```
### 11.3 Adapter 実装
**ConsoleBoxAdapter**:
```rust
impl ConsoleService for ConsoleBoxAdapter {
fn println(&self, msg: &str) {
// ConsoleBox は直接 println! を呼ぶだけなので、ここでも同様に実装
println!("{}", msg);
}
fn print(&self, msg: &str) {
print!("{}", msg);
}
}
```
**StringBoxAdapter**:
```rust
impl StringService for StringBoxAdapter {
fn len(&self, s: &str) -> i64 {
// 文字列長を返すUTF-8 バイト数ではなく文字数)
s.chars().count() as i64
}
}
```
### 11.4 global accessor パターン
Ring0Context と同じ OnceLock パターンで実装:
```rust
use std::sync::OnceLock;
static GLOBAL_CORE_PLUGIN_HOST: OnceLock<Arc<PluginHost>> = OnceLock::new();
pub fn init_core_plugin_host(host: PluginHost) {
GLOBAL_CORE_PLUGIN_HOST.set(Arc::new(host))
.expect("[Phase 95] CorePluginHost already initialized");
}
pub fn get_core_plugin_host() -> Arc<PluginHost> {
GLOBAL_CORE_PLUGIN_HOST.get()
.expect("[Phase 95] CorePluginHost not initialized")
.clone()
}
```
### 11.5 代表パス切り替え例
**Before** (eprintln):
```rust
eprintln!("[selfhost] PluginHost initialized successfully");
```
**After** (ConsoleService):
```rust
let host = crate::runtime::get_core_plugin_host();
host.core.console.println("[selfhost] PluginHost initialized successfully");
```
### 11.6 テスト実装
- `test_console_service_println`: println 呼び出し確認
- `test_console_service_print`: print 呼び出し確認
- `test_string_service_len`: 文字列長UTF-8 対応)確認
### 11.7 実装ファイル
1. `src/runtime/core_services.rs`: ConsoleService/StringService API 定義 + Adapter 実装(合計 266行
2. `src/runtime/mod.rs`: global accessor 実装77行追加
3. `src/runtime/plugin_host.rs`: Debug impl 追加、`optional` 型修正
4. `src/runner/selfhost.rs`: ConsoleService 使用デモ1箇所
### 11.8 設計原則
- **CoreServices から Box の内部実装を隠蔽**: Service trait 経由で型安全アクセス
- **global accessor で簡単アクセス**: `get_core_plugin_host().core.console.println(...)`
- **Fail-Fast 原則維持**: エラー時は即座に失敗(フォールバック禁止)
- **段階実装**: Phase 95 では Console/String のみ、Phase 96 で Array/Map 追加予定
### 11.9 次のステップPhase 96
- ArrayService 実装push, get, set, size
- MapService 実装get, set, has, size
- 代表パス拡大selfhost 以外の箇所にも展開)
- StringService 拡張substring, concat, replace 等)