refactor(kernel): Extract weak reference FFI to ffi/weak.rs module
**Problem**: - nyash_kernel/src/lib.rs is 1295 lines (monolithic FFI functions) - Hard to navigate and maintain - No clear module boundaries **Solution** (Task 2 - Medium Priority): - Create `ffi/` module structure - Extract weak reference functions to `ffi/weak.rs` (74 lines) - nyrt_weak_new - nyrt_weak_to_strong - nyrt_weak_drop - lib.rs: 1295 → 1221 lines (-74 lines, -5.7%) - Re-export via `pub use ffi::weak::*` for ABI compatibility **Architecture**: - Box-First: Clear module boundaries (SSOT for weak refs) - Reversible: Can merge back if needed (small, safe step) - ABI stable: #[no_mangle] exports preserved **Verification**: - ✅ Build successful (cargo build -p nyash_kernel) - ✅ phase285_p2_weak_upgrade_success_llvm: PASS - ✅ phase285_p2_weak_upgrade_fail_llvm: PASS - ✅ Quick profile: 154/154 PASS **Next steps** (deferred to future): - Extract string FFI (~11 functions) - Extract memory management FFI - Extract box operations FFI 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
4
crates/nyash_kernel/src/ffi/mod.rs
Normal file
4
crates/nyash_kernel/src/ffi/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
// FFI module coordinator
|
||||
// Phase 287 P4: Gradual modularization of nyash_kernel FFI functions
|
||||
|
||||
pub mod weak;
|
||||
79
crates/nyash_kernel/src/ffi/weak.rs
Normal file
79
crates/nyash_kernel/src/ffi/weak.rs
Normal file
@ -0,0 +1,79 @@
|
||||
// Weak reference FFI functions
|
||||
// Phase 287 P4: Extracted from lib.rs for modularization
|
||||
|
||||
/// nyrt_weak_new: Create weak reference from strong handle
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `strong_handle` - Strong Box handle (>0)
|
||||
///
|
||||
/// # Returns
|
||||
/// * Weak handle (bit 63 = 1) on success
|
||||
/// * 0 on failure (invalid handle)
|
||||
///
|
||||
/// # SSOT
|
||||
/// - docs/reference/language/lifecycle.md:179
|
||||
/// - docs/development/current/main/phases/phase-285/phase-285llvm-1-design.md
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_new(strong_handle: i64) -> i64 {
|
||||
use nyash_rust::runtime::host_handles as handles;
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
eprintln!("[nyrt_weak_new] called with handle: {}", strong_handle);
|
||||
|
||||
if strong_handle <= 0 {
|
||||
eprintln!("[nyrt_weak_new] invalid handle (<=0), returning 0");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get Arc from strong handle
|
||||
if let Some(arc) = handles::get(strong_handle as u64) {
|
||||
// Downgrade to Weak and allocate weak handle
|
||||
let weak = std::sync::Arc::downgrade(&arc);
|
||||
let weak_handle = weak_handles::to_handle_weak(weak);
|
||||
eprintln!("[nyrt_weak_new] success: strong {} → weak {}", strong_handle, weak_handle);
|
||||
return weak_handle;
|
||||
}
|
||||
|
||||
eprintln!("[nyrt_weak_new] handle {} not found in registry, returning 0", strong_handle);
|
||||
0 // Invalid handle
|
||||
}
|
||||
|
||||
/// nyrt_weak_to_strong: Upgrade weak reference to strong handle
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `weak_handle` - Weak handle (bit 63 = 1)
|
||||
///
|
||||
/// # Returns
|
||||
/// * Strong handle (>0) on success (target is alive)
|
||||
/// * 0 (Void/null) on failure (target is dead or invalid handle)
|
||||
///
|
||||
/// # SSOT
|
||||
/// - docs/reference/language/lifecycle.md:179
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_to_strong(weak_handle: i64) -> i64 {
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
eprintln!("[nyrt_weak_to_strong] called with weak_handle: {}", weak_handle);
|
||||
|
||||
// Upgrade weak handle to strong handle (0 on failure)
|
||||
let result = weak_handles::upgrade_weak_handle(weak_handle);
|
||||
|
||||
eprintln!("[nyrt_weak_to_strong] result: {} (0=null/failed, >0=success)", result);
|
||||
result
|
||||
}
|
||||
|
||||
/// nyrt_weak_drop: Release weak reference
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `weak_handle` - Weak handle (bit 63 = 1)
|
||||
///
|
||||
/// # Note
|
||||
/// Called when WeakRef goes out of scope (LLVM backend cleanup)
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_drop(weak_handle: i64) {
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
if weak_handle != 0 {
|
||||
weak_handles::drop_weak_handle(weak_handle);
|
||||
}
|
||||
}
|
||||
@ -3,8 +3,10 @@
|
||||
|
||||
mod encode;
|
||||
mod plugin;
|
||||
mod ffi;
|
||||
|
||||
pub use plugin::*;
|
||||
pub use ffi::weak::*;
|
||||
|
||||
// Phase 285LLVM-1.1: Global registry for user box field declarations
|
||||
use std::sync::RwLock;
|
||||
@ -1068,83 +1070,7 @@ pub extern "C" fn nyash_float_unbox_to_f64(float_handle: i64) -> f64 {
|
||||
}
|
||||
|
||||
// ---- Phase 285LLVM-1: WeakRef FFI functions ----
|
||||
|
||||
/// nyrt_weak_new: Create weak reference from strong handle
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `strong_handle` - Strong Box handle (>0)
|
||||
///
|
||||
/// # Returns
|
||||
/// * Weak handle (bit 63 = 1) on success
|
||||
/// * 0 on failure (invalid handle)
|
||||
///
|
||||
/// # SSOT
|
||||
/// - docs/reference/language/lifecycle.md:179
|
||||
/// - docs/development/current/main/phases/phase-285/phase-285llvm-1-design.md
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_new(strong_handle: i64) -> i64 {
|
||||
use nyash_rust::runtime::host_handles as handles;
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
eprintln!("[nyrt_weak_new] called with handle: {}", strong_handle);
|
||||
|
||||
if strong_handle <= 0 {
|
||||
eprintln!("[nyrt_weak_new] invalid handle (<=0), returning 0");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get Arc from strong handle
|
||||
if let Some(arc) = handles::get(strong_handle as u64) {
|
||||
// Downgrade to Weak and allocate weak handle
|
||||
let weak = std::sync::Arc::downgrade(&arc);
|
||||
let weak_handle = weak_handles::to_handle_weak(weak);
|
||||
eprintln!("[nyrt_weak_new] success: strong {} → weak {}", strong_handle, weak_handle);
|
||||
return weak_handle;
|
||||
}
|
||||
|
||||
eprintln!("[nyrt_weak_new] handle {} not found in registry, returning 0", strong_handle);
|
||||
0 // Invalid handle
|
||||
}
|
||||
|
||||
/// nyrt_weak_to_strong: Upgrade weak reference to strong handle
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `weak_handle` - Weak handle (bit 63 = 1)
|
||||
///
|
||||
/// # Returns
|
||||
/// * Strong handle (>0) on success (target is alive)
|
||||
/// * 0 (Void/null) on failure (target is dead or invalid handle)
|
||||
///
|
||||
/// # SSOT
|
||||
/// - docs/reference/language/lifecycle.md:179
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_to_strong(weak_handle: i64) -> i64 {
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
eprintln!("[nyrt_weak_to_strong] called with weak_handle: {}", weak_handle);
|
||||
|
||||
// Upgrade weak handle to strong handle (0 on failure)
|
||||
let result = weak_handles::upgrade_weak_handle(weak_handle);
|
||||
|
||||
eprintln!("[nyrt_weak_to_strong] result: {} (0=null/failed, >0=success)", result);
|
||||
result
|
||||
}
|
||||
|
||||
/// nyrt_weak_drop: Release weak reference
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `weak_handle` - Weak handle (bit 63 = 1)
|
||||
///
|
||||
/// # Note
|
||||
/// Called when WeakRef goes out of scope (LLVM backend cleanup)
|
||||
#[no_mangle]
|
||||
pub extern "C" fn nyrt_weak_drop(weak_handle: i64) {
|
||||
use nyash_rust::runtime::weak_handles;
|
||||
|
||||
if weak_handle != 0 {
|
||||
weak_handles::drop_weak_handle(weak_handle);
|
||||
}
|
||||
}
|
||||
// Phase 287 P4: Moved to ffi/weak.rs (nyrt_weak_new, nyrt_weak_to_strong, nyrt_weak_drop)
|
||||
|
||||
/// Register user-defined box declaration (LLVM harness support)
|
||||
/// Phase 285LLVM-1.1: Enable user box instantiation with fields in LLVM harness mode
|
||||
|
||||
Reference in New Issue
Block a user