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
use super::*;

/// A Forth VM in which some builtin words are implemented by `async fn`s (or
/// [`Future`]s).
///
/// # Asynchronous Forth VMs
///
/// Asynchronous builtins are asynchronous relative to the *host* context (i.e.,
/// the Rust program in which the Forth VM is embedded), rather than the Forth
/// program that executes within the VM. This means that, unlike a
/// synchronous [`Forth`] VM, the [`AsyncForth::process_line`] method is an
/// [`async fn`]. When the Forth program executes a builtin word that is
/// implemented by an [`async fn`] on the host, the [`AsyncForth::process_line`]
/// will [`.await`] the [`Future`] that implements the builtin word, and will
/// yield if the `Future` is not ready. This allows multiple [`AsyncForth`] VMs
/// to run asynchronously in an async context on the host, yielding when the
/// Forth programs in those VMs sleep or perform asynchronous I/O operations.
///
/// # Providing Async Builtins
///
/// Unlike synchronous builtins, which are provided to the VM as a slice of
/// [`BuiltinEntry`]s, asynchronous builtins require an implementation of the
/// [`AsyncBuiltins`] trait, which provides both a slice of
/// [`AsyncBuiltinEntry`]s and a [method to dispatch builtin names to
/// `Future`s](AsyncBuiltins::dispatch_async). See the documentation for the
/// [`AsyncBuiltins`] trait for details on providing async builtins.
///
/// # Synchronous Builtins
///
/// An `AsyncForth` VM may also have synchronous builtin words. These behave
/// identically to the synchronous builtins in a non-async [`Forth`] VM.
/// Synchronous builtins should be used for any builtin word that does not
/// require performing an asynchronous operation on the host, such as those
/// which perform mathematical operations.
///
/// Synchronous builtins can be provided when the VM is constructed as a static
/// slice of [`BuiltinEntry`]s. They may also be added at runtime using the
/// [`AsyncForth::add_sync_builtin`] and
/// [`AsyncForth::add_sync_builtin_static_name`] method. These methods are
/// identical to the [`Forth::add_builtin`] and
/// [`Forth::add_builtin_static_name`] methods.
///
/// [`Future`]: core::future::Future
/// [`async fn`]: https://doc.rust-lang.org/stable/std/keyword.async.html
/// [`.await`]: https://doc.rust-lang.org/stable/std/keyword.await.html
pub struct AsyncForth<T: 'static, A> {
    vm: Forth<T>,
    builtins: A,
}

