1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use alloc::vec::Vec;
use alloc::boxed::Box;
use alloc::collections::{VecDeque, BTreeMap};
use alloc::rc::Rc;

use crate::*;
use crate::gc::*;
use crate::slotmap::*;
use crate::runtime::*;
use crate::bytecode::*;
use crate::process::*;
use crate::compact_str::*;
use crate::vecmap::*;

new_key! {
    struct ProcessKey;
}

/// A state machine that performs an action during idle periods.
/// 
/// This could be used to invoke [`std::thread::sleep`] during idle periods to save CPU time.
pub struct IdleAction {
    count: usize,
    thresh: usize,
    action: Box<dyn FnMut()>,
}
impl IdleAction {
    /// Creates a new [`IdleAction`] that triggers automatically after `max_yields` idle steps.
    pub fn new(thresh: usize, action: Box<dyn FnMut()>) -> Self {
        Self { count: 0, thresh, action }
    }
    /// Consumes a step result and advances the state machine.
    /// If the step resulting in an idle action, this may trigger the idle action to fire and reset the state machine.
    pub fn consume<C: CustomTypes<S>, S: System<C>>(&mut self, res: &ProjectStep<'_, C, S>) {
        match res {
            ProjectStep::Idle | ProjectStep::Yield | ProjectStep::Pause => {
                self.count += 1;
                if self.count >= self.thresh {
                    self.trigger();
                }
            }
            ProjectStep::Normal | ProjectStep::ProcessTerminated { .. } | ProjectStep::Error { .. } | ProjectStep::Watcher { .. } => self.count = 0,
        }
    }
    /// Explicitly triggers the idle action and reset the state machine.
    pub fn trigger(&mut self) {
        self.count = 0;
        self.action.as_mut()();
    }
}

/// Simulates input from the user.
#[derive(Debug)]
pub enum Input {
    /// Simulate pressing the start (green flag) button.
    /// This has the effect of interrupting any running "on start" scripts and restarting them (with an empty context).
    /// Any other running processes are not affected.
    Start,
    /// Simulate pressing the stop button.
    /// This has the effect of stopping all currently-running processes.
    /// Note that some hat blocks could cause new processes to spin up after this operation.
    Stop,
    /// Simulates a key down hat from the keyboard.
    /// This should be repeated if the button is held down.
    KeyDown { key: KeyCode },
    /// Simulates a key up hat from the keyboard.
    /// Due to the nature of the TTY interface, key up events are not always available, so this hat does not need to be sent.
    /// If not sent, a timeout is used to determine when a key is released (sending this hat can short-circuit the timeout).
    KeyUp { key: KeyCode },
    /// Trigger the execution of a custom event (hat) block script with the given set of message-style input variables.
    /// The `interrupt` flag can be set to cause any running scripts to stop and wipe their current queues, placing this new execution front and center.
    /// The `max_queue` field controls the maximum size of the context/schedule execution queue; beyond this size, this (and only this) execution will be dropped.
    CustomEvent { name: CompactString, args: VecMap<CompactString, SimpleValue, false>, interrupt: bool, max_queue: usize },
}

/// Result of stepping through the execution of a [`Project`].
pub enum ProjectStep<'gc, C: CustomTypes<S>, S: System<C>> {
    /// There were no running processes to execute.
    Idle,
    /// The project had a running process, which yielded.
    Yield,
    /// The project had a running process, which did any non-yielding operation.
    Normal,
    /// The project had a running process which terminated successfully.
    /// This can be though of as a special case of [`ProjectStep::Normal`],
    /// but also returns the result and process so it can be queried for state information if needed.
    ProcessTerminated { result: Value<'gc, C, S>, proc: Process<'gc, C, S> },
    /// The project had a running process, which encountered a runtime error.
    /// The dead process is returned, which can be queried for diagnostic information.
    Error { error: ExecError<C, S>, proc: Process<'gc, C, S> },
    /// The project had a running process that requested to create or destroy a watcher.
    /// See [`ProcessStep::Watcher`] for more details.
    Watcher { create: bool, watcher: Watcher<'gc, C, S> },
    /// The project had a running process that requested to pause execution of the (entire) project.
    /// See [`ProcessStep::Pause`] for more details.
    Pause,
}

#[derive(Collect)]
#[collect(no_drop, bound = "")]
pub struct PartialProcContext<'gc, C: CustomTypes<S>, S: System<C>> {
                               pub locals: SymbolTable<'gc, C, S>,
    #[collect(require_static)] pub state: C::ProcessState,
    #[collect(require_static)] pub barrier: Option<Barrier>,
    #[collect(require_static)] pub reply_key: Option<InternReplyKey>,
    #[collect(require_static)] pub local_message: Option<CompactString>,
}

