Skip to content

Availability attribute support #51

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 14 commits into from
Feb 11, 2025

Conversation

christiangnrd
Copy link
Contributor

#50

Copy link
Contributor

github-actions bot commented Jan 28, 2025

Your PR requires formatting changes to meet the project's style guidelines.
Please consider running Runic (git runic master) to apply these changes.

Click here to view the suggested changes.
diff --git a/src/syntax.jl b/src/syntax.jl
index d447fed..fd07371 100644
--- a/src/syntax.jl
+++ b/src/syntax.jl
@@ -290,7 +290,7 @@ macro objcwrapper(ex...)
   # parse kwargs
   comparison = nothing
   immutable = nothing
-  availability = nothing
+    availability = nothing
   for kw in kwargs
     if kw isa Expr && kw.head == :(=)
       kw, value = kw.args
@@ -300,8 +300,8 @@ macro objcwrapper(ex...)
       elseif kw == :immutable
         value isa Bool || wrappererror("immutable keyword argument must be a literal boolean")
         immutable = value
-      elseif kw == :availability
-        availability = get_avail_exprs(__module__, value)
+            elseif kw == :availability
+                availability = get_avail_exprs(__module__, value)
       else
         wrappererror("unrecognized keyword argument: $kw")
       end
@@ -311,7 +311,7 @@ macro objcwrapper(ex...)
   end
   immutable = something(immutable, true)
   comparison = something(comparison, !immutable)
-  availability = something(availability, PlatformAvailability(:macos, v"0"))
+    availability = something(availability, PlatformAvailability(:macos, v"0"))
 
   # parse class definition
   if Meta.isexpr(def, :(<:))
@@ -352,9 +352,9 @@ macro objcwrapper(ex...)
 
     # add a pseudo constructor to the abstract type that also checks for nil pointers.
     function $name(ptr::id)
-      @static if !Sys.isapple() || ObjectiveC.is_unavailable($availability)
-        throw($UnavailableError(Symbol($name), $availability))
-      end
+            @static if !Sys.isapple() || ObjectiveC.is_unavailable($availability)
+                throw($UnavailableError(Symbol($name), $availability))
+            end
 
       ptr == nil && throw(UndefRefError())
       $instance(ptr)

Copy link

codecov bot commented Jan 28, 2025

Codecov Report

Attention: Patch coverage is 96.15385% with 3 lines in your changes missing coverage. Please review.

Project coverage is 77.71%. Comparing base (6911018) to head (1964af7).
Report is 28 commits behind head on master.

Files with missing lines Patch % Lines
src/version.jl 92.00% 2 Missing ⚠️
src/syntax.jl 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #51      +/-   ##
==========================================
+ Coverage   71.91%   77.71%   +5.79%     
==========================================
  Files          10       13       +3     
  Lines         769      987     +218     
==========================================
+ Hits          553      767     +214     
- Misses        216      220       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maleadt
Copy link
Member

maleadt commented Jan 28, 2025

What about making this more generic than only checking against the macOS version, taking an arbitrary bool instead? Is there precedent for, say, platform-dependent attributes? If so, what about:

availability = is_macos(v"15")
availability = (Sys.ARCH == :aarch64)

@christiangnrd
Copy link
Contributor Author

christiangnrd commented Jan 31, 2025

I don't think there is precedence for this, I tried to have it behave like the clang attribute, and the only platform that supports both it and Julia is macOS.

I'm not opposed to it, but this would leave the need for code in the toml config for wrapping, and I would like it if the version errors were informative. Although maybe it could be changed to something like

UnavailableError: `<Symbol>` is not available on your platform, you may need to upgrade your operating system.

@christiangnrd
Copy link
Contributor Author

Did not mean to close

@christiangnrd christiangnrd reopened this Jan 31, 2025
@maleadt
Copy link
Member

maleadt commented Feb 3, 2025

this would leave the need for code in the toml config for wrapping

The TOML could always use a more specific macos_availability attribute and generate the appropriate code.

I would like it if the version errors were informative

I wonder if we could symbolize the condition:

UnavailableError: `<Symbol>` is not available on your platform, which does not fulfill the necessary predicate (`is_macos(v"15")`)

That said, I'm totally fine with mimicking Clang and providing something more specific. I'd then mimic it somewhat closer though, also including the platform and introduced/deprecated/obsolete. Just spitballing, but what about:

# in ObjectiveC.jl:
struct macOS
  # ...
  macOS(introduced::VersionNumber, deprecated=nothing, obsolete=nothing) = ...
end

# in user code
@objcwrapper availability = macOS(v"14") Foo <: Object
@objcwrapper availability = [macOS(introduced=v"14", deprecated=v"15")] Bar <: Object

@christiangnrd christiangnrd force-pushed the availability branch 4 times, most recently from 1cf918b to 6c95198 Compare February 4, 2025 03:28
@christiangnrd
Copy link
Contributor Author

christiangnrd commented Feb 4, 2025

I went with the second option with lowercase macos since that's what it is with Clang.

Open to any suggestions to simplify the code but it works and would be fairly easy to extend with a new platform if ever needed.

If this is approved I'll submit a PR to Julia for the OncePer constructs to subtype Function to make the nightly tests pass. JuliaLang/julia#57289

@christiangnrd christiangnrd force-pushed the availability branch 2 times, most recently from f24ca87 to 1044bbc Compare February 8, 2025 22:06
@christiangnrd
Copy link
Contributor Author

Now based on #52 for the nightly fixes.

Copy link
Member

@maleadt maleadt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Some suggestions for simplifying/generalizing the implementation, I'd properly support multiple platforms already by supporting both macOS and Darwin availability selectors.

@christiangnrd
Copy link
Contributor Author

I think this is ready to merge!

Copy link
Member

@maleadt maleadt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Couple of minor suggestions.

I also don't particularly like the negative is_unavailable, but I guess that's how it's done in Clang?

@christiangnrd
Copy link
Contributor Author

I've applied your suggestions.

is_unavailable was chosen purely for making the logic easier to implement.

If it's okay with you, since this is internal, I'll make the change to is_available in a follow-up PR but I think we should merge this.

@maleadt maleadt merged commit 5d9d128 into JuliaInterop:master Feb 11, 2025
7 checks passed
@christiangnrd christiangnrd deleted the availability branch February 11, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants