refactor(naming): 箱理論 - Entry選択ロジック統一&NamingBox統合

Phase 25.4 メンテナンス: Task A延長線上の箱化リファクタリング

## 📦 箱化内容

### 1. Entry選択ロジック統一箱
- 新規作成: `src/runner/modes/common_util/entry_selection.rs`
- 3箇所の重複ロジックを `select_entry_function()` に集約
  - `selfhost/json.rs:48-59` (11行削減)
  - `pyvm.rs:32-43` (11行削減)
  - `pyvm.rs:102-113` (11行削減)
  - `llvm.rs:292` (直接アクセス→統一関数呼び出し)

### 2. NamingBox SSOT 統合
- arity-aware優先フォールバック実装
  1. `Main.main/0` (arity付き、NYASH_BUILD_STATIC_MAIN_ENTRY=1時)
  2. `Main.main` (legacy、arity無し、現行デフォルト)
  3. `main` (top-level、NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1時)
  4. `"Main.main"` (デフォルトフォールバック)

## 技術的成果
- **重複削減**: 33行の重複ロジック→1箇所に集約
- **NamingBox統一**: 全Entry選択が `src/mir/naming.rs` 経由
- **将来対応**: arity付き名前に自動対応(互換性維持)

## テスト結果
 cargo test mir_static_box_naming: 2 passed
 cargo test stage1_cli_entry_ssa_smoke: 2 passed

## 参考
- Phase 25.4-A完了commit: fa9cea51
- 箱理論原則: 重複ロジックを箱化→境界を明確化→一元管理

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-21 09:33:56 +09:00
parent fa9cea51b6
commit bceb20ed66
5 changed files with 50 additions and 40 deletions

View File

@ -0,0 +1,37 @@
/*!
* Entry function selection logic (Main.main → main fallback)
*
* NamingBox SSOT: Centralized entry point resolution with arity-aware fallback
*/
/// Select entry function for execution (Main.main → main fallback with env control)
///
/// Resolution order (NamingBox SSOT):
/// 1. Main.main/0 (arity-aware, future-proof for NYASH_BUILD_STATIC_MAIN_ENTRY=1)
/// 2. Main.main (legacy, arity-less, current default)
/// 3. main (top-level, only if NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1)
/// 4. "Main.main" (default fallback, caller handles missing function error)
pub fn select_entry_function(module: &crate::mir::MirModule) -> String {
use crate::config::env;
// NamingBox SSOT: Try arity-aware names first (future-proof)
// Currently "Main.main" without arity is used by default (NYASH_BUILD_STATIC_MAIN_ENTRY=0)
let main_with_arity = crate::mir::naming::encode_static_method("Main", "main", 0);
if module.functions.contains_key(&main_with_arity) {
return main_with_arity;
}
// Legacy: arity-less "Main.main" (current default)
if module.functions.contains_key("Main.main") {
return "Main.main".to_string();
}
// Fallback to top-level "main" if allowed
let allow_top = env::entry_allow_toplevel_main();
if allow_top && module.functions.contains_key("main") {
return "main".to_string();
}
// Default: "Main.main" (may not exist, but caller will handle error)
"Main.main".to_string()
}

View File

@ -6,6 +6,7 @@
pub mod core_bridge; pub mod core_bridge;
pub mod diag; pub mod diag;
pub mod entry_selection;
pub mod exec; pub mod exec;
pub mod hako; pub mod hako;
pub mod io; pub mod io;

View File

