vile / scheme / evaluation_control.v
122 lines · 106 sloc · 4.09 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5// CancellationRequest aborts the current host-controlled evaluation without
6// running user Scheme winders. Managed file extents still release their resources.
7pub struct CancellationRequest {
8 Error
9pub:
10 reason IError = none
11}
12
13pub fn (request &CancellationRequest) msg() string {
14 if request.reason is none { return 'evaluation canceled' }
15 return 'evaluation canceled: ${request.reason.msg()}'
16}
17
18// EvaluationFn runs one or more operations in a temporary host context.
19pub type EvaluationFn = fn (mut VM) !Value
20
21type CancellationReason = fn (voidptr) IError
22
23// A channel field otherwise constructs an unused channel for arena nodes.
24// The private alias permits an explicit nil default, read only by its kind.
25type EvaluationSignal = chan int
26
27enum EvaluationControlKind {
28 cancellation
29 arena_usage
30}
31
32@[heap]
33struct EvaluationControl {
34 previous &EvaluationControl = unsafe { nil }
35 kind EvaluationControlKind
36 done chan int = EvaluationSignal(unsafe { nil })
37 reason CancellationReason = empty_cancellation_reason
38 context_data voidptr = unsafe { nil }
39 region &arena.Arena = unsafe { nil }
40 limit i64
41}
42
43struct EvaluationControlRef {
44 value &EvaluationControl
45}
46
47fn empty_cancellation_reason(_data voidptr) IError { return none }
48
49// Only with_context pairs this static generic thunk with a borrowed &T. The
50// private erased pointer is never retained after the synchronous scope, copied,
51// or interpreted with another T. No V closure/trampoline/global registry is used.
52fn cancellation_reason[T](data voidptr) IError {
53 mut ctx := borrowed_context[T](data)
54 return ctx.err()
55}
56
57// A typed return preserves an interface pointer; a direct &T(data) cast would
58// construct a new interface around voidptr in this compiler. C is a plain cast.
59@[inline]
60fn borrowed_context[T](data voidptr) &T {
61 return unsafe { data }
62}
63
64// with_context accepts the standard V context protocol (done() and err()). The
65// generic adapter adds no context-module dependency to ordinary VM users.
66// Contexts belong to the host and must outlive the callback and any goroutines
67// that use them. Native reentry inherits this scope; saved Scheme continuations
68// do not retain it after the callback returns.
69pub fn (mut vm VM) with_context[T](mut ctx T, work EvaluationFn) !Value {
70 vm.ready()!
71 mut state := vm.system_state()
72 previous := state.evaluation
73 // Borrow the live host object. Pair its pointer with this exact T's static
74 // thunk; allocating a capturing V closure here could put the closure runtime's
75 // global bookkeeping into a short-lived VM arena.
76 control := &EvaluationControl{
77 previous: previous
78 done: ctx.done()
79 context_data: voidptr(&ctx)
80 reason: cancellation_reason[T]
81 }
82 state.evaluation = control
83 defer { state.evaluation = previous }
84 control.check_control()!
85 result := work(mut vm)!
86 control.check_control()!
87 return result
88}
89
90// Execution entries check before parsing, reading, compiling, or applying a
91// native procedure. Host management and cleanup remain available after cancel.
92fn (vm &VM) check_entry_control() ! {
93 if !isnil(vm.system) && !isnil(vm.system.evaluation) {
94 vm.system.evaluation.check_control()!
95 }
96}
97
98fn (control &EvaluationControl) check_control() ! {
99 mut cursor := EvaluationControlRef{control}
100 for !isnil(cursor.value) {
101 current := cursor.value
102 if current.kind == .arena_usage {
103 used := i64(current.region.used())
104 if used > current.limit {
105 return ArenaUsageLimitRequest{ limit: current.limit, used: used }
106 }
107 } else {
108 mut done := current.done
109 if done.closed {
110 return CancellationRequest{ reason: current.reason(current.context_data) }
111 }
112 }
113 cursor = EvaluationControlRef{current.previous}
114 }
115}
116
117// Keep the ordinary bytecode loop free of per-instruction host-control checks.
118// Explicit contexts and arena usage limits select this loop. Cached channel
119// checks allocate nothing; active usage limits query standard arena statistics.
120fn (mut m Machine) run_controlled(mut vm VM, control &EvaluationControl) !Value {
121 return m.dispatch[ControlledMachineLoop](mut vm, Instruction{}, control)
122}
123