@@ -269,6 +269,13 @@ PlatformAvailability(; introduced = nothing, deprecated = nothing, obsoleted = n
269
269
PlatformAvailability (introduced, deprecated, obsoleted, unavailable)
270
270
271
271
export macos
272
+
273
+ """
274
+ macos(introduced[, deprecated, obsoleted, unavailable])
275
+ macos(; [introduced, deprecated, obsoleted, unavailable])
276
+
277
+ Represents an availability statement for Objective-C wrappers.
278
+ """
272
279
struct macos
273
280
availability:: PlatformAvailability
274
281
macos (args... ) = new (PlatformAvailability (args... ))
@@ -314,17 +321,17 @@ function Base.showerror(io::IO, e::UnavailableError)
314
321
return
315
322
end
316
323
317
- function _getmacosavailabilityexpr (value )
318
- Meta . isexpr (value, :vect ) || Meta . isexpr (value, :call ) && value . args[ 1 ] == :macos || wrappererror ( " availability keyword argument must be a `macos(v \" x.y \" )` statement " )
319
- if Meta . isexpr (value, :vect )
320
- for expr in value . args
321
- if Meta . isexpr (expr, :call ) && expr . args[ 1 ] == :macos
322
- return expr
323
- end
324
- end
325
- return nothing
326
- else
327
- return value
324
+ function _getmacosavailability (expr )
325
+ try
326
+ # Don't run arbitrary code
327
+ Meta . isexpr (expr, :vect ) || Meta . isexpr ( expr, :call ) && expr . args[ 1 ] == :macos || error ()
328
+
329
+ avail = eval ( expr)
330
+ # Returns the first `macos` object in the vector, otherwise
331
+ # the error gets caught and a helpful message is displayed
332
+ return avail isa macos ? avail : avail[ findfirst (x -> x isa macos, avail)]
333
+ catch
334
+ wrappererror ( " `availability` keyword argument must be a valid `macos` constructor or a vector thereof " )
328
335
end
329
336
end
330
337
@@ -351,7 +358,7 @@ keyword arguments:
351
358
352
359
* `immutable`: if `true` (default), define the instance class as an immutable. Should be
353
360
disabled when you want to use finalizers.
354
- * `availability`: A version string that represents the first macOS version where this object is available .
361
+ * `availability`: A `macos` object that represents the availability of the object.
355
362
* `comparison`: if `true` (default `false`), define `==` and `hash` methods for the
356
363
wrapper class. This should not be necessary when using an immutable struct, in which
357
364
case the default `==` and `hash` methods are sufficient.
@@ -374,7 +381,7 @@ macro objcwrapper(ex...)
374
381
value isa Bool || wrappererror (" immutable keyword argument must be a literal boolean" )
375
382
immutable = value
376
383
elseif kw == :availability
377
- availability = ObjectiveC. _getmacosavailabilityexpr (value)
384
+ availability = ObjectiveC. _getmacosavailability (value)
378
385
else
379
386
wrappererror (" unrecognized keyword argument: $kw " )
380
387
end
@@ -384,7 +391,7 @@ macro objcwrapper(ex...)
384
391
end
385
392
immutable = something (immutable, true )
386
393
comparison = something (comparison, ! immutable)
387
- availability = something (availability, :( macos (v " 0" ) ))
394
+ availability = something (availability, macos (v " 0" ))
388
395
389
396
# parse class definition
390
397
if Meta. isexpr (def, :(< :))
@@ -498,9 +505,8 @@ contains a series of property declarations:
498
505
- `setter`: specifies the name of the Objective-C setter method. Without this, no
499
506
`setproperty!` definition will be generated.
500
507
- `getter`: specifies the name of the Objective-C getter method. Without this, the
501
- getter method is assumed to be identical to the property
502
- - `availability`: specifies earliest macOS version where this property became available,
503
- similar to the Objective-C attribute of the same name
508
+ getter method is assumed to be identical to the property.
509
+ - `availability`: A `macos` object that represents the availability of the property.
504
510
- `@getproperty myProperty function(obj) ... end`: define a custom getter for the property.
505
511
The function should take a single argument `obj`, which is the object that the property is
506
512
being accessed on. The function should return the property value.
@@ -578,9 +584,9 @@ macro objcproperties(typ, ex)
578
584
579
585
availability = nothing
580
586
if haskey (kwargs, :availability )
581
- availability = ObjectiveC. _getmacosavailabilityexpr (kwargs[:availability ])
587
+ availability = ObjectiveC. _getmacosavailability (kwargs[:availability ])
582
588
end
583
- availability = something (availability, :( macos (v " 0" ) ))
589
+ availability = something (availability, macos (v " 0" ))
584
590
585
591
getterproperty = if haskey (kwargs, :getter )
586
592
kwargs[:getter ]
0 commit comments