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
use super::{async_stack_action::AsyncStackAction, CpsError, Stack};
use crate::cps;
use alloc::{vec, vec::Vec};
use core::{
    future::Future,
    intrinsics::transmute,
    ops::{Deref, DerefMut},
    task::Context,
};

pub type StepFunction<T, V = ()> =
    fn(stack: &mut AsyncStack<V>, continuation: ContinuationFunction<T, V>) -> cps::Result;

pub type ContinuationFunction<T, V = ()> = extern "C" fn(&mut AsyncStack<V>, T) -> cps::Result;

pub type Trampoline<T, V> = (StepFunction<T, V>, ContinuationFunction<T, V>);

// Something like AsyncStackManager that creates async stacks with proper
// lifetime every time when it's given contexts can be implemented potentially.
// The reason we set those contexts unsafely with the `run_with_context` method
// in the current implementation is to keep struct type compatible between
// Stack and AsyncStack at the ABI level.
#[repr(C)]
#[derive(Debug)]
pub struct AsyncStack<V = ()> {
    stack: Stack,
    context: Option<*mut Context<'static>>,
    // This field is currently used only for validation in FFI but not by codes
    // generated by the compiler.
    next_actions: Vec<AsyncStackAction>,
    resolved_value: Option<V>,
}

impl<V> AsyncStack<V> {
    pub fn new(capacity: usize) -> Self {
        Self {
            stack: Stack::new(capacity),
            context: None,
            next_actions: vec![AsyncStackAction::Suspend],
            resolved_value: None,
        }
    }

    pub fn context(&mut self) -> Option<&mut Context<'_>> {
        self.context
            .map(|context| unsafe { &mut *transmute::<_, *mut Context<'_>>(context) })
    }

    pub fn run_with_context<T>(
        &mut self,
        context: &mut Context<'_>,
        callback: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.context = Some(unsafe { transmute(context) });

        let value = callback(self);

        self.context = None;

        value
    }

    pub fn suspend<T>(
        &mut self,
        step: StepFunction<T, V>,
        continuation: ContinuationFunction<T, V>,
        future: impl Future + Unpin,
    ) -> Result<(), CpsError> {
        self.validate_action(AsyncStackAction::Suspend)?;
        self.push_next_actions(&[
            AsyncStackAction::Resume,
            AsyncStackAction::Restore,
            AsyncStackAction::Suspend,
        ]);

        self.stack.push(future);
        self.stack.push(step);
        self.stack.push(continuation);

        Ok(())
    }

    // Trampoline a continuation function call to call it from the near bottom
    // of stack clearing the current stack frames.
    // Without this due to the lack of tail call elimination in Rust, machine
    // stacks can grow arbitrarily deep.
    // https://github.com/rust-lang/rfcs/issues/2691
    pub fn trampoline<T>(
        &mut self,
        continuation: ContinuationFunction<T, V>,
        value: T,
    ) -> Result<(), CpsError> {
        self.validate_action(AsyncStackAction::Suspend)?;
        self.push_next_actions(&[AsyncStackAction::Resume, AsyncStackAction::Suspend]);

        fn step<T, V>(
            stack: &mut AsyncStack<V>,
            continue_: ContinuationFunction<T, V>,
        ) -> cps::Result {
            let value = stack.pop::<T>();

            continue_(stack, value)
        }

        let step: StepFunction<T, V> = step;

        self.stack.push(value);
        self.stack.push(step);
        self.stack.push(continuation);

        self.context()
            .ok_or(CpsError::MissingContext)?
            .waker()
            .wake_by_ref();

        Ok(())
    }

    pub fn resume<T>(&mut self) -> Result<Trampoline<T, V>, CpsError> {
        self.validate_action(AsyncStackAction::Resume)?;

        let continuation = self.pop();
        let step = self.pop();

        Ok((step, continuation))
    }

    pub fn restore<F: Future + Unpin>(&mut self) -> Result<F, CpsError> {
        self.validate_action(AsyncStackAction::Restore)?;

        Ok(self.pop())
    }

    pub fn resolved_value(&mut self) -> Option<V> {
        self.resolved_value.take()
    }

    pub fn resolve(&mut self, value: V) {
        self.resolved_value = Some(value);
    }

    fn validate_action(&mut self, current_action: AsyncStackAction) -> Result<(), CpsError> {
        let next_action = self.next_actions.pop();

        if next_action != Some(current_action) {
            return Err(CpsError::UnexpectedAsyncStackAction(next_action));
        }

        Ok(())
    }

    fn push_next_actions(&mut self, next_actions: &[AsyncStackAction]) {
        self.next_actions.extend(next_actions.iter().rev().copied());
    }
}

impl<V> Deref for AsyncStack<V> {
    type Target = Stack;

