21
21
//! FIXME #7756: This has a lot of C glue for lack of globals.
22
22
23
23
use option:: Option ;
24
+ #[ cfg( test) ] use option:: { Some , None } ;
25
+ #[ cfg( test) ] use realstd;
26
+ #[ cfg( test) ] use realargs = realstd:: rt:: args;
24
27
25
28
/// One-time global initialization.
26
- pub unsafe fn init ( argc : int , argv : * * u8 ) {
27
- imp:: init ( argc, argv)
28
- }
29
+ #[ cfg( not( test) ) ]
30
+ pub unsafe fn init ( argc : int , argv : * * u8 ) { imp:: init ( argc, argv) }
31
+ #[ cfg( test) ]
32
+ pub unsafe fn init ( argc : int , argv : * * u8 ) { realargs:: init ( argc, argv) }
29
33
30
34
/// One-time global cleanup.
31
- pub fn cleanup ( ) {
32
- imp:: cleanup ( )
33
- }
35
+ #[ cfg( not( test) ) ] pub fn cleanup ( ) { imp:: cleanup ( ) }
36
+ #[ cfg( test) ] pub fn cleanup ( ) { realargs:: cleanup ( ) }
34
37
35
38
/// Take the global arguments from global storage.
36
- pub fn take ( ) -> Option < ~[ ~str ] > {
37
- imp:: take ( )
39
+ #[ cfg( not( test) ) ] pub fn take ( ) -> Option < ~[ ~str ] > { imp:: take ( ) }
40
+ #[ cfg( test) ] pub fn take ( ) -> Option < ~[ ~str ] > {
41
+ match realargs:: take ( ) {
42
+ realstd:: option:: Some ( a) => Some ( a) ,
43
+ realstd:: option:: None => None ,
44
+ }
38
45
}
39
46
40
47
/// Give the global arguments to global storage.
41
48
///
42
49
/// It is an error if the arguments already exist.
43
- pub fn put ( args : ~[ ~str ] ) {
44
- imp:: put ( args)
45
- }
50
+ #[ cfg( not( test) ) ] pub fn put ( args : ~[ ~str ] ) { imp:: put ( args) }
51
+ #[ cfg( test) ] pub fn put ( args : ~[ ~str ] ) { realargs:: put ( args) }
46
52
47
53
/// Make a clone of the global arguments.
48
- pub fn clone ( ) -> Option < ~[ ~str ] > {
49
- imp:: clone ( )
54
+ #[ cfg( not( test) ) ] pub fn clone ( ) -> Option < ~[ ~str ] > { imp:: clone ( ) }
55
+ #[ cfg( test) ] pub fn clone ( ) -> Option < ~[ ~str ] > {
56
+ match realargs:: clone ( ) {
57
+ realstd:: option:: Some ( a) => Some ( a) ,
58
+ realstd:: option:: None => None ,
59
+ }
50
60
}
51
61
52
62
#[ cfg( target_os = "linux" ) ]
@@ -58,9 +68,12 @@ mod imp {
58
68
use iter:: Iterator ;
59
69
use str;
60
70
use unstable:: finally:: Finally ;
71
+ use unstable:: mutex:: { Mutex , MUTEX_INIT } ;
61
72
use util;
62
73
use vec;
63
74
75
+ static mut global_args_ptr: uint = 0 ;
76
+
64
77
pub unsafe fn init ( argc : int , argv : * * u8 ) {
65
78
let args = load_argc_and_argv ( argc, argv) ;
66
79
put ( args) ;
@@ -94,20 +107,22 @@ mod imp {
94
107
}
95
108
96
109
fn with_lock < T > ( f : & fn ( ) -> T ) -> T {
110
+ static mut lock: Mutex = MUTEX_INIT ;
111
+
97
112
do ( || {
98
113
unsafe {
99
- rust_take_global_args_lock ( ) ;
114
+ lock . lock ( ) ;
100
115
f ( )
101
116
}
102
117
} ) . finally {
103
118
unsafe {
104
- rust_drop_global_args_lock ( ) ;
119
+ lock . unlock ( ) ;
105
120
}
106
121
}
107
122
}
108
123
109
124
fn get_global_ptr ( ) -> * mut Option < ~~[ ~str ] > {
110
- unsafe { rust_get_global_args_ptr ( ) }
125
+ unsafe { cast :: transmute ( & global_args_ptr ) }
111
126
}
112
127
113
128
// Copied from `os`.
@@ -117,12 +132,6 @@ mod imp {
117
132
}
118
133
}
119
134
120
- extern {
121
- fn rust_take_global_args_lock ( ) ;
122
- fn rust_drop_global_args_lock ( ) ;
123
- fn rust_get_global_args_ptr ( ) -> * mut Option < ~~[ ~str ] > ;
124
- }
125
-
126
135
#[ cfg( test) ]
127
136
mod tests {
128
137
use option:: { Some , None } ;
0 commit comments