-
Notifications
You must be signed in to change notification settings - Fork 61
Manual block encodings #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5387cab
Feat+Refactor(block2/abi): Add new utils on `BlockFlags`
PaulDance 56fc5fc
Feat(block2): Add new `with_encoding` unsafe block constructors
PaulDance 7b2d824
Feat(encode): Add ability to compute block encoding string
PaulDance c6bb5e1
Feat(block2): Add placeholders for safety debug assertions to manuall…
PaulDance 8793842
Chore: Update compiler UI test
PaulDance 6911801
Test(ui): Extend block tests for with_encoding
PaulDance 2d960ac
Test(block): Extend tests for with_encoding
PaulDance af13f70
Make test more generic
madsmtm 48d3aa1
Avoid opt_double! macro, since it's unnecessary
madsmtm 2b47bf6
Don't mention block_signature_string, it's not public
madsmtm 23fceed
All Encoding::size to use alignment in the future
madsmtm 0acb681
Make `with_encoding` safe
madsmtm 00173dd
Add changelog entries
madsmtm b132646
Fix test on Aarch64
madsmtm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
use alloc::ffi::CString; | ||
use alloc::string::ToString; | ||
use alloc::vec::Vec; | ||
use core::mem; | ||
|
||
use objc2::encode::{EncodeArguments, EncodeReturn, Encoding}; | ||
|
||
/// Computes the raw signature string of the object corresponding to the block | ||
/// taking `A` as inputs and returning `R`. | ||
/// | ||
/// Although this is currently implemented on a best-effort basis, this should | ||
/// still serve as a good way to obtain what to fill in the encoding string | ||
/// when implementing [`crate::ManualBlockEncoding`]. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```ignore | ||
/// assert_eq!(block_signature_string::<(i32, f32), u8>(), "C16@?0i8f12"); | ||
/// ``` | ||
#[allow(unused)] | ||
pub fn block_signature_string<A, R>() -> CString | ||
where | ||
A: EncodeArguments, | ||
R: EncodeReturn, | ||
{ | ||
block_signature_string_inner(A::ENCODINGS, &R::ENCODING_RETURN) | ||
} | ||
|
||
#[allow(unused)] | ||
fn block_signature_string_inner(args: &[Encoding], ret: &Encoding) -> CString { | ||
// TODO: alignment? | ||
let arg_sizes = args | ||
.iter() | ||
.map(Encoding::size) | ||
.map(Option::unwrap_or_default) | ||
.collect::<Vec<_>>(); | ||
let args_size = arg_sizes.iter().sum::<usize>(); | ||
|
||
// Take the hidden block parameter into account. | ||
let mut off = mem::size_of::<*const ()>(); | ||
let mut res = ret.to_string(); | ||
res.push_str(&(off + args_size).to_string()); | ||
res.push_str("@?0"); | ||
|
||
for (arg_enc, arg_size) in args.iter().zip(arg_sizes) { | ||
res.push_str(&arg_enc.to_string()); | ||
res.push_str(&off.to_string()); | ||
off += arg_size; | ||
} | ||
|
||
// UNWRAP: The above construction only uses controlled `ToString` | ||
// implementations that do not include nul bytes. | ||
CString::new(res).unwrap() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use alloc::borrow::ToOwned; | ||
|
||
#[test] | ||
fn test_block_signature_string() { | ||
for (args, ret, val) in [ | ||
( | ||
&[][..], | ||
&Encoding::Void, | ||
#[cfg(target_pointer_width = "64")] | ||
"v8@?0", | ||
#[cfg(target_pointer_width = "32")] | ||
"v4@?0", | ||
), | ||
( | ||
&[], | ||
&Encoding::Int, | ||
#[cfg(target_pointer_width = "64")] | ||
"i8@?0", | ||
#[cfg(target_pointer_width = "32")] | ||
"i4@?0", | ||
), | ||
( | ||
&[], | ||
&Encoding::Float, | ||
#[cfg(target_pointer_width = "64")] | ||
"f8@?0", | ||
#[cfg(target_pointer_width = "32")] | ||
"f4@?0", | ||
), | ||
( | ||
&[], | ||
&Encoding::Bool, | ||
#[cfg(target_pointer_width = "64")] | ||
"B8@?0", | ||
#[cfg(target_pointer_width = "32")] | ||
"B4@?0", | ||
), | ||
( | ||
&[Encoding::Int], | ||
&Encoding::Void, | ||
#[cfg(target_pointer_width = "64")] | ||
"v12@?0i8", | ||
#[cfg(target_pointer_width = "32")] | ||
"v8@?0i4", | ||
), | ||
( | ||
&[Encoding::Int], | ||
&Encoding::Int, | ||
#[cfg(target_pointer_width = "64")] | ||
"i12@?0i8", | ||
#[cfg(target_pointer_width = "32")] | ||
"i8@?0i4", | ||
), | ||
( | ||
&[Encoding::Long, Encoding::Double, Encoding::FloatComplex], | ||
&Encoding::Atomic(&Encoding::UChar), | ||
#[cfg(target_pointer_width = "64")] | ||
"AC32@?0l8d16jf24", | ||
#[cfg(target_pointer_width = "32")] | ||
"AC24@?0l4d8jf16", | ||
), | ||
( | ||
&[ | ||
Encoding::Union("ThisOrThat", &[Encoding::UShort, Encoding::Int]), | ||
Encoding::Struct( | ||
"ThisAndThat", | ||
&[ | ||
Encoding::ULongLong, | ||
Encoding::LongDoubleComplex, | ||
Encoding::Atomic(&Encoding::Bool), | ||
], | ||
), | ||
], | ||
&Encoding::String, | ||
// Probably unaligned. | ||
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64",))] | ||
"*53@?0(ThisOrThat=Si)8{ThisAndThat=QjDAB}12", | ||
#[cfg(all(target_arch = "x86", target_vendor = "apple"))] | ||
"*45@?0(ThisOrThat=Si)4{ThisAndThat=QjDAB}8", | ||
#[cfg(all(target_arch = "x86", not(target_vendor = "apple")))] | ||
"*41@?0(ThisOrThat=Si)4{ThisAndThat=QjDAB}8", | ||
#[cfg(target_arch = "arm")] | ||
"*37@?0(ThisOrThat=Si)4{ThisAndThat=QjDAB}8", | ||
), | ||
( | ||
&[ | ||
Encoding::Block, | ||
Encoding::Class, | ||
Encoding::Object, | ||
Encoding::Pointer(&Encoding::Char), | ||
Encoding::Sel, | ||
Encoding::String, | ||
Encoding::Unknown, | ||
Encoding::Unknown, | ||
Encoding::Unknown, | ||
], | ||
&Encoding::Pointer(&Encoding::Atomic(&Encoding::UChar)), | ||
#[cfg(target_pointer_width = "64")] | ||
"^AC56@?0@?8#16@24^c32:40*48?56?56?56", | ||
#[cfg(target_pointer_width = "32")] | ||
"^AC28@?0@?4#8@12^c16:20*24?28?28?28", | ||
), | ||
( | ||
&[Encoding::Array(123, &Encoding::Object)], | ||
&Encoding::Pointer(&Encoding::Class), | ||
#[cfg(target_pointer_width = "64")] | ||
"^#992@?0[123@]8", | ||
#[cfg(target_pointer_width = "32")] | ||
"^#496@?0[123@]4", | ||
), | ||
// Bitfields can probably not be passed around through functions, | ||
// so this may be a bit nonsensical, but let's test it anyway. | ||
( | ||
&[ | ||
Encoding::BitField(1, None), | ||
Encoding::BitField(2, None), | ||
Encoding::BitField(3, None), | ||
Encoding::BitField(6, None), | ||
Encoding::BitField(8, None), | ||
Encoding::BitField(42, None), | ||
Encoding::BitField(28, Some(&(2, Encoding::UInt))), | ||
], | ||
&Encoding::Sel, | ||
#[cfg(target_pointer_width = "64")] | ||
":25@?0b18b29b310b611b812b4213b2I2821", | ||
#[cfg(target_pointer_width = "32")] | ||
":21@?0b14b25b36b67b88b429b2I2817", | ||
), | ||
] { | ||
assert_eq!( | ||
block_signature_string_inner(args, ret), | ||
CString::new(val.to_owned().into_bytes()).unwrap() | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.