Skip to content
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

Improve generated extern "C" function signatures #639

Open
madsmtm opened this issue Jul 14, 2024 · 2 comments
Open

Improve generated extern "C" function signatures #639

madsmtm opened this issue Jul 14, 2024 · 2 comments
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request
Milestone

Comments

@madsmtm
Copy link
Owner

madsmtm commented Jul 14, 2024

  • Allow marking functions as safe.
  • Convert bool to/from Bool.
  • Convert *mut T in return types to Option<Retained<T>> (using either Retained::retain_autoreleased or Retained::from_raw depending on the memory management rules of the function).

See also #638 (comment).

@madsmtm madsmtm added enhancement New feature or request A-framework Affects the framework crates and the translator for them labels Jul 14, 2024
@madsmtm madsmtm added this to the objc2 v0.6 milestone Jul 14, 2024
@steven-joruk
Copy link

I'd like to implement this, the conversions seem simple enough.

Am I right in thinking that by "Allow marking functions as safe" you mean after a function has had a conversion, mark it as safe if all of its types are now safe?

@madsmtm
Copy link
Owner Author

madsmtm commented Sep 8, 2024

That'd be really nice!

Am I right in thinking that by "Allow marking functions as safe" you mean after a function has had a conversion, mark it as safe if all of its types are now safe?

No, that'd still not be sound, since the function could do anything, we generally cannot automatically verify that there aren't pre-conditions that are not encoded in the types.

What I meant was to add an option to translation-config.toml, so that we can manually mark methods (like MTLCreateSystemDefaultDevice) as safe.

So in the end, I'm envisioning something like this, you can use it as a reference when doing the implementation:

# Example config
fn.NSMouseInRect.unsafe = false
fn.NSStringFromPoint.unsafe = false

Generates:

// Not changed, because none of the types need to be, and it's not marked safe.
extern "C" {
    #[cfg(feature = "NSString")]
    pub fn NSRectFromString(a_string: &NSString) -> NSRect;
}

pub extern "C" fn NSEqualPoints(a_point: NSPoint, a_rect: NSRect, flipped: bool) -> bool {
    extern "C" {
        fn NSMouseInRect(a_point: NSPoint, a_rect: NSRect, flipped: Bool) -> Bool;
    }
    unsafe { NSMouseInRect(a_point, a_rect, Bool::new(flipped)).to_bool() }
}

// Still marked `unsafe`, it has a pointer parameter
#[cfg(feature = "NSString")]
pub unsafe extern "C" fn NSDecimalString(
    dcm: NonNull<NSDecimal>,
    locale: Option<&AnyObject>,
) -> Retained<NSString> {
    extern "C" {
        fn NSDecimalString(
            dcm: NonNull<NSDecimal>,
            locale: Option<&AnyObject>,
        ) -> NonNull<NSString>;
    }
    // Retain because `NSDecimalString` is not a "copy" nor "create" function
    unsafe { Retained::retain_autoreleased(NSDecimalString(dcm, locale)) }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-framework Affects the framework crates and the translator for them enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants