@@ -8,32 +8,9 @@ use std::cmp::min;
8
8
use std:: collections:: HashMap ;
9
9
use std:: convert:: TryFrom ;
10
10
use std:: hash:: { DefaultHasher , Hash , Hasher } ;
11
- use std:: sync:: RwLock ;
12
11
13
12
use crate :: analyzer:: { StackAnalyzer , StackStatus } ;
14
13
15
- // One global script map per thread.
16
- thread_local ! {
17
- static SCRIPT_MAP : RwLock <HashMap <u64 , Box <StructuredScript >>> =
18
- RwLock :: new( HashMap :: new( ) ) ;
19
- }
20
-
21
- pub ( crate ) fn thread_add_script ( id : u64 , script : StructuredScript ) {
22
- SCRIPT_MAP . with ( |script_map| {
23
- let mut map = script_map. write ( ) . unwrap ( ) ;
24
- map. entry ( id) . or_insert_with ( || Box :: new ( script) ) ;
25
- } ) ;
26
- }
27
-
28
- pub ( crate ) fn thread_get_script ( id : & u64 ) -> Box < StructuredScript > {
29
- SCRIPT_MAP . with ( |script_map| {
30
- let map = script_map. read ( ) . unwrap ( ) ;
31
- map. get ( id)
32
- . expect ( "script id not found in SCRIPT_MAP" )
33
- . clone ( )
34
- } )
35
- }
36
-
37
14
#[ derive( Clone , Debug , Hash ) ]
38
15
pub enum Block {
39
16
Call ( u64 ) ,
@@ -57,11 +34,11 @@ pub struct StructuredScript {
57
34
extra_endif_positions : Vec < usize > ,
58
35
max_if_interval : ( usize , usize ) ,
59
36
pub blocks : Vec < Block > ,
37
+ script_map : HashMap < u64 , StructuredScript > ,
60
38
}
61
39
62
40
impl Hash for StructuredScript {
63
41
fn hash < H : Hasher > ( & self , state : & mut H ) {
64
- self . size . hash ( state) ;
65
42
self . blocks . hash ( state) ;
66
43
}
67
44
}
@@ -84,13 +61,26 @@ impl StructuredScript {
84
61
extra_endif_positions : vec ! [ ] ,
85
62
max_if_interval : ( 0 , 0 ) ,
86
63
blocks,
64
+ script_map : HashMap :: new ( ) ,
87
65
}
88
66
}
89
67
90
68
pub fn len ( & self ) -> usize {
91
69
self . size
92
70
}
93
71
72
+ pub fn add_structured_script ( & mut self , id : u64 , script : StructuredScript ) {
73
+ self . script_map . entry ( id) . or_insert ( script) ;
74
+ }
75
+
76
+ pub fn get_structured_script ( & self , id : & u64 ) -> & StructuredScript {
77
+ self . script_map . get ( id)
78
+ . expect ( & format ! (
79
+ "script id: {} not found in script_map." ,
80
+ id
81
+ ) )
82
+ }
83
+
94
84
pub fn contains_flow_op ( & self ) -> bool {
95
85
!( self . unclosed_if_positions . is_empty ( )
96
86
&& self . extra_endif_positions ( ) . is_empty ( )
@@ -129,7 +119,11 @@ impl StructuredScript {
129
119
assert ! ( current_pos <= position, "Target position not found" ) ;
130
120
match block {
131
121
Block :: Call ( id) => {
132
- let called_script = thread_get_script ( id) ;
122
+ //let called_script = self.get_structured_script(id);
123
+ let called_script = self
124
+ . script_map
125
+ . get ( id)
126
+ . expect ( "Missing entry for a called script" ) ;
133
127
if position >= current_pos && position < current_pos + called_script. len ( ) {
134
128
return called_script. debug_info ( position - current_pos) ;
135
129
}
@@ -244,6 +238,12 @@ impl StructuredScript {
244
238
}
245
239
246
240
pub fn push_env_script ( mut self , mut data : StructuredScript ) -> StructuredScript {
241
+ if data. len ( ) == 0 {
242
+ return self ;
243
+ }
244
+ if self . len ( ) == 0 {
245
+ return data;
246
+ }
247
247
data. debug_identifier = format ! ( "{} {}" , self . debug_identifier, data. debug_identifier) ;
248
248
// Try closing ifs
249
249
let num_closable_ifs = min (
@@ -273,8 +273,8 @@ impl StructuredScript {
273
273
self . num_unclosed_ifs += data. num_unclosed_ifs ;
274
274
let id = calculate_hash ( & data) ;
275
275
self . blocks . push ( Block :: Call ( id) ) ;
276
- // Register script in the global script map
277
- thread_add_script ( id, data) ;
276
+ // Register script in the script map
277
+ self . add_structured_script ( id, data) ;
278
278
self
279
279
}
280
280
@@ -284,7 +284,10 @@ impl StructuredScript {
284
284
for block in self . blocks . as_slice ( ) {
285
285
match block {
286
286
Block :: Call ( id) => {
287
- let called_script = thread_get_script ( id) ;
287
+ let called_script = self
288
+ . script_map
289
+ . get ( id)
290
+ . expect ( "Missing entry for a called script" ) ;
288
291
// Check if the script with the hash id is in cache
289
292
match cache. get ( id) {
290
293
Some ( called_start) => {
0 commit comments