Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,19 @@ packages place it in.

- **Names** are the C names without the prefix, in a namespace of it: `moq_track_Producer`
is `moq::track_Producer`, and its method `moq_track_Producer_append_group` is
`moq::track_Producer::append_group`. A keyword, or a macro that a C or C++ system header
defines or a GNU dialect predefines, as `src/macros.txt` lists them (`unix`, `EOF`,
`errno`, as in moq-tokio's `tokio::unix_`), takes a trailing `_` (`counter::new_`),
`moq::track_Producer::append_group`. A keyword takes a trailing `_` (`counter::new_`),
as does a member named as its class, one of its fields, or a member a trait gives it,
and a parameter named as a member or as anything in the namespace, which it would hide.
A name already ending in `_` takes as many more as keep it apart and off every macro:
`unix_` becomes `unix__`.
A name the escape leaves alone keeps it, and one escaped onto another's takes more `_`s
until free: the values `Delete` and `Delete_` are `delete__` and `delete_`, in any order.
A name C++ reserves, one holding `__` or starting with `_` and a capital, first drops its
leading `_`s and keeps one `_` of each run: `__all__` is `all_`, and serde's parameter
`__deserializer` is `deserializer`.
A name that moves steps along its stem's names `x`, `x_`, `x_2`, `x_3`, ..., never making
`__`. So a macro that a C or C++ system header defines or a GNU dialect predefines, as
`src/macros.txt` lists them (`unix`, `EOF`, `errno`), moves to the next of its stem's
names no macro holds, keeping the stem's names in order: `unix` is `unix_`, as in
moq-tokio's `tokio::unix_`, and `unix_` is `unix_2`.
A name the escape leaves alone keeps it, and one escaped onto another's steps until free:
the values `Delete` and `Delete_` are `delete_2` and `delete_`, in any order.
- **Objects** are classes owning their handle through a `std::unique_ptr` whose deleter
calls `_free`, so they move and never copy, and a `Clone` type's `clone()` crosses into
Rust for a value of its own. A type implementing `Hash` has `hash()`, Rust's, by which
Expand Down
35 changes: 21 additions & 14 deletions ffi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! `moq_audio_encoder_finish_`. A `_` doubled at the split would read better, but C++
//! reserves every name containing `__`. Only the names that clash change.

use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};

use rustc_span::Span;

Expand Down Expand Up @@ -499,6 +499,14 @@ impl<'a> Abi<'a> {
abi
}

/// Every name the C header declares for the API, the runtime's aside, each once.
pub(crate) fn declared(&self) -> BTreeSet<&str> {
(self.claims.iter())
.filter(|claim| claim.what != "the runtime")
.map(|claim| claim.name.as_str())
.collect()
}

/// This ABI over `api`, the API it was made of without its functions taking a JNI value,
/// as every language but Kotlin exports it: each name stays the one the whole API gives
/// it, so the C its glue calls keeps the names the crate defines.
Expand Down Expand Up @@ -540,7 +548,15 @@ impl<'a> Abi<'a> {
lists: self.lists.clone(),
tuples: self.tuples.clone(),
arrays: self.arrays.clone(),
claims: Vec::new(),
// Its names, for [`Self::declared`], but not their owners, which are `self.api`'s.
claims: (self.claims.iter())
.map(|claim| Claim {
name: claim.name.clone(),
what: claim.what.clone(),
span: claim.span,
owner: None,
})
.collect(),
renames,
clashes: self.clashes.clone(),
checked: self.checked.clone(),
Expand Down Expand Up @@ -2374,18 +2390,9 @@ mod tests {
let macros = include_str!("macros.txt").lines().filter(|line| !line.starts_with('#'));
for stem in macros.map(|m| m.trim_end_matches('_')) {
let family: Vec<String> = (0..4).map(|n| format!("{stem}{}", "_".repeat(n))).collect();
// A C++ keyword's escape merges a name with its own, which each scope's names keep
// apart, as `cpp::named` does.
let keyword = crate::cpp::escape(stem) != super::unmacroed(stem, 0, |name| name == "i386");
let escapes: &[fn(&str) -> String] = match keyword {
true => &[escape],
false => &[escape, crate::cpp::escape],
};
for escape in escapes {
let escaped: std::collections::BTreeSet<String> = family.iter().map(|n| escape(n)).collect();
assert_eq!(escaped.len(), family.len(), "{stem}'s names escape alike");
assert!(!escaped.iter().any(|e| defined(e)), "{stem}'s names escape to a macro");
}
let escaped: std::collections::BTreeSet<String> = family.iter().map(|n| escape(n)).collect();
assert_eq!(escaped.len(), family.len(), "{stem}'s names escape alike");
assert!(!escaped.iter().any(|e| defined(e)), "{stem}'s names escape to a macro");
}
}

Expand Down
Loading