Phase 10.10: GC Switchable Runtime & Unified Debug System 実装完了
Phase 10.10の主要実装: - GcConfigBox: GC設定の実行時制御(counting/trace/barrier_strict) - DebugConfigBox: デバッグ設定の統一管理(JIT events/stats/dump/dot) - メソッドディスパッチ: system_methods.rsで両Boxのメソッド実装 - CountingGC動作確認: write_barriers正常カウント(VM実行時) 技術的詳細: - BoxCore/BoxBase統一アーキテクチャを活用 - setFlag/getFlag/apply/summaryメソッドで統一API提供 - 環境変数経由でVM/JITランタイムと連携 - GcConfigBox.apply()は次回実行から有効(ランタイム作成前に環境変数参照) テスト済み: - examples/gc_counting_demo.nyash: CountingGCの動作確認 - write_barriers=3でArray.push/set, Map.setを正しくカウント - NYASH_GC_TRACE=1でGC統計出力確認 Box-First哲学の体現: 設定も制御も観測もすべてBox! 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
99
src/boxes/debug_config_box.rs
Normal file
99
src/boxes/debug_config_box.rs
Normal file
@ -0,0 +1,99 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DebugConfigBox {
|
||||
pub base: BoxBase,
|
||||
// toggles/paths (staged until apply())
|
||||
pub jit_events: bool,
|
||||
pub jit_stats: bool,
|
||||
pub jit_stats_json: bool,
|
||||
pub jit_dump: bool,
|
||||
pub jit_dot_path: Option<String>,
|
||||
}
|
||||
|
||||
impl DebugConfigBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
jit_events: std::env::var("NYASH_JIT_EVENTS").ok().as_deref() == Some("1"),
|
||||
jit_stats: std::env::var("NYASH_JIT_STATS").ok().as_deref() == Some("1"),
|
||||
jit_stats_json: std::env::var("NYASH_JIT_STATS_JSON").ok().as_deref() == Some("1"),
|
||||
jit_dump: std::env::var("NYASH_JIT_DUMP").ok().as_deref() == Some("1"),
|
||||
jit_dot_path: std::env::var("NYASH_JIT_DOT").ok().filter(|s| !s.is_empty()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_flag(&mut self, name: &str, on: bool) -> Box<dyn NyashBox> {
|
||||
match name {
|
||||
"jit_events" => self.jit_events = on,
|
||||
"jit_stats" => self.jit_stats = on,
|
||||
"jit_stats_json" => self.jit_stats_json = on,
|
||||
"jit_dump" => self.jit_dump = on,
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name)))
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn set_path(&mut self, name: &str, value: &str) -> Box<dyn NyashBox> {
|
||||
match name {
|
||||
"jit_dot" | "jit_dot_path" => self.jit_dot_path = Some(value.to_string()),
|
||||
_ => return Box::new(StringBox::new(format!("Unknown path: {}", name)))
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn get_flag(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let v = match name {
|
||||
"jit_events" => self.jit_events,
|
||||
"jit_stats" => self.jit_stats,
|
||||
"jit_stats_json" => self.jit_stats_json,
|
||||
"jit_dump" => self.jit_dump,
|
||||
_ => false,
|
||||
};
|
||||
Box::new(BoolBox::new(v))
|
||||
}
|
||||
|
||||
pub fn get_path(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let v = match name {
|
||||
"jit_dot" | "jit_dot_path" => self.jit_dot_path.clone().unwrap_or_default(),
|
||||
_ => String::new(),
|
||||
};
|
||||
Box::new(StringBox::new(v))
|
||||
}
|
||||
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
let setb = |k: &str, v: bool| { if v { std::env::set_var(k, "1"); } else { std::env::remove_var(k); } };
|
||||
setb("NYASH_JIT_EVENTS", self.jit_events);
|
||||
setb("NYASH_JIT_STATS", self.jit_stats);
|
||||
setb("NYASH_JIT_STATS_JSON", self.jit_stats_json);
|
||||
setb("NYASH_JIT_DUMP", self.jit_dump);
|
||||
if let Some(p) = &self.jit_dot_path { std::env::set_var("NYASH_JIT_DOT", p); }
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn summary(&self) -> Box<dyn NyashBox> {
|
||||
let s = format!(
|
||||
"jit_events={} jit_stats={} jit_stats_json={} jit_dump={} jit_dot={}",
|
||||
self.jit_events, self.jit_stats, self.jit_stats_json, self.jit_dump,
|
||||
self.jit_dot_path.clone().unwrap_or_else(|| "<none>".to_string())
|
||||
);
|
||||
Box::new(StringBox::new(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for DebugConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "DebugConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
}
|
||||
|
||||
impl NyashBox for DebugConfigBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<DebugConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "DebugConfigBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(Self { base: self.base.clone(), ..self.clone() }) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new("DebugConfigBox".to_string()) }
|
||||
}
|
||||
62
src/boxes/gc_config_box.rs
Normal file
62
src/boxes/gc_config_box.rs
Normal file
@ -0,0 +1,62 @@
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox, VoidBox, BoxCore, BoxBase};
|
||||
use std::any::Any;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GcConfigBox { base: BoxBase, counting: bool, trace: bool, barrier_strict: bool }
|
||||
|
||||
impl GcConfigBox {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
base: BoxBase::new(),
|
||||
counting: std::env::var("NYASH_GC_COUNTING").ok().as_deref() == Some("1"),
|
||||
trace: std::env::var("NYASH_GC_TRACE").ok().as_deref() == Some("1"),
|
||||
barrier_strict: std::env::var("NYASH_GC_BARRIER_STRICT").ok().as_deref() == Some("1"),
|
||||
}
|
||||
}
|
||||
pub fn set_flag(&mut self, name: &str, on: bool) -> Box<dyn NyashBox> {
|
||||
match name {
|
||||
"counting" => self.counting = on,
|
||||
"trace" => self.trace = on,
|
||||
"barrier_strict" | "strict" => self.barrier_strict = on,
|
||||
_ => return Box::new(StringBox::new(format!("Unknown flag: {}", name)))
|
||||
}
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn get_flag(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let v = match name {
|
||||
"counting" => self.counting,
|
||||
"trace" => self.trace,
|
||||
"barrier_strict" | "strict" => self.barrier_strict,
|
||||
_ => false,
|
||||
};
|
||||
Box::new(BoolBox::new(v))
|
||||
}
|
||||
pub fn apply(&self) -> Box<dyn NyashBox> {
|
||||
let setb = |k: &str, v: bool| { if v { std::env::set_var(k, "1"); } else { std::env::remove_var(k); } };
|
||||
setb("NYASH_GC_COUNTING", self.counting);
|
||||
setb("NYASH_GC_TRACE", self.trace);
|
||||
setb("NYASH_GC_BARRIER_STRICT", self.barrier_strict);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
pub fn summary(&self) -> Box<dyn NyashBox> {
|
||||
let s = format!("counting={} trace={} barrier_strict={}", self.counting, self.trace, self.barrier_strict);
|
||||
Box::new(StringBox::new(s))
|
||||
}
|
||||
}
|
||||
|
||||
impl BoxCore for GcConfigBox {
|
||||
fn box_id(&self) -> u64 { self.base.id }
|
||||
fn parent_type_id(&self) -> Option<std::any::TypeId> { self.base.parent_type_id }
|
||||
fn fmt_box(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "GcConfigBox") }
|
||||
fn as_any(&self) -> &dyn Any { self }
|
||||
fn as_any_mut(&mut self) -> &mut dyn Any { self }
|
||||
}
|
||||
|
||||
impl NyashBox for GcConfigBox {
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox { BoolBox::new(other.as_any().is::<GcConfigBox>()) }
|
||||
fn type_name(&self) -> &'static str { "GcConfigBox" }
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> { Box::new(self.clone()) }
|
||||
fn share_box(&self) -> Box<dyn NyashBox> { self.clone_box() }
|
||||
fn to_string_box(&self) -> StringBox { StringBox::new(self.summary().to_string_box().value) }
|
||||
}
|
||||
|
||||
@ -48,5 +48,52 @@ impl JitPolicyBox {
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_whitelist(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let mut cur = crate::jit::policy::current();
|
||||
if !cur.hostcall_whitelist.iter().any(|s| s == name) {
|
||||
cur.hostcall_whitelist.push(name.to_string());
|
||||
}
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn clear_whitelist(&self) -> Box<dyn NyashBox> {
|
||||
let mut cur = crate::jit::policy::current();
|
||||
cur.hostcall_whitelist.clear();
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
|
||||
pub fn enable_preset(&self, name: &str) -> Box<dyn NyashBox> {
|
||||
let mut cur = crate::jit::policy::current();
|
||||
match name {
|
||||
// 最小: Array.push_h のみ許可(読み取り以外は変えない)
|
||||
"mutating_minimal" | "mutating_array_push" => {
|
||||
let id = crate::jit::r#extern::collections::SYM_ARRAY_PUSH_H;
|
||||
if !cur.hostcall_whitelist.iter().any(|s| s == id) {
|
||||
cur.hostcall_whitelist.push(id.to_string());
|
||||
}
|
||||
}
|
||||
// 例: Map.set_h も追加許可(必要に応じて拡張)
|
||||
"mutating_map_set" => {
|
||||
let id = crate::jit::r#extern::collections::SYM_MAP_SET_H;
|
||||
if !cur.hostcall_whitelist.iter().any(|s| s == id) {
|
||||
cur.hostcall_whitelist.push(id.to_string());
|
||||
}
|
||||
}
|
||||
// よく使う: Array.push_h + Array.set_h + Map.set_h を許可
|
||||
"mutating_common" => {
|
||||
let ids = [
|
||||
crate::jit::r#extern::collections::SYM_ARRAY_PUSH_H,
|
||||
crate::jit::r#extern::collections::SYM_ARRAY_SET_H,
|
||||
crate::jit::r#extern::collections::SYM_MAP_SET_H,
|
||||
];
|
||||
for id in ids { if !cur.hostcall_whitelist.iter().any(|s| s == id) { cur.hostcall_whitelist.push(id.to_string()); } }
|
||||
}
|
||||
_ => { return Box::new(StringBox::new(format!("Unknown preset: {}", name))); }
|
||||
}
|
||||
crate::jit::policy::set_current(cur);
|
||||
Box::new(VoidBox::new())
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,6 +79,8 @@ pub mod jit_stats_box;
|
||||
pub mod jit_policy_box;
|
||||
pub mod jit_events_box;
|
||||
pub mod jit_hostcall_registry_box;
|
||||
pub mod debug_config_box;
|
||||
pub mod gc_config_box;
|
||||
|
||||
// Web専用Box群(ブラウザ環境でのみ利用可能)
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
||||
Reference in New Issue
Block a user