Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
188 lines (181 loc) · 7.39 KB
/
Copy pathlib.rs
File metadata and controls
188 lines (181 loc) · 7.39 KB
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
//! Native primitives called directly by the Lean interpreter through the C ABI.
//! No agent decisions, model requests, or retry policies are implemented here.
mod filesystem;
mod lean;
mod process;
use lean::{LeanObj, Owned};
use std::io;
// All pointers crossing this boundary are borrowed Lean objects. The pinned Lean
// declarations in Native.lean specify their types; the Lean runtime owns them.
// Result constructors transfer one owned Lean value back to the runtime.
macro_rules! export {
($name:ident ($($arg:ident : $ty:ty),*) $body:block) => {
/// # Safety
/// Called by the pinned Lean runtime with arguments matching Native.lean.
/// Object arguments are borrowed for this call; results transfer ownership.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn $name($($arg: $ty),*) -> LeanObj {
lean::boundary(|| $body)
}
};
}
export!(harness_open_dir(path: LeanObj) {
filesystem::open_dir(unsafe { lean::string_cstr(path)? }).map(lean::mk_u32)
});
export!(harness_open_dir_at(parent: u32, name: LeanObj) {
filesystem::open_dir_at(parent, unsafe { lean::string_cstr(name)? }).map(lean::mk_u32)
});
export!(harness_open_read_at(parent: u32, name: LeanObj) {
filesystem::open_read_at(parent, unsafe { lean::string_cstr(name)? }).map(lean::mk_u32)
});
export!(harness_open_write_at(parent: u32, name: LeanObj, exclusive: u8, append: u8, mode: u32) {
filesystem::open_write_at(parent, unsafe { lean::string_cstr(name)? }, exclusive != 0, append != 0, mode)
.map(lean::mk_u32)
});
export!(harness_close(fd: u32) {
filesystem::close(fd)?;
Ok(lean::mk_unit())
});
export!(harness_open_output(fd: u32) {
filesystem::open_output(fd).map(lean::mk_u32)
});
export!(harness_read(fd: u32, maximum: u64) {
Ok(lean::mk_bytes(&filesystem::read(fd, maximum)?))
});
export!(harness_write(fd: u32, bytes: LeanObj) {
filesystem::write(fd, unsafe { lean::byte_slice(bytes) })?;
Ok(lean::mk_unit())
});
export!(harness_sync(fd: u32) {
filesystem::sync(fd)?;
Ok(lean::mk_unit())
});
export!(harness_flock(fd: u32, exclusive: u8, nonblock: u8) {
filesystem::flock(fd, exclusive != 0, nonblock != 0).map(lean::mk_bool)
});
fn file_info(values: [u64; 6]) -> Owned {
lean::mk_array(values.into_iter().map(lean::mk_u64).collect())
}
export!(harness_stat(fd: u32) {
filesystem::stat(fd).map(file_info)
});
export!(harness_stat_at(fd: u32, name: LeanObj) {
filesystem::stat_at(fd, unsafe { lean::string_cstr(name)? }).map(file_info)
});
export!(harness_read_link_at(fd: u32, name: LeanObj) {
filesystem::read_link_at(fd, unsafe { lean::string_cstr(name)? }).map(|value| lean::mk_string(&value))
});
export!(harness_list_dir(fd: u32, limit: u32) {
Ok(lean::mk_array(filesystem::list_dir(fd, limit)?.iter().map(|name| lean::mk_string(name)).collect()))
});
export!(harness_rename_at(fd: u32, old: LeanObj, new_name: LeanObj) {
filesystem::rename_at(fd, unsafe { lean::string_cstr(old)? }, unsafe { lean::string_cstr(new_name)? })?;
Ok(lean::mk_unit())
});
export!(harness_link_at(fd: u32, old: LeanObj, new_name: LeanObj) {
filesystem::link_at(fd, unsafe { lean::string_cstr(old)? }, unsafe { lean::string_cstr(new_name)? })?;
Ok(lean::mk_unit())
});
export!(harness_unlink_at(fd: u32, name: LeanObj) {
filesystem::unlink_at(fd, unsafe { lean::string_cstr(name)? })?;
Ok(lean::mk_unit())
});
export!(harness_mkdir(path: LeanObj, mode: u32) {
filesystem::mkdir(unsafe { lean::string_cstr(path)? }, mode)?;
Ok(lean::mk_unit())
});
export!(harness_temp_at(parent: u32, mode: u32) {
let (name, fd) = filesystem::temp_at(parent, mode)?;
Ok(lean::mk_pair(lean::mk_string(&name), lean::mk_u32(fd)))
});
export!(harness_sha256(bytes: LeanObj) {
filesystem::sha256(unsafe { lean::byte_slice(bytes) }).map(|hash| lean::mk_string(&hash))
});
unsafe fn decode_strings(value: LeanObj) -> io::Result<Vec<String>> {
// The caller supplies Array String, borrowed for the duration of this call.
let objects = unsafe { lean::array_slice(value) };
if objects.len() > 4096 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"too many process arguments",
));
}
objects
.iter()
.map(|&object| unsafe { lean::string_str(object).map(str::to_owned) })
.collect()
}
unsafe fn decode_environment(value: LeanObj) -> io::Result<Vec<(String, Option<String>)>> {
let objects = unsafe { lean::array_slice(value) };
if objects.len() > 4096 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"too many environment entries",
));
}
objects
.iter()
.map(|&entry| {
let key = unsafe { lean::string_str(lean::ctor_field(entry, 0))? }.to_owned();
let value = unsafe { lean::option_value(lean::ctor_field(entry, 1)) };
let value = value
.map(|object| unsafe { lean::string_str(object).map(str::to_owned) })
.transpose()?;
Ok((key, value))
})
.collect()
}
export!(harness_spawn(command: LeanObj, args: LeanObj, cwd: LeanObj, environment: LeanObj, inherited: LeanObj) {
let command = unsafe { lean::string_str(command)? };
let args = unsafe { decode_strings(args)? };
let cwd = unsafe { lean::option_value(cwd) };
let cwd = cwd.map(|object| unsafe { lean::string_str(object) }).transpose()?;
let environment = unsafe { decode_environment(environment)? };
let inherited = unsafe { lean::array_slice(inherited) };
if inherited.len() > 16 { return Err(io::Error::new(io::ErrorKind::InvalidInput, "too many inherited descriptors")); }
let inherited: Vec<u32> = inherited.iter().map(|&value| unsafe { lean::u32_value(value) }).collect();
let values = process::spawn(command, &args, cwd, &environment, &inherited)?;
Ok(lean::mk_array(values.into_iter().map(lean::mk_u32).collect()))
});
export!(harness_status_pipe() {
let [read, write] = process::status_pipe()?;
Ok(lean::mk_pair(lean::mk_u32(read), lean::mk_u32(write)))
});
export!(harness_peek_exit(pid: u32) {
Ok(process::peek_exit(pid)?.map_or_else(lean::mk_none, |code| lean::mk_some(lean::mk_u32(code))))
});
export!(harness_group_has_others(pid: u32) {
process::group_has_others(pid).map(lean::mk_bool)
});
export!(harness_try_wait(pid: u32) {
Ok(process::try_wait(pid)?.map_or_else(lean::mk_none, |code| lean::mk_some(lean::mk_u32(code))))
});
export!(harness_read_available(fd: u32, maximum: u64) {
Ok(filesystem::read_available(fd, maximum)?.map_or_else(lean::mk_none, |bytes| lean::mk_some(lean::mk_bytes(&bytes))))
});
export!(harness_write_available(fd: u32, bytes: LeanObj, offset: u64) {
filesystem::write_available(fd, unsafe { lean::byte_slice(bytes) }, offset).map(lean::mk_u64)
});
export!(harness_poll_readable(fd: u32, timeout: u32) {
filesystem::poll_readable(fd, timeout).map(lean::mk_bool)
});
export!(harness_signal_group(pid: u32, signal: u32) {
process::signal_group(pid, signal)?;
Ok(lean::mk_unit())
});
export!(harness_group_exists(pid: u32) {
process::group_exists(pid).map(lean::mk_bool)
});
export!(harness_open_tty() {
filesystem::open_tty().map(lean::mk_u32)
});
export!(harness_install_signals() {
process::install_signals()?;
Ok(lean::mk_unit())
});
export!(harness_cancelled() {
Ok(lean::mk_bool(process::cancelled()))
});
export!(harness_pid() {
Ok(lean::mk_u32(process::pid()))
});