#[derive(Collect)]
#[collect(no_drop, bound = "")]
struct Script<'gc, C: CustomTypes<S>, S: System<C>> {
    #[collect(require_static)] event: Rc<(Event, usize)>, // event and bytecode start pos
                               entity: Gc<'gc, RefLock<Entity<'gc, C, S>>>,
    #[collect(require_static)] process: Option<ProcessKey>,
                               context_queue: VecDeque<PartialProcContext<'gc, C, S>>,
}
impl<'gc, C: CustomTypes<S>, S: System<C>> Script<'gc, C, S> {
    fn consume_context(&mut self, state: &mut State<'gc, C, S>) {
        let process = self.process.and_then(|key| Some((key, state.processes.get_mut(key)?)));
        match process {
            Some(proc) => match proc.1.is_running() {
                true => return,
                false => unreachable!(), // should have already been cleaned up by the scheduler
            }
            None => match self.context_queue.pop_front() {
                None => return,
                Some(context) => {
                    let proc = Process::new(ProcContext { global_context: state.global_context, entity: self.entity, state: context.state, start_pos: self.event.1, locals: context.locals, barrier: context.barrier, reply_key: context.reply_key, local_message: context.local_message });
                    let key = state.processes.insert(proc);
                    state.process_queue.push_back(key);
                    self.process = Some(key);
                }
            },
        }
    }
    fn stop_all(&mut self, state: &mut State<'gc, C, S>) {
        if let Some(process) = self.process.take() {
            state.processes.remove(process);
        }
        self.context_queue.clear();
    }
    fn schedule(&mut self, state: &mut State<'gc, C, S>, context: PartialProcContext<'gc, C, S>, max_queue: usize) {
        self.context_queue.push_back(context);
        self.consume_context(state);
        if self.context_queue.len() > max_queue {
            self.context_queue.pop_back();
        }
    }
}

struct AllContextsConsumer {
    did_it: bool,
}
impl AllContextsConsumer {
    fn new() -> Self {
        Self { did_it: false }
    }
    fn do_once<C: CustomTypes<S>, S: System<C>>(&mut self, proj: &mut Project<C, S>) {
        if !core::mem::replace(&mut self.did_it, true) {
            for script in proj.scripts.iter_mut() {
                script.consume_context(&mut proj.state);
            }
        }
    }
}

