]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/wasm/args.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libstd / sys / wasm / args.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ffi::OsString;
12 use marker::PhantomData;
13 use mem;
14 use vec;
15
16 pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
17 // On wasm these should always be null, so there's nothing for us to do here
18 }
19
20 pub unsafe fn cleanup() {
21 }
22
23 pub fn args() -> Args {
24 // When the runtime debugging is enabled we'll link to some extra runtime
25 // functions to actually implement this. These are for now just implemented
26 // in a node.js script but they're off by default as they're sort of weird
27 // in a web-wasm world.
28 if !super::DEBUG {
29 return Args {
30 iter: Vec::new().into_iter(),
31 _dont_send_or_sync_me: PhantomData,
32 }
33 }
34
35 // You'll find the definitions of these in `src/etc/wasm32-shim.js`. These
36 // are just meant for debugging and should not be relied on.
37 extern {
38 fn rust_wasm_args_count() -> usize;
39 fn rust_wasm_args_arg_size(a: usize) -> usize;
40 fn rust_wasm_args_arg_fill(a: usize, ptr: *mut u8);
41 }
42
43 unsafe {
44 let cnt = rust_wasm_args_count();
45 let mut v = Vec::with_capacity(cnt);
46 for i in 0..cnt {
47 let n = rust_wasm_args_arg_size(i);
48 let mut data = vec![0; n];
49 rust_wasm_args_arg_fill(i, data.as_mut_ptr());
50 v.push(mem::transmute::<Vec<u8>, OsString>(data));
51 }
52 Args {
53 iter: v.into_iter(),
54 _dont_send_or_sync_me: PhantomData,
55 }
56 }
57 }
58
59 pub struct Args {
60 iter: vec::IntoIter<OsString>,
61 _dont_send_or_sync_me: PhantomData<*mut ()>,
62 }
63
64 impl Args {
65 pub fn inner_debug(&self) -> &[OsString] {
66 self.iter.as_slice()
67 }
68 }
69
70 impl Iterator for Args {
71 type Item = OsString;
72 fn next(&mut self) -> Option<OsString> {
73 self.iter.next()
74 }
75 fn size_hint(&self) -> (usize, Option<usize>) {
76 self.iter.size_hint()
77 }
78 }
79
80 impl ExactSizeIterator for Args {
81 fn len(&self) -> usize {
82 self.iter.len()
83 }
84 }
85
86 impl DoubleEndedIterator for Args {
87 fn next_back(&mut self) -> Option<OsString> {
88 self.iter.next_back()
89 }
90 }