From bf094bd98a6597b15c752c69be1cf563d6a4208e Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 18 Jul 2024 15:14:10 +0900 Subject: [PATCH] fix: exclude APIs that dup given file descriptors from WASI builds WASI doesn't support `dup(2)` system call, so we cannot implement the `print_dot_graph` and `print_dot_graphs` functions with exactly the same semantics as in other platforms. (cherry picked from commit 94a8262110db8352b3797578d61639679e406862) --- lib/binding_rust/ffi.rs | 2 +- lib/binding_rust/lib.rs | 10 ++++++---- lib/src/tree.c | 9 ++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/binding_rust/ffi.rs b/lib/binding_rust/ffi.rs index ba3bcab6..755d2d52 100644 --- a/lib/binding_rust/ffi.rs +++ b/lib/binding_rust/ffi.rs @@ -8,7 +8,7 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); #[cfg(not(feature = "bindgen"))] include!("./bindings.rs"); -#[cfg(any(unix, target_os = "wasi"))] +#[cfg(unix)] #[cfg(feature = "std")] extern "C" { pub(crate) fn _ts_dup(fd: std::os::raw::c_int) -> std::os::raw::c_int; diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index 46f319d8..c97b4eaa 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -548,13 +548,14 @@ impl Parser { /// want to pipe these graphs directly to a `dot(1)` process in order to /// generate SVG output. #[doc(alias = "ts_parser_print_dot_graphs")] + #[cfg(not(target_os = "wasi"))] #[cfg(feature = "std")] pub fn print_dot_graphs( &mut self, - #[cfg(any(unix, target_os = "wasi"))] file: &impl AsRawFd, + #[cfg(unix)] file: &impl AsRawFd, #[cfg(windows)] file: &impl AsRawHandle, ) { - #[cfg(any(unix, target_os = "wasi"))] + #[cfg(unix)] { let fd = file.as_raw_fd(); unsafe { @@ -946,13 +947,14 @@ impl Tree { /// graph directly to a `dot(1)` process in order to generate SVG /// output. #[doc(alias = "ts_tree_print_dot_graph")] + #[cfg(not(target_os = "wasi"))] #[cfg(feature = "std")] pub fn print_dot_graph( &self, - #[cfg(any(unix, target_os = "wasi"))] file: &impl AsRawFd, + #[cfg(unix)] file: &impl AsRawFd, #[cfg(windows)] file: &impl AsRawHandle, ) { - #[cfg(any(unix, target_os = "wasi"))] + #[cfg(unix)] { let fd = file.as_raw_fd(); unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), fd) } diff --git a/lib/src/tree.c b/lib/src/tree.c index 14936732..55e79a7e 100644 --- a/lib/src/tree.c +++ b/lib/src/tree.c @@ -148,7 +148,7 @@ void ts_tree_print_dot_graph(const TSTree *self, int fd) { fclose(file); } -#else +#elif !defined(__wasi__) // WASI doesn't support dup #include @@ -162,4 +162,11 @@ void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor) { fclose(file); } +#else + +void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor) { + (void)self; + (void)file_descriptor; +} + #endif