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

Minor NSArray improvements #26

Merged
merged 3 commits into from
Feb 14, 2024
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
11 changes: 7 additions & 4 deletions src/foundation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ else
const NSInteger = Int32
const NSUInteger = UInt32
end
const MSIntegerMin = typemin(NSInteger)
const NSIntegerMin = typemin(NSInteger)
const NSIntegerMax = typemax(NSInteger)
const NSUIntegerMax = typemax(NSUInteger)

Expand Down Expand Up @@ -179,9 +179,12 @@ end

NSArray() = NSArray(@objc [NSArray array]::id{NSArray})

function NSArray(elements::Vector)
arr = @objc [NSArray arrayWithObjects:elements::Ptr{id{Object}}
count:length(elements)::NSUInteger]::id{NSArray}
function NSArray(elements::Vector{<:NSObject})
arr = GC.@preserve elements begin
pointers = [element.ptr for element in elements]
@objc [NSArray arrayWithObjects:pointers::id{Object}
count:length(elements)::NSUInteger]::id{NSArray}
end
return NSArray(arr)
end

Expand Down
17 changes: 13 additions & 4 deletions src/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@
ex = quote
$(ex.args...)

Base.unsafe_convert(T::Type{<:id}, dev::$instance) = convert(T, dev.ptr)

# add a pseudo constructor to the abstract type that also checks for nil pointers.
function $name(ptr::id)
ptr == nil && throw(UndefRefError())
Expand All @@ -283,14 +281,25 @@
ex = quote
$(ex.args...)

Base.:(==)(a::$instance, b::$instance) = a.ptr == b.ptr
Base.hash(dev::$instance, h::UInt) = hash(dev.ptr, h)
Base.:(==)(a::$instance, b::$instance) = pointer(a) == pointer(b)
Base.hash(obj::$instance, h::UInt) = hash(pointer(obj), h)

Check warning on line 285 in src/syntax.jl

View check run for this annotation

Codecov / codecov/patch

src/syntax.jl#L284-L285

Added lines #L284 - L285 were not covered by tests
end
end

esc(ex)
end

Base.pointer(obj::Object) = obj.ptr

# when passing a single object, we automatically convert it to a pointer
Base.unsafe_convert(T::Type{<:id}, obj::Object) = convert(T, pointer(obj))

# in the case of a vector of objects, we expect the caller to have converted them
# XXX: it's too bad `cconvert` cannot do the `[pointer(obj) for obj in objs]` for us
# (because we can only derive unsafe references in `unsafe_convert`)
Base.unsafe_convert(T::Type{<:id}, ptrs::Vector{<:id}) =
reinterpret(T, pointer(ptrs))
Comment on lines +298 to +301
Copy link

Choose a reason for hiding this comment

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

FWIW, this is what Base.RefArray is for doing, as it will recursively derive a pointer for you after cconverting recursively

Copy link
Member Author

@maleadt maleadt Feb 15, 2024

Choose a reason for hiding this comment

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

Ah, interesting, I figured something like that would exist already.
I think I'll have to copy the pattern though, as AFAIU RefArray only works to ultimately convert to a Ptr, while we're using a custom id pointer here.

EDIT: #27



# Property Accesors

Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ using .Foundation
end

@testset "NSHost" begin
@test hostname() == gethostname()
@test startswith(gethostname(), hostname())
end

@testset "NSBundle" begin
Expand All @@ -143,6 +143,8 @@ end
@test reinterpret(NSString, arr2[1]) == "Hello"
@test reinterpret(NSString, arr2[2]) == "World"
@test Vector{NSString}(arr2) == arr1

@test_throws MethodError NSArray([NSUInteger(42)])
end

@testset "NSDictionary" begin
Expand Down
Loading