Skip to content

Commit fc6de65

Browse files
committed
Enable the os and io modules on wasi
1 parent 8276803 commit fc6de65

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

vm/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ crc32fast = "1.2.0"
9797
adler32 = "1.0.3"
9898
gethostname = "0.2.0"
9999
subprocess = "0.2.2"
100-
num_cpus = "1"
101100
socket2 = { version = "0.3", features = ["unix"] }
102101
rustyline = "6.0"
103102
openssl = { version = "0.10", features = ["vendored"] }
104103
openssl-sys = "0.9"
105104
openssl-probe = "0.1"
106105

106+
[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies]
107+
num_cpus = "1"
108+
107109
[target.'cfg(not(any(target_arch = "wasm32", target_os = "redox")))'.dependencies]
108110
dns-lookup = "1.0"
109111
flate2 = { version = "1.0", features = ["zlib"], default-features = false }

vm/src/stdlib/io.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ fn buffered_reader_close(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult
516516
}
517517

518518
// disable FileIO on WASM
519-
#[cfg(not(target_arch = "wasm32"))]
519+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
520520
mod fileio {
521521
use super::super::os;
522522
use super::*;
@@ -658,23 +658,9 @@ mod fileio {
658658
Ok(len)
659659
}
660660

661-
#[cfg(windows)]
662661
fn file_io_close(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
663662
let raw_handle = i64::try_from_object(vm, vm.get_attribute(instance.clone(), "__fileno")?)?;
664-
unsafe {
665-
winapi::um::handleapi::CloseHandle(raw_handle as _);
666-
}
667-
vm.set_attr(&instance, "closefd", vm.new_bool(true))?;
668-
vm.set_attr(&instance, "__closed", vm.new_bool(true))?;
669-
Ok(())
670-
}
671-
672-
#[cfg(unix)]
673-
fn file_io_close(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
674-
let raw_fd = i64::try_from_object(vm, vm.get_attribute(instance.clone(), "__fileno")?)?;
675-
unsafe {
676-
libc::close(raw_fd as _);
677-
}
663+
drop(os::rust_file(raw_handle));
678664
vm.set_attr(&instance, "closefd", vm.new_bool(true))?;
679665
vm.set_attr(&instance, "__closed", vm.new_bool(true))?;
680666
Ok(())
@@ -1051,7 +1037,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
10511037
"DEFAULT_BUFFER_SIZE" => ctx.new_int(8 * 1024),
10521038
});
10531039

1054-
#[cfg(not(target_arch = "wasm32"))]
1040+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
10551041
extend_module!(vm, module, {
10561042
"FileIO" => fileio::make_fileio(ctx, raw_io_base),
10571043
});

vm/src/stdlib/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mod unicodedata;
3737
mod warnings;
3838
mod weakref;
3939

40-
#[cfg(not(target_arch = "wasm32"))]
40+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
4141
#[macro_use]
4242
mod os;
4343

@@ -112,10 +112,12 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
112112
modules.insert("symtable".to_owned(), Box::new(symtable::make_module));
113113
}
114114

