forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.rs
More file actions
107 lines (95 loc) · 3.23 KB
/
Copy pathnode.rs
File metadata and controls
107 lines (95 loc) · 3.23 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
use crate::{PyObjectRef, PyResult, VirtualMachine};
use rustpython_compiler_core::SourceFile;
pub(crate) trait Node: Sized {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef;
fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self>;
/// Used in `Option::ast_from_object`; if `true`, that impl will return None.
fn is_none(&self) -> bool {
false
}
}
impl<T: Node> Node for Vec<T> {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
vm.ctx
.new_list(
self.into_iter()
.map(|node| node.ast_to_object(vm, source_file))
.collect(),
)
.into()
}
fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
// Recursion guard for each element: prevents stack overflow when a
// sequence element transitively references the sequence itself
// (e.g. `l = ast.List(...); l.elts = [l]`). See issue #4862.
vm.extract_elements_with(&object, |obj| {
vm.with_recursion("while traversing AST node", || {
Node::ast_from_object(vm, source_file, obj)
})
})
}
}
impl<T: Node> Node for Box<T> {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
(*self).ast_to_object(vm, source_file)
}
fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
// Recursion guard: every descent through a Box<AstNode> increments the
// VM's recursion depth so cyclic or pathologically deep ASTs raise
// RecursionError instead of overflowing the native stack.
// See issue #4862.
vm.with_recursion("while traversing AST node", || {
T::ast_from_object(vm, source_file, object).map(Self::new)
})
}
fn is_none(&self) -> bool {
(**self).is_none()
}
}
impl<T: Node> Node for Option<T> {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
match self {
Some(node) => node.ast_to_object(vm, source_file),
None => vm.ctx.none(),
}
}
fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
if vm.is_none(&object) {
Ok(None)
} else {
let x = T::ast_from_object(vm, source_file, object)?;
Ok((!x.is_none()).then_some(x))
}
}
}
pub(super) struct BoxedSlice<T>(pub(super) Box<[T]>);
impl<T: Node> Node for BoxedSlice<T> {
fn ast_to_object(self, vm: &VirtualMachine, source_file: &SourceFile) -> PyObjectRef {
self.0.into_vec().ast_to_object(vm, source_file)
}
fn ast_from_object(
vm: &VirtualMachine,
source_file: &SourceFile,
object: PyObjectRef,
) -> PyResult<Self> {
Ok(Self(
<Vec<T> as Node>::ast_from_object(vm, source_file, object)?.into_boxed_slice(),
))
}
}