fish-shell/fish-rust/src/ffi.rs

215 lines
5.7 KiB
Rust
Raw Normal View History

2023-02-15 05:54:18 +08:00
use crate::wchar;
use crate::wchar_ffi::WCharToFFI;
#[rustfmt::skip]
use ::std::fmt::{self, Debug, Formatter};
#[rustfmt::skip]
use ::std::pin::Pin;
#[rustfmt::skip]
use ::std::slice;
use crate::wchar::wstr;
2023-01-15 06:56:24 +08:00
use autocxx::prelude::*;
use cxx::SharedPtr;
2023-01-15 06:56:24 +08:00
// autocxx has been hacked up to know about this.
pub type wchar_t = u32;
include_cpp! {
#include "builtin.h"
#include "common.h"
#include "env.h"
#include "event.h"
#include "fallback.h"
2023-01-15 06:56:24 +08:00
#include "fds.h"
#include "flog.h"
#include "io.h"
#include "parse_constants.h"
2023-01-15 06:56:24 +08:00
#include "parser.h"
#include "parse_util.h"
2023-01-15 06:56:24 +08:00
#include "proc.h"
#include "re.h"
#include "tokenizer.h"
#include "wildcard.h"
#include "wutil.h"
2023-01-15 06:56:24 +08:00
safety!(unsafe_ffi)
generate_pod!("wcharz_t")
generate!("make_fd_nonblocking")
generate!("wperror")
generate_pod!("pipes_ffi_t")
generate!("env_stack_t")
2023-01-15 06:56:24 +08:00
generate!("make_pipes_ffi")
2023-02-05 16:35:06 +08:00
generate!("valid_var_name_char")
2023-01-15 06:56:24 +08:00
generate!("get_flog_file_fd")
generate!("parse_util_unescape_wildcards")
2023-02-05 16:35:06 +08:00
generate!("fish_wcwidth")
generate!("fish_wcswidth")
2023-01-15 06:56:24 +08:00
generate!("wildcard_match")
generate!("wgettext_ptr")
2023-01-15 06:56:24 +08:00
generate!("block_t")
generate!("parser_t")
generate!("job_t")
generate!("process_t")
2023-02-19 00:13:58 +08:00
generate!("library_data_t")
generate_pod!("library_data_pod_t")
generate!("proc_wait_any")
generate!("output_stream_t")
generate!("io_streams_t")
generate_pod!("RustFFIJobList")
generate_pod!("RustFFIProcList")
generate_pod!("RustBuiltin")
generate!("builtin_missing_argument")
generate!("builtin_unknown_option")
generate!("builtin_print_help")
2023-02-19 00:13:58 +08:00
generate!("builtin_print_error_trailer")
generate!("wait_handle_t")
generate!("wait_handle_store_t")
2023-02-11 01:19:22 +08:00
generate!("event_fire_generic")
2023-02-11 23:51:43 +08:00
generate!("escape_string")
generate!("sig2wcs")
generate!("wcs2sig")
generate!("signal_get_desc")
Port fd_monitor (and its needed components) I needed to rename some types already ported to rust so they don't clash with their still-extant cpp counterparts. Helper ffi functions added to avoid needing to dynamically allocate an FdMonitorItem for every fd (we use dozens per basic prompt). I ported some functions from cpp to rust that are used only in the backend but without removing their existing cpp counterparts so cpp code can continue to use their version of them (`wperror` and `make_detached_pthread`). I ran into issues porting line-by-line logic because rust inverts the behavior of `std::remove_if(..)` by making it (basically) `Vec::retain_if(..)` so I replaced bools with an explict enum to make everything clearer. I'll port the cpp tests for this separately, for now they're using ffi. Porting closures was ugly. It's nothing hard, but it's very ugly as now each capturing lambda has been changed into an explicit struct that contains its parameters (that needs to be dynamically allocated), a standalone callback (member) function to replace the lambda contents, and a separate trampoline function to call it from rust over the shared C abi (not really relevant to x86_64 w/ its single calling convention but probably needed on other platforms). I don't like that `fd_monitor.rs` has its own `c_void`. I couldn't find a way to move that to `ffi.rs` but still get cxx bridge to consider it a shared POD. Every time I moved it to a different module, it would consider it to be an opaque rust type instead. I worry this means we're going to have multiple `c_void1`, `c_void2`, etc. types as we continue to port code to use function pointers. Also, rust treats raw pointers as foreign so you can't do `impl Send for * const Foo` even if `Foo` is from the same module. That necessitated a wrapper type (`void_ptr`) that implements `Send` and `Sync` so we can move stuff between threads. The code in fd_monitor_t has been split into two objects, one that is used by the caller and a separate one associated with the background thread (this is made nice and clean by rust's ownership model). Objects not needed under the lock (i.e. accessed by the background thread exclusively) were moved to the separate `BackgroundFdMonitor` type.
2023-02-18 09:21:44 +08:00
generate!("fd_event_signaller_t")
generate_pod!("re::flags_t")
generate_pod!("re::re_error_t")
generate!("re::regex_t")
generate!("re::regex_result_ffi")
generate!("re::try_compile_ffi")
}
impl parser_t {
pub fn get_jobs(&self) -> &[SharedPtr<job_t>] {
let ffi_jobs = self.ffi_jobs();
unsafe { slice::from_raw_parts(ffi_jobs.jobs, ffi_jobs.count) }
}
pub fn libdata_pod(&mut self) -> &mut library_data_pod_t {
let libdata = self.pin().ffi_libdata_pod();
unsafe { &mut *libdata }
}
pub fn remove_var(&mut self, var: &wstr, flags: c_int) -> c_int {
self.pin().remove_var_ffi(&var.to_ffi(), flags)
}
}
pub fn try_compile(anchored: &wstr, flags: &re::flags_t) -> Pin<Box<re::regex_result_ffi>> {
re::try_compile_ffi(&anchored.to_ffi(), flags).within_box()
}
impl job_t {
#[allow(clippy::mut_from_ref)]
pub fn get_procs(&self) -> &mut [UniquePtr<process_t>] {
let ffi_procs = self.ffi_processes();
unsafe { slice::from_raw_parts_mut(ffi_procs.procs, ffi_procs.count) }
}
2023-01-15 06:56:24 +08:00
}
/// Allow wcharz_t to be "into" wstr.
impl From<wcharz_t> for &wchar::wstr {
fn from(w: wcharz_t) -> Self {
let len = w.length();
let v = unsafe { slice::from_raw_parts(w.str_ as *const u32, len) };
wchar::wstr::from_slice(v).expect("Invalid UTF-32")
}
}
/// Allow wcharz_t to be "into" WString.
impl From<wcharz_t> for wchar::WString {
fn from(w: wcharz_t) -> Self {
let len = w.length();
let v = unsafe { slice::from_raw_parts(w.str_ as *const u32, len).to_vec() };
Self::from_vec(v).expect("Invalid UTF-32")
}
}
impl Debug for re::regex_t {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("regex_t")
}
}
/// A bogus trait for turning &mut Foo into Pin<&mut Foo>.
/// autocxx enforces that non-const methods must be called through Pin,
/// but this means we can't pass around mutable references to types like parser_t.
/// We also don't want to assert that parser_t is Unpin.
/// So we just allow constructing a pin from a mutable reference; none of the C++ code.
/// It's worth considering disabling this in cxx; for now we use this trait.
/// Eventually parser_t and io_streams_t will not require Pin so we just unsafe-it away.
pub trait Repin {
fn pin(&mut self) -> Pin<&mut Self> {
unsafe { Pin::new_unchecked(self) }
}
fn unpin(self: Pin<&mut Self>) -> &mut Self {
unsafe { self.get_unchecked_mut() }
}
}
// Implement Repin for our types.
impl Repin for block_t {}
impl Repin for env_stack_t {}
impl Repin for io_streams_t {}
impl Repin for job_t {}
impl Repin for output_stream_t {}
impl Repin for parser_t {}
impl Repin for process_t {}
impl Repin for re::regex_result_ffi {}
unsafe impl Send for re::regex_t {}
2023-01-15 06:56:24 +08:00
pub use autocxx::c_int;
pub use ffi::*;
pub use libc::c_char;
Port fd_monitor (and its needed components) I needed to rename some types already ported to rust so they don't clash with their still-extant cpp counterparts. Helper ffi functions added to avoid needing to dynamically allocate an FdMonitorItem for every fd (we use dozens per basic prompt). I ported some functions from cpp to rust that are used only in the backend but without removing their existing cpp counterparts so cpp code can continue to use their version of them (`wperror` and `make_detached_pthread`). I ran into issues porting line-by-line logic because rust inverts the behavior of `std::remove_if(..)` by making it (basically) `Vec::retain_if(..)` so I replaced bools with an explict enum to make everything clearer. I'll port the cpp tests for this separately, for now they're using ffi. Porting closures was ugly. It's nothing hard, but it's very ugly as now each capturing lambda has been changed into an explicit struct that contains its parameters (that needs to be dynamically allocated), a standalone callback (member) function to replace the lambda contents, and a separate trampoline function to call it from rust over the shared C abi (not really relevant to x86_64 w/ its single calling convention but probably needed on other platforms). I don't like that `fd_monitor.rs` has its own `c_void`. I couldn't find a way to move that to `ffi.rs` but still get cxx bridge to consider it a shared POD. Every time I moved it to a different module, it would consider it to be an opaque rust type instead. I worry this means we're going to have multiple `c_void1`, `c_void2`, etc. types as we continue to port code to use function pointers. Also, rust treats raw pointers as foreign so you can't do `impl Send for * const Foo` even if `Foo` is from the same module. That necessitated a wrapper type (`void_ptr`) that implements `Send` and `Sync` so we can move stuff between threads. The code in fd_monitor_t has been split into two objects, one that is used by the caller and a separate one associated with the background thread (this is made nice and clean by rust's ownership model). Objects not needed under the lock (i.e. accessed by the background thread exclusively) were moved to the separate `BackgroundFdMonitor` type.
2023-02-18 09:21:44 +08:00
/// A version of [`* const core::ffi::c_void`] (or [`* const libc::c_void`], if you prefer) that
/// implements `Copy` and `Clone`, because those two don't. Used to represent a `void *` ptr for ffi
/// purposes.
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct void_ptr(pub *const core::ffi::c_void);
impl core::fmt::Debug for void_ptr {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:p}", &self.0)
}
}
unsafe impl Send for void_ptr {}
unsafe impl Sync for void_ptr {}
impl core::convert::From<*const core::ffi::c_void> for void_ptr {
fn from(value: *const core::ffi::c_void) -> Self {
Self(value as *const _)
}
}
impl core::convert::From<*const u8> for void_ptr {
fn from(value: *const u8) -> Self {
Self(value as *const _)
}
}
impl core::convert::From<*const autocxx::c_void> for void_ptr {
fn from(value: *const autocxx::c_void) -> Self {
Self(value as *const _)
}
}