//! The Rust side of the C ABI: a crate of `extern "C"` functions over the facade, with the runtime beside it and the header in `include/`. //! //! Each function lifts its C arguments to Rust values, calls the Rust item by its path, //! and lowers the result. A value is copied in from the caller's memory, which Rust //! never frees, and lowered into memory the caller frees. A handle is borrowed as the //! receiver or parameter is, or moved out of when taken by value. use std::cell::Cell; use std::collections::HashSet; use std::fmt::Write; use std::hash::Hasher; use std::path::{Path, PathBuf}; use rustc_data_structures::stable_hash::StableHasher; use crate::abi::{self, Abi, CFG, Ty}; use crate::config::Package; use crate::ir::{ Access, Closure, Collection, Family, Function, Held, Jni, Literal, Mutability, On, Opaque, Receiver, Task, TaskKind, Traits, Type, }; const RUNTIME: &str = include_str!("../runtime/rt.rs"); const ANDROID: &str = include_str!("../runtime/rt/android.rs"); const BORROW: &str = include_str!("../runtime/rt/borrow.rs"); const THREAD: &str = include_str!("../runtime/rt/thread.rs"); const FUTURE: &str = include_str!("../runtime/rt/future.rs"); /// The crate's build script, which refuses to build for a target but `targets`, on which an /// item a target compiles would be missing, or one it leaves out left over, and tells the /// crate which it builds for, as the `cfg` [`CFG`]. On Android, it aligns the library to /// 16 KB pages. On macOS, a CPython extension leaves Python's symbols for the interpreter /// that loads it to resolve. fn build(targets: &[String]) -> String { let list: Vec = targets.iter().map(|t| format!("{t:?}")).collect(); let list = list.join(", "); let names = targets.join(", "); format!( "// Generated by rust2-ffi. Do not edit.\n\n\ fn main() {{\n\ \t// What it prints depends on this script alone, and on what cargo passes it, not on\n\ \t// the other files beside it, such as the Python extension a build copies there.\n\ \tprintln!(\"cargo::rerun-if-changed=build.rs\");\n\ \tlet target = std::env::var(\"TARGET\").expect(\"cargo sets TARGET\");\n\ \tprintln!(\"cargo::rustc-check-cfg=cfg({CFG}, values({}))\");\n\ \tif ![{list}].contains(&target.as_str()) {{\n\ \t\tpanic!(\"exported for {names}, not {{target}}: export again with --target {{target}}\");\n\ \t}}\n\ \tprintln!(\"cargo::rustc-cfg={CFG}=\\\"{{target}}\\\"\");\n\ \t// rustc leaves Android's page size to the NDK, whose r27 and older align to 4 KB, and\n\ \t// Google Play rejects a 64-bit library that is not aligned to 16 KB.\n\ \tif std::env::var(\"CARGO_CFG_TARGET_OS\").is_ok_and(|os| os == \"android\") {{\n\ \t\tprintln!(\"cargo::rustc-link-arg-cdylib=-Wl,-z,max-page-size=16384\");\n\ \t}}\n\ \t#[cfg(feature = \"python\")]\n\ \tpyo3_build_config::add_extension_module_link_args();\n\ }}\n", list.replace('"', "\\\"") ) } /// The name the crate depends on the facade by, its root suffixed: the facade may be named /// `bytes` or `std`, or as the crate itself, which the crate names too. fn dependency(krate: &str) -> String { format!("{krate}_export") } /// The name the crate depends on a crate the facade does not re-export by, apart from the /// facade's even when they share a name. fn reached(krate: &str) -> String { format!("{krate}_reached") } /// The name the crate depends on a foreign type's crate by, apart from the facade's even /// when the facade shares that crate's name. fn foreign(krate: &str) -> String { format!("{krate}_foreign") } /// The Rust path of a foreign type, by its own crate's path: std's as std names it, and /// another crate's through the name the crate depends on it by. fn foreign_path(path: &[String]) -> String { let krate = match path[0].as_str() { "std" => "std".to_string(), krate => foreign(krate), }; format!("::{krate}") + &path[1..].iter().map(|s| format!("::{}", ident(s))).collect::() } /// A Rust name as the glue writes it: a keyword raw, as socket2's `Socket::type` needs. fn ident(name: &str) -> String { const KEYWORDS: &[&str] = &[ "abstract", "as", "async", "await", "become", "box", "break", "const", "continue", "do", "dyn", "else", "enum", "extern", "false", "final", "fn", "for", "gen", "if", "impl", "in", "let", "loop", "macro", "match", "mod", "move", "mut", "override", "priv", "pub", "ref", "return", "static", "struct", "trait", "true", "try", "type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield", ]; match KEYWORDS.contains(&name) { true => format!("r#{name}"), false => name.to_string(), } } /// The Rust path of a collection, without its arguments. fn collection(c: Collection) -> String { match c { Collection::Vec => "::std::vec::Vec".to_string(), Collection::Box => "::std::boxed::Box".to_string(), Collection::Slice => unreachable!("a slice is spelled `[T]`"), Collection::Array(_) => unreachable!("an array is spelled `[T; N]`"), Collection::Iter | Collection::IterRef => "::std::vec::IntoIter".to_string(), Collection::BTreeSet => "::std::collections::BTreeSet".to_string(), Collection::BTreeMap => "::std::collections::BTreeMap".to_string(), Collection::JsonMap => format!("::{}::Map", foreign("serde_json")), } } /// The Rust spelling of a C type. pub(crate) fn rs(ty: &Ty) -> String { match ty { Ty::Void => "()".to_string(), Ty::Bool => "bool".to_string(), Ty::Int(int) => int.rust().to_string(), Ty::F32 => "f32".to_string(), Ty::F64 => "f64".to_string(), Ty::Char => "u32".to_string(), Ty::Buf => "rt::Buf".to_string(), Ty::U128 => "rt::U128".to_string(), Ty::Duration => "rt::Duration".to_string(), Ty::SocketAddr(Family::Any) => "rt::SocketAddr".to_string(), Ty::SocketAddr(Family::V4) => "rt::SocketAddrV4".to_string(), Ty::SocketAddr(Family::V6) => "rt::SocketAddrV6".to_string(), Ty::IpAddr(Family::Any) => "rt::IpAddr".to_string(), Ty::IpAddr(Family::V4) => "rt::Ipv4Addr".to_string(), Ty::IpAddr(Family::V6) => "rt::Ipv6Addr".to_string(), Ty::Struct(name) => name.clone(), // A C enum is an `int`. Ty::Enum(_) | Ty::Poll | Ty::Status => "i32".to_string(), Ty::Handle { .. } => "*const ::std::ffi::c_void".to_string(), Ty::Future(_) => "*mut rt::Future".to_string(), Ty::Waker => "rt::Waker".to_string(), Ty::ForeignFuture => "rt::ForeignFuture".to_string(), Ty::ForeignReader => "rt::ForeignReader".to_string(), Ty::Out(inner) => format!("*mut {}", rs(inner)), Ty::In(inner) => format!("*const {}", rs(inner)), Ty::InOut(inner) => format!("*mut {}", rs(inner)), Ty::Jni(_) => "*mut ::std::ffi::c_void".to_string(), Ty::Fn(params, ret) => { let params: Vec = std::iter::once("*mut ::std::ffi::c_void".to_string()) .chain(params.iter().map(rs)) .collect(); format!("Option {}>", params.join(", "), rs(ret)) } Ty::Data => "*mut ::std::ffi::c_void".to_string(), } } /// The crate that builds the C ABI. pub(crate) struct Crate<'a> { pub(crate) abi: &'a Abi<'a>, /// The facade, a dependency of the crate. pub(crate) facade: &'a Package, /// The `[patch]` table the crate's own workspace resolves the exports with, as the /// check did. pub(crate) patch: &'a str, /// The dependency on each crate the facade builds with, by its crate name, as this /// host resolves it, which a staged API's crates are named by too. pub(crate) crates: &'a std::collections::BTreeMap, } /// Generated files and the names checked before any file is written. pub(crate) struct Output { pub(crate) names: crate::lock::Names, files: Vec<(PathBuf, String)>, /// Directories the export owns, where an earlier export may have written files for /// crates or packages this one has not. stale: Vec, } impl Output { fn file(&mut self, path: &Path, text: &str) { self.files.push((path.to_path_buf(), text.to_string())); } /// Refuse a baseline below a directory the export owns, which removes what it did not write. pub(crate) fn check_baseline(&self, baseline: &Path) -> Result<(), String> { let baseline = std::fs::canonicalize(baseline).map_err(|err| format!("reading {}: {err}", baseline.display()))?; for dir in &self.stale { if baseline.starts_with(resolve(dir)?) { return Err(format!( "--baseline {} lies inside {} which the export replaces: commit the baseline outside --out", baseline.display(), dir.display() )); } } Ok(()) } /// Write a validated export, removing the C++ headers and Go files an earlier one wrote /// that this one does not. A file whose content is unchanged keeps its mtime, so a build /// of the export that cargo or a consumer fingerprints stays fresh. pub(crate) fn write(self) -> Result { let files: HashSet<&Path> = self.files.iter().map(|(path, _)| path.as_path()).collect(); for dir in &self.stale { match std::fs::symlink_metadata(dir) { // A link, as the export never writes, goes itself: pruning would follow it. Ok(meta) if meta.is_symlink() => { std::fs::remove_file(dir).map_err(|err| format!("removing {}: {err}", dir.display()))? } Ok(_) => prune(dir, &files)?, Err(err) if err.kind() == std::io::ErrorKind::NotFound => {} Err(err) => return Err(format!("reading {}: {err}", dir.display())), } } for (path, text) in &self.files { crate::driver::write(path, text)?; } Ok(self.names) } } /// Remove what `dir` holds that is not one of `keep`, and each directory that leaves empty. fn prune(dir: &Path, keep: &HashSet<&Path>) -> Result<(), String> { let entries = std::fs::read_dir(dir).map_err(|err| format!("reading {}: {err}", dir.display()))?; for entry in entries { let entry = entry.map_err(|err| format!("reading {}: {err}", dir.display()))?; let path = entry.path(); let kind = entry .file_type() .map_err(|err| format!("reading {}: {err}", path.display()))?; if kind.is_dir() { prune(&path, keep)?; } else if !keep.contains(path.as_path()) { std::fs::remove_file(&path).map_err(|err| format!("removing {}: {err}", path.display()))?; } } let empty = std::fs::read_dir(dir) .map_err(|err| format!("reading {}: {err}", dir.display()))? .next() .is_none(); if empty { std::fs::remove_dir(dir).map_err(|err| format!("removing {}: {err}", dir.display()))?; } Ok(()) } impl Crate<'_> { /// The facade's package name. fn packages(&self) -> String { self.facade.name.clone() } /// Write the crate into `out`: `Cargo.toml`, `src/`, `include/.h`, `.hpp`, and a /// `.hpp` per crate in `include//`, /// `kotlin/.kt`, whose JNI functions the crate's `kotlin` feature builds, /// `python/.py`, whose CPython extension its `python` feature builds, /// `swift/.swift` with the clang module it imports the header through, and /// `go/.go`, and `dart/.dart`, whose glue its `dart` feature builds. pub(crate) fn generate(&self, out: &Path) -> Result { // Cargo unifies in what the facade enables in its dependencies. let facade = self.facade; let mut features: Vec = facade.features.iter().map(|f| format!("{f:?}")).collect(); features.sort(); let dependencies = format!( "{} = {{ package = {:?}, path = {:?}, default-features = false, features = [{}] }}\n", dependency(&facade.root()), facade.name, facade.dir.display().to_string(), features.join(", ") ); // A foreign type, exported as an object when no export is its crate, resolves to the // version the exports build with, as `bytes` does: one per major version per // resolution. Its features are the ones the exports enable, so the glue adds none. let api = self.abi.api; let foreign: String = [ ("url", "url", "2"), ("serde_json", "serde_json", "1"), ("tracing_core", "tracing-core", "0.1"), ] .into_iter() .filter(|(krate, ..)| { (api.objects.iter()).any(|o| o.foreign && o.path[0] == *krate) || (api.enums.iter()).any(|e| e.foreign && e.path[0] == *krate) }) .map(|(krate, package, version)| { format!( "{} = {{ package = \"{package}\", version = \"{version}\", default-features = false }}\n", foreign(krate) ) }) .collect(); // A crate the facade does not re-export, whose types an export names, as the facade // resolves it, so they are the facade's types. // One only some targets' APIs name builds on those alone, as the facade's does. let mut every = String::new(); let mut targeted: std::collections::BTreeMap<&str, String> = Default::default(); // jni, on the targets of the functions that take its values, which the glue names // through it. It routes no facade path, since a facade item may be named `jni`. let methods = (api.records.iter().flat_map(|r| &r.methods)) .chain(api.enums.iter().flat_map(|e| &e.methods)) .chain(api.objects.iter().flat_map(|o| &o.methods)); let on = (api.functions.iter().chain(methods)) .filter(|f| f.jni()) .fold(0, |on, f| on | f.on.0); let jni = (on != 0).then(|| crate::ir::Reached { krate: "jni".to_string(), targets: (api.targets.iter().enumerate()) .filter(|(i, _)| on >> i & 1 == 1) .map(|(_, t)| t.clone()) .collect(), }); for r in api.reached.iter().chain(&jni) { let Some(keys) = self.crates.get(&r.krate) else { return Err(format!( "the facade builds with no crate `{}` here, which the API names", r.krate )); }; let line = format!("{} = {{ {keys}, default-features = false }}\n", reached(&r.krate)); match r.targets == api.targets { true => every.push_str(&line), false => (r.targets.iter()).for_each(|t| targeted.entry(t).or_default().push_str(&line)), } } let targeted: String = (targeted.into_iter()) .map(|(target, lines)| format!("[target.{target:?}.dependencies]\n{lines}\n")) .collect(); let dependencies = format!("{dependencies}{foreign}{every}"); // The facade's oldest toolchain is the crate's, which cargo sets as rustc compiles it, // though no older than the crate's edition needs; cargo checks the value's form. let rust_version = match std::env::var("CARGO_PKG_RUST_VERSION") { Ok(version) if !version.is_empty() => { let parts: Vec = version.split('.').map(|p| p.parse().unwrap_or(0)).collect(); let version = if parts[..] < [1, 85][..] { "1.85" } else { &version }; format!("rust-version = {version:?}\n") } _ => String::new(), }; let manifest = |version: &str| { format!( "# Generated by rust2-ffi: the C ABI of the pub API of {packages}.\n\ [package]\n\ name = \"{package}-ffi\"\n\ version = \"{version}\"\n\ edition = \"2024\"\n\ {rust_version}\ publish = false\n\ \n\ [lib]\n\ name = \"{lib}\"\n\ crate-type = [\"staticlib\", \"cdylib\"]\n\ \n\ [dependencies]\n\ {dependencies}\ bytes = \"1\"\n\ jni-sys = {{ version = \"0.4\", optional = true }}\n\ pyo3-ffi = {{ version = \"0.30\", optional = true, features = [\"abi3-py311\", \"extension-module\"] }}\n\ smallvec = {{ version = \"1.16\", optional = true }}\n\ \n\ [build-dependencies]\n\ pyo3-build-config = {{ version = \"0.30\", optional = true }}\n\ \n\ [features]\n\ # The JNI functions kotlin/{lib}.kt calls.\n\ kotlin = [\"dep:jni-sys\"]\n\ # The CPython extension python/{lib}.py loads.\n\ python = [\"dep:pyo3-ffi\", \"dep:pyo3-build-config\", \"dep:smallvec\"]\n\ # What dart/{lib}.dart calls that `dart:ffi` lacks.\n\ dart = []\n\ # What web/index.ts calls that the C ABI lacks, built for wasm32-wasip1.\n\ web = []\n\ \n\ {targeted}\ [workspace]\n\ {patch}", patch = self.patch, package = facade.name, packages = self.packages(), lib = self.abi.prefix, ) }; // Only Kotlin passes a JNI value, so every other language exports the rest. let rest = self.abi.api.without_jni(); let rest = self.abi.without_jni(&rest); let (header, contract) = crate::c::header(&rest, &self.packages()); let cpp = crate::cpp::headers(&rest, &self.packages())?; let kotlin = crate::kotlin::generate(self.abi, &self.packages(), contract)?; let python = crate::python::generate(&rest, &self.packages(), contract)?; let swift = crate::swift::generate(&rest, &self.packages(), contract)?; let (module, go_mod) = go_module(out, &self.abi.prefix)?; let go = crate::go::generate(&rest, &self.packages(), contract, &module)?; let dart = crate::dart::generate(&rest, &self.packages(), contract)?; let web = crate::web::generate(&rest, self.facade, contract)?; let mut names: std::collections::BTreeMap> = [ ("C++", cpp.names), ("Kotlin", kotlin.names), ("Python", python.names), ("Swift", swift.names), ("Go", go.names), ("Dart", dart.names), ] .into_iter() .map(|(language, names)| (language.to_string(), names)) .collect(); if let Some(web) = &web { names.insert("TypeScript".to_string(), web.names.clone()); } let mut output = Output { names, files: Vec::new(), stale: vec![ out.join("include").join(&self.abi.prefix), out.join("go"), out.join("web"), out.join("src").join("web"), ], }; let mut generator = Gen { abi: self.abi, packages: self.packages(), contract, dependency: dependency(&facade.root()), lends: false, holding: Cell::new(false), on: On::ALL, out: String::new(), checking: Cell::new(false), reserved: Cell::new(false), web: web.is_some(), }; generator.lib(); output.file(&out.join("Cargo.toml"), &manifest("0.0.0")); output.file(&out.join("src/lib.rs"), &generator.out); output.file(&out.join("build.rs"), &build(&self.abi.api.targets)); output.file(&out.join("src/rt.rs"), RUNTIME); output.file(&out.join("src/rt/android.rs"), ANDROID); output.file(&out.join("src/rt/borrow.rs"), BORROW); output.file(&out.join("src/rt/thread.rs"), THREAD); output.file(&out.join("src/rt/future.rs"), FUTURE); output.file(&out.join("include").join(format!("{}.h", self.abi.prefix)), &header); for (path, header) in &cpp.files { output.file(&out.join("include").join(path), header); } output.file(&out.join("src/jni/mod.rs"), &kotlin.glue); output.file(&out.join("src/jni/rt.rs"), crate::kotlin::GLUE_RUNTIME); output.file( &out.join("kotlin").join(format!("{}.kt", self.abi.prefix)), &kotlin.kotlin, ); output.file( &out.join("kotlin").join(format!("{}.pro", self.abi.prefix)), &kotlin.rules, ); output.file(&out.join("src/python/mod.rs"), &python.glue); output.file(&out.join("src/python/rt.rs"), crate::python::GLUE_RUNTIME); output.file( &out.join("python").join(format!("{}.py", self.abi.prefix)), &python.python, ); output.file(&out.join("src/dart/mod.rs"), &dart.glue); output.file(&out.join("src/dart/runtime.rs"), crate::dart::GLUE_RUNTIME); output.file(&out.join("dart").join(format!("{}.dart", self.abi.prefix)), &dart.dart); if let Some(web) = web { output.file(&out.join("src/web/mod.rs"), &web.glue); output.file(&out.join("src/web/runtime.rs"), crate::web::GLUE_RUNTIME); for (path, text) in &web.files { output.file(&out.join("web").join(path), text); } } let swift_dir = out.join("swift"); output.file(&swift_dir.join(format!("{}.swift", self.abi.prefix)), &swift.swift); output.file(&swift_dir.join("module.modulemap"), &swift.modulemap); output.file( &swift_dir.join(format!("{}_atomics.h", self.abi.prefix)), &swift.atomics, ); // What an earlier export wrote may name packages this one has not. let go_dir = out.join("go"); if let Some(go_mod) = go_mod { output.file(&go_dir.join("go.mod"), &go_mod); } for (path, text) in go.files { output.file(&go_dir.join(path), &text); } // The header the packages include, inside their module, as a module's zip holds it. output.file(&go_dir.join("rt").join(format!("{}.h", self.abi.prefix)), &header); // Cargo names a path package's units by its name, version, and path in its // workspace, the same for every export of one facade, and judges them fresh by // mtimes, which an unchanged file keeps. So two exports in one target directory // would share units, and one could build with the other's. A version whose build // metadata hashes what cargo compiles separates them; an unchanged export keeps it. let mut hasher = StableHasher::new(); for (path, text) in &output.files { if path.extension().is_some_and(|e| e == "rs" || e == "toml") { hasher.write_u64(text.len() as u64); hasher.write(text.as_bytes()); } } let version = format!("0.0.0+{:016x}", Hasher::finish(&hasher)); let cargo_toml = out.join("Cargo.toml"); for (path, text) in &mut output.files { if *path == cargo_toml { *text = manifest(&version); } } Ok(output) } } /// The path the module directive of the `go.mod` at `path` declares, as the go command reads it. fn module_path(path: &Path) -> Result { #[derive(serde::Deserialize)] struct GoMod { #[serde(rename = "Module")] module: Option, } #[derive(serde::Deserialize)] struct Module { #[serde(rename = "Path")] path: String, } let output = std::process::Command::new("go") .args(["mod", "edit", "-json"]) .arg(path) // Reading a go.mod that names a newer Go must not download that toolchain. .env("GOTOOLCHAIN", "local") .output() .map_err(|err| match err.kind() { std::io::ErrorKind::NotFound => format!( "the go command is not on PATH: the export reads the module of {} with `go mod edit -json`", path.display() ), _ => format!("running go mod edit -json {}: {err}", path.display()), })?; if !output.status.success() { return Err(format!( "go mod edit -json {} failed:\n{}", path.display(), String::from_utf8_lossy(&output.stderr).trim_end() )); } let go_mod: GoMod = serde_json::from_slice(&output.stdout) .map_err(|err| format!("reading go mod edit -json {}: {err}", path.display()))?; go_mod .module .map(|module| module.path) .ok_or_else(|| format!("{} declares no module", path.display())) } /// Resolve existing symlinks and missing path components without creating output. fn resolve(path: &Path) -> Result { // Resolved without creating it, since an export the lock refuses writes nothing. let absolute = std::path::absolute(path).map_err(|err| format!("reading {}: {err}", path.display()))?; let mut out = PathBuf::new(); for component in absolute.components() { if component == std::path::Component::ParentDir { out.pop(); } else { out.push(component); if out .try_exists() .map_err(|err| format!("reading {}: {err}", out.display()))? { out = std::fs::canonicalize(&out).map_err(|err| format!("reading {}: {err}", path.display()))?; } } } Ok(out) } /// The import path of `/go`, from the `go.mod` of the module it is in, as Go finds it: /// its own, which the export keeps as it is, or one above it; or else the module `prefix`, /// with the `go.mod` declaring it there. The `go.mod` to write in `go/` comes with it. fn go_module(out: &Path, prefix: &str) -> Result<(String, Option), String> { let out = resolve(out)?; let default = format!("module {prefix}\n\ngo 1.24\n"); let own = out.join("go").join("go.mod"); if own.is_file() { let text = std::fs::read_to_string(&own).map_err(|err| format!("reading {}: {err}", own.display()))?; // One in the form the export writes, as the release builder writes it too, needs no // go command to read. let plain = (text.strip_prefix("module ")) .and_then(|rest| rest.strip_suffix("\n\ngo 1.24\n")) .filter(|path| { !path.is_empty() && !path.contains("//") && path.chars().all(|c| c.is_ascii_alphanumeric() || "._~/-".contains(c)) }); let module = match plain { Some(path) => path.to_string(), None => module_path(&own)?, }; return Ok((module, Some(text))); } for dir in out.ancestors() { let path = dir.join("go.mod"); if !path.is_file() { continue; } let module = module_path(&path)?; let rest = out.join("go"); let rest = rest.strip_prefix(dir).expect("an ancestor holds it"); let rest: Vec = rest .components() .map(|c| c.as_os_str().to_string_lossy().into_owned()) .collect(); return Ok((format!("{module}/{}", rest.join("/")), None)); } Ok((prefix.to_string(), Some(default))) } struct Gen<'a> { abi: &'a Abi<'a>, /// The exported packages, for the docs. packages: String, /// The header's contract, which `_contract` returns. contract: u64, /// The name the crate depends on the facade by. dependency: String, /// Whether the function being written returns a borrow lent from its receiver's /// handle, which `lender` holds. lends: bool, /// Whether the value being lowered is what an exclusive lend returns, whose handle /// holds the receiver's borrow `a_self_loan` holds. holding: Cell, /// The targets of the item being written, which compile what names its Rust types. on: On, out: String, /// Whether the code being written returns a failed check as an `Err`, with `?`, rather /// than aborting, as a function with a `status` does. checking: Cell, /// Whether the argument being lifted had its moved handles reserved, so its lift moves /// their values out unchecked. reserved: Cell, /// Whether the API serves the web, whose glue the crate's `web` feature builds. web: bool, } impl Gen<'_> { /// A C type, or anything else every target compiles. fn line(&mut self, line: impl AsRef) { self.out.push_str(line.as_ref()); self.out.push('\n'); } fn cfg(&self, on: On) -> Option { self.abi.cfg(on) } /// What names the facade's types, on the targets of the item being written. fn item(&mut self, item: String) { if let Some(cfg) = self.cfg(self.on) { self.out.push_str(&format!("#[cfg({cfg})]\n")); } self.line(item); } /// A C function, which on a target its item is not on fails when called: the binding /// serves those targets too, and checks a call's target before making it. fn export(&mut self, function: String) { let Some(cfg) = self.cfg(self.on) else { return self.line(function); }; let at = function.find(" extern \"C\" fn ").expect("a C function"); let head = &function[..at + function[at..].find(" {\n").expect("a C function's body")]; let name = &head[head.rfind(" fn ").expect("a C function") + 4..]; let name = &name[..name.find('(').expect("a C function's parameters")]; let stub = format!( "#[cfg(not({cfg}))]\n#[allow(unused_variables, reason = \"a call fails before reading any\")]\n{head} {{\n\trt::fail(\"{name}\", \"it is not on this library's target\")\n}}\n" ); self.out.push_str(&format!("#[cfg({cfg})]\n")); self.line(function); self.line(stub); } /// The `#[cfg]` of a match arm for `variant`, if not on every target. fn arm(&self, on: On) -> String { self.cfg(on) .map(|cfg| format!("\n\t\t#[cfg({cfg})]")) .unwrap_or_default() } /// A Rust path of the facade's, through the crate's dependency on it, or on the crate /// its second segment names, where the facade does not re-export that crate. fn path(&self, path: &[String]) -> String { match self.abi.api.reached.iter().find(|r| path.get(1) == Some(&r.krate)) { Some(r) => { format!("::{}", reached(&r.krate)) + &path[2..].iter().map(|s| format!("::{}", ident(s))).collect::() } None => { format!("::{}", self.dependency) + &path[1..].iter().map(|s| format!("::{}", ident(s))).collect::() } } } /// `e`, a check's `Result`: its value, returning the failure from the function being /// written if it takes a `status`, else aborting. fn checked(&self, e: &str) -> String { match self.checking.get() { true => format!("{e}?"), false => format!("rt::check({e}, w)"), } } /// Whether lifting a value of `ty` checks a handle that can fail. fn checks(&self, ty: &Type) -> bool { self.abi.value_status(ty) } /// Whether `ty` moves an object's value out of its handle, as passed by value, held /// alone or in any value. fn moves(&self, ty: &Type) -> bool { self.holds(ty, &|i| !self.abi.api.objects[i].copy, &mut Vec::new()) } /// Whether lifting `ty` within a moved argument reserves anything into `takes`: a value /// it moves out, or a checked `Copy` one it copies. fn reserves(&self, ty: &Type) -> bool { let pick = |i: usize| !self.abi.api.objects[i].copy || self.abi.checked[i]; self.holds(ty, &pick, &mut Vec::new()) } /// Whether a value of `ty`, passed by value, holds an object whose index `pick` picks. fn holds(&self, ty: &Type, pick: &dyn Fn(usize) -> bool, seen: &mut Vec<(bool, usize)>) -> bool { let api = self.abi.api; match ty { Type::Object(i) => pick(*i), Type::Option(inner) | Type::Box(inner) | Type::List(_, inner) => self.holds(inner, pick, seen), Type::Tuple(elements) => elements.iter().any(|ty| self.holds(ty, pick, seen)), Type::Record(i) if !seen.contains(&(false, *i)) => { seen.push((false, *i)); (api.records[*i].fields.iter()).any(|f| self.holds(&f.ty, pick, seen)) } Type::Enum(i) if !seen.contains(&(true, *i)) => { seen.push((true, *i)); (api.enums[*i].variants.iter().flat_map(|v| &v.fields)).any(|f| self.holds(&f.ty, pick, seen)) } _ => false, } } /// Whether lifting a value of `ty` with `take` can take a buffer C handed Rust. A list's /// items are only read, so a list takes none. fn takes_bufs(&self, ty: &Type, seen: &mut Vec<(bool, usize)>) -> bool { let api = self.abi.api; match ty { _ if Abi::is_buf(ty) => true, Type::Option(inner) | Type::Box(inner) | Type::Ref(_, inner) => self.takes_bufs(inner, seen), Type::Tuple(elements) => elements.iter().any(|ty| self.takes_bufs(ty, seen)), Type::Record(i) if !seen.contains(&(false, *i)) => { seen.push((false, *i)); (api.records[*i].fields.iter()).any(|f| self.takes_bufs(&f.ty, seen)) } Type::Enum(i) if !seen.contains(&(true, *i)) => { seen.push((true, *i)); (api.enums[*i].variants.iter().flat_map(|v| &v.fields)).any(|f| self.takes_bufs(&f.ty, seen)) } _ => false, } } /// The statement freeing each buffer a lift of the C value `e` of type `ty` with `take` /// would take, leaving its handles to the caller: what a refused call does with an /// argument it never lifted. fn release(&self, ty: &Type, e: &str) -> Option { match ty { _ if Abi::is_buf(ty) => Some(format!("unsafe {{ {e}.free() }};")), Type::Option(inner) => match inner.base() { base if Abi::nullable(base) => self.release(inner, e), _ => (self.release(inner, &format!("{e}.value"))).map(|free| format!("if {e}.some {{ {free} }}")), }, Type::Box(inner) | Type::Ref(_, inner) => self.release(inner, e), Type::Tuple(elements) => { let frees: Vec = (elements.iter().enumerate()) .filter_map(|(n, ty)| self.release(ty, &format!("{e}._{n}"))) .collect(); (!frees.is_empty()).then(|| frees.join(" ")) } Type::Record(i) if self.takes_bufs(ty, &mut Vec::new()) => { Some(format!("release_{}(&{e});", self.abi.record(*i))) } Type::Enum(i) if !self.abi.api.enums[*i].plain() && self.takes_bufs(ty, &mut Vec::new()) => { Some(format!("release_{}(&{e});", self.abi.enumeration(*i))) } _ => None, } } /// Whether an argument of `ty` is reserved though it moves nothing: its lift can fail a /// check and takes a buffer, so every check runs before any buffer is taken. A borrowed /// object takes none. fn refusable(&self, ty: &Type) -> bool { let value = match ty { Type::Ref(_, inner) => inner, Type::Option(inner) => match &**inner { Type::Ref(_, inner) => inner, _ => ty, }, _ => ty, }; !matches!(value, Type::Object(_)) && self.checks(value) && self.takes_bufs(value, &mut Vec::new()) } /// The statement reserving into `takes` each value an argument `e` of type `ty` moves /// out, and borrowing each checked `Copy` one it copies, so the move happens only once /// every check passed, and no check after it can fail. fn reserve(&self, ty: &Type, e: &str) -> String { match ty { Type::Object(i) if !self.abi.api.objects[*i].copy => { self.checked(&format!( "unsafe {{ rt::Object::<{}>::reserve({e}, takes, w) }}", self.object_path(*i) )) + ";" } Type::Object(i) if self.abi.checked[*i] => { let hold = match self.abi.api.objects[*i].sync { true => "hold", false => "hold_local", }; self.checked(&format!( "unsafe {{ rt::Object::<{}>::{hold}({e}, takes, w) }}", self.object_path(*i) )) + ";" } Type::Object(_) => String::new(), Type::Record(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.record(*i))) + ";", Type::Enum(i) => self.checked(&format!("reserve_{}(&{e}, takes, w)", self.abi.enumeration(*i))) + ";", // A borrowed record is lifted from its C value as an owned one is. Type::Box(inner) | Type::Ref(_, inner) => self.reserve(inner, e), Type::Option(inner) => match inner.base() { Type::Object(_) => format!("if !{e}.is_null() {{ {} }}", self.reserve(inner, e)), base if Abi::nullable(base) => format!("if !{e}.is_none() {{ {} }}", self.reserve(inner, e)), _ => format!("if {e}.some {{ {} }}", self.reserve(inner, &format!("{e}.value"))), }, Type::List(_, inner) => format!( "for c in unsafe {{ {e}.items(w) }} {{ {} }}", self.reserve(inner, "(*c)") ), Type::Tuple(elements) => (elements.iter().enumerate()) .filter(|(_, ty)| self.reserves(ty)) .map(|(n, ty)| self.reserve(ty, &format!("{e}._{n}"))) .collect::>() .join(" "), _ => String::new(), } } /// What an object's handle holds: its Rust type, with its lifetimes `'static`, wrapped /// in `rt::Unsent` if it is not `Send`, which keeps it on its thread. fn object_path(&self, i: usize) -> String { match self.abi.api.objects[i].send { true => self.rust(&Type::Object(i)), false => format!("rt::Unsent<{}>", self.rust(&Type::Object(i))), } } /// A new handle owning `e`, a value of object `i`'s type. fn new_object(&self, i: usize, e: &str) -> String { match self.abi.api.objects[i].send { true => format!("rt::Object::new({e})"), false => format!("rt::Object::unsent({e})"), } } /// The Rust spelling of a type a callback's method takes or returns, or a type /// argument: a type with its instance's arguments, and an object's lifetimes `'static`. fn rust(&self, ty: &Type) -> String { let api = self.abi.api; let generic = |path: &[String], lifetimes: usize, args: &[Type]| { let path = self.path(path); let mut params = vec!["'static".to_string(); lifetimes]; params.extend(args.iter().map(|arg| self.rust(arg))); match params.is_empty() { true => path, false => format!("{path}::<{}>", params.join(", ")), } }; match ty { Type::Unit => "()".to_string(), Type::Bool => "bool".to_string(), Type::Int(int) => int.rust().to_string(), Type::NonZero(int) => format!("::std::num::NonZero<{}>", int.rust()), Type::U128 => "u128".to_string(), Type::F32 => "f32".to_string(), Type::F64 => "f64".to_string(), Type::Char => "char".to_string(), Type::Str => "str".to_string(), Type::String => "::std::string::String".to_string(), Type::Slice => "[u8]".to_string(), Type::Vec => "::std::vec::Vec".to_string(), Type::Bytes => "::bytes::Bytes".to_string(), Type::Duration => "::std::time::Duration".to_string(), Type::Array(n) => format!("[u8; {n}]"), Type::Path => "::std::path::Path".to_string(), Type::PathBuf => "::std::path::PathBuf".to_string(), Type::SocketAddr(Family::Any) => "::std::net::SocketAddr".to_string(), Type::SocketAddr(Family::V4) => "::std::net::SocketAddrV4".to_string(), Type::SocketAddr(Family::V6) => "::std::net::SocketAddrV6".to_string(), Type::IpAddr(Family::Any) => "::std::net::IpAddr".to_string(), Type::IpAddr(Family::V4) => "::std::net::Ipv4Addr".to_string(), Type::IpAddr(Family::V6) => "::std::net::Ipv6Addr".to_string(), Type::List(c, inner) => match (c, &**inner) { // A map's items are its key and value. (Collection::BTreeMap | Collection::JsonMap, Type::Tuple(pair)) => { format!("{}<{}, {}>", collection(*c), self.rust(&pair[0]), self.rust(&pair[1])) } (Collection::Box, _) => format!("{}<[{}]>", collection(*c), self.rust(inner)), (Collection::Slice, _) => format!("[{}]", self.rust(inner)), (Collection::Array(n), _) => format!("[{}; {n}]", self.rust(inner)), (Collection::IterRef, Type::String) => format!("{}<&str>", collection(*c)), (Collection::IterRef, _) => format!("{}<&[u8]>", collection(*c)), _ => format!("{}<{}>", collection(*c), self.rust(inner)), }, Type::Option(inner) => format!("Option<{}>", self.rust(inner)), // A trailing comma keeps a tuple of one element a tuple. Type::Tuple(elements) => format!( "({},)", elements.iter().map(|ty| self.rust(ty)).collect::>().join(", ") ), Type::Ref(Mutability::Not, inner) => format!("&{}", self.rust(inner)), Type::Ref(Mutability::Mut, inner) => format!("&mut {}", self.rust(inner)), Type::Record(i) => match &api.records[*i].opaque { Some(opaque) if opaque.foreign => foreign_path(&api.records[*i].path), Some(opaque) => generic(&api.records[*i].path, opaque.lifetimes, &api.records[*i].args), None => generic(&api.records[*i].path, 0, &api.records[*i].args), }, Type::Enum(i) if api.enums[*i].foreign => foreign_path(&api.enums[*i].path), Type::Enum(i) => generic(&api.enums[*i].path, 0, &api.enums[*i].args), Type::Object(i) if let Some(held) = api.objects[*i].opaque => match held { Held::Arc => "rt::Opaque".to_string(), Held::Value => "rt::Owned".to_string(), }, Type::Object(i) if api.objects[*i].foreign => foreign_path(&api.objects[*i].path), Type::Object(i) => generic(&api.objects[*i].path, api.objects[*i].lifetimes, &api.objects[*i].args), // A negative argument is an expression, which Rust takes only in braces. Type::Const(Literal::Int(_, v)) if *v < 0 => format!("{{{v}}}"), Type::Const(value) => value.to_string(), other => unreachable!("no callback or type argument is {other:?}"), } } /// The Rust value of type `ty` that the C value `e` holds. `take` is the Rust `bool` /// whether `e` was handed over, as a value passed by value is, so a buffer in it with an /// owner is taken without a copy; else what it holds is copied out, and it stays the /// caller's. fn lift(&self, ty: &Type, e: &str, take: &str) -> String { match ty { Type::Const(_) => unreachable!("a const argument is no value"), Type::Unit => e.to_string(), Type::Bool | Type::Int(_) | Type::F32 | Type::F64 => e.to_string(), Type::NonZero(_) => { format!("::std::num::NonZero::new({e}).unwrap_or_else(|| rt::fail(w, \"a NonZero is zero\"))") } Type::Char => format!("rt::char({e}, w)"), Type::Str | Type::String => format!("unsafe {{ {e}.string_in(w, {take}) }}"), Type::Slice | Type::Vec => format!("unsafe {{ {e}.vec_in(w, {take}) }}"), Type::Bytes => format!("unsafe {{ {e}.bytes_in(w, {take}) }}"), Type::Path | Type::PathBuf => format!("unsafe {{ {e}.path_in(w, {take}) }}"), Type::U128 => format!("{e}.lift()"), Type::Array(_) => format!("{e}.items"), Type::Duration => format!("{e}.lift(w)"), Type::SocketAddr(_) | Type::IpAddr(_) => format!("{e}.lift()"), Type::Waker => format!("{e}.lift()"), Type::Future | Type::Reader => format!("{e}.lift(w)"), Type::Jni(_) => unreachable!("a JNI value is lent by `function`"), // A block is copied out whole. Type::List(c, _) if ty.block().is_some() => format!( "{}::from(unsafe {{ {e}.items(w) }})", match c { Collection::Box => "::std::boxed::Box::<[_]>", // A slice no call borrows in place is lifted into a `Vec`, lent as one. _ => "::std::vec::Vec::<_>", } ), // An iterator iterates the `Vec` of its items. Type::List(Collection::Iter, inner) => format!( "::std::iter::IntoIterator::into_iter({})", self.lift(&Type::List(Collection::Vec, inner.clone()), e, take) ), Type::List(Collection::IterRef, _) => unreachable!("an iterator of borrows is lent by `arg`"), Type::List(c, inner) => { let collected = match c { Collection::BTreeMap | Collection::JsonMap => format!("{}<_, _>", collection(*c)), Collection::Box => format!("{}<[_]>", collection(*c)), Collection::Vec | Collection::Slice => format!("{}<_>", collection(Collection::Vec)), Collection::BTreeSet => format!("{}<_>", collection(*c)), Collection::Array(_) | Collection::Iter | Collection::IterRef => { unreachable!("{c:?} is lifted apart") } }; // An array's items are behind its pointer, so only read. let lifted = self.lift(inner, "(*c)", "false"); // A failed check inside returns from the closure each item lifts in. let fails = !self.reserved.get() && (self.checks(inner) || self.moves(inner)); match self.checking.get() && fails { true => format!( "unsafe {{ {e}.items(w) }}.iter().map(|c| -> ::std::result::Result<_, rt::Status> {{ Ok({lifted}) }}).collect::<::std::result::Result<{collected}, rt::Status>>()?" ), false => format!("unsafe {{ {e}.items(w) }}.iter().map(|c| {lifted}).collect::<{collected}>()"), } } // A foreign enum's option converts into its filter, as `tracing::LevelFilter`. Type::Option(inner) if self.filtered(inner) => format!( "::std::convert::From::from(if {e}.some {{ Some({}) }} else {{ None }})", self.lift(inner, &format!("{e}.value"), take) ), Type::Option(inner) => match inner.base() { Type::Object(_) => format!( "if {e}.is_null() {{ None }} else {{ Some({}) }}", self.lift(inner, e, take) ), base if Abi::nullable(base) => { format!( "if {e}.is_none() {{ None }} else {{ Some({}) }}", self.lift(inner, e, take) ) } _ => format!( "if {e}.some {{ Some({}) }} else {{ None }}", self.lift(inner, &format!("{e}.value"), take) ), }, Type::Box(inner) => format!("::std::boxed::Box::new({})", self.lift(inner, e, take)), Type::Ref(_, inner) => self.lift(inner, e, take), // Reserved, its checked copies passed their checks. Type::Record(i) if self.reserved.get() && self.reserves(ty) => { format!("lift_{}_taken(&{e}, w, {take})", self.abi.record(*i)) } Type::Enum(i) if self.reserved.get() && self.reserves(ty) => { format!("lift_{}_taken(&{e}, w, {take})", self.abi.enumeration(*i)) } Type::Record(i) => match self.checks(ty) { true => self.checked(&format!("lift_{}(&{e}, w, {take})", self.abi.record(*i))), false => format!("lift_{}(&{e}, w, {take})", self.abi.record(*i)), }, Type::Enum(i) => match (self.abi.api.enums[*i].plain(), self.checks(ty)) { (true, _) => format!("lift_{}({e}, w)", self.abi.enumeration(*i)), (false, true) => self.checked(&format!("lift_{}(&{e}, w, {take})", self.abi.enumeration(*i))), (false, false) => format!("lift_{}(&{e}, w, {take})", self.abi.enumeration(*i)), }, Type::Object(i) => { let object = &self.abi.api.objects[*i]; let path = match object.opaque { Some(Held::Arc) => "rt::Opaque".to_string(), Some(Held::Value) => "rt::Owned".to_string(), None => self.object_path(*i), }; let value = match (object.copy, self.reserved.get()) { // One no export can misuse is copied out uncounted, as it is borrowed. (true, _) if !self.abi.checked[*i] => format!("*unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), // Reserved, it is borrowed until the call's moves are made. (true, true) => format!("unsafe {{ rt::Object::<{path}>::held({e}) }}"), (true, _) => format!( "*{}", self.checked(&format!( "unsafe {{ rt::Object::<{path}>::{}({e}, w) }}", self.shared(*i) )) ), (false, true) => format!("unsafe {{ rt::Object::<{path}>::taken({e}) }}"), (false, false) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::take({e}, w) }}")), }; match object.opaque { Some(Held::Arc) => format!("{value}.arc(w)"), Some(Held::Value) => format!("{value}.take(w)"), None if !object.send => format!("rt::Unsent::into_inner({value})"), None => value, } } Type::Callback(i, form) => abi::dyn_wrap(*form, &format!("Foreign_{}({e})", self.abi.callback(*i))), Type::Closure(closure) => self.closure(closure, e), Type::Range(_) => format!("{e}.lift()"), Type::Bound(_) | Type::Bounds(_) => format!("{e}.lift(w)"), Type::Tuple(elements) => { // The trailing comma makes `(x,)` a tuple, not parentheses. let lifted: Vec = (elements.iter().enumerate()) .map(|(n, ty)| format!("{},", self.lift(ty, &format!("{e}._{n}"), take))) .collect(); format!("({})", lifted.concat()) } } } /// The Rust closure calling the function C implements as `e`, with its data `e_data`, /// which lends each object the closure takes as a handle for that call. A kept one frees /// its data with `e_free` once dropped. fn closure(&self, closure: &Closure, e: &str) -> String { // A failed check of what it returns has no caller to return to. let checking = self.checking.replace(false); let made = self.closure_body(closure, e); self.checking.set(checking); made } fn closure_body(&self, closure: &Closure, e: &str) -> String { let typed: Vec = (closure.params.iter().enumerate()) .map(|(n, ty)| format!("c{n}: {}", self.rust(ty))) .collect(); // Each object the closure borrows, and how it is lent. let lent = |ty: &Type| match ty { Type::Ref(m, inner) => match (&**inner, m) { (&Type::Object(i), Mutability::Mut) if !self.abi.api.objects[i].send => Some((i, "lend_mut_unsent")), (&Type::Object(i), Mutability::Mut) => Some((i, "lend_mut")), (&Type::Object(i), Mutability::Not) if !self.abi.api.objects[i].send => Some((i, "lend_unsent")), (&Type::Object(i), Mutability::Not) if self.abi.api.objects[i].sync => Some((i, "lend")), (&Type::Object(i), Mutability::Not) => Some((i, "lend_local")), _ => None, }, _ => None, }; let mut args: Vec = std::iter::once("data.get()".to_string()) .chain(closure.params.iter().enumerate().map(|(n, ty)| match lent(ty) { Some(_) => format!("h{n}"), None => self.view(ty, &format!("c{n}")), })) .collect(); // A `Result` writes its value and its error through out pointers. let mut outs = String::new(); if let Some(err) = &closure.err { if closure.ok != Type::Unit { outs.push_str(&format!( "let mut out = ::std::mem::MaybeUninit::<{}>::zeroed();\n\t\t\t", rs(&self.abi.ty(&closure.ok)) )); args.push("out.as_mut_ptr()".to_string()); } outs.push_str(&format!( "let mut err = ::std::mem::MaybeUninit::<{}>::zeroed();\n\t\t\t", rs(&self.abi.ty(err)) )); args.push("err.as_mut_ptr()".to_string()); } let mut call = format!("unsafe {{ f({}) }}", args.join(", ")); for (n, ty) in closure.params.iter().enumerate().rev() { if let Some((i, lend)) = lent(ty) { call = format!("rt::Object::<{}>::{lend}(c{n}, w, |h{n}| {call})", self.object_path(i)); } } let ret = self.rust(&closure.ok); let (ret, lifted) = match &closure.err { None => (ret, self.returned(&closure.ok, "result")), Some(err) => { let ok = match closure.ok { Type::Unit => "()".to_string(), _ => self.returned(&closure.ok, "unsafe { out.assume_init() }"), }; ( format!("::std::result::Result<{ret}, {}>", self.rust(err)), format!( "match result {{\n\t\t\t\ttrue => Ok({ok}),\n\t\t\t\tfalse => Err({}),\n\t\t\t}}", self.returned(err, "unsafe { err.assume_init() }") ), ) } }; let data = match closure.kept { true => format!("rt::Kept::new({e}_data, {e}_free)"), false => format!("rt::Data::new({e}_data)"), }; format!( "{{\n\t\tlet f = {e}.unwrap_or_else(|| rt::fail(w, \"a function is null\"));\n\t\tlet data = {data};\n\t\tmove |{}| -> {ret} {{\n\t\t\t{outs}let result = {call};\n\t\t\t{lifted}\n\t\t}}\n\t}}", typed.join(", "), ) } /// The Rust value a closure C implements returned as the C value `e`, which Rust then /// owns, as a buffer `_buf_copy` made. fn returned(&self, ty: &Type, e: &str) -> String { match ty { Type::String => format!("unsafe {{ {e}.take_string(w) }}"), ty => self.lift(ty, e, "true"), } } /// Whether an option of `ty` may be a foreign enum's filter, such as `tracing::LevelFilter` /// of `tracing::Level`, which converts to and from the option. fn filtered(&self, ty: &Type) -> bool { matches!(ty, Type::Enum(i) if self.abi.api.enums[*i].foreign) } /// How a call borrows object `i` shared: across threads if it is `Sync`, else on one. fn shared(&self, i: usize) -> &'static str { match self.abi.api.objects[i].sync { true => "borrow", false => "borrow_local", } } /// The C value of the Rust value `e` of type `ty`, which the caller frees. fn lower(&self, ty: &Type, e: &str) -> String { match ty { Type::Const(_) => unreachable!("a const argument is no value"), Type::Unit => "()".to_string(), Type::Bool | Type::Int(_) | Type::F32 | Type::F64 => e.to_string(), Type::NonZero(_) => format!("({e}).get()"), Type::Char => format!("{e} as u32"), Type::Str => format!("rt::Buf::string({e}.to_owned())"), Type::String => format!("rt::Buf::string({e})"), Type::Slice => format!("rt::Buf::vec({e}.to_vec())"), Type::Vec => format!("rt::Buf::vec({e})"), Type::Bytes => format!("rt::Buf::owned({e})"), Type::Path => format!("rt::Buf::path_buf({e}.to_path_buf())"), Type::PathBuf => format!("rt::Buf::path_buf({e})"), Type::U128 => format!("rt::U128::lower({e})"), Type::Duration => format!("rt::Duration::lower({e})"), Type::Array(n) => format!("{} {{ items: {e} }}", self.abi.array(*n)), Type::SocketAddr(_) | Type::IpAddr(_) => format!("{}::lower({e})", rs(&self.abi.ty(ty))), // A block hands over its own allocation. Type::List(Collection::Box | Collection::Array(_), _) if ty.block().is_some() => { format!("rt::List::owned(::std::vec::Vec::from({e}))") } Type::List(..) if ty.block().is_some() => format!("rt::List::owned({e})"), Type::List(_, inner) => format!( "rt::List::owned(::std::iter::IntoIterator::into_iter({e}).map(|v| {}).collect())", self.lower(inner, "v") ), Type::Option(inner) => { // A foreign enum's filter, as `tracing::LevelFilter`, converts into its option. let e = &match self.filtered(inner) { true => format!("::std::option::Option::<{}>::from({e})", self.rust(inner)), false => e.to_string(), }; let none = match inner.base() { Type::Object(_) => "::std::ptr::null()".to_string(), base if Abi::is_buf(base) => "rt::Buf::NONE".to_string(), Type::List(..) => "rt::List::NONE".to_string(), _ => { let name = self.abi.option(inner); return format!( "match {e} {{ Some(v) => {name} {{ some: true, value: {} }}, None => {name} {{ some: false, value: unsafe {{ ::std::mem::zeroed() }} }} }}", self.lower(inner, "v") ); } }; format!("match {e} {{ Some(v) => {}, None => {none} }}", self.lower(inner, "v")) } Type::Box(inner) => self.lower(inner, &format!("*{e}")), Type::Ref(_, inner) => match &**inner { Type::Str | Type::Slice | Type::Path => self.lower(inner, e), Type::Object(i) if self.lends => { let lent = match self.abi.api.objects[*i].send { true => "lent", false => "lent_unsent", }; format!("rt::Object::<{}>::{lent}({e}, lender)", self.object_path(*i)) } // A handle in the clone is a new handle, and a slice's clone a `Vec`. inner => self.lower(inner, &format!("::std::borrow::ToOwned::to_owned({e})")), }, Type::Record(i) => format!("lower_{}({e})", self.abi.record(*i)), Type::Enum(i) => format!("lower_{}({e})", self.abi.enumeration(*i)), Type::Object(i) if let Some(held) = self.abi.api.objects[*i].opaque => match held { Held::Arc => format!("rt::Object::new(rt::Opaque::new({e}))"), Held::Value => format!("rt::Object::new(rt::Owned::new({e}))"), }, Type::Object(i) if self.holding.get() => { let holding = match self.abi.api.objects[*i].send { true => "holding", false => "holding_unsent", }; format!("rt::Object::{holding}({e}, Some(a_self_loan.loan()))") } Type::Object(i) => self.new_object(*i, e), Type::Callback(..) | Type::Closure(..) => unreachable!("a callback is never returned"), Type::Waker => format!("rt::Waker::lower({e})"), Type::Future | Type::Reader | Type::Jni(_) => unreachable!("{ty:?} is never returned"), Type::Range(_) => format!("rt::Range::lower({e})"), Type::Bound(_) => format!("rt::Bound::lower({e})"), Type::Bounds(_) => format!("rt::Bounds::lower({e})"), Type::Tuple(elements) => { let names: Vec = (0..elements.len()).map(|n| format!("t{n},")).collect(); let fields: Vec = (elements.iter().enumerate()) .map(|(n, ty)| format!("_{n}: {}", self.lower(ty, &format!("t{n}")))) .collect(); format!( "match {e} {{ ({}) => {} {{ {} }} }}", names.concat(), self.abi.tuple(elements), fields.join(", ") ) } } } /// What frees the parts of the C value `e` that Rust allocated, if any. fn free(&self, ty: &Type, e: &str) -> Option { match ty { _ if Abi::is_buf(ty) => Some(format!("unsafe {{ {e}.free() }};")), Type::List(_, inner) => { let each = self.free(inner, "c").unwrap_or_default(); Some(format!( "unsafe {{ {e}.free(\"{}_free\", |c| {{ let _ = c; {each} }}) }};", self.abi.list(inner) )) } Type::Option(inner) => match inner.base() { base if Abi::is_buf(base) => Some(format!("unsafe {{ {e}.free() }};")), // A null handle or array frees nothing. Type::Object(_) | Type::List(..) => self.free(inner, e), _ => self .free(inner, &format!("{e}.value")) .map(|free| format!("if {e}.some {{ {free} }}")), }, Type::Box(inner) => self.free(inner, e), Type::Tuple(elements) => { let frees: Vec = (elements.iter().enumerate()) .filter_map(|(n, ty)| self.free(ty, &format!("{e}._{n}"))) .collect(); (!frees.is_empty()).then(|| frees.join(" ")) } Type::Object(i) => Some(format!( "unsafe {{ rt::Object::<{}>::free({e}) }};", self.object_path(*i) )), Type::Record(i) if self.owns(ty) => Some(format!("free_{}({e});", self.abi.record(*i))), Type::Enum(i) if self.owns(ty) => Some(format!("free_{}({e});", self.abi.enumeration(*i))), _ => None, } } /// Whether a value of `ty` holds memory Rust allocated. fn owns(&self, ty: &Type) -> bool { match ty { _ if Abi::is_buf(ty) => true, Type::Object(_) | Type::List(..) => true, Type::Option(inner) | Type::Box(inner) => self.owns(inner), Type::Tuple(elements) => elements.iter().any(|ty| self.owns(ty)), Type::Record(i) => self.abi.api.records[*i].fields.iter().any(|f| self.owns(&f.ty)), Type::Enum(i) => self.abi.api.enums[*i] .variants .iter() .any(|v| v.fields.iter().any(|f| self.owns(&f.ty))), _ => false, } } fn lib(&mut self) { let api = self.abi.api; self.line(format!( "//! Generated by rust2-ffi: the C ABI of the pub API of {}. Do not edit.\n", self.packages )); self.line("#![allow(non_camel_case_types, non_snake_case, reason = \"the C names\")]"); self.line("#![allow(unused_unsafe, reason = \"every conversion is in its own block\")]"); self.line("#![allow(unused_parens, reason = \"each element is lifted from `(*c)`\")]"); self.line("#![allow(clippy::all, reason = \"generated\")]"); self.line("#![allow(dead_code, reason = \"the runtime serves every API\")]"); self.line("#![allow(deprecated, reason = \"a deprecated item is still the API's\")]"); self.line("#![allow(unused_mut, reason = \"a borrow is declared alike for every method\")]"); self.line("#![allow(unreachable_code, reason = \"a call returning an uninhabited type never returns\")]\n"); self.line("mod rt;\n"); self.line("#[cfg(feature = \"kotlin\")]\nmod jni;\n"); self.line("#[cfg(feature = \"python\")]\nmod python;\n"); self.line("#[cfg(feature = \"dart\")]\nmod dart;\n"); if self.web { self.line("#[cfg(all(feature = \"web\", target_arch = \"wasm32\"))]\nmod web;\n"); } // The facade may export nothing, which cargo would call unused. self.line(format!("use {} as _;\n", self.dependency)); let p = self.abi.prefix.clone(); self.line(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {p}_buf_free(buf: rt::Buf) {{\n\tunsafe {{ buf.free() }}\n}}\n" )); self.line(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {p}_buf_copy(view: rt::Buf) -> rt::Buf {{\n\trt::Buf::vec(unsafe {{ view.bytes(\"{p}_buf_copy\") }}.to_vec())\n}}\n" )); self.line(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {p}_buf_clone(buf: rt::Buf) -> rt::Buf {{\n\tunsafe {{ buf.share(\"{p}_buf_clone\") }}\n}}\n" )); self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_contract() -> u64 {{\n\t0x{:016x}\n}}\n", self.contract )); // Its index among the targets the header serves, by which a binding checks a call of // an item only some have before making it. for (i, target) in api.targets.iter().enumerate() { self.line(format!("#[cfg({CFG} = {target:?})]\nconst TARGET: u32 = {i};\n")); } self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_target() -> u32 {{\n\tTARGET\n}}\n" )); self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_status_message(status: i32) -> *const ::std::ffi::c_char {{\n\trt::Status::text(status).as_ptr()\n}}\n" )); if api.unsent() { self.line(format!( "#[unsafe(no_mangle)]\npub extern \"C\" fn {p}_thread_drain() {{\n\trt::Thread::drain_here()\n}}\n" )); self.line(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {p}_thread_wake(wake: rt::Waker) {{\n\tlet wake = wake.lift();\n\trt::Thread::wake_here(::std::sync::Arc::new(move || wake.wake_by_ref()))\n}}\n" )); } for (name, ty) in &self.abi.ranges { let (kind, int) = match ty { Type::Range(int) => ("Range", int), Type::Bound(int) => ("Bound", int), Type::Bounds(int) => ("Bounds", int), other => unreachable!("{other:?} is no range"), }; self.line(format!("pub type {name} = rt::{kind}<{}>;\n", int.rust())); } for (name, elem) in &self.abi.lists.clone() { let c = rs(&self.abi.ty(elem)); self.line(format!("pub type {name} = rt::List<{c}>;\n")); self.on = self.abi.on(elem); let free = self .free(&Type::List(Collection::Vec, Box::new(elem.clone())), "this") .expect("an array is freed"); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: {name}) {{\n\t{free}\n}}\n" )); } for (name, n) in &self.abi.arrays { self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub items: [u8; {n}],\n}}\n" )); } for (name, elements) in &self.abi.tuples.clone() { let fields: String = (elements.iter().enumerate()) .map(|(n, ty)| format!("\tpub _{n}: {},\n", rs(&self.abi.ty(ty)))) .collect(); self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n{fields}}}\n" )); } for (name, inner) in &self.abi.options.clone() { let value = rs(&self.abi.ty(inner)); self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub some: bool,\n\tpub value: {value},\n}}\n" )); } for i in 0..api.records.len() { self.record(i); } for i in 0..api.enums.len() { self.enumeration(i); } for i in 0..api.callbacks.len() { self.callback(i); } for (i, object) in api.objects.iter().enumerate() { self.on = object.on; let name = self.abi.object(i); let path = self.object_path(i); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *const ::std::ffi::c_void) {{\n\tunsafe {{ rt::Object::<{path}>::free(this) }}\n}}\n" )); // A call on a value that is not `Send` runs on its home, if it has one elsewhere. let away = |name: &str, params: &[&str]| match object.send { true => String::new(), false => { let params: Vec = params.iter().map(|p| p.to_string()).collect(); let this = params[0].split_once(": ").map_or("this", |(name, _)| name); away(name, ¶ms, &[(path.clone(), this.to_string())]) } }; if self.abi.checked[i] { let away = away( &format!("{name}_drop"), &["this: *const ::std::ffi::c_void", "status: *mut i32"], ); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_drop(this: *const ::std::ffi::c_void, status: *mut i32) {{\n\tlet w = \"{name}_drop\";\n\t{away}if let Err(s) = unsafe {{ rt::Object::<{path}>::close(this, w) }} {{\n\t\tunsafe {{ rt::failed::<()>(status, s, w) }}\n\t}}\n}}\n" )); } if object.shared { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_borrowed(this: *const ::std::ffi::c_void) -> bool {{\n\tunsafe {{ rt::Object::<{path}>::borrowed(this, \"{name}_borrowed\") }}\n}}\n" )); } let borrow = self.shared(i); // Each borrows its handles shared, which fails only as a call can. let status = self.abi.traits_status(i).then_some("status"); self.checking.set(status.is_some()); let borrowing = |e: &str| match status { Some(_) => self.checked(&format!("unsafe {{ rt::Object::<{path}>::{borrow}({e}, w) }}")), None => format!("unsafe {{ rt::Object::<{path}>::peek({e}, w) }}"), }; let borrowing: Vec = ["this", "a", "b"].into_iter().map(borrowing).collect(); self.checking.set(false); self.traits(i, object.traits, status, &borrowing); let owner = Ty::Handle { name: name.clone(), constant: false, }; for method in &object.methods { self.function(method, Some((&owner, Type::Object(i)))); } } for function in &api.functions { self.function(function, None); } } /// The functions object `i`'s std traits export, borrowing its handles `this`, `a`, /// and `b` as `borrow` does, which takes `status` if a borrow can fail. fn traits(&mut self, i: usize, traits: Traits, status: Option<&str>, borrow: &[String]) { let name = self.abi.object(i); let holder = self.object_path(i); let path = self.rust(&Type::Object(i)); let send = self.abi.api.objects[i].send; let handle = "*const ::std::ffi::c_void"; let [this, a, b] = borrow else { unreachable!("three handles") }; // Through the borrow, then its `rt::Unsent` if it is not `Send`. let deref = match send { true => "&*", false => "&**", }; let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); // Each runs on the home of its first handle's value, if that is elsewhere. let away = |function: &str, handles: &[&str]| { if send { return String::new(); } let mut params: Vec = handles.iter().map(|h| format!("{h}: {handle}")).collect(); params.extend(status.map(|s| format!("{s}: *mut i32"))); let held: Vec<(String, String)> = handles.iter().map(|h| (holder.clone(), h.to_string())).collect(); away(function, ¶ms, &held) }; if traits.clone { let clone = format!("<{path} as ::std::clone::Clone>::clone({deref}this)"); // A clone of a value holding an exclusive lend shares it, as Rust's clones of a // borrowing value share their borrow. let clone = match (self.abi.held[i], send) { (true, true) => format!("rt::Object::holding({clone}, this.loan())"), (true, false) => format!("rt::Object::holding_unsent({clone}, this.loan())"), (false, _) => self.new_object(i, &clone), }; let body = wrap(status, handle, &format!("let this = {this};\n\t{clone}")); let away = away(&format!("{name}_clone"), &["this"]); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_clone(this: {handle}{param}) -> {handle} {{\n\tlet w = \"{name}_clone\";\n\t{away}{body}\n}}\n" )); } if traits.eq { let body = wrap( status, "bool", &format!("let a = {a};\n\tlet b = {b};\n\t<{path} as ::std::cmp::PartialEq>::eq({deref}a, {deref}b)"), ); let away = away(&format!("{name}_eq"), &["a", "b"]); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: {handle}, b: {handle}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{away}{body}\n}}\n" )); } if traits.hash { let body = wrap( status, "u64", &format!("let this = {this};\n\trt::hash::<{path}>({deref}this)"), ); let away = away(&format!("{name}_hash"), &["this"]); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: {handle}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{away}{body}\n}}\n" )); } if traits.display { let body = wrap( status, "rt::Buf", &format!("let this = {this};\n\trt::Buf::string(::std::string::ToString::to_string({deref}this))"), ); let away = away(&format!("{name}_to_string"), &["this"]); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{away}{body}\n}}\n" )); } if traits.debug { let body = wrap( status, "rt::Buf", &format!("let this = {this};\n\trt::Buf::string(format!(\"{{:?}}\", {deref}this))"), ); let away = away(&format!("{name}_debug"), &["this"]); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: {handle}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{away}{body}\n}}\n" )); } if traits.default { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_default() -> {handle} {{\n\t{}\n}}\n", self.new_object(i, &format!("<{path} as ::std::default::Default>::default()")) )); if !send { self.export(homed(&format!("{name}_default"), &[], handle, &[])); } } } /// The functions every value type has: its std traits, read through a pointer, and /// `free`. fn value_traits(&mut self, name: &str, path: &str, ty: &Type, traits: Traits) { // One holding a handle a check of can fail returns the failure through a `status`. let status = self.checks(ty).then_some("status"); self.checking.set(status.is_some()); let lift = self.lift(ty, "unsafe { *this }", "false"); let lift_a = self.lift(ty, "unsafe { *a }", "false"); let lift_b = self.lift(ty, "unsafe { *b }", "false"); self.checking.set(false); let param = status.map(|s| format!(", {s}: *mut i32")).unwrap_or_default(); let c = rs(&self.abi.ty(ty)); if traits.eq { let body = wrap( status, "bool", &format!("<{path} as ::std::cmp::PartialEq>::eq(&{lift_a}, &{lift_b})"), ); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_eq(a: *const {c}, b: *const {c}{param}) -> bool {{\n\tlet w = \"{name}_eq\";\n\t{body}\n}}\n" )); } if traits.hash { let body = wrap(status, "u64", &format!("rt::hash::<{path}>(&{lift})")); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_hash(this: *const {c}{param}) -> u64 {{\n\tlet w = \"{name}_hash\";\n\t{body}\n}}\n" )); } if traits.display { let body = wrap( status, "rt::Buf", &format!("rt::Buf::string(::std::string::ToString::to_string(&{lift}))"), ); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_to_string(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_to_string\";\n\t{body}\n}}\n" )); } if traits.debug { let body = wrap( status, "rt::Buf", &format!("rt::Buf::string(format!(\"{{:?}}\", &{lift}))"), ); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_debug(this: *const {c}{param}) -> rt::Buf {{\n\tlet w = \"{name}_debug\";\n\t{body}\n}}\n" )); } if traits.default { self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_default() -> {c} {{\n\t{}\n}}\n", self.lower(ty, &format!("<{path} as ::std::default::Default>::default()")) )); } // An opaque value is `Copy`, with nothing to free. let opaque = matches!(ty, Type::Record(i) if self.abi.api.records[*i].opaque.is_some()); if !matches!(self.abi.ty(ty), Ty::Enum(_)) && !opaque { let free = self.free(ty, "unsafe { this.read() }").unwrap_or_default(); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_free(this: *mut {c}) {{\n\tif this.is_null() {{\n\t\treturn;\n\t}}\n\t{free}\n}}\n" )); } } fn record(&mut self, i: usize) { let record = &self.abi.api.records[i]; self.on = record.on; let name = self.abi.record(i); let path = self.rust(&Type::Record(i)); let ty = Type::Record(i); if let Some(opaque) = record.opaque { self.opaque(i, &opaque); return; } let mut fields = String::new(); for (n, field) in record.fields.iter().enumerate() { let _ = writeln!(fields, "\tpub f{n}: {},", rs(&self.abi.ty(&field.ty))); } self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n{fields}}}\n" )); // One holding a handle a check of can fail returns the failure. let checks = self.checks(&ty); self.checking.set(checks); let lifted: Vec = record .fields .iter() .enumerate() .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"), "take")) .collect(); self.checking.set(false); let build = match record.tuple { true => format!("{path}({})", lifted.join(", ")), false => { let fields: Vec = record .fields .iter() .zip(&lifted) .map(|(f, l)| format!("{}: {l}", ident(&f.name))) .collect(); format!("{path} {{ {} }}", fields.join(", ")) } }; let (ret, build) = match checks { true => ( format!("::std::result::Result<{path}, rt::Status>"), format!("Ok({build})"), ), false => (path.clone(), build), }; self.item(format!( "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {ret} {{\n\tlet _ = (w, take);\n\t{build}\n}}\n" )); let bindings: Vec = (0..record.fields.len()).map(|n| format!("f{n}")).collect(); let pattern = match record.tuple { true => format!("{path}({})", bindings.join(", ")), false => { let fields: Vec = record .fields .iter() .zip(&bindings) // A field named as its binding is, `f0`, takes the shorthand, which // the lint would ask for. .map(|(f, b)| match f.name == *b { true => b.clone(), false => format!("{}: {b}", ident(&f.name)), }) .collect(); format!("{path} {{ {} }}", fields.join(", ")) } }; let lowered: Vec = record .fields .iter() .enumerate() .map(|(n, f)| format!("f{n}: {}", self.lower(&f.ty, &format!("f{n}")))) .collect(); self.item(format!( "fn lower_{name}(v: {path}) -> {name} {{\n\tlet {pattern} = v;\n\t{name} {{ {} }}\n}}\n", lowered.join(", ") )); if self.owns(&ty) { let frees: Vec = record .fields .iter() .enumerate() .filter_map(|(n, f)| self.free(&f.ty, &format!("c.f{n}"))) .collect(); self.item(format!("fn free_{name}(c: {name}) {{\n\t{}\n}}\n", frees.join("\n\t"))); } if self.takes_bufs(&ty, &mut Vec::new()) { let releases: Vec = (record.fields.iter().enumerate()) .filter_map(|(n, f)| self.release(&f.ty, &format!("c.f{n}"))) .collect(); self.item(format!( "fn release_{name}(c: &{name}) {{\n\t{}\n}}\n", releases.join("\n\t") )); } if self.reserves(&ty) { self.checking.set(true); let reserves: Vec = (record.fields.iter().enumerate()) .filter(|(_, f)| self.reserves(&f.ty)) .map(|(n, f)| self.reserve(&f.ty, &format!("c.f{n}"))) .collect(); self.checking.set(false); self.reserved.set(true); let lifted: Vec = (record.fields.iter().enumerate()) .map(|(n, f)| self.lift(&f.ty, &format!("c.f{n}"), "take")) .collect(); self.reserved.set(false); let build = match record.tuple { true => format!("{path}({})", lifted.join(", ")), false => { let fields: Vec = (record.fields.iter().zip(&lifted)) .map(|(f, l)| format!("{}: {l}", ident(&f.name))) .collect(); format!("{path} {{ {} }}", fields.join(", ")) } }; self.reserved_helpers(&name, &path, &reserves.join("\n\t"), &build); } self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some((&owner, ty.clone()))); } } /// An opaque value: its bytes in a struct of `MaybeUninit` units, which any bytes a /// language hands back are, read and written unaligned, as a unit's alignment may be less than its own. fn opaque(&mut self, i: usize, opaque: &Opaque) { let record = &self.abi.api.records[i]; let name = self.abi.record(i); let path = self.rust(&Type::Record(i)); let ty = Type::Record(i); let (unit, units) = (format!("u{}", opaque.unit() * 8), opaque.units()); self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub opaque: [::std::mem::MaybeUninit<{unit}>; {units}],\n}}\n" )); self.item(format!( "const _: () = assert!(::std::mem::size_of::<{path}>() <= ::std::mem::size_of::<{name}>(), \"{name} is smaller than its Rust type\");\n" )); // A layout check alone passes a type that lost `Copy`, which `lift_` would then drop twice. self.item(format!( "const _: fn() = {{\n\tfn copy() {{}}\n\tcopy::<{path}>\n}};\n" )); self.item(format!( "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = (w, take);\n\tunsafe {{ c.opaque.as_ptr().cast::<{path}>().read_unaligned() }}\n}}\n" )); // Its size on each target, which a `rustc` that ordered its fields to leave padding // would change: the collector proved none can at this size. let sizes: Vec = (record.sizes.iter()) .map(|(target, size)| format!("\t#[cfg({CFG} = {target:?})]\n\tconst SIZE: usize = {size};\n")) .collect(); self.item(format!("impl {name} {{\n{}}}\n", sizes.join("\n"))); self.item(format!( "const _: () = assert!(::std::mem::size_of::<{path}>() == {name}::SIZE, \"{path} is not the size the export read, so it may have padding\");\n" )); // Zeroed past its own bytes. Generic, since an `impl Trait` return hides the type it // writes, which the collector checked is this. self.item(format!( "fn lower_{name}(v: T) -> {name} {{\n\tconst {{ assert!(::std::mem::size_of::() == ::std::mem::size_of::<{path}>() && ::std::mem::align_of::() == ::std::mem::align_of::<{path}>()) }};\n\tlet mut c = {name} {{ opaque: [::std::mem::MaybeUninit::zeroed(); {units}] }};\n\tunsafe {{ c.opaque.as_mut_ptr().cast::().write_unaligned(v) }};\n\tc\n}}\n" )); self.value_traits(&name, &path, &ty, record.traits); let owner = Ty::Struct(name.clone()); for method in &record.methods { self.function(method, Some((&owner, ty.clone()))); } } fn enumeration(&mut self, i: usize) { let e = &self.abi.api.enums[i]; self.on = e.on; let name = self.abi.enumeration(i); let path = self.rust(&Type::Enum(i)); let ty = Type::Enum(i); // One holding a handle a check of can fail returns the failure. let checks = self.checks(&ty); let wildcard = match e.exhaustive { true => "", false => "\n\t\t_ => unreachable!(\"a variant this export does not know\"),", }; // A foreign enum's variants are its type's constants. A variant without fields is // named with its braces, which a unit, tuple, or struct one takes alike, as a tuple // one whose fields a `cfg` leaves out needs: icu_collections' `StringListNotSorted()`. let variant = |v: &crate::ir::Variant| match e.foreign { true => abi::snake(&v.name).to_uppercase(), false => format!("{} {{}}", v.name), }; if e.plain() { let lift: Vec = (e.variants.iter()) .map(|v| { let missing = self.cfg(v.on).map(|cfg| { format!( "\n\t\t#[cfg(not({cfg}))]\n\t\t{} => rt::fail(w, \"the variant is not on this library's target\"),", v.discriminant ) }); let arm = self.arm(v.on); format!( "{arm}\n\t\t{} => {path}::{},{}", v.discriminant, variant(v), missing.unwrap_or_default() ) }) .collect(); let lower: Vec = (e.variants.iter()) .map(|v| format!("{}\n\t\t{path}::{} => {},", self.arm(v.on), variant(v), v.discriminant)) .collect(); self.item(format!( "fn lift_{name}(c: i32, w: &str) -> {path} {{\n\tmatch c {{{}\n\t\t_ => rt::fail(w, \"an enum value is invalid\"),\n\t}}\n}}\n", lift.concat() )); self.item(format!( "fn lower_{name}(v: {path}) -> i32 {{\n\tmatch v {{{}{wildcard}\n\t}}\n}}\n", lower.concat() )); } else { let mut members = String::new(); for (n, variant) in e.variants.iter().enumerate().filter(|(_, v)| !v.fields.is_empty()) { let payload = abi::payload(&name, &variant.name); let mut fields = String::new(); for (k, field) in variant.fields.iter().enumerate() { let _ = writeln!(fields, "\tpub f{k}: {},", rs(&self.abi.ty(&field.ty))); } self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {payload} {{\n{fields}}}\n" )); let _ = writeln!(members, "\tpub v{n}: {payload},"); } self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub union {name}_union {{\n{members}}}\n" )); self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub tag: i32,\n\tpub u: {name}_union,\n}}\n" )); let mut lift = String::new(); let mut lower = String::new(); let mut free = String::new(); let mut release = String::new(); for (n, variant) in e.variants.iter().enumerate() { let bindings: Vec = (0..variant.fields.len()).map(|k| format!("f{k}")).collect(); let pattern = match (variant.fields.is_empty(), variant.tuple) { (true, _) => format!("{path}::{}", variant.name), (false, true) => format!("{path}::{}({})", variant.name, bindings.join(", ")), (false, false) => { let fields: Vec = variant .fields .iter() .zip(&bindings) .map(|(f, b)| format!("{}: {b}", ident(&f.name))) .collect(); format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) } }; let arm = self.arm(variant.on); if let Some(cfg) = self.cfg(variant.on) { let _ = write!( lift, "\n\t\t#[cfg(not({cfg}))]\n\t\t{n} => rt::fail(w, \"the variant is not on this library's target\")," ); } if variant.fields.is_empty() { let lifted = match checks { true => format!("Ok({pattern})"), false => pattern.clone(), }; let _ = write!(lift, "{arm}\n\t\t{n} => {lifted},"); let _ = write!( lower, "{arm}\n\t\t{pattern} => {name} {{ tag: {n}, u: unsafe {{ ::std::mem::zeroed() }} }}," ); continue; } let payload = abi::payload(&name, &variant.name); self.checking.set(checks); let lifted: Vec = variant .fields .iter() .enumerate() .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"), "take")) .collect(); self.checking.set(false); let build = match variant.tuple { true => format!("{path}::{}({})", variant.name, lifted.join(", ")), false => { let fields: Vec = variant .fields .iter() .zip(&lifted) .map(|(f, l)| format!("{}: {l}", ident(&f.name))) .collect(); format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) } }; let build = match checks { true => format!("Ok({build})"), false => build, }; let _ = write!( lift, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{build}\n\t\t}}" ); let lowered: Vec = variant .fields .iter() .enumerate() .map(|(k, f)| format!("f{k}: {}", self.lower(&f.ty, &format!("f{k}")))) .collect(); let _ = write!( lower, "{arm}\n\t\t{pattern} => {name} {{ tag: {n}, u: {name}_union {{ v{n}: {payload} {{ {} }} }} }},", lowered.join(", ") ); let releases: Vec = (variant.fields.iter().enumerate()) .filter_map(|(k, f)| self.release(&f.ty, &format!("p.f{k}"))) .collect(); if !releases.is_empty() { let _ = write!( release, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", releases.join("\n\t\t\t") ); } let frees: Vec = variant .fields .iter() .enumerate() .filter_map(|(k, f)| self.free(&f.ty, &format!("p.f{k}"))) .collect(); if !frees.is_empty() { let _ = write!( free, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", frees.join("\n\t\t\t") ); } } let ret = match checks { true => format!("::std::result::Result<{path}, rt::Status>"), false => path.clone(), }; self.item(format!( "fn lift_{name}(c: &{name}, w: &str, take: bool) -> {ret} {{\n\tlet _ = take;\n\tmatch c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}\n}}\n" )); self.item(format!( "fn lower_{name}(v: {path}) -> {name} {{\n\tmatch v {{{lower}{wildcard}\n\t}}\n}}\n" )); if self.owns(&ty) { self.item(format!( "fn free_{name}(c: {name}) {{\n\tmatch c.tag {{{free}\n\t\t_ => {{}}\n\t}}\n}}\n" )); } if self.takes_bufs(&ty, &mut Vec::new()) { self.item(format!( "fn release_{name}(c: &{name}) {{\n\tmatch c.tag {{{release}\n\t\t_ => {{}}\n\t}}\n}}\n" )); } } if !e.plain() && self.reserves(&ty) { let mut reserve = String::new(); let mut lift = String::new(); for (n, variant) in e.variants.iter().enumerate() { let arm = self.arm(variant.on); let fields = |lifted: &[String]| match (variant.fields.is_empty(), variant.tuple) { (true, _) => format!("{path}::{}", variant.name), (false, true) => format!("{path}::{}({})", variant.name, lifted.join(", ")), (false, false) => { let fields: Vec = (variant.fields.iter().zip(lifted)) .map(|(f, l)| format!("{}: {l}", ident(&f.name))) .collect(); format!("{path}::{} {{ {} }}", variant.name, fields.join(", ")) } }; if let Some(cfg) = self.cfg(variant.on) { let missing = format!( "\n\t\t#[cfg(not({cfg}))]\n\t\t{n} => rt::fail(w, \"the variant is not on this library's target\")," ); reserve.push_str(&missing); lift.push_str(&missing); } if variant.fields.is_empty() { let _ = write!(lift, "{arm}\n\t\t{n} => {},", fields(&[])); continue; } self.checking.set(true); let reserves: Vec = (variant.fields.iter().enumerate()) .filter(|(_, f)| self.reserves(&f.ty)) .map(|(k, f)| self.reserve(&f.ty, &format!("p.f{k}"))) .collect(); self.checking.set(false); if !reserves.is_empty() { let _ = write!( reserve, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", reserves.join("\n\t\t\t") ); } self.reserved.set(true); let lifted: Vec = (variant.fields.iter().enumerate()) .map(|(k, f)| self.lift(&f.ty, &format!("p.f{k}"), "take")) .collect(); self.reserved.set(false); let _ = write!( lift, "{arm}\n\t\t{n} => {{\n\t\t\tlet p = unsafe {{ c.u.v{n} }};\n\t\t\t{}\n\t\t}}", fields(&lifted) ); } let reserve = format!("match c.tag {{{reserve}\n\t\t_ => {{}}\n\t}}"); let build = format!("match c.tag {{{lift}\n\t\t_ => rt::fail(w, \"an enum tag is invalid\"),\n\t}}"); self.reserved_helpers(&name, &path, &reserve, &build); } self.value_traits(&name, &path, &ty, e.traits); let owner = self.abi.ty(&ty); for method in &e.methods { self.function(method, Some((&owner, ty.clone()))); } } /// A record's or data enum's helpers for a moved argument holding one: `reserve_`, /// running `reserve`, which reserves into `takes` what it moves out and borrows what it /// copies, and `lift__taken`, which makes `build` of what was reserved, unchecked. fn reserved_helpers(&mut self, name: &str, path: &str, reserve: &str, build: &str) { self.item(format!( "fn reserve_{name}(c: &{name}, takes: &mut rt::Takes<'_>, w: &str) -> ::std::result::Result<(), rt::Status> {{\n\tlet _ = (c, &takes, w);\n\t{reserve}\n\tOk(())\n}}\n" )); self.item(format!( "fn lift_{name}_taken(c: &{name}, w: &str, take: bool) -> {path} {{\n\tlet _ = (w, take);\n\t{build}\n}}\n" )); } /// A callback: its table of foreign functions, and the Rust type that implements the /// trait by calling them. fn callback(&mut self, i: usize) { let callback = &self.abi.api.callbacks[i]; self.on = callback.on; let name = self.abi.callback(i); let path = self.path(&callback.path); let mut table = String::from("\tpub free: Option,\n"); let mut methods = String::new(); for method in &callback.methods { let method_name = method.name(); let c_params: Vec = std::iter::once("*mut ::std::ffi::c_void".to_string()) .chain(method.params.iter().map(|p| rs(&self.abi.ty(&p.ty)))) .collect(); let ret = rs(&self.abi.ty(&method.ok)); let _ = writeln!( table, "\tpub {method_name}: Option {ret}>,", c_params.join(", ") ); let receiver = match method.receiver { Some(Receiver::Mut) => "&mut self", _ => "&self", }; let params: Vec = method .params .iter() .enumerate() .map(|(n, p)| format!("p{n}: {}", self.rust(&p.ty))) .collect(); let args: Vec = std::iter::once("self.0.data".to_string()) .chain( method .params .iter() .enumerate() .map(|(n, p)| self.view(&p.ty, &format!("p{n}"))), ) .collect(); let _ = write!( methods, "\tfn {method_name}({receiver}{}) -> {} {{\n\t\tlet w = \"{name}_vtable.{method_name}\";\n\t\tlet f = unsafe {{ (*self.0.vtable).{method_name} }}.unwrap_or_else(|| rt::fail(w, \"the function is null\"));\n\t\tlet result = unsafe {{ f({}) }};\n\t\t{}\n\t}}\n", params.iter().map(|p| format!(", {p}")).collect::(), self.rust(&method.ok), args.join(", "), self.lift(&method.ok, "result", "true"), ); } self.line(format!("#[repr(C)]\npub struct {name}_vtable {{\n{table}}}\n")); self.line(format!( "#[repr(C)]\n#[derive(Clone, Copy)]\npub struct {name} {{\n\tpub vtable: *const {name}_vtable,\n\tpub data: *mut ::std::ffi::c_void,\n}}\n" )); // Each item apart, as each takes the target's `#[cfg]`. self.item(format!( "/// The foreign implementation, which frees its data when dropped.\npub struct Foreign_{name}({name});\n" )); self.item(format!( "// SAFETY: the callback's contract: its functions may be called from any thread.\nunsafe impl Send for Foreign_{name} {{}}\n" )); self.item(format!( "// SAFETY: as above.\nunsafe impl Sync for Foreign_{name} {{}}\n" )); self.item(format!( "impl Drop for Foreign_{name} {{\n\tfn drop(&mut self) {{\n\t\tif let Some(free) = unsafe {{ (*self.0.vtable).free }} {{\n\t\t\tunsafe {{ free(self.0.data) }};\n\t\t}}\n\t}}\n}}\n" )); self.item(format!("impl {path} for Foreign_{name} {{\n{methods}}}\n")); } /// The C value lending the Rust value `e` of type `ty` to a callback for its call. fn view(&self, ty: &Type, e: &str) -> String { match ty { Type::Ref(_, inner) => match &**inner { // An opaque value is `Copy`, lent as a copy. Type::Record(_) => self.lower(inner, &format!("*{e}")), Type::Str => format!("rt::Buf::view({e}.as_bytes())"), Type::Path => format!("rt::Buf::view({e}.as_os_str().as_encoded_bytes())"), _ => format!("rt::Buf::view({e})"), }, Type::String => format!("rt::Buf::view({e}.as_bytes())"), Type::Vec | Type::Bytes => format!("rt::Buf::view(&{e}[..])"), Type::PathBuf => format!("rt::Buf::view({e}.as_os_str().as_encoded_bytes())"), Type::Option(inner) => match &**inner { inner if Abi::is_buf(inner) => format!( "match &{e} {{ Some(v) => {}, None => rt::Buf::NONE }}", self.view(inner, "v") ), inner => format!( "match {e} {{ Some(v) => {name} {{ some: true, value: {} }}, None => {name} {{ some: false, value: unsafe {{ ::std::mem::zeroed() }} }} }}", self.view(inner, "v"), name = self.abi.option(inner) ), }, _ => self.lower(ty, e), } } /// An argument: the statement reserving each value it moves out of a handle, the /// statement binding it from its C parameter `c` to `local`, and the expression passing /// it. A future owns its arguments, so it borrows nothing from the caller. fn arg(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String, String) { if self.moves(ty) { let reserve = self.reserve(ty, c); self.reserved.set(true); let lifted = self.lift(ty, c, "true"); self.reserved.set(false); return (reserve, format!("let {local} = {lifted};"), local.to_string()); } if self.refusable(ty) { let reserve = self.reserve(ty, c); self.reserved.set(true); let (stmt, pass) = self.arg_of(ty, c, local, future); self.reserved.set(false); return (reserve, stmt, pass); } let (stmt, pass) = self.arg_of(ty, c, local, future); (String::new(), stmt, pass) } fn arg_of(&self, ty: &Type, c: &str, local: &str, future: bool) -> (String, String) { match ty { Type::Ref(m, inner) => match (&**inner, m, future) { (Type::Object(i), _, _) => { let (binding, borrow) = self.borrow(*i, *m, c, future); let object = &self.abi.api.objects[*i]; let pass = match (m, object.opaque) { (Mutability::Not, Some(_)) => format!("{local}.get(w)"), (Mutability::Mut, Some(_)) => format!("{local}.get_mut(w)"), // Through the borrow, then its `rt::Unsent`. (Mutability::Mut, None) if !object.send => format!("&mut **{local}"), (Mutability::Not, None) if !object.send => format!("&**{local}"), (Mutability::Mut, None) => format!("&mut *{local}"), (Mutability::Not, None) => format!("&*{local}"), }; (format!("{binding} {local} = {borrow};"), pass) } // Borrowed for the call: held, freeing a buffer it took once the call returns. (Type::Str, Mutability::Not, false) => ( format!("let {local} = unsafe {{ {c}.hold(w) }};"), format!("{local}.str(w)"), ), (Type::Slice, Mutability::Not, false) => ( format!("let {local} = unsafe {{ {c}.hold(w) }};"), format!("{local}.bytes()"), ), (Type::Path, Mutability::Not, false) => ( format!("let {local} = unsafe {{ {c}.hold_path(w) }};"), format!("{local}.path()"), ), // A slice of a block is borrowed in place, as bytes are. (list @ Type::List(Collection::Slice, _), Mutability::Not, false) if list.block().is_some() => { (format!("let {local} = unsafe {{ {c}.items(w) }};"), local.to_string()) } (inner, m, _) => { let (binding, pass) = match m { Mutability::Mut => ("let mut", format!("&mut {local}")), Mutability::Not => ("let", format!("&{local}")), }; (format!("{binding} {local} = {};", self.lift(inner, c, "true")), pass) } }, // An iterator of borrowed strings or bytes, each lent from its owned copy for the call. Type::List(Collection::IterRef, item) => ( format!( "let {local}_items = {};\n\tlet {local} = {local}_items.iter().map(|v| &v[..]).collect::<::std::vec::Vec<_>>();", self.lift(&Type::List(Collection::Vec, item.clone()), c, "true") ), format!("::std::iter::IntoIterator::into_iter({local})"), ), // `Some` of a borrow, lent as the borrow alone is: a null handle or buffer is `None`. Type::Option(opt) if let Type::Ref(m, inner) = &**opt => match (&**inner, future) { (Type::Object(i), _) => { let (binding, borrow) = self.borrow(*i, *m, c, future); let object = &self.abi.api.objects[*i]; let pass = match (m, object.opaque) { (Mutability::Not, Some(_)) => format!("{local}.as_deref().map(|v| v.get(w))"), (Mutability::Mut, Some(_)) => format!("{local}.as_deref_mut().map(|v| v.get_mut(w))"), (Mutability::Mut, None) if !object.send => format!("{local}.as_deref_mut().map(|v| &mut **v)"), (Mutability::Not, None) if !object.send => format!("{local}.as_deref().map(|v| &**v)"), (Mutability::Mut, None) => format!("{local}.as_deref_mut()"), (Mutability::Not, None) => format!("{local}.as_deref()"), }; ( format!("{binding} {local} = if {c}.is_null() {{ None }} else {{ Some({borrow}) }};"), pass, ) } (Type::Str | Type::Slice | Type::Path, false) => { let (hold, view) = match &**inner { Type::Str => ("hold", "v.str(w)"), Type::Slice => ("hold", "v.bytes()"), _ => ("hold_path", "v.path()"), }; ( format!( "let {local} = if {c}.is_none() {{ None }} else {{ Some(unsafe {{ {c}.{hold}(w) }}) }};" ), format!("{local}.as_ref().map(|v| {view})"), ) } // A future owns its arguments, so it lends a buffer's owned form. (Type::Str | Type::Slice | Type::Path, true) => ( format!("let {local} = {};", self.lift(ty, c, "true")), format!("{local}.as_deref()"), ), // A lifted `Vec` lends itself or, by coercion to the parameter's type, its slice, // since a borrowed `Vec` and a slice share their type. (Type::List(Collection::Vec | Collection::Slice, _), _) => { let (binding, borrow) = match m { Mutability::Mut => ("let mut", "&mut"), Mutability::Not => ("let", "&"), }; ( format!("{binding} {local} = {};", self.lift(ty, c, "true")), format!("match {borrow} {local} {{ Some(v) => Some(v), None => None }}"), ) } _ => ( format!("let {local} = {};", self.lift(ty, c, "true")), format!("{local}.as_ref()"), ), }, _ => ( format!("let {local} = {};", self.lift(ty, c, "true")), local.to_string(), ), } } /// The binding and expression borrowing object `i`'s handle `c` as `m` does. A future /// may be polled on any thread, so it holds a value that is not `Sync` exclusively. fn borrow(&self, i: usize, m: Mutability, c: &str, future: bool) -> (&'static str, String) { let exclusive = m == Mutability::Mut || (future && !self.abi.api.objects[i].sync); let borrow = match (exclusive, future) { (false, false) => self.shared(i), (true, false) => "borrow_mut", (false, true) => "borrow_arc", (true, true) => "borrow_mut_arc", }; let binding = match m { Mutability::Mut => "let mut", Mutability::Not => "let", }; // A type no export can misuse is borrowed shared for a call uncounted. if !exclusive && !future && !self.abi.checked[i] { return ( binding, format!("unsafe {{ rt::Object::<{}>::peek({c}, w) }}", self.object_path(i)), ); } ( binding, self.checked(&format!( "unsafe {{ rt::Object::<{}>::{borrow}({c}, w) }}", self.object_path(i) )), ) } /// The out pointers a call's value is written through: one per element of a tuple. fn outs(function: &Function) -> Vec { match &function.ok { Type::Unit => Vec::new(), Type::Tuple(elements) => (0..elements.len()).map(|n| format!("out{n}")).collect(), _ => vec!["out".to_string()], } } /// The statements that hand a call's `result` to C: return it, or for a fallible /// function or a tuple, write it through its out pointers or `err`, and return whether /// it succeeded. fn finish(&self, function: &Function, ok: &Type, ret: (&str, &str, &str)) -> String { let (ok_value, ready, error) = ret; let write = |ty: &Type, out: &str, v: &str| { format!( "unsafe {{ rt::write::<{}>({out}.cast(), || {}) }};", rs(&self.abi.ty(ty)), self.lower(ty, v) ) }; // The outs C passes follow what the function returns, which `ok` may own. let write_ok = match (&function.ok, ok) { (_, Type::Unit) => String::new(), (Type::Tuple(_), Type::Tuple(elements)) => { let names: Vec = (0..elements.len()).map(|n| format!("v{n}")).collect(); let writes: Vec = elements .iter() .zip(Self::outs(function)) .zip(&names) .map(|((ty, out), v)| write(ty, &out, v)) .collect(); // The trailing comma makes `(v0,)` a tuple pattern, not parentheses. format!("let ({},) = v;\n\t{}", names.join(", "), writes.join("\n\t")) } (_, ok) => write(ok, "out", "v"), }; match &function.err { None if ok_value.is_empty() => match (&function.ok, ok) { (_, Type::Unit) => "result".to_string(), (Type::Tuple(_), _) => format!("let v = result;\n\t{write_ok}"), _ => self.lower(ok, "result"), }, None => match ok { Type::Unit => format!("let _ = result;\n\t{ready}"), _ => format!("let v = result;\n\t{write_ok}\n\t{ready}"), }, Some(err) => { // What an exclusive lend fails with holds no borrow. let holding = self.holding.replace(false); let lowered = self.lower(err, "e"); self.holding.set(holding); format!( "match result {{\n\t\tOk(v) => {{\n\t\t\tlet _ = &v;\n\t\t\t{write_ok}\n\t\t\t{ready}\n\t\t}}\n\t\tErr(e) => {{\n\t\t\tunsafe {{ rt::write::<{}>(err.cast(), || {lowered}) }};\n\t\t\t{error}\n\t\t}}\n\t}}", rs(&self.abi.ty(err)), ) } } } /// An exported function, a method of `owner` if it has one: its C type, and its type. fn function(&mut self, function: &Function, owner: Option<(&Ty, Type)>) { let on = std::mem::replace(&mut self.on, function.on); self.function_on(function, owner); self.on = on; } fn function_on(&mut self, function: &Function, owner: Option<(&Ty, Type)>) { let export = self.abi.export(function, owner.as_ref().map(|(ty, _)| *ty)); let name = export.call.name.clone(); let status = export.call.status(); self.checking.set(status.is_some()); // An eager call borrows its arguments only for the call, as a synchronous one does, // unless the future it returns borrows them: it then owns them, as an `async fn`'s // future does, and holds each it lends in place until the future is dropped. let future = function.future && (!function.eager || function.holds); let mut held = Vec::new(); // Each argument's statements: those that cannot fail a check first, so what C gave // Rust, such as a kept closure or a buffer, drops if a check fails; then those that // check a handle and take nothing, then the reserved ones, lifted once every check // passed, so a call that fails a check changes nothing. A reserved argument's // buffers are freed if the call is refused before it lifts them. let mut checks = Vec::new(); let mut reserves = Vec::new(); let mut moves = Vec::new(); let mut stmts = Vec::new(); let mut releases = Vec::new(); // What gives back the exclusive lend each value a call ends held, once it returns. let mut ended = Vec::new(); let mut sort = |ty: &Type, (reserve, stmt, _): &(String, String, String)| match (reserve.is_empty(), self.checks(ty)) { (false, _) => { reserves.push(reserve.clone()); moves.push(stmt.clone()); } (true, true) => checks.push(stmt.clone()), (true, false) => stmts.push(stmt.clone()), }; let mut args = Vec::new(); let mut params = Vec::new(); let mut c_params = export.call.params.iter(); let receiver_ty = owner.as_ref().zip(function.receiver); if let Some(((_, owner_type), receiver)) = &receiver_ty { let (_, c_ty) = c_params.next().expect("a method takes self"); params.push(format!("this: {}", rs(c_ty))); match owner_type { Type::Object(i) => { let ty = match receiver { Receiver::Ref => Type::Ref(Mutability::Not, Box::new(Type::Object(*i))), Receiver::Mut => Type::Ref(Mutability::Mut, Box::new(Type::Object(*i))), Receiver::Owned => Type::Object(*i), }; let made = match receiver { // The receiver's exclusive borrow, kept alive past the call by the // handle it returns, as Rust keeps it while that value lives. Receiver::Mut if function.lends_mut => { let (_, borrow) = self.borrow(*i, Mutability::Mut, "this", true); let pass = match self.abi.api.objects[*i].send { true => "a_self", false => "&mut **a_self", }; ( String::new(), format!( "let mut a_self_loan = {borrow};\n\tlet a_self = unsafe {{ a_self_loan.lend() }};" ), pass.to_string(), ) } Receiver::Owned if self.ends(function, *i) => { ended.push(format!( "unsafe {{ rt::Object::<{}>::end(this) }};", self.object_path(*i) )); self.ending(*i, "this", "a_self") } _ => self.arg(&ty, "this", "a_self", future), }; if function.ties && self.abi.held[*i] { sort( &Type::Object(*i), &(String::new(), self.unheld(*i, "this"), String::new()), ); } sort(&ty, &made); // Through each `Deref` to the type whose method it is, as Rust's auto-deref. let deref = match receiver { Receiver::Mut => "::std::ops::DerefMut::deref_mut", _ => "::std::ops::Deref::deref", }; args.push((0..function.derefs).fold(made.2, |pass, _| format!("{deref}({pass})"))); if function.holds && *receiver != Receiver::Owned { held.push("a_self".to_string()); } } value => { if function.holds && *receiver != Receiver::Owned { held.push("a_self".to_string()); } // Read through a pointer, so the caller keeps it, and for `&mut self` // written back through it once the call returns. let binding = match receiver { Receiver::Mut => "let mut", _ => "let", }; let made = ( String::new(), format!("{binding} a_self = {};", self.lift(value, "unsafe { *this }", "false")), String::new(), ); sort(value, &made); args.push(match receiver { Receiver::Owned => "a_self".to_string(), Receiver::Ref => "&a_self".to_string(), Receiver::Mut => "&mut a_self".to_string(), }); } } } // Each parameter's place among Rust's after `self`, past the `()` ones C does not // pass and a future's context, which is the poll's. let omitted = |at: &usize| { function.units.contains(at) || (function.future && function.task.as_ref().is_some_and(|task| task.at == *at)) }; let mut places = (0..).filter(|at| !omitted(at)); // The env Kotlin's glue passes, which every JNI value the call takes is of. if let Some(k) = function.params.iter().position(|p| p.ty == Type::Jni(Jni::Env)) { let guard = format!( "let mut jni_guard = unsafe {{ ::{}::AttachGuard::from_unowned(p{k}.cast()) }};\n\tlet jni_env = jni_guard.borrow_env_mut();", reached("jni") ); sort(&Type::Jni(Jni::Env), &(String::new(), guard, String::new())); } for (n, param) in function.params.iter().enumerate() { let place = places.next().expect("a place for each parameter"); let (_, c_ty) = c_params.next().expect("a C parameter for each"); params.push(format!("p{n}: {}", rs(c_ty))); if let Type::Closure(closure) = ¶m.ty { let (_, data) = c_params.next().expect("a lent function's data"); params.push(format!("p{n}_data: {}", rs(data))); if closure.kept { let (_, free) = c_params.next().expect("a kept function's free"); params.push(format!("p{n}_free: {}", rs(free))); } } let made = match &function.task { // A context made of C's waker, lent for the call. Some(Task { at, kind: TaskKind::Context(m), }) if *at == place && !function.future => { let (binding, pass) = match m { Mutability::Mut => ("let mut", format!("&mut a{n}")), Mutability::Not => ("let", format!("&a{n}")), }; ( String::new(), format!( "let a{n}_waker = p{n}.lift();\n\t{binding} a{n} = ::std::task::Context::from_waker(&a{n}_waker);" ), pass, ) } _ if param.ty == Type::Jni(Jni::Env) => (String::new(), String::new(), "&mut *jni_env".to_string()), _ if param.ty == Type::Jni(Jni::Object) => ( String::new(), format!( "let a{n} = unsafe {{ ::{}::objects::JObject::from_raw(&*jni_env, p{n}.cast()) }};", reached("jni") ), format!("a{n}"), ), _ if param.interned && *param.ty.base() == Type::Slice => ( String::new(), format!("let a{n} = rt::intern_bytes(unsafe {{ p{n}.hold(w) }}.bytes());"), format!("a{n}"), ), _ if param.interned => ( String::new(), format!("let a{n} = rt::intern(unsafe {{ p{n}.hold(w) }}.str(w));"), format!("a{n}"), ), // A future made of a `Poll` owns a future C implements, and lends it pinned to // each poll. Some(_) if function.future && param.ty == Type::Future => ( String::new(), format!("let mut a{n} = p{n}.lift(w);"), format!("::std::pin::Pin::new(&mut a{n})"), ), _ => { let lent = match ¶m.ty { Type::Option(inner) => matches!(**inner, Type::Ref(..)), ty => matches!(ty, Type::Ref(..)), }; if function.holds && lent { held.push(format!("a{n}")); } // A handle an exclusive lend may have returned, as C passes one: by value, // borrowed, or as `Some` of either. let top = match ¶m.ty { Type::Option(inner) => inner, ty => ty, }; let held_object = match top { Type::Ref(_, inner) => match **inner { Type::Object(j) => Some(j), _ => None, }, Type::Object(j) => Some(*j), _ => None, } .filter(|&j| self.abi.held[j]); if let Some(j) = held_object && function.ties { sort( &Type::Object(j), &(String::new(), self.unheld(j, &format!("p{n}")), String::new()), ); } let made = match (¶m.ty, held_object) { (Type::Object(j), Some(_)) if self.ends(function, *j) => { ended.push(format!( "unsafe {{ rt::Object::<{}>::end(p{n}) }};", self.object_path(*j) )); self.ending(*j, &format!("p{n}"), &format!("a{n}")) } _ => self.arg(¶m.ty, &format!("p{n}"), &format!("a{n}"), future), }; if !made.0.is_empty() && let Some(release) = self.release(¶m.ty, &format!("p{n}")) { releases.push(release); } made } }; if !made.1.is_empty() { sort(¶m.ty, &made); } args.push(made.2); } drop(sort); let mut stmts = { let mut all = stmts; all.extend(checks); let reserved = !reserves.is_empty(); // Only a call with a status returns on a failed check; one without aborts. let release = (status.is_some() && !releases.is_empty()).then(|| releases.join(" ")); if reserved { all.insert( 0, "let mut reserved = rt::Takes::default();\n\tlet takes = &mut reserved;".to_string(), ); if let Some(release) = &release { all.insert(0, format!("let unlifted = rt::Unlifted::new(|| {{ {release} }});")); } all.extend(reserves); all.push("takes.commit();".to_string()); if release.is_some() { all.push("unlifted.lifted();".to_string()); } } all.extend(moves); // The copies are made, so their borrows end before the call runs. if reserved { all.push("drop(reserved);".to_string()); } all }; // Each owned argument the call lends moves to the heap, where it stays while the // future borrows it, and the call reborrows it through the reference it is now. for local in &held { stmts.push(format!( "let {local}_held = rt::Pinned::new({local});\n\tlet {local} = unsafe {{ {local}_held.get() }};" )); } for arg in &mut args { for local in &held { *arg = deref_local(arg, local); } } // A future made of a `Poll` calls the function at each poll, passing the poll's // context, or a value made from its waker and kept until the next poll, so what that // poll registers stays registered. let mut made = String::new(); // Each argument C does not pass, by its place among Rust's parameters after `self`, // inserted in order so each lands where Rust takes it. let mut extra: Vec<(usize, String)> = function.units.iter().map(|&at| (at, "()".to_string())).collect(); if let Some(task) = function.task.as_ref().filter(|_| function.future) { let pass = match &task.kind { TaskKind::Context(Mutability::Mut) => "&mut *t_cx".to_string(), TaskKind::Context(Mutability::Not) => "&*t_cx".to_string(), TaskKind::Made { object, make } => { made = format!( "let t_made = t_held.insert(<{}>::{make}(::std::task::Context::waker(t_cx).clone()));\n\t\t\t", self.rust(&Type::Object(*object)) ); "&*t_made".to_string() } }; extra.push((task.at, pass)); } extra.sort_by_key(|(at, _)| *at); for (at, arg) in extra { args.insert(usize::from(receiver_ty.is_some()) + at, arg); } // Rust writes back what the call left in each value C passed for `&mut`, on every // path, errors included, and in a value's `&mut self`, in place. let mut writes: String = match &receiver_ty { Some(((_, value), Receiver::Mut)) if !matches!(value, Type::Object(_)) => { format!("\n\tunsafe {{ this.write({}) }};", self.lower(value, "a_self")) } _ => String::new(), }; writes += &(function.params.iter().enumerate()) .filter(|(_, param)| param.ty.written()) .map(|(n, param)| { let Type::Ref(_, inner) = ¶m.ty else { unreachable!("a written parameter is `&mut`") }; let out = Abi::written(function, &crate::abi::escape(¶m.name)); format!( "\n\tunsafe {{ rt::write::<{}>({out}.cast(), || {}) }};", rs(&self.abi.ty(inner)), self.lower(inner, &format!("a{n}")) ) }) .collect::(); let mut callee = match &owner { Some((_, owner_type)) => format!("<{}>::{}", self.rust(owner_type), ident(function.name())), None => self.path(&function.path), }; let mut turbofish = String::new(); if let Some(instance) = function.instance.as_ref().filter(|i| !i.args.is_empty()) { let args: Vec = (instance.args.iter()) .map(|ty| ty.as_ref().map_or_else(|| "_".to_string(), |ty| self.rust(ty))) .collect(); turbofish = format!("::<{}>", args.join(", ")); callee = format!("{callee}{turbofish}"); } let call = match function.access { // A method of the type the receiver derefs to, which the glue need not name: its // receiver is that type's already, whose inherent method a call finds first. Access::Call if function.derefs > 0 => { format!( "({}).{}{turbofish}({})", args[0], ident(function.name()), args[1..].join(", ") ) } Access::Call => format!("{callee}({})", args.join(", ")), Access::Field { set: false, .. } => format!("&({}).{}", args[0], ident(function.name())), Access::Field { set: true, .. } => format!("({}).{} = {}", args[0], ident(function.name()), args[1]), Access::Const => callee, Access::Parse => format!( "<{} as ::std::str::FromStr>::from_str({}).map_err(|e| ::std::string::ToString::to_string(&e))", self.rust(&owner.as_ref().expect("a parse has its type").1), args[0] ), Access::Text => format!("::std::string::ToString::to_string({})", args[0]), }; if function.lends { let Some((_, Type::Object(i))) = &owner else { unreachable!("a lent return is a method of an object's") }; let lender = match self.abi.api.objects[*i].sync { true => "lender", false => "lender_local", }; stmts.push(format!( "let lender = unsafe {{ rt::Object::<{}>::{lender}(this, w) }};", self.object_path(*i) )); } let body = stmts.join("\n\t"); // The handles it takes of values that is not `Send`, the first present of which // decides the thread it runs on; a JNI env is its thread's, so a call taking one // stays on that thread. let mut unsent: Vec<(String, String)> = Vec::new(); if !function.jni() { if let Some(((_, Type::Object(i)), _)) = &receiver_ty && !self.abi.api.objects[*i].send { unsent.push((self.object_path(*i), "this".to_string())); } for (n, param) in function.params.iter().enumerate() { if let Some(i) = self.unsent_object(¶m.ty) { unsent.push((self.object_path(i), format!("p{n}"))); } } } let away = |name: &str, params: &[String]| away(name, params, &unsent); if let Some(abi::Future { start, poll, free, .. }) = &export.future { // A function that is not `async` runs when called, and what it returns // borrows nothing, so it is called now; an `async fn` runs when polled. let (now, later) = match (function.eager, &function.task) { (true, _) if function.holds => { let held: String = held.iter().map(|local| format!("{local}_held, ")).collect(); ( format!("\n\tlet future = rt::Holding::new({call}, ({held}));"), "future".to_string(), ) } (true, _) => (format!("\n\tlet future = {call};"), "future".to_string()), (false, None) => (String::new(), call), (false, Some(task)) => { let held = match task.kind { TaskKind::Made { .. } => "let mut t_held = None;\n\t\t", TaskKind::Context(_) => "", }; ( String::new(), format!( "{{\n\t\t{held}::std::future::poll_fn(move |t_cx: &mut ::std::task::Context<'_>| {{\n\t\t\t{made}{call}\n\t\t}})\n\t\t}}" ), ) } }; // The value C takes is lowered when it polls, outside the future, so a result // borrowing what the future holds is cloned before the future ends. let (ok, own) = owned(&function.ok); let later = match (own, &function.err) { (None, _) => format!("{later}.await"), (Some(own), Some(_)) => format!("{later}.await.map(|v| {own})"), (Some(own), None) => format!("{{ let v = {later}.await; {own} }}"), }; // A `String` error may stand for another crate's, which may borrow what the // future owns, so it becomes its message before the future ends. let later = match &function.err { Some(Type::String) => format!("{later}.map_err(|e| ::std::string::ToString::to_string(&e))"), _ => later, }; let bind: String = Self::outs(function) .iter() .enumerate() .map(|(n, out)| format!("let {out} = outs[{n}];\n\t\t\t")) .collect(); // The make's body, emitted again inside `_start` so its aborts name the // function C called. let make = format!( "{body}{now}\n\trt::Future::{}(async move {{ {later} }}, move |result, outs: &[*mut ::std::ffi::c_void], err: *mut ::std::ffi::c_void| -> i32 {{\n\t\tlet _ = (outs, err);\n\t\t{bind}{}\n\t}})", match function.send { true => "new", false => "unsent", }, self.finish(function, &ok, ("v", "rt::READY", "rt::ERROR")) ); self.checking.set(false); let status_param = status.map(|status| format!("{status}: *mut i32")); let make_params: Vec = params.iter().chain(&status_param).cloned().collect(); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> *mut rt::Future {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}{}\n}}\n", make_params.join(", "), away(&name, &make_params), wrap(status, "*mut rt::Future", &make) )); if export.homed { self.export(homed(&name, &make_params, "*mut rt::Future", &unsent)); } let poll_params: Vec = poll.params[2..] .iter() .map(|(n, ty)| format!("{n}: {}", rs(ty))) .collect(); let outs: Vec = Self::outs(function).iter().map(|out| format!("{out}.cast()")).collect(); let outs = outs.join(", "); let err = match function.err { Some(_) => "err.cast()", None => "::std::ptr::null_mut()", }; // Only a poll of a future that is not `Send` can fail, on another thread. let poll_status = poll.status().unwrap_or("::std::ptr::null_mut()"); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}(future: *mut rt::Future, waker: rt::Waker{}) -> i32 {{\n\tunsafe {{ rt::Future::poll(future, waker, &[{outs}], {err}, {poll_status}, \"{}\") }}\n}}\n", poll.name, poll_params.iter().map(|p| format!(", {p}")).collect::(), poll.name )); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}(future: *mut rt::Future) {{\n\tunsafe {{ rt::Future::free(future) }}\n}}\n", free.name )); // The call and its first poll in one: most futures are ready at once. A failed // check writes its status and returns before `future` is written. It runs on the // thread that made it, so takes no status of its poll's. let poll_outs = poll_params.len() - usize::from(poll.status().is_some()); let start_params: Vec = (params.iter().cloned()) .chain(poll_params.into_iter().take(poll_outs)) .chain(["future: *mut *mut rt::Future".to_string()]) .chain(status_param) .collect(); let started = format!( "let made = {{\n\t{make}\n\t}};\n\tunsafe {{ rt::Future::start(made, &[{outs}], {err}, future, w) }}" ); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {}({}) -> i32 {{\n\tlet w = \"{}\";\n\tlet _ = w;\n\t{}{}\n}}\n", start.name, start_params.join(", "), start.name, away(&start.name, &start_params), wrap(status, "i32", &started) )); if export.homed { self.export(homed(&start.name, &start_params, "i32", &unsent)); } return; } let outs: Vec = export.call.params[params.len()..] .iter() .map(|(n, ty)| format!("{n}: {}", rs(ty))) .collect(); params.extend(outs); let ret = rs(&export.call.ret); self.lends = function.lends; self.holding.set(function.lends_mut); let finish = match function.err { Some(_) => self.finish(function, &function.ok, ("v", "true", "false")), None => self.finish(function, &function.ok, ("", "", "")), }; self.lends = false; self.holding.set(false); let ended: String = ended.iter().map(|end| format!("\n\t{end}")).collect(); let call = match (function.collect, &function.err, &function.access) { // An iterator's items, all taken now. (true, ..) => format!("::std::iter::Iterator::collect::<::std::vec::Vec<_>>({call})"), // A parse's error is its message already. (_, _, Access::Parse) => call, // A `String` error may stand for another crate's, as its message. (_, Some(Type::String), _) => format!("({call}).map_err(|e| ::std::string::ToString::to_string(&e))"), _ => call, }; // A Java exception the call left pending is what Kotlin gets, as from any native // method: nothing is written for Kotlin's glue to read, and the result drops with // the exception taken, since a destructor may call into Kotlin, then throws it again. let pending = match function.jni() { true => { let zero = match status { Some(_) => "Ok(unsafe { ::std::mem::zeroed() })", None => "unsafe { ::std::mem::zeroed() }", }; format!( "\n\tif let Some(thrown) = jni_env.exception_occurred() {{\n\t\tjni_env.exception_clear();\n\t\tdrop(result);\n\t\tlet _ = jni_env.throw(thrown);\n\t\tif !jni_env.exception_check() {{\n\t\t\t::std::process::abort();\n\t\t}}\n\t\treturn {zero};\n\t}}" ) } false => String::new(), }; self.checking.set(false); self.export(format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}({}) -> {ret} {{\n\tlet w = \"{name}\";\n\tlet _ = w;\n\t{}{}\n}}\n", params.join(", "), away(&name, ¶ms), wrap(status, &ret, &format!("{body}\n\tlet result = {call};{ended}{pending}{writes}\n\t{finish}")) )); if export.homed { self.export(homed(&name, ¶ms, &ret, &unsent)); } } /// Whether a call of `function` ends a value of object `i` it takes by value at its top: /// none of the value's lifetimes names anything else in its signature, so the value, and /// any exclusive lend it holds, cannot outlive a synchronous call. fn ends(&self, function: &Function, i: usize) -> bool { self.abi.held[i] && !function.ties && !function.future && function.access == Access::Call } /// An argument of object `i` taken by value from its handle `c`, which may hold an /// exclusive lend, as [`Self::arg`] makes one: the call it moves into gives the lend back /// once it returns, as `end` does. fn ending(&self, i: usize, c: &str, local: &str) -> (String, String, String) { let reserve = self.checked(&format!( "unsafe {{ rt::Object::<{}>::reserve_ending({c}, takes, w) }}", self.object_path(i) )) + ";"; self.reserved.set(true); let lifted = self.lift(&Type::Object(i), c, "true"); self.reserved.set(false); (reserve, format!("let {local} = {lifted};"), local.to_string()) } /// The check that object `i`'s handle `c` holds no exclusive lend, for a call whose /// signature would carry its borrow past the value. fn unheld(&self, i: usize, c: &str) -> String { self.checked(&format!( "unsafe {{ rt::Object::<{}>::unheld({c}, w) }}", self.object_path(i) )) + ";" } /// The object that is not `Send` whose handle an argument of `ty` is at its top: by /// value, borrowed, or as `Some` of either. fn unsent_object(&self, ty: &Type) -> Option { match ty { Type::Object(i) if !self.abi.api.objects[*i].send => Some(*i), Type::Ref(_, inner) | Type::Option(inner) => match &**inner { Type::Object(i) if !self.abi.api.objects[*i].send => Some(*i), Type::Ref(_, inner) => self.unsent_object(inner), _ => None, }, _ => None, } } } /// The body of a C function returning `ret`, which runs `body`, statements then the value /// returned: if it takes `status`, as a closure whose failed check `?` returns, written /// through `status`. fn wrap(status: Option<&str>, ret: &str, body: &str) -> String { let Some(status) = status else { return body.to_string(); }; format!( "let checked = (|| -> ::std::result::Result<{ret}, rt::Status> {{\n\t{body_ok}\n\t}})();\n\tmatch checked {{\n\t\tOk(v) => v,\n\t\tErr(s) => unsafe {{ rt::failed({status}, s, w) }},\n\t}}", body_ok = ok_tail(body) ) } /// The names of a C function's parameters, `name: type` each, as its arguments. fn names(params: &[String]) -> String { let names: Vec<&str> = (params.iter()) .map(|p| p.split_once(": ").map_or(p.as_str(), |(name, _)| name)) .collect(); names.join(", ") } /// The value that is not `Send` the first present of `handles`, each an object's holder /// and a handle, stays on: its thread's record, if any is present. fn owner(handles: &[(String, String)]) -> String { let homes: Vec = (handles.iter()) .map(|(holder, handle)| format!("unsafe {{ rt::Object::<{holder}>::owner({handle}) }}")) .collect(); (homes.iter().skip(1)).fold(homes[0].clone(), |chain, next| format!("{chain}.or_else(|| {next})")) } /// The statement that runs the C function `name` taking `params` on the home that the /// value of the first present of `handles` stays on, if that is another thread, calling it /// again there and returning what it returns: so a call on a value that is not `Send` from /// a binding with no thread that stays put runs on the value's thread. Nothing if it takes /// no such handle. fn away(name: &str, params: &[String], handles: &[(String, String)]) -> String { if handles.is_empty() { return String::new(); } format!( "if let Some(home) = {}.filter(|home| home.away()) {{\n\t\treturn unsafe {{ home.run(move || {name}({})) }};\n\t}}\n\t", owner(handles), names(params) ) } /// The twin of the C function `name` taking `params` and returning `ret`, which runs it on /// a home, a thread Rust owns, which the value that is not `Send` it makes stays on. When /// one of `handles`, each optional, is present, it runs where that value stays instead, as /// the function itself does. fn homed(name: &str, params: &[String], ret: &str, handles: &[(String, String)]) -> String { let present = match handles.is_empty() { true => String::new(), false => format!( "if {}.is_some() {{\n\t\treturn unsafe {{ {name}({}) }};\n\t}}\n\t", owner(handles), names(params) ), }; format!( "#[unsafe(no_mangle)]\npub unsafe extern \"C\" fn {name}_homed({}) -> {ret} {{\n\t{present}let home = rt::Thread::home();\n\tlet made = unsafe {{ home.run(move || {name}({})) }};\n\trt::Thread::release(home);\n\tmade\n}}\n", params.join(", "), names(params) ) } /// `body`, statements then a value, with the value wrapped in `Ok`. fn ok_tail(body: &str) -> String { format!("Ok({{\n\t{body}\n\t}})") } /// What a function returns, owned: each reference at its top or in a returned tuple as /// its clone's type, with the expression cloning `v` into it, if any is one. fn owned(ok: &Type) -> (Type, Option) { let one = |ty: &Type| match ty { Type::Ref(_, inner) => Some(match &**inner { Type::Str => Type::String, Type::Slice => Type::Vec, Type::Path => Type::PathBuf, Type::List(Collection::Slice, item) => Type::List(Collection::Vec, item.clone()), inner => inner.clone(), }), _ => None, }; match ok { Type::Tuple(elements) if elements.iter().any(|e| one(e).is_some()) => { let names: Vec = (0..elements.len()).map(|n| format!("v{n}")).collect(); let owned: Vec = (elements.iter().zip(&names)) .map(|(e, n)| match one(e) { Some(_) => format!("::std::borrow::ToOwned::to_owned({n})"), None => n.clone(), }) .collect(); let ty = Type::Tuple(elements.iter().map(|e| one(e).unwrap_or_else(|| e.clone())).collect()); let own = format!("{{ let ({},) = v; ({},) }}", names.join(", "), owned.join(", ")); (ty, Some(own)) } ok => match one(ok) { Some(ty) => (ty, Some("::std::borrow::ToOwned::to_owned(v)".to_string())), None => (ok.clone(), None), }, } } #[test] fn go_module_resolves_missing_parent() { struct RemoveOnDrop(PathBuf); impl Drop for RemoveOnDrop { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } } let root = std::env::current_dir() .unwrap() .join(".tmp") .join(format!("go-module-{}", std::process::id())); let _ = std::fs::remove_dir_all(&root); std::fs::create_dir_all(root.parent().unwrap()).unwrap(); std::fs::create_dir(&root).unwrap(); let _cleanup = RemoveOnDrop(root.clone()); std::fs::write(root.join("go.mod"), "module example.com/lock\n\ngo 1.24\n").unwrap(); let output = root.join("new/../bind"); assert_eq!( go_module(&output, "lock").unwrap(), ("example.com/lock/bind/go".into(), None) ); assert!(!root.join("new").exists()); assert!(!root.join("bind").exists()); let baseline = root.join("baseline.json"); std::fs::write(&baseline, "{}").unwrap(); let mut generated = Output { names: Default::default(), files: vec![], stale: vec![output.join("go")], }; assert!(generated.check_baseline(&baseline).is_ok()); assert!(!root.join("new").exists()); assert!(!root.join("bind").exists()); #[cfg(unix)] { std::os::unix::fs::symlink(&root, root.join("alias")).unwrap(); generated.stale = vec![root.join("alias/new/..")]; assert!( generated .check_baseline(&baseline) .unwrap_err() .contains("export replaces") ); std::os::unix::fs::symlink(&baseline, root.join("baseline-link.json")).unwrap(); assert!(generated.check_baseline(&root.join("baseline-link.json")).is_err()); } } /// `expr` with each use of the local `name`, a reference, read through it as `(*name)`, so /// what it lends borrows the referent rather than the local. fn deref_local(expr: &str, name: &str) -> String { let ident = |c: char| c.is_alphanumeric() || c == '_'; let mut out = String::new(); let mut rest = expr; while let Some(at) = rest.find(name) { let (before, after) = (&rest[..at], &rest[at + name.len()..]); let whole = !before.ends_with(ident) && !after.starts_with(ident); out.push_str(before); match whole { true => out.push_str(&format!("(*{name})")), false => out.push_str(name), } rest = after; } out.push_str(rest); out } /// A second write of the same export keeps every file's mtime, and drops only the files in /// the directories it owns that it no longer writes, with any directory that leaves empty. #[test] fn rewrite_keeps_unchanged_files() { let root = std::env::current_dir() .unwrap() .join(".tmp") .join(format!("rewrite-{}", std::process::id())); let _ = std::fs::remove_dir_all(&root); let go = root.join("go"); let output = |files: &[(&str, &str)]| Output { names: Default::default(), files: files .iter() .map(|(path, text)| (root.join(path), text.to_string())) .collect(), stale: vec![go.clone()], }; let first = [ ("Cargo.toml", "a"), ("go/rt/rt.go", "rt"), ("go/old/old.go", "old"), ("go/old/deep/old.go", "old"), ]; output(&first).write().unwrap(); std::fs::write(root.join("go/rt/stray.go"), "stray").unwrap(); std::fs::write(root.join("kept.txt"), "kept").unwrap(); // Set in the past, so a rewrite would move it. let past = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1 << 30); for path in ["Cargo.toml", "go/rt/rt.go"] { let file = std::fs::File::options().write(true).open(root.join(path)).unwrap(); file.set_modified(past).unwrap(); } let mtime = |path: &str| std::fs::metadata(root.join(path)).unwrap().modified().unwrap(); output(&[("Cargo.toml", "a"), ("go/rt/rt.go", "rt"), ("go/new.go", "new")]) .write() .unwrap(); assert_eq!([mtime("Cargo.toml"), mtime("go/rt/rt.go")], [past, past]); assert_eq!(std::fs::read_to_string(root.join("go/new.go")).unwrap(), "new"); assert!(!root.join("go/old").exists(), "an emptied directory stays"); assert!( !root.join("go/rt/stray.go").exists(), "a file the export does not write stays" ); assert!( root.join("kept.txt").exists(), "a file outside the export's directories went" ); output(&[("Cargo.toml", "b")]).write().unwrap(); assert_eq!(std::fs::read_to_string(root.join("Cargo.toml")).unwrap(), "b"); assert!(!go.exists(), "the export's emptied directory stays"); // A directory it owns that is a link goes as the link, leaving what it points to. #[cfg(unix)] { let elsewhere = root.join("elsewhere"); std::fs::create_dir_all(&elsewhere).unwrap(); std::fs::write(elsewhere.join("kept.go"), "kept").unwrap(); std::os::unix::fs::symlink(&elsewhere, &go).unwrap(); output(&[("go/rt/rt.go", "rt")]).write().unwrap(); assert!(elsewhere.join("kept.go").exists(), "pruning followed a link"); assert!(!go.is_symlink() && go.join("rt/rt.go").exists()); // So does a changed file that is a link. std::fs::remove_file(go.join("rt/rt.go")).unwrap(); std::os::unix::fs::symlink(elsewhere.join("kept.go"), go.join("rt/rt.go")).unwrap(); output(&[("go/rt/rt.go", "rt")]).write().unwrap(); assert_eq!(std::fs::read_to_string(elsewhere.join("kept.go")).unwrap(), "kept"); assert!(!go.join("rt/rt.go").is_symlink()); assert_eq!(std::fs::read_to_string(go.join("rt/rt.go")).unwrap(), "rt"); // And a link left where a changed file is first written. std::os::unix::fs::symlink(elsewhere.join("kept.go"), root.join(".Cargo.toml.rust2-ffi")).unwrap(); output(&[("Cargo.toml", "linked")]).write().unwrap(); assert_eq!(std::fs::read_to_string(elsewhere.join("kept.go")).unwrap(), "kept"); assert!(!root.join("Cargo.toml").is_symlink()); assert_eq!(std::fs::read_to_string(root.join("Cargo.toml")).unwrap(), "linked"); } // A hard link to a changed file keeps its content. let linked = root.join("linked.txt"); std::fs::write(&linked, "kept").unwrap(); std::fs::remove_file(root.join("Cargo.toml")).unwrap(); std::fs::hard_link(&linked, root.join("Cargo.toml")).unwrap(); output(&[("Cargo.toml", "c")]).write().unwrap(); assert_eq!(std::fs::read_to_string(&linked).unwrap(), "kept"); assert_eq!(std::fs::read_to_string(root.join("Cargo.toml")).unwrap(), "c"); std::fs::remove_dir_all(&root).unwrap(); }