/
lib.rs
225 lines (195 loc) · 6.16 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::{ffi::c_char, fs::Metadata, time::SystemTime};
#[no_mangle]
pub extern "C" fn print_hello() {
println!("Hello, world! 2");
}
#[no_mangle]
pub extern "C" fn stderr_log(msg_utf16: *const u16, msg_utf16_length: u32) {
let msg = String::from_utf16(unsafe {
std::slice::from_raw_parts(msg_utf16, msg_utf16_length.try_into().unwrap())
})
.unwrap();
let flt = unsafe { translate(msg_utf16_length.try_into().unwrap()) };
let err = format!("{} {}\n", flt, msg);
// TODO: eprintln!("{} {}", flt, err); splits into multiple (5) lines
eprint!("{}", err);
}
#[no_mangle]
pub extern "C" fn read_file_size(path: *const c_char) -> u64 {
let path = unsafe { std::ffi::CStr::from_ptr(path) };
let metadata = std::fs::metadata(path.to_str().unwrap());
if let Err(err) = &metadata {
let err = format!("{path:?} {err:?}");
eprintln!("{err}");
}
metadata.unwrap().len()
}
// TODO: test default -> Rust FileData -> memory bytes pointer -> Dart FileData
// #[no_mangle]
// pub extern "C" fn file_data(path_utf8: *const u8, path_utf8_length: u32) -> FileData {
// let path = std::str::from_utf8(unsafe {
// std::slice::from_raw_parts(path_utf8, path_utf8_length.try_into().unwrap())
// })
// .unwrap();
// let metadata = std::fs::metadata(path).unwrap();
// metadata.into()
// }
#[no_mangle]
pub extern "C" fn file_data_raw(path_utf8: *const u8, path_utf8_length: u32) -> *const u8 {
let path = std::str::from_utf8(unsafe {
std::slice::from_raw_parts(path_utf8, path_utf8_length.try_into().unwrap())
})
.unwrap();
let metadata = std::fs::metadata(path).unwrap();
let data: FileData = metadata.into();
let mut bytes = Vec::new();
data.append_bytes(&mut bytes);
forget_and_return_pointer(bytes)
}
pub struct FileData {
pub size: u64,
pub read_only: bool,
pub modified: Option<u64>,
pub accessed: Option<u64>,
pub created: Option<u64>,
}
impl From<Metadata> for FileData {
fn from(metadata: Metadata) -> Self {
Self {
size: metadata.len(),
read_only: metadata.permissions().readonly(),
modified: metadata.modified().ok().as_ref().map(timestamp),
accessed: metadata.accessed().ok().as_ref().map(timestamp),
created: metadata.created().ok().as_ref().map(timestamp),
}
}
}
impl FileData {
fn append_bytes(&self, bytes: &mut Vec<u8>) {
bytes.extend(self.size.to_ne_bytes());
bytes.push(if self.read_only { 1 } else { 0 });
append_option(bytes, &self.modified, |bytes, a| {
bytes.extend(a.to_ne_bytes())
});
append_option(bytes, &self.accessed, |bytes, a| {
bytes.extend(a.to_ne_bytes())
});
append_option(bytes, &self.created, |bytes, a| {
bytes.extend(a.to_ne_bytes())
});
}
}
// final bool captureStdout;
// final bool captureStderr;
// final bool inheritStdin;
#[no_mangle]
pub extern "C" fn current_time() -> u64 {
timestamp(&std::time::SystemTime::now())
}
#[no_mangle]
pub extern "C" fn get_args() -> *const u8 {
// Vec::from_raw_parts(ptr, length, capacity);
let mut bytes = Vec::new();
bytes.extend(vec_size(std::env::args()));
std::env::args().for_each(|arg| {
append_str(&mut bytes, &arg);
});
forget_and_return_pointer(bytes)
}
#[no_mangle]
pub unsafe extern "C" fn dealloc(pointer: *mut u8, bytes: usize) {
let data = Vec::from_raw_parts(pointer, bytes, bytes);
std::mem::drop(data);
}
#[no_mangle]
pub unsafe extern "C" fn alloc(bytes: usize) -> *mut u8 {
let mut data = Vec::with_capacity(bytes);
let pointer = data.as_mut_ptr();
std::mem::forget(data);
pointer
}
#[no_mangle]
pub extern "C" fn get_env_vars() -> *const u8 {
let mut bytes = Vec::new();
bytes.extend(vec_size(std::env::vars()));
let iter =
std::env::vars().flat_map(|(name, value)| -> Vec<u8> { EnvVar { name, value }.into() });
bytes.extend(iter);
forget_and_return_pointer(bytes)
}
pub struct EnvVar {
name: String,
value: String,
}
impl From<EnvVar> for Vec<u8> {
fn from(env_var: EnvVar) -> Self {
let mut bytes = Vec::new();
append_str(&mut bytes, &env_var.name);
append_str(&mut bytes, &env_var.value);
bytes
}
}
fn forget_and_return_pointer(bytes: Vec<u8>) -> *const u8 {
let bytes = bytes.into_boxed_slice();
let pointer = bytes.as_ptr();
std::mem::forget(bytes);
pointer
}
fn timestamp(time: &SystemTime) -> u64 {
u64::try_from(
time.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis(),
)
.unwrap()
}
fn vec_size(v: impl Iterator) -> [u8; 4] {
let size = v.count();
println!("size {}", size);
u32::try_from(size).unwrap().to_ne_bytes()
}
fn append_str(bytes: &mut Vec<u8>, value: &str) {
println!("value {}", value);
bytes.extend(u32::try_from(value.len()).unwrap().to_ne_bytes());
bytes.extend(value.as_bytes());
}
fn append_option<T: Copy>(
bytes: &mut Vec<u8>,
option: &Option<T>,
append: impl Fn(&mut Vec<u8>, &T),
) {
bytes.push(if option.is_some() { 1 } else { 0 });
if let Some(value) = option {
append(bytes, value);
}
}
pub struct MyStruct {
pub name: String,
pub list: Vec<u8>,
pub list_double: Option<f32>,
pub opt: Option<bool>,
pub int_v: i32,
}
// impl MyStruct {
// #[wasm_bindgen(getter)]
// pub fn name(&self) -> String {
// self.name.clone()
// }
// #[wasm_bindgen(getter)]
// pub fn list(&self) -> Uint8Array {
// Uint8Array::from(self.list.as_slice())
// }
// }
#[link(wasm_import_module = "example_imports")]
extern "C" {
// functions can have integer/float arguments/return values
fn translate(a: i32) -> f64;
// Note that the ABI of Rust and wasm is somewhat in flux, so while this
// works, it's recommended to rely on raw integer/float values where
// possible.
// pub fn translate_fancy(my_struct: MyStruct) -> u32;
// you can also explicitly specify the name to import, this imports `bar`
// instead of `baz` from `example_imports`.
// #[link_name = "bar"]
// fn baz();
}