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

Availability attribute support #51

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
matrix:
version: ['1.10', 'pre', 'nightly']
os: ['macOS-latest']
arch: [x64]
arch: [x64, aarch64]
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ObjectiveC"
uuid = "e86c9b32-1129-44ac-8ea0-90d5bb39ded9"
version = "3.3.0"
version = "3.4.0"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
3 changes: 3 additions & 0 deletions src/ObjectiveC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const tracing = @load_preference("tracing", false)::Bool
include("primitives.jl")
include("methods.jl")

# Get macOS and Darwin version
include("version.jl")

# Calls & Properties
include("abi.jl")
include("syntax.jl")
Expand Down
32 changes: 31 additions & 1 deletion src/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,23 @@ function instance_message(instance, typ, msg, rettyp, argtyps, argvals)
end
end

# TODO: support availability
macro objc(ex)
objcm(__module__, ex)
end

export UnavailableError
"""
UnavailableError(symbol::Symbol, minver::VersionNumber)

Attempt to contruct an Objective-C object or property that is
not available in the current macOS version.
"""
struct UnavailableError <: Exception
symbol::Symbol
minver::VersionNumber
end
Base.showerror(io::IO, e::UnavailableError) = print(io, "UnavailableError: `", e.symbol, "` was introduced in macOS v", e.minver)

# Wrapper Classes

Expand All @@ -278,6 +291,7 @@ keyword arguments:

* `immutable`: if `true` (default), define the instance class as an immutable. Should be
disabled when you want to use finalizers.
* `availability`: A version string that represents the first macOS version where this object is available.
* `comparison`: if `true` (default `false`), define `==` and `hash` methods for the
wrapper class. This should not be necessary when using an immutable struct, in which
case the default `==` and `hash` methods are sufficient.
Expand All @@ -289,6 +303,7 @@ macro objcwrapper(ex...)
# parse kwargs
comparison = nothing
immutable = nothing
availability = nothing
for kw in kwargs
if kw isa Expr && kw.head == :(=)
kw, value = kw.args
Expand All @@ -298,6 +313,9 @@ macro objcwrapper(ex...)
elseif kw == :immutable
value isa Bool || wrappererror("immutable keyword argument must be a literal boolean")
immutable = value
elseif kw == :availability
Meta.isexpr(value, :macrocall) && value.args[1] == Symbol("@v_str") || wrappererror("availability keyword argument must be a `v_str` statement")
availability = macroexpand(__module__, value; recursive=false)
else
wrappererror("unrecognized keyword argument: $kw")
end
Expand All @@ -307,6 +325,7 @@ macro objcwrapper(ex...)
end
immutable = something(immutable, true)
comparison = something(comparison, !immutable)
availability = something(availability, v"0")

# parse class definition
if Meta.isexpr(def, :(<:))
Expand Down Expand Up @@ -347,6 +366,10 @@ 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.macos_version() < $availability
throw($UnavailableError(Symbol($name), $availability))
end

