Skip to content

Commit 6c88e46

Browse files
brsonthestinger
authored andcommitted
std:rt: args module is not used by win/mac. #7951
1 parent 206ae57 commit 6c88e46

File tree

1 file changed

+123
-69
lines changed

1 file changed

+123
-69
lines changed

src/libstd/rt/args.rs

Lines changed: 123 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -14,112 +14,166 @@
1414
//! the processes `argc` and `argv` arguments to be stored
1515
//! in a globally-accessible location for use by the `os` module.
1616
//!
17+
//! Only valid to call on linux. Mac and Windows use syscalls to
18+
//! discover the command line arguments.
19+
//!
1720
//! XXX: Would be nice for this to not exist.
1821
//! XXX: This has a lot of C glue for lack of globals.
1922
20-
use libc;
21-
use option::{Option, Some, None};
22-
use str;
23-
use uint;
24-
use unstable::finally::Finally;
25-
use util;
23+
use option::Option;
2624

2725
/// One-time global initialization.
2826
pub unsafe fn init(argc: int, argv: **u8) {
29-
let args = load_argc_and_argv(argc, argv);
30-
put(args);
27+
imp::init(argc, argv)
3128
}
3229

3330
/// One-time global cleanup.
3431
pub fn cleanup() {
35-
rtassert!(take().is_some());
32+
imp::cleanup()
3633
}
3734

3835
/// Take the global arguments from global storage.
3936
pub fn take() -> Option<~[~str]> {
40-
with_lock(|| unsafe {
41-
let ptr = get_global_ptr();
42-
let val = util::replace(&mut *ptr, None);
43-
val.map(|s: &~~[~str]| (**s).clone())
44-
})
37+
imp::take()
4538
}
4639

4740
/// Give the global arguments to global storage.
4841
///
4942
/// It is an error if the arguments already exist.
5043
pub fn put(args: ~[~str]) {
51-
with_lock(|| unsafe {
52-
let ptr = get_global_ptr();
53-
rtassert!((*ptr).is_none());
54-
(*ptr) = Some(~args.clone());
55-
})
44+
imp::put(args)
5645
}
5746

5847
/// Make a clone of the global arguments.
5948
pub fn clone() -> Option<~[~str]> {
60-
with_lock(|| unsafe {
61-
let ptr = get_global_ptr();
62-
(*ptr).map(|s: &~~[~str]| (**s).clone())
63-
})
49+
imp::clone()
6450
}
6551

66-
fn with_lock<T>(f: &fn() -> T) -> T {
67-
do (|| {
68-
unsafe {
69-
rust_take_global_args_lock();
70-
f()
71-
}
72-
}).finally {
73-
unsafe {
74-
rust_drop_global_args_lock();
75-
}
76-
}
77-
}
52+
#[cfg(target_os = "linux")]
53+
#[cfg(target_os = "android")]
54+
#[cfg(target_os = "freebsd")]
55+
mod imp {
7856

79-
fn get_global_ptr() -> *mut Option<~~[~str]> {
80-
unsafe { rust_get_global_args_ptr() }
81-
}
57+
use libc;
58+
use option::{Option, Some, None};
59+
use str;
60+
use uint;
61+
use unstable::finally::Finally;
62+
use util;
8263

83-
// Copied from `os`.
84-
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
85-
let mut args = ~[];
86-
for uint::range(0, argc as uint) |i| {
87-
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i)));
64+
pub unsafe fn init(argc: int, argv: **u8) {
65+
let args = load_argc_and_argv(argc, argv);
66+
put(args);
8867
}
89-
return args;
90-
}
91-
92-
extern {
93-
fn rust_take_global_args_lock();
94-
fn rust_drop_global_args_lock();
95-
fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>;
96-
}
9768

98-
#[cfg(test)]
99-
mod tests {
100-
use option::{Some, None};
101-
use super::*;
102-
use unstable::finally::Finally;
69+
pub fn cleanup() {
70+
rtassert!(take().is_some());
71+
}
10372

104-
#[test]
105-
fn smoke_test() {
106-
// Preserve the actual global state.
107-
let saved_value = take();
73+
pub fn take() -> Option<~[~str]> {
74+
with_lock(|| unsafe {
75+
let ptr = get_global_ptr();
76+
let val = util::replace(&mut *ptr, None);
77+
val.map(|s: &~~[~str]| (**s).clone())
78+
})
79+
}
10880

109-
let expected = ~[~"happy", ~"today?"];
81+
pub fn put(args: ~[~str]) {
82+
with_lock(|| unsafe {
83+
let ptr = get_global_ptr();
84+
rtassert!((*ptr).is_none());
85+
(*ptr) = Some(~args.clone());
86+
})
87+
}
11088

111-
put(expected.clone());
112-
assert!(clone() == Some(expected.clone()));
113-
assert!(take() == Some(expected.clone()));
114-
assert!(take() == None);
89+
pub fn clone() -> Option<~[~str]> {
90+
with_lock(|| unsafe {
91+
let ptr = get_global_ptr();
92+
(*ptr).map(|s: &~~[~str]| (**s).clone())
93+
})
94+
}
11595

96+
fn with_lock<T>(f: &fn() -> T) -> T {
11697
do (|| {
98+
unsafe {
99+
rust_take_global_args_lock();
100+
f()
101+
}
117102
}).finally {
118-
// Restore the actual global state.
119-
match saved_value {
120-
Some(ref args) => put(args.clone()),
121-
None => ()
103+
unsafe {
104+
rust_drop_global_args_lock();
105+
}
106+
}
107+
}
108+
109+
fn get_global_ptr() -> *mut Option<~~[~str]> {
110+
unsafe { rust_get_global_args_ptr() }
111+
}
112+
113+
// Copied from `os`.
114+
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
115+
let mut args = ~[];
116+
for uint::range(0, argc as uint) |i| {
117+
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i)));
118+
}
119+
return args;
120+
}
121+
122+
extern {
123+
fn rust_take_global_args_lock();
124+
fn rust_drop_global_args_lock();
125+
fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>;
126+
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use option::{Some, None};
131+
use super::*;
132+
use unstable::finally::Finally;
133+
134+
#[test]
135+
fn smoke_test() {
136+
// Preserve the actual global state.
137+
let saved_value = take();
138+
139+
let expected = ~[~"happy", ~"today?"];
140+
141+
put(expected.clone());
142+
assert!(clone() == Some(expected.clone()));
143+
assert!(take() == Some(expected.clone()));
144+
assert!(take() == None);
145+
146+
do (|| {
147+
}).finally {
148+
// Restore the actual global state.
149+
match saved_value {
150+
Some(ref args) => put(args.clone()),
151+
None => ()
152+
}
122153
}
123154
}
124155
}
125156
}
157+
158+
#[cfg(target_os = "macos")]
159+
#[cfg(target_os = "win32")]
160+
mod imp {
161+
162+
pub unsafe fn init(_argc: int, _argv: **u8) {
163+
}
164+
165+
pub fn cleanup() {
166+
}
167+
168+
pub fn take() -> Option<~[~str]> {
169+
fail!()
170+
}
171+
172+
pub fn put(_args: ~[~str]) {
173+
fail!()
174+
}
175+
176+
pub fn clone() -> Option<~[~str]> {
177+
fail!()
178+
}
179+
}

0 commit comments

Comments
 (0)