Phase 12: 統一TypeBox ABI実装開始 - ChatGPT5による極小コアABI基盤構築
- TypeBox ABI雛形: メソッドスロット管理システム追加 - Type Registry: Array/Map/StringBoxの基本メソッド定義 - Host API: C ABI逆呼び出しシステム実装 - Phase 12ドキュメント整理: 設計文書統合・アーカイブ化 - MIR Builder: クリーンアップと分離実装完了 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
43
src/runtime/host_handles.rs
Normal file
43
src/runtime/host_handles.rs
Normal file
@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* Host Handle Registry (global)
|
||||
*
|
||||
* 目的:
|
||||
* - C ABI(TLV)でユーザー/内蔵Boxを渡すためのホスト管理ハンドルを提供。
|
||||
* - u64ハンドルID → Arc<dyn NyashBox> をグローバルに保持し、VM/PluginHost/JITから参照可能にする。
|
||||
*/
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, RwLock, atomic::{AtomicU64, Ordering}};
|
||||
|
||||
use crate::box_trait::NyashBox;
|
||||
|
||||
struct Registry {
|
||||
next: AtomicU64,
|
||||
map: RwLock<HashMap<u64, Arc<dyn NyashBox>>>,
|
||||
}
|
||||
|
||||
impl Registry {
|
||||
fn new() -> Self { Self { next: AtomicU64::new(1), map: RwLock::new(HashMap::new()) } }
|
||||
fn alloc(&self, obj: Arc<dyn NyashBox>) -> u64 {
|
||||
let h = self.next.fetch_add(1, Ordering::Relaxed);
|
||||
if let Ok(mut m) = self.map.write() { m.insert(h, obj); }
|
||||
h
|
||||
}
|
||||
fn get(&self, h: u64) -> Option<Arc<dyn NyashBox>> {
|
||||
self.map.read().ok().and_then(|m| m.get(&h).cloned())
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
fn drop_handle(&self, h: u64) { if let Ok(mut m) = self.map.write() { m.remove(&h); } }
|
||||
}
|
||||
|
||||
static REG: OnceCell<Registry> = OnceCell::new();
|
||||
fn reg() -> &'static Registry { REG.get_or_init(Registry::new) }
|
||||
|
||||
/// Box<dyn NyashBox> → HostHandle (u64)
|
||||
pub fn to_handle_box(bx: Box<dyn NyashBox>) -> u64 { reg().alloc(Arc::from(bx)) }
|
||||
/// Arc<dyn NyashBox> → HostHandle (u64)
|
||||
pub fn to_handle_arc(arc: Arc<dyn NyashBox>) -> u64 { reg().alloc(arc) }
|
||||
/// HostHandle(u64) → Arc<dyn NyashBox>
|
||||
pub fn get(h: u64) -> Option<Arc<dyn NyashBox>> { reg().get(h) }
|
||||
|
||||
Reference in New Issue
Block a user