forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoroutine.rs
More file actions
152 lines (133 loc) · 3.77 KB
/
Copy pathcoroutine.rs
File metadata and controls
152 lines (133 loc) · 3.77 KB
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
use super::{PyCode, PyStrRef, PyTypeRef};
use crate::{
coroutine::{Coro, Variant},
frame::FrameRef,
function::OptionalArg,
slots::{IteratorIterable, SlotIterator},
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
#[pyclass(module = false, name = "coroutine")]
#[derive(Debug)]
pub struct PyCoroutine {
inner: Coro,
}
impl PyValue for PyCoroutine {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.coroutine_type
}
}
#[pyimpl(with(SlotIterator))]
impl PyCoroutine {
pub fn as_coro(&self) -> &Coro {
&self.inner
}
pub fn new(frame: FrameRef, name: PyStrRef) -> Self {
PyCoroutine {
inner: Coro::new(frame, Variant::Coroutine, name),
}
}
#[pyproperty(magic)]
fn name(&self) -> PyStrRef {
self.inner.name()
}
#[pyproperty(magic, setter)]
fn set_name(&self, name: PyStrRef) {
self.inner.set_name(name)
}
#[pymethod(magic)]
fn repr(zelf: PyRef<Self>) -> String {
zelf.inner.repr(zelf.get_id())
}
#[pymethod]
fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.inner.send(value, vm)
}
#[pymethod]
fn throw(
&self,
exc_type: PyObjectRef,
exc_val: OptionalArg,
exc_tb: OptionalArg,
vm: &VirtualMachine,
) -> PyResult {
self.inner.throw(
exc_type,
exc_val.unwrap_or_none(vm),
exc_tb.unwrap_or_none(vm),
vm,
)
}
#[pymethod]
fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
self.inner.close(vm)
}
#[pymethod(name = "__await__")]
fn r#await(zelf: PyRef<Self>) -> PyCoroutineWrapper {
PyCoroutineWrapper { coro: zelf }
}
#[pyproperty]
fn cr_await(&self, _vm: &VirtualMachine) -> Option<PyObjectRef> {
self.inner.frame().yield_from_target()
}
#[pyproperty]
fn cr_frame(&self, _vm: &VirtualMachine) -> FrameRef {
self.inner.frame()
}
#[pyproperty]
fn cr_running(&self, _vm: &VirtualMachine) -> bool {
self.inner.running()
}
#[pyproperty]
fn cr_code(&self, _vm: &VirtualMachine) -> PyRef<PyCode> {
self.inner.frame().code.clone()
}
// TODO: coroutine origin tracking:
// https://docs.python.org/3/library/sys.html#sys.set_coroutine_origin_tracking_depth
#[pyproperty]
fn cr_origin(&self, _vm: &VirtualMachine) -> Option<(PyStrRef, usize, PyStrRef)> {
None
}
}
impl IteratorIterable for PyCoroutine {}
impl SlotIterator for PyCoroutine {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
}
#[pyclass(module = false, name = "coroutine_wrapper")]
#[derive(Debug)]
pub struct PyCoroutineWrapper {
coro: PyRef<PyCoroutine>,
}
impl PyValue for PyCoroutineWrapper {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.coroutine_wrapper_type
}
}
#[pyimpl(with(SlotIterator))]
impl PyCoroutineWrapper {
#[pymethod]
fn send(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult {
self.coro.send(val, vm)
}
#[pymethod]
fn throw(
&self,
exc_type: PyObjectRef,
exc_val: OptionalArg,
exc_tb: OptionalArg,
vm: &VirtualMachine,
) -> PyResult {
self.coro.throw(exc_type, exc_val, exc_tb, vm)
}
}
impl IteratorIterable for PyCoroutineWrapper {}
impl SlotIterator for PyCoroutineWrapper {
fn next(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult {
zelf.send(vm.ctx.none(), vm)
}
}
pub fn init(ctx: &PyContext) {
PyCoroutine::extend_class(ctx, &ctx.types.coroutine_type);
PyCoroutineWrapper::extend_class(ctx, &ctx.types.coroutine_wrapper_type);
}