1
- use std:: collections:: LinkedList ;
2
1
use std:: fmt;
3
2
4
3
use crate :: obj:: objdict:: PyDictRef ;
@@ -11,7 +10,7 @@ use crate::vm::VirtualMachine;
11
10
*/
12
11
#[ derive( Clone ) ]
13
12
pub struct Scope {
14
- locals : LinkedList < PyDictRef > ,
13
+ locals : Vec < PyDictRef > ,
15
14
pub globals : PyDictRef ,
16
15
}
17
16
@@ -24,14 +23,11 @@ impl fmt::Debug for Scope {
24
23
25
24
impl Scope {
26
25
pub fn new ( locals : Option < PyDictRef > , globals : PyDictRef , vm : & VirtualMachine ) -> Scope {
27
- let mut locals_list = LinkedList :: new ( ) ;
28
- if let Some ( dict) = locals {
29
- locals_list. push_front ( dict)
30
- }
31
- let scope = Scope {
32
- locals : locals_list,
33
- globals,
26
+ let locals = match locals {
27
+ Some ( dict) => vec ! [ dict] ,
28
+ None => vec ! [ ] ,
34
29
} ;
30
+ let scope = Scope { locals, globals } ;
35
31
scope. store_name ( vm, "__annotations__" , vm. ctx . new_dict ( ) . into_object ( ) ) ;
36
32
scope
37
33
}
@@ -51,19 +47,20 @@ impl Scope {
51
47
}
52
48
53
49
pub fn get_locals ( & self ) -> PyDictRef {
54
- match self . locals . front ( ) {
50
+ match self . locals . first ( ) {
55
51
Some ( dict) => dict. clone ( ) ,
56
52
None => self . globals . clone ( ) ,
57
53
}
58
54
}
59
55
60
56
pub fn get_only_locals ( & self ) -> Option < PyDictRef > {
61
- self . locals . front ( ) . cloned ( )
57
+ self . locals . first ( ) . cloned ( )
62
58
}
63
59
64
60
pub fn new_child_scope_with_locals ( & self , locals : PyDictRef ) -> Scope {
65
- let mut new_locals = self . locals . clone ( ) ;
66
- new_locals. push_front ( locals) ;
61
+ let mut new_locals = Vec :: with_capacity ( self . locals . len ( ) + 1 ) ;
62
+ new_locals. push ( locals) ;
63
+ new_locals. append ( & mut self . locals . clone ( ) ) ;
67
64
Scope {
68
65
locals : new_locals,
69
66
globals : self . globals . clone ( ) ,
0 commit comments