Phase 11.8/12: MIR Core-13 roadmap, Nyash ABI design, async/await enhancements with TaskGroupBox foundation

Major additions:
- Phase 11.8 MIR cleanup specification (Core-15→14→13 roadmap)
- Nyash ABI unified design document (3×u64 structure)
- TaskGroupBox foundation with cancelAll/joinAll methods
- Enhanced async/await with checkpoint auto-insertion
- Structured concurrency preparation (parent-child task relationships)

Documentation:
- docs/development/roadmap/phases/phase-11.8_mir_cleanup/: Complete Core-13 path
- docs/development/roadmap/phases/phase-12/NYASH-ABI-DESIGN.md: Unified ABI spec
- Updated Phase 12 README with AOT/JIT explanation for script performance
- Added async_task_system/ design docs

Implementation progress:
- FutureBox spawn tracking with weak/strong reference management
- VM checkpoint integration before/after await
- LLVM backend async support preparation
- Verifier rules for await-checkpoint enforcement
- Result<T,E> normalization for timeout/cancellation

Technical insights:
- MIR as 'atomic instructions', Box as 'molecules' philosophy
- 'Everything is Box' enables full-stack with minimal instructions
- Unified BoxCall for array/plugin/async operations future consolidation

Next steps:
- Complete TaskGroupBox implementation
- Migrate from global to scoped task management
- Implement LIFO cleanup on scope exit
- Continue Core-13 instruction consolidation

🚀 'From 15 atoms to infinite programs: The Nyash Box Theory'
This commit is contained in:
Moe Charm
2025-09-02 03:41:51 +09:00
parent 11506cee3b
commit c9366d5c54
37 changed files with 2203 additions and 90 deletions

View File

@ -5,6 +5,7 @@ use crate::{backend::vm::VMValue, box_trait::{NyashBox, IntegerBox, BoolBox, Str
/// Symbol name for awaiting a FutureBox and returning a value/handle (i64)
pub const SYM_FUTURE_AWAIT_H: &str = "nyash.future.await_h";
pub const SYM_FUTURE_SPAWN_INSTANCE3_I64: &str = "nyash.future.spawn_instance3_i64";
#[cfg(feature = "cranelift-jit")]
pub extern "C" fn nyash_future_await_h(arg0: i64) -> i64 {
@ -34,12 +35,19 @@ pub extern "C" fn nyash_future_await_h(arg0: i64) -> i64 {
});
}
let Some(fut) = fut_opt else { return 0; };
// Block until completion, get NyashBox result
// Cooperative wait with scheduler polling and timeout
let max_ms: u64 = std::env::var("NYASH_AWAIT_MAX_MS").ok().and_then(|s| s.parse().ok()).unwrap_or(5000);
let start = std::time::Instant::now();
while !fut.ready() {
crate::runtime::global_hooks::safepoint_and_poll();
std::thread::yield_now();
if start.elapsed() >= std::time::Duration::from_millis(max_ms) {
// Timeout: return 0 (caller may handle as failure)
return 0;
}
}
// Get NyashBox result and always return a handle
let out_box: Box<dyn NyashBox> = fut.get();
// Fast-path: primitive returns
if let Some(ib) = out_box.as_any().downcast_ref::<IntegerBox>() { return ib.value; }
if let Some(bb) = out_box.as_any().downcast_ref::<BoolBox>() { return if bb.value { 1 } else { 0 }; }
// Otherwise, register handle and return id (works for String/Map/Array/Instance/etc.)
let arc: std::sync::Arc<dyn NyashBox> = std::sync::Arc::from(out_box);
let h = handles::to_handle(arc);
h as i64

View File

@ -9,3 +9,4 @@ pub mod handles;
pub mod birth;
pub mod runtime;
pub mod r#async;
pub mod result;

41
src/jit/extern/result.rs vendored Normal file
View File

@ -0,0 +1,41 @@
//! Result-related JIT extern symbols
use crate::box_trait::NyashBox;
/// Symbol name for wrapping a handle into Result.Ok(handle)
pub const SYM_RESULT_OK_H: &str = "nyash.result.ok_h";
/// Symbol name for wrapping a handle into Result.Err(handle)
pub const SYM_RESULT_ERR_H: &str = "nyash.result.err_h";
#[cfg(feature = "cranelift-jit")]
pub extern "C" fn nyash_result_ok_h(handle: i64) -> i64 {
use crate::jit::rt::handles;
use crate::boxes::result::NyashResultBox;
if handle <= 0 { return 0; }
if let Some(obj) = handles::get(handle as u64) {
let boxed = obj.clone_box();
let res = NyashResultBox::new_ok(boxed);
let arc: std::sync::Arc<dyn NyashBox> = std::sync::Arc::new(res);
let h = handles::to_handle(arc);
return h as i64;
}
0
}
#[cfg(feature = "cranelift-jit")]
pub extern "C" fn nyash_result_err_h(handle: i64) -> i64 {
use crate::jit::rt::handles;
use crate::boxes::result::NyashResultBox;
// If handle <= 0, synthesize a Timeout StringBox error for await paths.
let err_box: Box<dyn NyashBox> = if handle <= 0 {
Box::new(crate::box_trait::StringBox::new("Timeout".to_string()))
} else if let Some(obj) = handles::get(handle as u64) {
obj.clone_box()
} else {
Box::new(crate::box_trait::StringBox::new("UnknownError".to_string()))
};
let res = NyashResultBox::new_err(err_box);
let arc: std::sync::Arc<dyn NyashBox> = std::sync::Arc::new(res);
let h = handles::to_handle(arc);
h as i64
}