module scheme import arena // CancellationRequest aborts the current host-controlled evaluation without // running user Scheme winders. Managed file extents still release their resources. pub struct CancellationRequest { Error pub: reason IError = none } pub fn (request &CancellationRequest) msg() string { if request.reason is none { return 'evaluation canceled' } return 'evaluation canceled: ${request.reason.msg()}' } // EvaluationFn runs one or more operations in a temporary host context. pub type EvaluationFn = fn (mut VM) !Value type CancellationReason = fn (voidptr) IError // A channel field otherwise constructs an unused channel for arena nodes. // The private alias permits an explicit nil default, read only by its kind. type EvaluationSignal = chan int enum EvaluationControlKind { cancellation arena_usage } @[heap] struct EvaluationControl { previous &EvaluationControl = unsafe { nil } kind EvaluationControlKind done chan int = EvaluationSignal(unsafe { nil }) reason CancellationReason = empty_cancellation_reason context_data voidptr = unsafe { nil } region &arena.Arena = unsafe { nil } limit i64 } struct EvaluationControlRef { value &EvaluationControl } fn empty_cancellation_reason(_data voidptr) IError { return none } // Only with_context pairs this static generic thunk with a borrowed &T. The // private erased pointer is never retained after the synchronous scope, copied, // or interpreted with another T. No V closure/trampoline/global registry is used. fn cancellation_reason[T](data voidptr) IError { mut ctx := borrowed_context[T](data) return ctx.err() } // A typed return preserves an interface pointer; a direct &T(data) cast would // construct a new interface around voidptr in this compiler. C is a plain cast. @[inline] fn borrowed_context[T](data voidptr) &T { return unsafe { data } } // with_context accepts the standard V context protocol (done() and err()). The // generic adapter adds no context-module dependency to ordinary VM users. // Contexts belong to the host and must outlive the callback and any goroutines // that use them. Native reentry inherits this scope; saved Scheme continuations // do not retain it after the callback returns. pub fn (mut vm VM) with_context[T](mut ctx T, work EvaluationFn) !Value { vm.ready()! mut state := vm.system_state() previous := state.evaluation // Borrow the live host object. Pair its pointer with this exact T's static // thunk; allocating a capturing V closure here could put the closure runtime's // global bookkeeping into a short-lived VM arena. control := &EvaluationControl{ previous: previous done: ctx.done() context_data: voidptr(&ctx) reason: cancellation_reason[T] } state.evaluation = control defer { state.evaluation = previous } control.check_control()! result := work(mut vm)! control.check_control()! return result } // Execution entries check before parsing, reading, compiling, or applying a // native procedure. Host management and cleanup remain available after cancel. fn (vm &VM) check_entry_control() ! { if !isnil(vm.system) && !isnil(vm.system.evaluation) { vm.system.evaluation.check_control()! } } fn (control &EvaluationControl) check_control() ! { mut cursor := EvaluationControlRef{control} for !isnil(cursor.value) { current := cursor.value if current.kind == .arena_usage { used := i64(current.region.used()) if used > current.limit { return ArenaUsageLimitRequest{ limit: current.limit, used: used } } } else { mut done := current.done if done.closed { return CancellationRequest{ reason: current.reason(current.context_data) } } } cursor = EvaluationControlRef{current.previous} } } // Keep the ordinary bytecode loop free of per-instruction host-control checks. // Explicit contexts and arena usage limits select this loop. Cached channel // checks allocate nothing; active usage limits query standard arena statistics. fn (mut m Machine) run_controlled(mut vm VM, control &EvaluationControl) !Value { return m.dispatch[ControlledMachineLoop](mut vm, Instruction{}, control) }