1 import Base 2 import ./execution_effects.bend as Effects 3 import ./world.bend as W 4 import ./execution_reservation_bundle.bend as Bundle 5 import ./execution_composer_layout.bend as Layout 6 import ./execution_relation_operation.bend as Operation 7 import ./execution_relation_store.bend as RelationStore 8 import ./execution_lifetime_scope_command.bend as ScopeCommands 9 import ./execution_lifetime_scope_store.bend as ScopeStore 10 import ./execution_lifetime_scope_resource_dispatch.bend as ScopeDispatch 11 import ./relation_types.bend as Relation 12 13 # Framework storage for one declared component family. Payloads remain affine 14 # until a whole-World barrier applies them or returns them in a refusal receipt. 15 type Entry<-S:Data,-P:Type,-D:Type> is Type: 16 Entry{handle:W.Handle,incoming:Maybe<&1,P>} 17 Despawn{handle:W.Handle} 18 Spawn{handle:W.Handle,bundle:D} 19 Relation{route:Layout.Route,operation:Operation.Operation} 20 LifetimeScope{route:Layout.Route,operation:ScopeCommands.Operation} 21 type ScopeDenial is Data: 22 MissingAuthority{} 23 ForeignNamespace{} 24 ScopeStoreRefused{} 25 type ScopeStatus is Data: 26 ScopeComplete{} 27 ScopePaused{} 28 ScopeDenied{reason:ScopeDenial} 29 type LinkedStatus is Data: 30 LinkedComplete{} 31 LinkedPaused{} 32 LinkedDenied{reason:ScopeDenial} 33 type Receipt<-S:Data,-P:Type,-D:Type> is Type: 34 Applied{handle:W.Handle,previous:Maybe<&1,P>} 35 Refused{handle:W.Handle,incoming:Maybe<&1,P>} 36 Despawned{handle:W.Handle,removed:D} 37 DespawnRefused{handle:W.Handle} 38 LinkedDespawn{handle:W.Handle,status:LinkedStatus,receipts:List>,frames:List<&2,Relation.CleanupFrame>} 39 Spawned{handle:W.Handle,previous:D} 40 SpawnRefused{handle:W.Handle,bundle:D,reason:Bundle.Refusal} 41 RelationPublished{route:Layout.Route,receipt:RelationStore.Receipt} 42 RelationUndeclared{route:Layout.Route,operation:Operation.Operation} 43 ScopePublished{route:Layout.Route,operation:ScopeCommands.Operation,status:ScopeStatus,receipts:List>,frames:List<&2,Relation.CleanupFrame>} 44 ScopeStoreApplied{route:Layout.Route,request:ScopeStore.Request,result:ScopeDispatch.Outcome} 45 type Buffer<-S:Data,-P:Type,-D:Type> is Type: 46 Buffer{pending:List>,receipts:List>} 47 # Bevy ad678262, schedule/executor/single_threaded.rs: run_without_applying_deferred 48 # keeps the system's authored buffer, including after a handled RunSystemError. 49 # Enqueue here only transfers ownership; no component storage is available. 50 def empty(~S:Data,~P:Type,~D:Type) -> Buffer:Buffer{[],[]} 51 def relation(~S:Data,~P:Type,~D:Type,buffer:Buffer,route:Layout.Route,operation:Operation.Operation) -> Buffer: 52 match buffer: 53 case Buffer{pending,receipts}:Buffer{Relation{route,operation} <> pending,receipts} 54 def lifetime_scope(~S:Data,~P:Type,~D:Type,buffer:Buffer,route:Layout.Route,operation:ScopeCommands.Operation) -> Buffer: 55 match buffer: 56 case Buffer{pending,receipts}:Buffer{LifetimeScope{route,operation} <> pending,receipts} 57 def insert(~S:Data,~P:Type,~D:Type,buffer:Buffer,handle:W.Handle,incoming:P) -> Buffer: 58 match buffer: 59 case Buffer{pending,receipts}:Buffer{Entry{handle,Some{incoming}} <> pending,receipts} 60 def remove(~S:Data,~P:Type,~D:Type,buffer:Buffer,handle:W.Handle) -> Buffer: 61 match buffer: 62 case Buffer{pending,receipts}:Buffer{Entry{handle,None{}} <> pending,receipts} 63 def despawn(~S:Data,~P:Type,~D:Type,buffer:Buffer,handle:W.Handle) -> Buffer: 64 match buffer: 65 case Buffer{pending,receipts}:Buffer{Despawn{handle} <> pending,receipts} 66 def pending(~S:Data,~P:Type,~D:Type,buffer:Buffer) -> Buffer & Bool: 67 match buffer: 68 case Buffer{[],receipts}:(Buffer{[],receipts},False{}) 69 case Buffer{head <> rest,receipts}:(Buffer{head <> rest,receipts},True{}) 70 def take_receipts(~S:Data,~P:Type,~D:Type,buffer:Buffer) -> Buffer & List>: 71 match buffer: 72 case Buffer{pending,receipts}:(Buffer{pending,[]},List.reverse(&1,Receipt,receipts)) 73 74 # Admission returns the actual bundle if canonical reservation was refused. 75 type SpawnAdmission<-S:Data,-D:Type> is Type: 76 SpawnQueued{handle:W.Handle} 77 SpawnRejected{bundle:D,error:W.WorldError} 78 def stage(~S:Data,~P:Type,~D:Type,buffer:Buffer,bundle:D,answer:W.Reservation) -> Buffer & SpawnAdmission: 79 match buffer answer: 80 case Buffer{pending,receipts} W.ReservationRejected{error}:(Buffer{pending,receipts},SpawnRejected{bundle,error}) 81 case Buffer{pending,receipts} W.Reserved{+handle}:(Buffer{Spawn{handle,bundle} <> pending,receipts},SpawnQueued{handle}) 82 def spawn(~S:Data,~P:Type,~D:Type,~Permit:Data,~F:Type -> Type,~effects:Effects.Effects,~token:Permit,~request:Permit -> F(W.Reservation),buffer:Buffer,bundle:D) -> F(Buffer & SpawnAdmission): 83 Effects.map(~F,W.Reservation,(Buffer & SpawnAdmission),effects,request(token),answer => stage(~S,~P,~D,buffer,bundle,answer))