    fn deref(&self) -> &Stack {
        &self.stack
    }
}

impl<V> DerefMut for AsyncStack<V> {
    fn deref_mut(&mut self) -> &mut Stack {
        &mut self.stack
    }
}

// We can mark async stacks Send + Send because:
//
// - Stack should implement Send + Sync. Currently, we don't as we don't need
//   to.
// - Option<*mut Context> is cleared to None on every non-preemptive run.
unsafe impl<V: Send> Send for AsyncStack<V> {}

unsafe impl<V: Sync> Sync for AsyncStack<V> {}

#[cfg(test)]
mod tests {
    use super::*;
    use core::{
        future::{ready, Ready},
        ptr::null,
        task::{RawWaker, RawWakerVTable, Waker},
    };

    const TEST_CAPACITY: usize = 1;
    const RAW_WAKER_DATA: () = ();
    const RAW_WAKER_V_TABLE: RawWakerVTable =
        RawWakerVTable::new(clone_waker, do_nothing, do_nothing, do_nothing);

    fn create_waker() -> Waker {
        unsafe { Waker::from_raw(RawWaker::new(&RAW_WAKER_DATA, &RAW_WAKER_V_TABLE)) }
    }

    fn clone_waker(_: *const ()) -> RawWaker {
        RawWaker::new(null(), &RAW_WAKER_V_TABLE)
    }

    fn do_nothing(_: *const ()) {}

    type TestResult = usize;

    fn step(_: &mut AsyncStack, _: ContinuationFunction<TestResult, ()>) -> cps::Result {
        cps::Result::new()
    }

    extern "C" fn continue_(_: &mut AsyncStack, _: TestResult) -> cps::Result {
        cps::Result::new()
    }

    #[allow(dead_code)]
    extern "C" {
        fn _test_async_stack_ffi_safety(_: &mut AsyncStack);
    }

    #[test]
    fn push_f64() {
        let mut stack = AsyncStack::<()>::new(TEST_CAPACITY);

        stack.push(42.0f64);

        assert_eq!(stack.pop::<f64>(), 42.0);
    }

    #[test]
    fn wake() {
        let waker = create_waker();
        let mut stack = AsyncStack::<()>::new(TEST_CAPACITY);
        let mut context = Context::from_waker(&waker);

        stack.run_with_context(&mut context, |stack| {
            stack.context().unwrap().waker().wake_by_ref()
        });
    }

    #[test]
    fn suspend() {
        let mut stack = AsyncStack::new(TEST_CAPACITY);

        stack.suspend(step, continue_, ready(42)).unwrap();
    }

    #[tokio::test]
    async fn suspend_and_resume() {
        let mut stack = AsyncStack::new(TEST_CAPACITY);

        type TestFuture = Ready<usize>;

        let future: TestFuture = ready(42);

        stack.suspend(step, continue_, future).unwrap();
        stack.resume::<()>().unwrap();
        assert_eq!(stack.restore::<TestFuture>().unwrap().await, 42);
    }

    #[tokio::test]
    async fn fail_to_restore_before_resume() {
        let mut stack = AsyncStack::new(TEST_CAPACITY);

        type TestFuture = Ready<()>;

        let future: TestFuture = ready(());

        stack.suspend(step, continue_, future).unwrap();
        assert_eq!(
            stack.restore::<TestFuture>().unwrap_err(),
            CpsError::UnexpectedAsyncStackAction(Some(AsyncStackAction::Resume))
        );
    }

    #[tokio::test]
    async fn trampoline_and_resume() {
        type Stack = AsyncStack<usize>;

        let mut stack = Stack::new(TEST_CAPACITY);

        extern "C" fn continue_(stack: &mut Stack, value: usize) -> cps::Result {
            stack.resolve(value);

            cps::Result::new()
        }

        let waker = create_waker();
        let mut context = Context::from_waker(&waker);

        stack.run_with_context(&mut context, |stack| {
            stack.trampoline(continue_, 42).unwrap();
        });

        let (step, continue_) = stack.resume::<()>().unwrap();

        stack.run_with_context(&mut context, |stack| {
            step(stack, continue_);
        });

        assert_eq!(stack.resolved_value(), Some(42));
    }
}