feat(phase90): Ring0Context fs/time/thread migration complete
Phase 90 完了: IO/fs/time/thread 系の Ring0 移行 **Phase 90-A: fs 系移行(7箇所)** - FsApi trait 追加(6メソッド) - StdFs 実装(std::fs ベース) - IoError 拡張(4バリアント追加) - 移行: strip.rs(4), dispatch.rs(1), mod.rs(3) **Phase 90-B: io 系移行** - Phase 88 完了済み(スキップ) **Phase 90-C: time 系移行(3箇所)** - TimeApi に elapsed() 追加 - 移行: selfhost_exe.rs(1), io.rs(1), plugin_loader_unified.rs(1) **Phase 90-D: thread 系移行(2箇所)** - ThreadApi trait 追加(sleep メソッド) - StdThread 実装 - 移行: global_hooks.rs(1), plugin_loader_unified.rs(1) **Phase 90-E: 統合テスト** - ビルド成功(6 warnings, 0 errors) - テスト: 522/554 passed (94.2%) - 退行なし **実装成果**: - Ring0Context 拡張: fs, thread フィールド追加 - 総移行: 12箇所(fs: 7, time: 3, thread: 2) - 移行率: fs(2.9%), time(2.1%), thread(5.4%) **次のステップ**: Phase 91 (PluginHost/CoreServices skeleton) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -243,8 +243,13 @@ pub fn spawn_task_after(delay_ms: u64, name: &str, f: Box<dyn FnOnce() + Send +
|
||||
}
|
||||
}
|
||||
// Fallback: run inline after blocking sleep
|
||||
// Phase 90-D: thread 系移行
|
||||
let ring0 = crate::runtime::ring0::get_global_ring0();
|
||||
let ring0_clone = ring0.clone();
|
||||
std::thread::spawn(move || {
|
||||
std::thread::sleep(std::time::Duration::from_millis(delay_ms));
|
||||
ring0_clone
|
||||
.thread
|
||||
.sleep(std::time::Duration::from_millis(delay_ms));
|
||||
f();
|
||||
});
|
||||
false
|
||||
|
||||
@ -327,16 +327,21 @@ impl PluginHost {
|
||||
.downcast_ref::<crate::boxes::future::FutureBox>()
|
||||
{
|
||||
let max_ms: u64 = crate::config::env::await_max_ms();
|
||||
let start = std::time::Instant::now();
|
||||
// Phase 90-C/D: time/thread 系移行
|
||||
let ring0 = crate::runtime::ring0::get_global_ring0();
|
||||
let start = ring0
|
||||
.time
|
||||
.monotonic_now()
|
||||
.map_err(|_e| crate::bid::error::BidError::PluginError)?;
|
||||
let mut spins = 0usize;
|
||||
while !fut.ready() {
|
||||
crate::runtime::global_hooks::safepoint_and_poll();
|
||||
std::thread::yield_now();
|
||||
spins += 1;
|
||||
if spins % 1024 == 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
ring0.thread.sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
|
||||
if ring0.time.elapsed(start) >= std::time::Duration::from_millis(max_ms) {
|
||||
let err = crate::box_trait::StringBox::new("Timeout");
|
||||
return Ok(Some(Box::new(NyashResultBox::new_err(Box::new(err)))));
|
||||
}
|
||||
|
||||
@ -1,12 +1,38 @@
|
||||
//! Phase 88: Ring0 エラー型定義
|
||||
|
||||
/// IO 操作エラー
|
||||
/// IO 操作エラー (Phase 90-A: fs 系エラー追加)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IoError(pub String);
|
||||
pub enum IoError {
|
||||
/// ファイル読み込み失敗
|
||||
ReadFailed(String),
|
||||
/// ファイル書き込み失敗
|
||||
WriteFailed(String),
|
||||
/// メタデータ取得失敗
|
||||
MetadataFailed(String),
|
||||
/// 正規化失敗
|
||||
CanonicalizeFailed(String),
|
||||
/// stdin 読み込み失敗
|
||||
StdinReadFailed(String),
|
||||
/// stdout 書き込み失敗
|
||||
StdoutWriteFailed(String),
|
||||
/// stderr 書き込み失敗
|
||||
StderrWriteFailed(String),
|
||||
/// その他のエラー(Phase 88 互換用)
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for IoError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "IoError: {}", self.0)
|
||||
match self {
|
||||
IoError::ReadFailed(msg) => write!(f, "IoError (ReadFailed): {}", msg),
|
||||
IoError::WriteFailed(msg) => write!(f, "IoError (WriteFailed): {}", msg),
|
||||
IoError::MetadataFailed(msg) => write!(f, "IoError (MetadataFailed): {}", msg),
|
||||
IoError::CanonicalizeFailed(msg) => write!(f, "IoError (CanonicalizeFailed): {}", msg),
|
||||
IoError::StdinReadFailed(msg) => write!(f, "IoError (StdinReadFailed): {}", msg),
|
||||
IoError::StdoutWriteFailed(msg) => write!(f, "IoError (StdoutWriteFailed): {}", msg),
|
||||
IoError::StderrWriteFailed(msg) => write!(f, "IoError (StderrWriteFailed): {}", msg),
|
||||
IoError::Other(msg) => write!(f, "IoError: {}", msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -7,8 +7,10 @@ mod std_impls;
|
||||
mod traits;
|
||||
|
||||
pub use errors::{IoError, TimeError};
|
||||
pub use std_impls::{NoopMem, StdIo, StdLog, StdTime};
|
||||
pub use traits::{IoApi, LogApi, LogLevel, MemApi, MemStats, TimeApi};
|
||||
pub use std_impls::{NoopMem, StdFs, StdIo, StdLog, StdThread, StdTime};
|
||||
pub use traits::{
|
||||
FsApi, FsMetadata, IoApi, LogApi, LogLevel, MemApi, MemStats, ThreadApi, TimeApi,
|
||||
};
|
||||
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
@ -20,6 +22,8 @@ pub struct Ring0Context {
|
||||
pub io: Arc<dyn IoApi>,
|
||||
pub time: Arc<dyn TimeApi>,
|
||||
pub log: Arc<dyn LogApi>,
|
||||
pub fs: Arc<dyn FsApi>, // Phase 90-A
|
||||
pub thread: Arc<dyn ThreadApi>, // Phase 90-D
|
||||
}
|
||||
|
||||
impl Ring0Context {
|
||||
@ -29,8 +33,17 @@ impl Ring0Context {
|
||||
io: Arc<dyn IoApi>,
|
||||
time: Arc<dyn TimeApi>,
|
||||
log: Arc<dyn LogApi>,
|
||||
fs: Arc<dyn FsApi>,
|
||||
thread: Arc<dyn ThreadApi>,
|
||||
) -> Self {
|
||||
Self { mem, io, time, log }
|
||||
Self {
|
||||
mem,
|
||||
io,
|
||||
time,
|
||||
log,
|
||||
fs,
|
||||
thread,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,6 +54,8 @@ impl std::fmt::Debug for Ring0Context {
|
||||
.field("io", &"<dyn IoApi>")
|
||||
.field("time", &"<dyn TimeApi>")
|
||||
.field("log", &"<dyn LogApi>")
|
||||
.field("fs", &"<dyn FsApi>")
|
||||
.field("thread", &"<dyn ThreadApi>")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
@ -52,6 +67,8 @@ pub fn default_ring0() -> Ring0Context {
|
||||
io: Arc::new(StdIo),
|
||||
time: Arc::new(StdTime),
|
||||
log: Arc::new(StdLog),
|
||||
fs: Arc::new(StdFs),
|
||||
thread: Arc::new(StdThread),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
use super::errors::{IoError, TimeError};
|
||||
use super::traits::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// noop メモリ実装(Phase 88: 将来 hakmem に接続)
|
||||
@ -27,21 +28,21 @@ impl IoApi for StdIo {
|
||||
use std::io::Write;
|
||||
std::io::stdout()
|
||||
.write_all(data)
|
||||
.map_err(|e| IoError(format!("stdout write failed: {}", e)))
|
||||
.map_err(|e| IoError::StdoutWriteFailed(format!("{}", e)))
|
||||
}
|
||||
|
||||
fn stderr_write(&self, data: &[u8]) -> Result<(), IoError> {
|
||||
use std::io::Write;
|
||||
std::io::stderr()
|
||||
.write_all(data)
|
||||
.map_err(|e| IoError(format!("stderr write failed: {}", e)))
|
||||
.map_err(|e| IoError::StderrWriteFailed(format!("{}", e)))
|
||||
}
|
||||
|
||||
fn stdin_read(&self, buf: &mut [u8]) -> Result<usize, IoError> {
|
||||
use std::io::Read;
|
||||
std::io::stdin()
|
||||
.read(buf)
|
||||
.map_err(|e| IoError(format!("stdin read failed: {}", e)))
|
||||
.map_err(|e| IoError::StdinReadFailed(format!("{}", e)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,6 +57,10 @@ impl TimeApi for StdTime {
|
||||
fn monotonic_now(&self) -> Result<std::time::Instant, TimeError> {
|
||||
Ok(std::time::Instant::now())
|
||||
}
|
||||
|
||||
fn elapsed(&self, start: std::time::Instant) -> std::time::Duration {
|
||||
start.elapsed()
|
||||
}
|
||||
}
|
||||
|
||||
/// eprintln!/println! ベースのログ実装
|
||||
@ -99,3 +104,54 @@ impl LogApi for StdLog {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// std::fs ベースのファイルシステム実装 (Phase 90-A)
|
||||
pub struct StdFs;
|
||||
|
||||
impl FsApi for StdFs {
|
||||
fn read_to_string(&self, path: &Path) -> Result<String, IoError> {
|
||||
std::fs::read_to_string(path).map_err(|e| {
|
||||
IoError::ReadFailed(format!("read_to_string({}): {}", path.display(), e))
|
||||
})
|
||||
}
|
||||
|
||||
fn read(&self, path: &Path) -> Result<Vec<u8>, IoError> {
|
||||
std::fs::read(path)
|
||||
.map_err(|e| IoError::ReadFailed(format!("read({}): {}", path.display(), e)))
|
||||
}
|
||||
|
||||
fn write_all(&self, path: &Path, data: &[u8]) -> Result<(), IoError> {
|
||||
std::fs::write(path, data)
|
||||
.map_err(|e| IoError::WriteFailed(format!("write({}): {}", path.display(), e)))
|
||||
}
|
||||
|
||||
fn exists(&self, path: &Path) -> bool {
|
||||
path.exists()
|
||||
}
|
||||
|
||||
fn metadata(&self, path: &Path) -> Result<FsMetadata, IoError> {
|
||||
let meta = std::fs::metadata(path).map_err(|e| {
|
||||
IoError::MetadataFailed(format!("metadata({}): {}", path.display(), e))
|
||||
})?;
|
||||
Ok(FsMetadata {
|
||||
is_file: meta.is_file(),
|
||||
is_dir: meta.is_dir(),
|
||||
len: meta.len(),
|
||||
})
|
||||
}
|
||||
|
||||
fn canonicalize(&self, path: &Path) -> Result<PathBuf, IoError> {
|
||||
std::fs::canonicalize(path).map_err(|e| {
|
||||
IoError::CanonicalizeFailed(format!("canonicalize({}): {}", path.display(), e))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// std::thread ベースのスレッド実装 (Phase 90-D)
|
||||
pub struct StdThread;
|
||||
|
||||
impl ThreadApi for StdThread {
|
||||
fn sleep(&self, duration: std::time::Duration) {
|
||||
std::thread::sleep(duration);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
//! Box 名・Nyash 型を一切知らない。
|
||||
|
||||
use super::errors::{IoError, TimeError};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::SystemTime;
|
||||
|
||||
/// メモリ API(Phase 88: noop、将来 hakmem 接続)
|
||||
@ -45,6 +46,9 @@ pub trait TimeApi: Send + Sync {
|
||||
|
||||
/// モノトニック時刻取得(高精度タイマー用)
|
||||
fn monotonic_now(&self) -> Result<std::time::Instant, TimeError>;
|
||||
|
||||
/// 経過時間取得 (Phase 90-C)
|
||||
fn elapsed(&self, start: std::time::Instant) -> std::time::Duration;
|
||||
}
|
||||
|
||||
/// ログレベル
|
||||
@ -81,3 +85,39 @@ pub trait LogApi: Send + Sync {
|
||||
self.log(LogLevel::Error, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// ファイルシステムメタデータ
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FsMetadata {
|
||||
pub is_file: bool,
|
||||
pub is_dir: bool,
|
||||
pub len: u64,
|
||||
}
|
||||
|
||||
/// ファイルシステム API (Phase 90-A)
|
||||
pub trait FsApi: Send + Sync {
|
||||
/// ファイルを文字列として読み込む
|
||||
fn read_to_string(&self, path: &Path) -> Result<String, IoError>;
|
||||
|
||||
/// ファイルをバイト列として読み込む
|
||||
fn read(&self, path: &Path) -> Result<Vec<u8>, IoError>;
|
||||
|
||||
/// ファイルに書き込む
|
||||
fn write_all(&self, path: &Path, data: &[u8]) -> Result<(), IoError>;
|
||||
|
||||
/// パスが存在するか確認
|
||||
fn exists(&self, path: &Path) -> bool;
|
||||
|
||||
/// ファイルメタデータを取得
|
||||
fn metadata(&self, path: &Path) -> Result<FsMetadata, IoError>;
|
||||
|
||||
/// パスを正規化
|
||||
fn canonicalize(&self, path: &Path) -> Result<PathBuf, IoError>;
|
||||
}
|
||||
|
||||
/// スレッド API (Phase 90-D)
|
||||
pub trait ThreadApi: Send + Sync {
|
||||
/// 指定時間スリープ
|
||||
fn sleep(&self, duration: std::time::Duration);
|
||||
// spawn は Phase 91 以降で追加予定
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user