@ -29,18 +29,8 @@ pub fn run_pyvm_harness(module: &crate::mir::MirModule, tag: &str) -> Result<i32
tag, tag,
mir_json_path.display() mir_json_path.display()
); );
// Determine entry function (prefer Main.main; top-level main only if allowed) // NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let allow_top = crate::config::env::entry_allow_toplevel_main(); let entry = super::entry_selection::select_entry_function(&module);
let entry = if module.functions.contains_key("Main.main") {
"Main.main"
} else if allow_top && module.functions.contains_key("main") {
"main"
} else if module.functions.contains_key("main") {
eprintln!("[entry] Warning: using top-level 'main' without explicit allow; set NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1 to silence.");
"main"
} else {
"Main.main"
};
// Optional: MiniVM stdin loader — when enabled, read entire stdin and pass as argv[0] // Optional: MiniVM stdin loader — when enabled, read entire stdin and pass as argv[0]
let mut cmd = std::process::Command::new(py3); let mut cmd = std::process::Command::new(py3);
crate::runner::child_env::apply_core_wrapper_env(&mut cmd); crate::runner::child_env::apply_core_wrapper_env(&mut cmd);
@ -58,7 +48,7 @@ pub fn run_pyvm_harness(module: &crate::mir::MirModule, tag: &str) -> Result<i32
"--in", "--in",
&mir_json_path.display().to_string(), &mir_json_path.display().to_string(),
"--entry", "--entry",
entry, &entry,
"--args-env", "--args-env",
"NYASH_SCRIPT_ARGS_JSON", "NYASH_SCRIPT_ARGS_JSON",
]) ])
@ -99,18 +89,8 @@ pub fn run_pyvm_harness_lib(module: &nyash_rust::mir::MirModule, tag: &str) -> R
tag, tag,
mir_json_path.display() mir_json_path.display()
); );
// Determine entry function (prefer Main.main; top-level main only if allowed) // NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let allow_top = crate::config::env::entry_allow_toplevel_main(); let entry = super::entry_selection::select_entry_function(&module);
let entry = if module.functions.contains_key("Main.main") {
"Main.main"
} else if allow_top && module.functions.contains_key("main") {
"main"
} else if module.functions.contains_key("main") {
eprintln!("[entry] Warning: using top-level 'main' without explicit allow; set NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1 to silence.");
"main"
} else {
"Main.main"
};
let mut cmd = std::process::Command::new(py3); let mut cmd = std::process::Command::new(py3);
crate::runner::child_env::apply_core_wrapper_env(&mut cmd); crate::runner::child_env::apply_core_wrapper_env(&mut cmd);
if std::env::var("NYASH_MINIVM_READ_STDIN").ok().as_deref() == Some("1") { if std::env::var("NYASH_MINIVM_READ_STDIN").ok().as_deref() == Some("1") {
@ -126,7 +106,7 @@ pub fn run_pyvm_harness_lib(module: &nyash_rust::mir::MirModule, tag: &str) -> R
"--in", "--in",
&mir_json_path.display().to_string(), &mir_json_path.display().to_string(),
"--entry", "--entry",
entry, &entry,
"--args-env", "--args-env",
"NYASH_SCRIPT_ARGS_JSON", "NYASH_SCRIPT_ARGS_JSON",
]) ])

View File

@ -45,18 +45,8 @@ pub fn run_pyvm_module(module: &MirModule, label: &str) -> Option<i32> {
mir_json_path.display() mir_json_path.display()
); );
} }
// Select entry (prefer Main.main; top-level main only if allowed) // NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let allow_top = crate::config::env::entry_allow_toplevel_main(); let entry = super::super::entry_selection::select_entry_function(&module);
let entry = if module.functions.contains_key("Main.main") {
"Main.main"
} else if allow_top && module.functions.contains_key("main") {
"main"
} else if module.functions.contains_key("main") {
eprintln!("[entry] Warning: using top-level 'main' without explicit allow; set NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN=1 to silence.");
"main"
} else {
"Main.main"
};
let mut cmd = std::process::Command::new(py3); let mut cmd = std::process::Command::new(py3);
crate::runner::child_env::apply_core_wrapper_env(&mut cmd); crate::runner::child_env::apply_core_wrapper_env(&mut cmd);
let status = cmd let status = cmd
@ -65,7 +55,7 @@ pub fn run_pyvm_module(module: &MirModule, label: &str) -> Option<i32> {
"--in", "--in",
&mir_json_path.display().to_string(), &mir_json_path.display().to_string(),
"--entry", "--entry",
entry, &entry,
]) ])
.status() .status()
.map_err(|e| format!("spawn pyvm: {}", e)) .map_err(|e| format!("spawn pyvm: {}", e))

View File

@ -289,7 +289,9 @@ impl NyashRunner {
{ {
println!("🔧 Mock LLVM Backend Execution:"); println!("🔧 Mock LLVM Backend Execution:");
println!(" Build with --features llvm-inkwell-legacy for Rust/inkwell backend, or set NYASH_LLVM_OBJ_OUT and NYASH_LLVM_USE_HARNESS=1 for harness."); println!(" Build with --features llvm-inkwell-legacy for Rust/inkwell backend, or set NYASH_LLVM_OBJ_OUT and NYASH_LLVM_USE_HARNESS=1 for harness.");
if let Some(main_func) = module.functions.get("Main.main") { // NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let entry = crate::runner::modes::common_util::entry_selection::select_entry_function(&module);
if let Some(main_func) = module.functions.get(&entry) {
for (_bid, block) in &main_func.blocks { for (_bid, block) in &main_func.blocks {
for inst in &block.instructions { for inst in &block.instructions {
match inst { match inst {