forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine.rs
More file actions
186 lines (176 loc) · 5.83 KB
/
coroutine.rs
File metadata and controls
186 lines (176 loc) · 5.83 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
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
use crate::builtins::{PyStrRef, PyTypeRef};
use crate::exceptions::{self, PyBaseExceptionRef};
use crate::frame::{ExecutionResult, FrameRef};
use crate::pyobject::{PyObjectRef, PyResult, TypeProtocol};
use crate::vm::VirtualMachine;
use crate::common::lock::PyMutex;
use crossbeam_utils::atomic::AtomicCell;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Variant {
Gen,
Coroutine,
AsyncGen,
}
impl Variant {
fn exec_result(self, res: ExecutionResult, vm: &VirtualMachine) -> PyResult {
res.into_result(self == Self::AsyncGen, vm)
}
fn name(self) -> &'static str {
match self {
Self::Gen => "generator",
Self::Coroutine => "coroutine",
Self::AsyncGen => "async generator",
}
}
fn stop_iteration(self, vm: &VirtualMachine) -> PyTypeRef {
match self {
Self::AsyncGen => vm.ctx.exceptions.stop_async_iteration.clone(),
_ => vm.ctx.exceptions.stop_iteration.clone(),
}
}
}
#[derive(Debug)]
pub struct Coro {
frame: FrameRef,
pub closed: AtomicCell<bool>,
running: AtomicCell<bool>,
exceptions: PyMutex<Vec<PyBaseExceptionRef>>,
variant: Variant,
name: PyMutex<PyStrRef>,
}
impl Coro {
pub fn new(frame: FrameRef, variant: Variant, name: PyStrRef) -> Self {
Coro {
frame,
closed: AtomicCell::new(false),
running: AtomicCell::new(false),
exceptions: PyMutex::new(vec![]),
variant,
name: PyMutex::new(name),
}
}
fn maybe_close(&self, res: &PyResult<ExecutionResult>) {
match res {
Ok(ExecutionResult::Return(_)) | Err(_) => self.closed.store(true),
Ok(ExecutionResult::Yield(_)) => {}
}
}
fn run_with_context<F>(&self, vm: &VirtualMachine, func: F) -> PyResult<ExecutionResult>
where
F: FnOnce(FrameRef) -> PyResult<ExecutionResult>,
{
if self.running.compare_exchange(false, true).is_err() {
return Err(vm.new_value_error(format!("{} already executing", self.variant.name())));
}
let curr_exception_stack_len;
{
let mut vm_excs = vm.exceptions.borrow_mut();
curr_exception_stack_len = vm_excs.len();
vm_excs.append(&mut self.exceptions.lock());
}
let result = vm.with_frame(self.frame.clone(), func);
self.exceptions
.lock()
.extend(vm.exceptions.borrow_mut().drain(curr_exception_stack_len..));
self.running.store(false);
result
}
pub fn send(&self, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if self.closed.load() {
return Err(vm.new_exception_empty(self.variant.stop_iteration(vm)));
}
let value = if self.frame.lasti() > 0 {
Some(value)
} else if !vm.is_none(&value) {
return Err(vm.new_type_error(format!(
"can't send non-None value to a just-started {}",
self.variant.name()
)));
} else {
None
};
let result = self.run_with_context(vm, |f| f.resume(value, vm));
self.maybe_close(&result);
match result {
Ok(exec_res) => self.variant.exec_result(exec_res, vm),
Err(e) => {
if e.isinstance(&vm.ctx.exceptions.stop_iteration) {
let err = vm
.new_runtime_error(format!("{} raised StopIteration", self.variant.name()));
err.set_cause(Some(e));
Err(err)
} else if self.variant == Variant::AsyncGen
&& e.isinstance(&vm.ctx.exceptions.stop_async_iteration)
{
let err = vm
.new_runtime_error("async generator raised StopAsyncIteration".to_owned());
err.set_cause(Some(e));
Err(err)
} else {
Err(e)
}
}
}
}
pub fn throw(
&self,
exc_type: PyObjectRef,
exc_val: PyObjectRef,
exc_tb: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult {
if self.closed.load() {
return Err(exceptions::normalize(exc_type, exc_val, exc_tb, vm)?);
}
let result = self.run_with_context(vm, |f| f.gen_throw(vm, exc_type, exc_val, exc_tb));
self.maybe_close(&result);
self.variant.exec_result(result?, vm)
}
pub fn close(&self, vm: &VirtualMachine) -> PyResult<()> {
if self.closed.load() {
return Ok(());
}
let result = self.run_with_context(vm, |f| {
f.gen_throw(
vm,
vm.ctx.exceptions.generator_exit.clone().into_object(),
vm.ctx.none(),
vm.ctx.none(),
)
});
self.closed.store(true);
match result {
Ok(ExecutionResult::Yield(_)) => {
Err(vm.new_runtime_error(format!("{} ignored GeneratorExit", self.variant.name())))
}
Err(e) if !is_gen_exit(&e, vm) => Err(e),
_ => Ok(()),
}
}
pub fn running(&self) -> bool {
self.running.load()
}
pub fn closed(&self) -> bool {
self.closed.load()
}
pub fn frame(&self) -> FrameRef {
self.frame.clone()
}
pub fn name(&self) -> PyStrRef {
self.name.lock().clone()
}
pub fn set_name(&self, name: PyStrRef) {
*self.name.lock() = name;
}
pub fn repr(&self, id: usize) -> String {
format!(
"<{} object {} at {:#x}>",
self.variant.name(),
self.name.lock(),
id
)
}
}
pub fn is_gen_exit(exc: &PyBaseExceptionRef, vm: &VirtualMachine) -> bool {
exc.isinstance(&vm.ctx.exceptions.generator_exit)
}