115+
#[cfg(any(unix, windows, target_os = "wasi"))]
116+
modules.insert(os::MODULE_NAME.to_owned(), Box::new(os::make_module));
117+
115118
// disable some modules on WASM
116119
#[cfg(not(target_arch = "wasm32"))]
117120
{
118-
modules.insert(os::MODULE_NAME.to_owned(), Box::new(os::make_module));
119121
modules.insert("_socket".to_owned(), Box::new(socket::make_module));
120122
modules.insert(
121123
"_multiprocessing".to_owned(),

vm/src/stdlib/os.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,24 @@ pub fn rust_file(raw_fileno: i64) -> File {
8585
unsafe { File::from_raw_handle(raw_fileno) }
8686
}
8787

88-
#[cfg(all(not(unix), not(windows)))]
88+
#[cfg(target_os = "wasi")]
89+
pub fn raw_file_number(handle: File) -> i64 {
90+
// This should be safe, since the wasi api is pretty well defined, but once
91+
// there's a std::os::wasi::fs::FileExt, we should use that instead.
92+
unsafe { std::mem::transmute::<_, u32>(handle).into() }
93+
}
94+
95+
#[cfg(target_os = "wasi")]
96+
pub fn rust_file(raw_fileno: i64) -> File {
97+
unsafe { std::mem::transmute(raw_fileno as u32) }
98+
}
99+
100+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
89101
pub fn rust_file(raw_fileno: i64) -> File {
90102
unimplemented!();
91103
}
92104

93-
#[cfg(all(not(unix), not(windows)))]
105+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
94106
pub fn raw_file_number(handle: File) -> i64 {
95107
unimplemented!();
96108
}
@@ -168,8 +180,10 @@ fn os_close(fileno: i64) {
168180
type OpenFlags = i32;
169181
#[cfg(windows)]
170182
type OpenFlags = u32;
183+
#[cfg(target_os = "wasi")]
184+
type OpenFlags = u16;
171185

172-
#[cfg(any(unix, windows))]
186+
#[cfg(any(unix, windows, target_os = "wasi"))]
173187
pub fn os_open(
174188
name: PyStringRef,
175189
flags: OpenFlags,
@@ -213,6 +227,7 @@ pub fn os_open(
213227
#[cfg(windows)]
214228
let flags = flags & !(libc::O_WRONLY as u32);
215229

230+
#[cfg(not(target_os = "wasi"))]
216231
options.custom_flags(flags);
217232
let handle = options
218233
.open(fname)
@@ -221,7 +236,7 @@ pub fn os_open(
221236
Ok(raw_file_number(handle))
222237
}
223238

224-
#[cfg(all(not(unix), not(windows)))]
239+
#[cfg(not(any(unix, windows, target_os = "wasi")))]
225240
pub fn os_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
226241
unimplemented!()
227242
}
@@ -490,7 +505,7 @@ fn bytes_as_osstr<'a>(b: &'a [u8], vm: &VirtualMachine) -> PyResult<&'a ffi::OsS
490505
use std::os::unix::ffi::OsStrExt;
491506
Some(ffi::OsStr::from_bytes(b))
492507
}
493-
#[cfg(windows)]
508+
#[cfg(not(unix))]
494509
{
495510
std::str::from_utf8(b).ok().map(|s| s.as_ref())
496511
}
@@ -889,9 +904,10 @@ fn os_stat(
889904
windows
890905
)))]
891906
fn os_stat(
892-
_file: Either<PyStringRef, i64>,
907+
file: Either<PyPathLike, i64>,
893908
_dir_fd: DirFd,
894-
_follow_symlinks: FollowSymlinks,
909+
follow_symlinks: FollowSymlinks,
910+
vm: &VirtualMachine,
895911
) -> PyResult {
896912
unimplemented!();
897913
}
@@ -978,6 +994,10 @@ fn os_get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult<bool> {
978994
}
979995

980996
fn os_set_inheritable(fd: i64, inheritable: bool, vm: &VirtualMachine) -> PyResult<()> {
997+
#[cfg(not(any(unix, windows)))]
998+
{
999+
unimplemented!()
1000+
}
9811001
#[cfg(unix)]
9821002
{
9831003
let fd = fd as RawFd;
@@ -1523,11 +1543,13 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
15231543
"supports_follow_symlinks" => supports_follow_symlinks.into_object(),
15241544
});
15251545

1526-
extend_module_platform_specific(&vm, module)
1546+
extend_module_platform_specific(&vm, &module);
1547+
1548+
module
15271549
}
15281550

15291551
#[cfg(unix)]
1530-
fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef {
1552+
fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
15311553
let ctx = &vm.ctx;
15321554

15331555
let uname_result = UnameResult::make_class(ctx);
@@ -1611,16 +1633,15 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
16111633
extend_module!(vm, module, {
16121634
"pipe2" => ctx.new_function(os_pipe2),
16131635
});
1614-
1615-
module
16161636
}
16171637

16181638
#[cfg(windows)]
1619-
fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef {
1639+
fn extend_module_platform_specific(vm: &VirtualMachine, module: &PyObjectRef) {
16201640
let ctx = &vm.ctx;
16211641
extend_module!(vm, module, {
16221642
"O_BINARY" => ctx.new_int(libc::O_BINARY),
16231643
});
1624-
1625-
module
16261644
}
1645+
1646+
#[cfg(not(any(unix, windows)))]
1647+
fn extend_module_platform_specific(_vm: &VirtualMachine, _module: &PyObjectRef) {}

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl VirtualMachine {
239239

240240
import::init_importlib(self, initialize_parameter)?;
241241

242-
#[cfg(not(target_arch = "wasm32"))]
242+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
243243
{
244244
let io = self.import("io", &[], 0)?;
245245
let io_open = self.get_attribute(io.clone(), "open")?;

0 commit comments

Comments
 (0)