impl<T, A> AsyncForth<T, A>
where
    T: 'static,
    A: for<'forth> AsyncBuiltins<'forth, T>,
{
    /// Construct a new `AsyncForth` from the provided synchronous VM and async
    /// builtins.
    pub fn from_forth(vm: Forth<T>, builtins: A) -> Self {
        Self { vm, builtins }
    }

    pub unsafe fn new(
        bufs: Buffers<T>,
        dict: OwnedDict<T>,
        host_ctxt: T,
        sync_builtins: &'static [BuiltinEntry<T>],
        async_builtins: A,
    ) -> Result<Self, Error> {
        let vm = Forth::new_async(bufs, dict, host_ctxt, sync_builtins, A::BUILTINS)?;
        Ok(Self {
            vm,
            builtins: async_builtins,
        })
    }

    /// Constructs a new VM whose dictionary is a fork of this VM's dictionary.
    ///
    /// The current dictionary owned by this VM is frozen (made immutable), and
    /// a reference to it is shared with this VM and the new child VM. When both
    /// this VM and the child are dropped, the frozen dictionary is deallocated.
    ///
    /// This function takes two [`OwnedDict`]s as arguments: `new_dict` is the
    /// dictionary allocation for the forked child VM, while `my_dict` is a new
    /// allocation for this VM's mutable dictionary (which replaces the current
    /// dictionary, as it will become frozen).
    ///
    /// The child VM is created with empty stacks, and the provided input and
    /// output buffers.
    ///
    /// # Safety
    ///
    /// This method requires the same invariants be upheld as
    /// [`AsyncForth::new`].
    pub unsafe fn fork(
        &mut self,

        bufs: Buffers<T>,
        new_dict: OwnedDict<T>,
        my_dict: OwnedDict<T>,
        host_ctxt: T,
    ) -> Result<Self, Error>
    where
        A: Clone,
    {
        let vm = self.vm.fork(bufs, new_dict, my_dict, host_ctxt)?;
        Ok(Self {
            vm,
            builtins: self.builtins.clone(),
        })
    }

    /// Borrows this VM's [`OutputBuf`].
    #[inline]
    #[must_use]
    pub fn output(&self) -> &OutputBuf {
        &self.vm.output
    }

    /// Mutably borrows this VM's [`OutputBuf`].
    #[inline]
    #[must_use]
    pub fn output_mut(&mut self) -> &mut OutputBuf {
        &mut self.vm.output
    }

    /// Mutably borrows this VM's input [`WordStrBuf`].
    #[inline]
    #[must_use]
    pub fn input_mut(&mut self) -> &mut WordStrBuf {
        &mut self.vm.input
    }

    /// Borrows this VM's host context.
    #[inline]
    #[must_use]
    pub fn host_ctxt(&self) -> &T {
        &self.vm.host_ctxt
    }

    /// Mutably borrows this VM's host context.
    #[inline]
    #[must_use]
    pub fn host_ctxt_mut(&mut self) -> &mut T {
        &mut self.vm.host_ctxt
    }

    pub fn add_sync_builtin_static_name(
        &mut self,
        name: &'static str,
        bi: WordFunc<T>,
    ) -> Result<(), Error> {
        self.vm.add_builtin_static_name(name, bi)
    }

    pub fn add_sync_builtin(&mut self, name: &str, bi: WordFunc<T>) -> Result<(), Error> {
        self.vm.add_builtin(name, bi)
    }

    #[cfg(test)]
    #[allow(dead_code)]
    pub(crate) fn vm_mut(&mut self) -> &mut Forth<T> {
        &mut self.vm
    }

    pub async fn process_line(&mut self) -> Result<(), Error> {
        let res = async {
            loop {
                match self.vm.start_processing_line()? {
                    ProcessAction::Done => {
                        self.vm.output.push_str("ok.\n")?;
                        break Ok(());
                    }
                    ProcessAction::Continue => {}
                    ProcessAction::Execute => while self.async_pig().await? != Step::Done {},
                }
            }
        }
        .await;
        match res {
            Ok(_) => Ok(()),
            Err(e) => {
                self.vm.data_stack.clear();
                self.vm.return_stack.clear();
                self.vm.call_stack.clear();
                Err(e)
            }
        }
    }

    // Single step execution (async version).
    async fn async_pig(&mut self) -> Result<Step, Error> {
        let Self {
            ref mut vm,
            ref builtins,
        } = self;

        let top = match vm.call_stack.try_peek() {
            Ok(t) => t,
            Err(StackError::StackEmpty) => return Ok(Step::Done),
            Err(e) => return Err(Error::Stack(e)),
        };

        let kind = unsafe { top.eh.as_ref().kind };
        let res = unsafe {
            match kind {
                EntryKind::StaticBuiltin => (top.eh.cast::<BuiltinEntry<T>>().as_ref().func)(vm),
                EntryKind::RuntimeBuiltin => (top.eh.cast::<BuiltinEntry<T>>().as_ref().func)(vm),
                EntryKind::Dictionary => (top.eh.cast::<DictionaryEntry<T>>().as_ref().func)(vm),
                EntryKind::AsyncBuiltin => builtins.dispatch_async(&top.eh.as_ref().name, vm).await,
            }
        };

        match res {
            Ok(_) => {
                let _ = vm.call_stack.pop();
            }
            Err(Error::PendingCallAgain) => {
                // ok, just don't pop
            }
            Err(e) => return Err(e),
        }

        Ok(Step::NotDone)
    }

    pub fn release(self) -> T {
        self.vm.release()
    }
}