ptr == nil && throw(UndefRefError())
$instance(ptr)
end
Expand Down Expand Up @@ -391,7 +414,7 @@ propertyerror(s::String) = error("""Objective-C property declaration: $s.

"""
@objcproperties ObjCType begin
@autoproperty myProperty::ObjCType [type=JuliaType] [setter=setMyProperty]
@autoproperty myProperty::ObjCType [type=JuliaType] [setter=setMyProperty] [getter=getMyProperty] [availability=v"x.y"]

@getproperty myProperty function(obj)
...
Expand All @@ -417,6 +440,8 @@ contains a series of property declarations:
`setproperty!` definition will be generated.
- `getter`: specifies the name of the Objective-C getter method. Without this, the
getter method is assumed to be identical to the property
- `availability`: specifies earliest macOS version where this property became available,
similar to the Objective-C attribute of the same name
- `@getproperty myProperty function(obj) ... end`: define a custom getter for the property.
The function should take a single argument `obj`, which is the object that the property is
being accessed on. The function should return the property value.
Expand Down Expand Up @@ -492,13 +517,18 @@ macro objcproperties(typ, ex)
# caller's module and decide on the appropriate ABI. that necessitates use of
# :hygienic-scope to handle the mix of esc/hygienic code.

availability = get(kwargs, :availability, v"0")

getterproperty = if haskey(kwargs, :getter)
kwargs[:getter]
else
property
end
getproperty_ex = objcm(__module__, :([object::id{$(esc(typ))} $getterproperty]::$srcTyp))
getproperty_ex = quote
@static if !Sys.isapple() || ObjectiveC.macos_version() < $availability
throw($UnavailableError(Symbol($(esc(typ)),".",field), $availability))
end
value = $(Expr(:var"hygienic-scope", getproperty_ex, @__MODULE__, __source__))
end

Expand Down
78 changes: 78 additions & 0 deletions src/version.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# version and support queries

@noinline function _syscall_version(name)
size = Ref{Csize_t}()
err = @ccall sysctlbyname(
name::Cstring, C_NULL::Ptr{Cvoid}, size::Ptr{Csize_t},
C_NULL::Ptr{Cvoid}, 0::Csize_t
)::Cint
Base.systemerror("sysctlbyname", err != 0)

osrelease = Vector{UInt8}(undef, size[])
err = @ccall sysctlbyname(
name::Cstring, osrelease::Ptr{Cvoid}, size::Ptr{Csize_t},
C_NULL::Ptr{Cvoid}, 0::Csize_t
)::Cint
Base.systemerror("sysctlbyname", err != 0)

verstr = view(String(osrelease), 1:(size[] - 1))
return parse(VersionNumber, verstr)
end

@static if isdefined(Base, :OncePerProcess) # VERSION >= v"1.12.0-DEV.1421"
const darwin_version = OncePerProcess{VersionNumber}() do
_syscall_version("kern.osrelease")
end
const macos_version = OncePerProcess{VersionNumber}() do
_syscall_version("kern.osproductversion")
end
else
const _darwin_version = Ref{VersionNumber}()
function darwin_version()
if !isassigned(_darwin_version)
_darwin_version[] = _syscall_version("kern.osrelease")
end
_darwin_version[]
end

const _macos_version = Ref{VersionNumber}()
function macos_version()
if !isassigned(_macos_version)
_macos_version[] = _syscall_version("kern.osproductversion")
end
_macos_version[]
end
end

@doc """
ObjectiveC.darwin_version()::VersionNumber

Returns the host Darwin kernel version.

See also [`ObjectiveC.macos_version`](@ref).
""" darwin_version

@doc """
ObjectiveC.macos_version()::VersionNumber

Returns the host macOS version.

See also [`ObjectiveC.darwin_version`](@ref).
""" macos_version

"""
Metal.is_macos([ver::VersionNumber]) -> Bool

Returns whether the OS is macOS with version `ver` or newer.

See also [`Metal.macos_version`](@ref).
"""
function is_macos(ver = nothing)
return if !Sys.isapple()
false
elseif ver === nothing
true
else
macos_version() >= ver
end
end
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,38 @@ using Test

using ObjectiveC

@testset "version" begin
@test ObjectiveC.darwin_version() isa VersionNumber
@test ObjectiveC.macos_version() isa VersionNumber
@test ObjectiveC.is_macos(ObjectiveC.macos_version())
end

# Availability
@objcwrapper availability = v"1000" TestWrapperAvail <: Object
@objcwrapper availability = v"0" TestPropAvail <: Object
@objcproperties TestPropAvail begin
@autoproperty length::Culong
@autoproperty UTF8String::Ptr{Cchar} availability = v"0"
@autoproperty UnavailableProperty::Cint availability = v"1000"
end
@testset "availability" begin
# wrapper
fakeidwrap = id{TestWrapperAvail}(1)
@test_throws UnavailableError TestWrapperAvail(fakeidwrap)

# property
str1 = "foo"
prop = TestPropAvail(@objc [NSString stringWithUTF8String:str1::Ptr{UInt8}]::id{TestPropAvail})

@test :length in propertynames(prop)
@test :UTF8String in propertynames(prop)
@test :UnavailableProperty in propertynames(prop)

@test prop.length == length(str1)
@test unsafe_string(prop.UTF8String) == str1
@test_throws UnavailableError prop.UnavailableProperty
end

@testset "@objc macro" begin
# class methods
@objc [NSString new]::id{Object}
Expand Down
Loading