#[derive(Collect)]
#[collect(no_drop, bound = "")]
struct State<'gc, C: CustomTypes<S>, S: System<C>> {
                               global_context: Gc<'gc, RefLock<GlobalContext<'gc, C, S>>>,
                               processes: SlotMap<ProcessKey, Process<'gc, C, S>>,
    #[collect(require_static)] process_queue: VecDeque<ProcessKey>,
}
#[derive(Collect)]
#[collect(no_drop, bound = "")]
pub struct Project<'gc, C: CustomTypes<S>, S: System<C>> {
    state: State<'gc, C, S>,
    scripts: Vec<Script<'gc, C, S>>,
}
impl<'gc, C: CustomTypes<S>, S: System<C>> Project<'gc, C, S> {
    pub fn from_init(mc: &Mutation<'gc>, init_info: &InitInfo, bytecode: Rc<ByteCode>, settings: Settings, system: Rc<S>) -> Self {
        let global_context = GlobalContext::from_init(mc, init_info, bytecode, settings, system);
        let mut project = Self::new(Gc::new(mc, RefLock::new(global_context)));

        for entity_info in init_info.entities.iter() {
            let entity = *project.state.global_context.borrow().entities.get(&entity_info.name).unwrap();
            for (event, pos) in entity_info.scripts.iter() {
                project.add_script(*pos, entity, event.clone());
            }
        }

        project
    }
    pub fn new(global_context: Gc<'gc, RefLock<GlobalContext<'gc, C, S>>>) -> Self {
        Self {
            state: State {
                global_context,
                processes: Default::default(),
                process_queue: Default::default(),
            },
            scripts: Default::default(),
        }
    }
    pub fn add_script(&mut self, start_pos: usize, entity: Gc<'gc, RefLock<Entity<'gc, C, S>>>, event: Event) {
        self.scripts.push(Script {
            event: Rc::new((event, start_pos)),
            entity,
            process: None,
            context_queue: Default::default(),
        });
    }
    pub fn input(&mut self, mc: &Mutation<'gc>, input: Input) {
        let mut all_contexts_consumer = AllContextsConsumer::new();
        match input {
            Input::Start => {
                for i in 0..self.scripts.len() {
                    if let Event::OnFlag = &self.scripts[i].event.0 {
                        let state = C::ProcessState::from(ProcessKind { entity: self.scripts[i].entity, dispatcher: None });

                        all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                        self.scripts[i].stop_all(&mut self.state);
                        self.scripts[i].schedule(&mut self.state, PartialProcContext { state, locals: Default::default(), barrier: None, reply_key: None, local_message: None }, 0);
                    }
                }
            }
            Input::CustomEvent { name, args, interrupt, max_queue } => {
                for i in 0..self.scripts.len() {
                    if let Event::Custom { name: script_event_name, fields } = &self.scripts[i].event.0 {
                        if name != *script_event_name { continue }

                        let mut locals = SymbolTable::default();
                        for field in fields.iter() {
                            let value = args.get(field).map(|x| Value::from_simple(mc, x.clone())).unwrap_or_else(|| Number::new(0.0).unwrap().into());
                            locals.define_or_redefine(field, value.into());
                        }

                        let state = C::ProcessState::from(ProcessKind { entity: self.scripts[i].entity, dispatcher: None });

                        all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                        if interrupt { self.scripts[i].stop_all(&mut self.state); }
                        self.scripts[i].schedule(&mut self.state, PartialProcContext { locals, state, barrier: None, reply_key: None, local_message: None }, max_queue);
                    }
                }
            }
            Input::Stop => {
                for script in self.scripts.iter_mut() {
                    script.stop_all(&mut self.state);
                }
                self.state.processes.clear();
                self.state.process_queue.clear();
            }
            Input::KeyDown { key: input_key } => {
                for i in 0..self.scripts.len() {
                    if let Event::OnKey { key_filter } = &self.scripts[i].event.0 {
                        if key_filter.map(|x| x == input_key).unwrap_or(true) {
                            let state = C::ProcessState::from(ProcessKind { entity: self.scripts[i].entity, dispatcher: None });

                            all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                            self.scripts[i].schedule(&mut self.state, PartialProcContext { state, locals: Default::default(), barrier:None, reply_key: None, local_message: None }, 0);
                        }
                    }
                }
            }
            Input::KeyUp { .. } => unimplemented!(),
        }
    }
    pub fn step(&mut self, mc: &Mutation<'gc>) -> ProjectStep<'gc, C, S> {
        let mut all_contexts_consumer = AllContextsConsumer::new();

        let msg = self.state.global_context.borrow().system.receive_message();
        if let Some(IncomingMessage { msg_type, values, reply_key }) = msg {
            let values: BTreeMap<_,_> = values.into_iter().collect();
            for i in 0..self.scripts.len() {
                if let Event::NetworkMessage { msg_type: script_msg_type, fields } = &self.scripts[i].event.0 {
                    if msg_type != *script_msg_type { continue }

                    let mut locals = SymbolTable::default();
                    for field in fields.iter() {
                        let value = values.get(field).map(|x| Value::from_simple(mc, x.clone())).unwrap_or_else(|| Number::new(0.0).unwrap().into());
                        locals.define_or_redefine(field, value.into());
                    }

                    let state = C::ProcessState::from(ProcessKind { entity: self.scripts[i].entity, dispatcher: None });

                    all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                    self.scripts[i].schedule(&mut self.state, PartialProcContext { locals, state, barrier: None, reply_key: reply_key.clone(), local_message: None }, usize::MAX);
                }
            }
        }

        let (proc_key, proc) = loop {
            match self.state.process_queue.pop_front() {
                None => {
                    debug_assert!(self.scripts.iter().all(|x| x.context_queue.is_empty()));
                    return ProjectStep::Idle;
                }
                Some(proc_key) => if let Some(proc) = self.state.processes.get_mut(proc_key) { break (proc_key, proc) }
            }
        };

        match proc.step(mc) {
            Ok(x) => match x {
                ProcessStep::Normal => {
                    self.state.process_queue.push_front(proc_key);
                    ProjectStep::Normal
                }
                ProcessStep::Yield => {
                    all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                    self.state.process_queue.push_back(proc_key);
                    ProjectStep::Yield
                }
                ProcessStep::Watcher { create, watcher } => {
                    self.state.process_queue.push_front(proc_key);
                    ProjectStep::Watcher { create, watcher }
                }
                ProcessStep::Pause => {
                    self.state.process_queue.push_front(proc_key);
                    ProjectStep::Pause
                }
                ProcessStep::Fork { pos, locals, entity } => {
                    let state = C::ProcessState::from(ProcessKind { entity, dispatcher: Some(proc) });
                    let proc = Process::new(ProcContext { global_context: self.state.global_context, entity, state, start_pos: pos, locals, barrier: None, reply_key: None, local_message: None });
                    let fork_proc_key = self.state.processes.insert(proc);

                    all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                    self.state.process_queue.push_back(fork_proc_key); // forked process starts at end of exec queue
                    self.state.process_queue.push_front(proc_key); // keep executing the same process as before
                    ProjectStep::Normal
                }
                ProcessStep::CreatedClone { clone } => {
                    let original = clone.borrow().original.unwrap();
                    let mut new_scripts = vec![];
                    for script in self.scripts.iter() {
                        if Gc::ptr_eq(script.entity, original) {
                            new_scripts.push(Script {
                                event: script.event.clone(),
                                entity: clone,
                                process: None,
                                context_queue: Default::default(),
                            });
                        }
                    }
                    for script in new_scripts.iter_mut() {
                        if let Event::OnClone = &script.event.0 {
                            let state = C::ProcessState::from(ProcessKind { entity: script.entity, dispatcher: Some(self.state.processes.get(proc_key).unwrap()) });

                            all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                            script.schedule(&mut self.state, PartialProcContext { state, locals: Default::default(), barrier: None, reply_key: None, local_message: None }, 0);
                        }
                    }
                    self.scripts.extend(new_scripts);
                    self.state.process_queue.push_front(proc_key); // keep executing the same process as before
                    ProjectStep::Normal
                }
                ProcessStep::DeletedClone { clone } => {
                    debug_assert!(clone.borrow().original.is_some());
                    self.scripts.retain_mut(|script| !Gc::ptr_eq(script.entity, clone));
                    self.state.processes.retain_mut(|_, proc| !Gc::ptr_eq(proc.get_call_stack().first().unwrap().entity, clone));
                    self.state.process_queue.push_front(proc_key); // keep executing the same process as before
                    ProjectStep::Normal
                }
                ProcessStep::Broadcast { msg_type, barrier, targets } => {
                    for i in 0..self.scripts.len() {
                        if let Event::LocalMessage { msg_type: recv_type } = &self.scripts[i].event.0 {
                            if recv_type.as_ref().map(|x| *x == msg_type).unwrap_or(true) {
                                if let Some(targets) = &targets {
                                    if !targets.iter().any(|&target| Gc::ptr_eq(self.scripts[i].entity, target)) {
                                        continue
                                    }
                                }

                                let state = C::ProcessState::from(ProcessKind { entity: self.scripts[i].entity, dispatcher: Some(self.state.processes.get(proc_key).unwrap()) });

                                all_contexts_consumer.do_once(self); // need to consume all contexts before scheduling things in the future
                                self.scripts[i].stop_all(&mut self.state);
                                self.scripts[i].schedule(&mut self.state, PartialProcContext { state, locals: Default::default(), barrier: barrier.clone(), reply_key: None, local_message: Some(msg_type.clone()) }, 0);
                            }
                        }
                    }
                    self.state.process_queue.push_front(proc_key); // keep executing same process, if it was a wait, it'll yield next step
                    ProjectStep::Normal
                }
                ProcessStep::Terminate { result } => {
                    let proc = self.state.processes.remove(proc_key).unwrap();
                    all_contexts_consumer.do_once(self); // need to consume all contexts after dropping a process
                    ProjectStep::ProcessTerminated { result, proc }
                }
                ProcessStep::Abort { mode } => match mode {
                    AbortMode::Current => {
                        debug_assert!(!proc.is_running());
                        self.state.processes.remove(proc_key);
                        ProjectStep::Normal
                    }
                    AbortMode::All => {
                        debug_assert!(!proc.is_running());
                        self.state.processes.clear();
                        self.state.process_queue.clear();
                        ProjectStep::Normal
                    }
                    AbortMode::Others => {
                        debug_assert!(proc.is_running());
                        self.state.processes.retain_mut(|k, _| k == proc_key);
                        debug_assert_eq!(self.state.processes.len(), 1);
                        self.state.process_queue.clear();
                        self.state.process_queue.push_front(proc_key); // keep executing the calling process
                        ProjectStep::Normal
                    }
                    AbortMode::MyOthers => {
                        debug_assert!(proc.is_running());
                        let entity = proc.get_call_stack().last().unwrap().entity;
                        self.state.processes.retain_mut(|k, v| k == proc_key || !Gc::ptr_eq(entity, v.get_call_stack().first().unwrap().entity));
                        self.state.process_queue.push_front(proc_key); // keep executing the calling process
                        ProjectStep::Normal
                    }
                }
                ProcessStep::Idle => unreachable!(),
            }
            Err(error) => {
                let proc = self.state.processes.remove(proc_key).unwrap();
                all_contexts_consumer.do_once(self); // need to consume all contexts after dropping a process
                ProjectStep::Error { error, proc }
            }
        }
    }
    pub fn get_global_context(&self) -> Gc<'gc, RefLock<GlobalContext<'gc, C, S>>> {
        self.state.global_context
    }
}