Files
hakorune/src/scope_tracker.rs
Moe Charm 41361a2f50 🚀 feat: Phase 9.78b Step 1&2完了 - ChatGPT5による実装
ChatGPT5が実装したPhase 9.78b Step 1&2の統合

## 🎯 実装内容
1. Phase 9.78b Step 1: BoxDeclarationをcore::modelへ移動
   - src/core/mod.rs, model.rs 新規作成
   - BoxDeclarationを純粋データモデルとして分離

2. Phase 9.78b Step 2: NyashRuntime骨組み作成
   - src/runtime/nyash_runtime.rs 追加
   - 統一Box管理の基盤

3. ビルドエラー修正
   - Arc重複インポート修正
   - as_str() → as_ref() 変更
   - parent_name.to_string() 型変換
   - インポートパス調整

## 📊 ビルド結果
-  フルビルド成功 (47.34秒)
-  ユニットテスト: 145/145成功
-  統合テスト: 16/16成功
-  WASMビルド成功 (1.9MB)
-  MIRテスト: 1失敗 (ref_new命令)

## 🚀 次のステップ
- Phase 9.78b Step 3: BoxFactory dyn化
- Codexの設計に基づく段階的実装継続
2025-08-20 18:57:10 +09:00

84 lines
2.5 KiB
Rust

/*!
* ScopeTracker - Track Box instances for proper lifecycle management
*
* Phase 9.78a: Unified Box lifecycle management for VM
*/
use std::sync::Arc;
use crate::box_trait::NyashBox;
use crate::instance_v2::InstanceBox;
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
use crate::runtime::plugin_loader_v2::PluginBoxV2;
/// Tracks Box instances created in different scopes for proper fini calls
pub struct ScopeTracker {
/// Stack of scopes, each containing Boxes created in that scope
scopes: Vec<Vec<Arc<dyn NyashBox>>>,
}
impl ScopeTracker {
/// Create a new scope tracker
pub fn new() -> Self {
Self {
scopes: vec![Vec::new()], // Start with one root scope
}
}
/// Enter a new scope
pub fn push_scope(&mut self) {
self.scopes.push(Vec::new());
}
/// Exit current scope and call fini on all Boxes created in it
pub fn pop_scope(&mut self) {
if let Some(scope) = self.scopes.pop() {
// Call fini in reverse order of creation
for arc_box in scope.into_iter().rev() {
// InstanceBox: call fini()
if let Some(instance) = arc_box.as_any().downcast_ref::<InstanceBox>() {
let _ = instance.fini();
continue;
}
// PluginBox: call plugin fini
#[cfg(all(feature = "plugins", not(target_arch = "wasm32")))]
if let Some(plugin) = arc_box.as_any().downcast_ref::<PluginBoxV2>() {
plugin.call_fini();
continue;
}
// Builtin and others: no-op for now
}
}
// Ensure we always have at least one scope
if self.scopes.is_empty() {
self.scopes.push(Vec::new());
}
}
/// Register a Box in the current scope
pub fn register_box(&mut self, nyash_box: Arc<dyn NyashBox>) {
if let Some(current_scope) = self.scopes.last_mut() {
current_scope.push(nyash_box);
}
}
/// Clear all scopes (used when resetting VM state)
pub fn clear(&mut self) {
// Pop all scopes and call fini
while self.scopes.len() > 1 {
self.pop_scope();
}
// Clear the root scope
if let Some(root_scope) = self.scopes.first_mut() {
root_scope.clear();
}
}
}
impl Default for ScopeTracker {
fn default() -> Self {
Self::new()
}
}