objc2_javascript_core/generated/
JSValue.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::ffi::*;
4use core::ptr::NonNull;
5#[cfg(feature = "objc2")]
6use objc2::__framework_prelude::*;
7#[cfg(feature = "objc2-core-foundation")]
8use objc2_core_foundation::*;
9#[cfg(feature = "objc2-foundation")]
10use objc2_foundation::*;
11
12use crate::*;
13
14#[cfg(feature = "objc2")]
15extern_class!(
16    /// A JSValue is a reference to a JavaScript value. Every JSValue
17    /// originates from a JSContext and holds a strong reference to it.
18    /// When a JSValue instance method creates a new JSValue, the new value
19    /// originates from the same JSContext.
20    ///
21    /// All JSValues values also originate from a JSVirtualMachine
22    /// (available indirectly via the context property). It is an error to pass a
23    /// JSValue to a method or property of a JSValue or JSContext originating from a
24    /// different JSVirtualMachine. Doing so will raise an Objective-C exception.
25    ///
26    /// See also [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jsvalue?language=objc)
27    #[unsafe(super(NSObject))]
28    #[derive(Debug, PartialEq, Eq, Hash)]
29    #[cfg(feature = "objc2")]
30    pub struct JSValue;
31);
32
33#[cfg(feature = "objc2")]
34extern_conformance!(
35    unsafe impl NSObjectProtocol for JSValue {}
36);
37
38#[cfg(feature = "objc2")]
39impl JSValue {
40    extern_methods!(
41        #[cfg(feature = "JSContext")]
42        /// The JSContext that this value originates from.
43        #[unsafe(method(context))]
44        #[unsafe(method_family = none)]
45        pub unsafe fn context(&self) -> Option<Retained<JSContext>>;
46
47        #[cfg(feature = "JSContext")]
48        /// Create a JSValue by converting an Objective-C object.
49        ///
50        /// The resulting JSValue retains the provided Objective-C object.
51        ///
52        /// Parameter `value`: The Objective-C object to be converted.
53        ///
54        /// Returns: The new JSValue.
55        ///
56        /// # Safety
57        ///
58        /// - `value` should be of the correct type.
59        /// - `value` might not allow `None`.
60        /// - `context` might not allow `None`.
61        #[unsafe(method(valueWithObject:inContext:))]
62        #[unsafe(method_family = none)]
63        pub unsafe fn valueWithObject_inContext(
64            value: Option<&AnyObject>,
65            context: Option<&JSContext>,
66        ) -> Option<Retained<JSValue>>;
67
68        #[cfg(feature = "JSContext")]
69        /// Create a JavaScript value from a BOOL primitive.
70        ///
71        /// Parameter `context`: The JSContext in which the resulting JSValue will be created.
72        ///
73        /// Returns: The new JSValue representing the equivalent boolean value.
74        ///
75        /// # Safety
76        ///
77        /// `context` might not allow `None`.
78        #[unsafe(method(valueWithBool:inContext:))]
79        #[unsafe(method_family = none)]
80        pub unsafe fn valueWithBool_inContext(
81            value: bool,
82            context: Option<&JSContext>,
83        ) -> Option<Retained<JSValue>>;
84
85        #[cfg(feature = "JSContext")]
86        /// Create a JavaScript value from a double primitive.
87        ///
88        /// Parameter `context`: The JSContext in which the resulting JSValue will be created.
89        ///
90        /// Returns: The new JSValue representing the equivalent boolean value.
91        ///
92        /// # Safety
93        ///
94        /// `context` might not allow `None`.
95        #[unsafe(method(valueWithDouble:inContext:))]
96        #[unsafe(method_family = none)]
97        pub unsafe fn valueWithDouble_inContext(
98            value: c_double,
99            context: Option<&JSContext>,
100        ) -> Option<Retained<JSValue>>;
101
102        #[cfg(feature = "JSContext")]
103        /// Create a JavaScript value from an
104        /// <code>
105        /// int32_t
106        /// </code>
107        /// primitive.
108        ///
109        /// Parameter `context`: The JSContext in which the resulting JSValue will be created.
110        ///
111        /// Returns: The new JSValue representing the equivalent boolean value.
112        ///
113        /// # Safety
114        ///
115        /// `context` might not allow `None`.
116        #[unsafe(method(valueWithInt32:inContext:))]
117        #[unsafe(method_family = none)]
118        pub unsafe fn valueWithInt32_inContext(
119            value: i32,
120            context: Option<&JSContext>,
121        ) -> Option<Retained<JSValue>>;
122
123        #[cfg(feature = "JSContext")]
124        /// Create a JavaScript value from a
125        /// <code>
126        /// uint32_t
127        /// </code>
128        /// primitive.
129        ///
130        /// Parameter `context`: The JSContext in which the resulting JSValue will be created.
131        ///
132        /// Returns: The new JSValue representing the equivalent boolean value.
133        ///
134        /// # Safety
135        ///
136        /// `context` might not allow `None`.
137        #[unsafe(method(valueWithUInt32:inContext:))]
138        #[unsafe(method_family = none)]
139        pub unsafe fn valueWithUInt32_inContext(
140            value: u32,
141            context: Option<&JSContext>,
142        ) -> Option<Retained<JSValue>>;
143
144        #[cfg(feature = "JSContext")]
145        /// Create a new, empty JavaScript object.
146        ///
147        /// Parameter `context`: The JSContext in which the resulting object will be created.
148        ///
149        /// Returns: The new JavaScript object.
150        ///
151        /// # Safety
152        ///
153        /// `context` might not allow `None`.
154        #[unsafe(method(valueWithNewObjectInContext:))]
155        #[unsafe(method_family = none)]
156        pub unsafe fn valueWithNewObjectInContext(
157            context: Option<&JSContext>,
158        ) -> Option<Retained<JSValue>>;
159
160        #[cfg(feature = "JSContext")]
161        /// Create a new, empty JavaScript array.
162        ///
163        /// Parameter `context`: The JSContext in which the resulting array will be created.
164        ///
165        /// Returns: The new JavaScript array.
166        ///
167        /// # Safety
168        ///
169        /// `context` might not allow `None`.
170        #[unsafe(method(valueWithNewArrayInContext:))]
171        #[unsafe(method_family = none)]
172        pub unsafe fn valueWithNewArrayInContext(
173            context: Option<&JSContext>,
174        ) -> Option<Retained<JSValue>>;
175
176        #[cfg(all(feature = "JSContext", feature = "objc2-foundation"))]
177        /// Create a new JavaScript regular expression object.
178        ///
179        /// Parameter `pattern`: The regular expression pattern.
180        ///
181        /// Parameter `flags`: The regular expression flags.
182        ///
183        /// Parameter `context`: The JSContext in which the resulting regular expression object will be created.
184        ///
185        /// Returns: The new JavaScript regular expression object.
186        ///
187        /// # Safety
188        ///
189        /// - `pattern` might not allow `None`.
190        /// - `flags` might not allow `None`.
191        /// - `context` might not allow `None`.
192        #[unsafe(method(valueWithNewRegularExpressionFromPattern:flags:inContext:))]
193        #[unsafe(method_family = none)]
194        pub unsafe fn valueWithNewRegularExpressionFromPattern_flags_inContext(
195            pattern: Option<&NSString>,
196            flags: Option<&NSString>,
197            context: Option<&JSContext>,
198        ) -> Option<Retained<JSValue>>;
199
200        #[cfg(all(feature = "JSContext", feature = "objc2-foundation"))]
201        /// Create a new JavaScript error object.
202        ///
203        /// Parameter `message`: The error message.
204        ///
205        /// Parameter `context`: The JSContext in which the resulting error object will be created.
206        ///
207        /// Returns: The new JavaScript error object.
208        ///
209        /// # Safety
210        ///
211        /// - `message` might not allow `None`.
212        /// - `context` might not allow `None`.
213        #[unsafe(method(valueWithNewErrorFromMessage:inContext:))]
214        #[unsafe(method_family = none)]
215        pub unsafe fn valueWithNewErrorFromMessage_inContext(
216            message: Option<&NSString>,
217            context: Option<&JSContext>,
218        ) -> Option<Retained<JSValue>>;
219
220        #[cfg(all(feature = "JSContext", feature = "block2"))]
221        /// Create a new promise object using the provided executor callback.
222        ///
223        /// Parameter `callback`: A callback block invoked while the promise object is being initialized. The resolve and reject parameters are functions that can be called to notify any pending reactions about the state of the new promise object.
224        ///
225        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
226        ///
227        /// Returns: The JSValue representing a new promise JavaScript object.
228        ///
229        /// This method is equivalent to calling the Promise constructor in JavaScript. the resolve and reject callbacks each normally take a single value, which they forward to all relevent pending reactions. While inside the executor callback context will act as if it were in any other callback, except calleeFunction will be
230        /// <code>
231        /// nil
232        /// </code>
233        /// . This also means means the new promise object may be accessed via
234        /// <code>
235        /// [context thisValue]
236        /// </code>
237        /// .
238        ///
239        /// # Safety
240        ///
241        /// - `context` might not allow `None`.
242        /// - `callback` might not allow `None`.
243        #[unsafe(method(valueWithNewPromiseInContext:fromExecutor:))]
244        #[unsafe(method_family = none)]
245        pub unsafe fn valueWithNewPromiseInContext_fromExecutor(
246            context: Option<&JSContext>,
247            callback: Option<&block2::DynBlock<dyn Fn(*mut JSValue, *mut JSValue)>>,
248        ) -> Option<Retained<JSValue>>;
249
250        #[cfg(feature = "JSContext")]
251        /// Create a new resolved promise object with the provided value.
252        ///
253        /// Parameter `result`: The result value to be passed to any reactions.
254        ///
255        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
256        ///
257        /// Returns: The JSValue representing a new promise JavaScript object.
258        ///
259        /// This method is equivalent to calling
260        /// <code>
261        /// [JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [resolve callWithArguments:
262        /// @
263        /// [result]]; } inContext:context]
264        /// </code>
265        ///
266        /// # Safety
267        ///
268        /// - `result` should be of the correct type.
269        /// - `result` might not allow `None`.
270        /// - `context` might not allow `None`.
271        #[unsafe(method(valueWithNewPromiseResolvedWithResult:inContext:))]
272        #[unsafe(method_family = none)]
273        pub unsafe fn valueWithNewPromiseResolvedWithResult_inContext(
274            result: Option<&AnyObject>,
275            context: Option<&JSContext>,
276        ) -> Option<Retained<JSValue>>;
277
278        #[cfg(feature = "JSContext")]
279        /// Create a new rejected promise object with the provided value.
280        ///
281        /// Parameter `reason`: The result value to be passed to any reactions.
282        ///
283        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
284        ///
285        /// Returns: The JSValue representing a new promise JavaScript object.
286        ///
287        /// This method is equivalent to calling
288        /// <code>
289        /// [JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [reject callWithArguments:
290        /// @
291        /// [reason]]; } inContext:context]
292        /// </code>
293        ///
294        /// # Safety
295        ///
296        /// - `reason` should be of the correct type.
297        /// - `reason` might not allow `None`.
298        /// - `context` might not allow `None`.
299        #[unsafe(method(valueWithNewPromiseRejectedWithReason:inContext:))]
300        #[unsafe(method_family = none)]
301        pub unsafe fn valueWithNewPromiseRejectedWithReason_inContext(
302            reason: Option<&AnyObject>,
303            context: Option<&JSContext>,
304        ) -> Option<Retained<JSValue>>;
305
306        #[cfg(all(feature = "JSContext", feature = "objc2-foundation"))]
307        /// Create a new, unique, symbol object.
308        ///
309        /// Parameter `description`: The description of the symbol object being created.
310        ///
311        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
312        ///
313        /// Returns: The JSValue representing a unique JavaScript value with type symbol.
314        ///
315        /// # Safety
316        ///
317        /// - `description` might not allow `None`.
318        /// - `context` might not allow `None`.
319        #[unsafe(method(valueWithNewSymbolFromDescription:inContext:))]
320        #[unsafe(method_family = none)]
321        pub unsafe fn valueWithNewSymbolFromDescription_inContext(
322            description: Option<&NSString>,
323            context: Option<&JSContext>,
324        ) -> Option<Retained<JSValue>>;
325
326        #[cfg(all(feature = "JSContext", feature = "objc2-foundation"))]
327        /// Create a new BigInt value from a numeric string.
328        ///
329        /// Parameter `string`: The string representation of the BigInt JavaScript value being created.
330        ///
331        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
332        ///
333        /// Returns: The JSValue representing a JavaScript value with type BigInt.
334        ///
335        /// This is equivalent to calling the
336        /// <code>
337        /// BigInt
338        /// </code>
339        /// constructor from JavaScript with a string argument.
340        #[unsafe(method(valueWithNewBigIntFromString:inContext:))]
341        #[unsafe(method_family = none)]
342        pub unsafe fn valueWithNewBigIntFromString_inContext(
343            string: &NSString,
344            context: &JSContext,
345        ) -> Option<Retained<JSValue>>;
346
347        #[cfg(feature = "JSContext")]
348        /// Create a new BigInt value from a
349        /// <code>
350        /// int64_t
351        /// </code>
352        /// .
353        ///
354        /// Parameter `int64`: The signed 64-bit integer of the BigInt JavaScript value being created.
355        ///
356        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
357        ///
358        /// Returns: The JSValue representing a JavaScript value with type BigInt.
359        #[unsafe(method(valueWithNewBigIntFromInt64:inContext:))]
360        #[unsafe(method_family = none)]
361        pub unsafe fn valueWithNewBigIntFromInt64_inContext(
362            int64: i64,
363            context: &JSContext,
364        ) -> Option<Retained<JSValue>>;
365
366        #[cfg(feature = "JSContext")]
367        /// Create a new BigInt value from a
368        /// <code>
369        /// uint64_t
370        /// </code>
371        /// .
372        ///
373        /// Parameter `uint64`: The unsigned 64-bit integer of the BigInt JavaScript value being created.
374        ///
375        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
376        ///
377        /// Returns: The JSValue representing a JavaScript value with type BigInt.
378        #[unsafe(method(valueWithNewBigIntFromUInt64:inContext:))]
379        #[unsafe(method_family = none)]
380        pub unsafe fn valueWithNewBigIntFromUInt64_inContext(
381            uint64: u64,
382            context: &JSContext,
383        ) -> Option<Retained<JSValue>>;
384
385        #[cfg(feature = "JSContext")]
386        /// Create a new BigInt value from a double.
387        ///
388        /// Parameter `value`: The value of the BigInt JavaScript value being created.
389        ///
390        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
391        ///
392        /// Returns: The JSValue representing a JavaScript value with type BigInt.
393        ///
394        /// If the value is not an integer, an exception is thrown.
395        #[unsafe(method(valueWithNewBigIntFromDouble:inContext:))]
396        #[unsafe(method_family = none)]
397        pub unsafe fn valueWithNewBigIntFromDouble_inContext(
398            value: c_double,
399            context: &JSContext,
400        ) -> Option<Retained<JSValue>>;
401
402        #[cfg(feature = "JSContext")]
403        /// Create the JavaScript value
404        /// <code>
405        /// null
406        /// </code>
407        /// .
408        ///
409        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
410        ///
411        /// Returns: The JSValue representing the JavaScript value
412        /// <code>
413        /// null
414        /// </code>
415        /// .
416        ///
417        /// # Safety
418        ///
419        /// `context` might not allow `None`.
420        #[unsafe(method(valueWithNullInContext:))]
421        #[unsafe(method_family = none)]
422        pub unsafe fn valueWithNullInContext(
423            context: Option<&JSContext>,
424        ) -> Option<Retained<JSValue>>;
425
426        #[cfg(feature = "JSContext")]
427        /// Create the JavaScript value
428        /// <code>
429        /// undefined
430        /// </code>
431        /// .
432        ///
433        /// Parameter `context`: The JSContext to which the resulting JSValue belongs.
434        ///
435        /// Returns: The JSValue representing the JavaScript value
436        /// <code>
437        /// undefined
438        /// </code>
439        /// .
440        ///
441        /// # Safety
442        ///
443        /// `context` might not allow `None`.
444        #[unsafe(method(valueWithUndefinedInContext:))]
445        #[unsafe(method_family = none)]
446        pub unsafe fn valueWithUndefinedInContext(
447            context: Option<&JSContext>,
448        ) -> Option<Retained<JSValue>>;
449
450        /// When converting between JavaScript values and Objective-C objects a copy is
451        /// performed. Values of types listed below are copied to the corresponding
452        /// types on conversion in each direction. For NSDictionaries, entries in the
453        /// dictionary that are keyed by strings are copied onto a JavaScript object.
454        /// For dictionaries and arrays, conversion is recursive, with the same object
455        /// conversion being applied to all entries in the collection.
456        ///
457        /// <pre>
458        ///
459        /// ```text
460        ///    Objective-C type  |   JavaScript type
461        ///  --------------------+---------------------
462        ///          nil         |     undefined
463        ///         NSNull       |        null
464        ///        NSString      |       string
465        ///        NSNumber      |   number, boolean
466        ///      NSDictionary    |   Object object
467        ///        NSArray       |    Array object
468        ///         NSDate       |     Date object
469        ///        NSBlock (1)   |   Function object (1)
470        ///           id (2)     |   Wrapper object (2)
471        ///         Class (3)    | Constructor object (3)
472        /// ```
473        ///
474        /// </pre>
475        ///
476        /// (1) Instances of NSBlock with supported arguments types will be presented to
477        /// JavaScript as a callable Function object. For more information on supported
478        /// argument types see JSExport.h. If a JavaScript Function originating from an
479        /// Objective-C block is converted back to an Objective-C object the block will
480        /// be returned. All other JavaScript functions will be converted in the same
481        /// manner as a JavaScript object of type Object.
482        ///
483        /// (2) For Objective-C instances that do not derive from the set of types listed
484        /// above, a wrapper object to provide a retaining handle to the Objective-C
485        /// instance from JavaScript. For more information on these wrapper objects, see
486        /// JSExport.h. When a JavaScript wrapper object is converted back to Objective-C
487        /// the Objective-C instance being retained by the wrapper is returned.
488        ///
489        /// (3) For Objective-C Class objects a constructor object containing exported
490        /// class methods will be returned. See JSExport.h for more information on
491        /// constructor objects.
492        ///
493        /// For all methods taking arguments of type id, arguments will be converted
494        /// into a JavaScript value according to the above conversion.
495        ///
496        /// Convert this JSValue to an Objective-C object.
497        ///
498        /// The JSValue is converted to an Objective-C object according
499        /// to the conversion rules specified above.
500        ///
501        /// Returns: The Objective-C representation of this JSValue.
502        #[unsafe(method(toObject))]
503        #[unsafe(method_family = none)]
504        pub unsafe fn toObject(&self) -> Option<Retained<AnyObject>>;
505
506        /// Convert a JSValue to an Objective-C object of a specific class.
507        ///
508        /// The JSValue is converted to an Objective-C object of the specified Class.
509        /// If the result is not of the specified Class then
510        /// <code>
511        /// nil
512        /// </code>
513        /// will be returned.
514        ///
515        /// Returns: An Objective-C object of the specified Class or
516        /// <code>
517        /// nil
518        /// </code>
519        /// .
520        ///
521        /// # Safety
522        ///
523        /// - `expected_class` probably has further requirements.
524        /// - `expected_class` might not allow `None`.
525        #[unsafe(method(toObjectOfClass:))]
526        #[unsafe(method_family = none)]
527        pub unsafe fn toObjectOfClass(
528            &self,
529            expected_class: Option<&AnyClass>,
530        ) -> Option<Retained<AnyObject>>;
531
532        /// Convert a JSValue to a boolean.
533        ///
534        /// The JSValue is converted to a boolean according to the rules specified
535        /// by the JavaScript language.
536        ///
537        /// Returns: The boolean result of the conversion.
538        #[unsafe(method(toBool))]
539        #[unsafe(method_family = none)]
540        pub unsafe fn toBool(&self) -> bool;
541
542        /// Convert a JSValue to a double.
543        ///
544        /// Returns: The double result of the conversion.
545        ///
546        /// Convert the JSValue to a number according to the rules specified by the JavaScript language. Unless the JSValue is a BigInt then this is equivalent to
547        /// <code>
548        /// Number(value)
549        /// </code>
550        /// in JavaScript.
551        #[unsafe(method(toDouble))]
552        #[unsafe(method_family = none)]
553        pub unsafe fn toDouble(&self) -> c_double;
554
555        /// Convert a JSValue to an
556        /// <code>
557        /// int32_t
558        /// </code>
559        /// .
560        ///
561        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the JSValue is a BigInt, then the value is truncated to an
562        /// <code>
563        /// int32_t
564        /// </code>
565        /// .
566        ///
567        /// Returns: The
568        /// <code>
569        /// int32_t
570        /// </code>
571        /// result of the conversion.
572        #[unsafe(method(toInt32))]
573        #[unsafe(method_family = none)]
574        pub unsafe fn toInt32(&self) -> i32;
575
576        /// Convert a JSValue to a
577        /// <code>
578        /// uint32_t
579        /// </code>
580        /// .
581        ///
582        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the JSValue is a BigInt, then the value is truncated to a
583        /// <code>
584        /// uint32_t
585        /// </code>
586        /// .
587        ///
588        /// Returns: The
589        /// <code>
590        /// uint32_t
591        /// </code>
592        /// result of the conversion.
593        #[unsafe(method(toUInt32))]
594        #[unsafe(method_family = none)]
595        pub unsafe fn toUInt32(&self) -> u32;
596
597        /// Convert a JSValue to a
598        /// <code>
599        /// int64_t
600        /// </code>
601        /// .
602        ///
603        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the value is truncated to an
604        /// <code>
605        /// int64_t
606        /// </code>
607        /// .
608        #[unsafe(method(toInt64))]
609        #[unsafe(method_family = none)]
610        pub unsafe fn toInt64(&self) -> i64;
611
612        /// Convert a JSValue to a
613        /// <code>
614        /// uint64_t
615        /// </code>
616        /// .
617        ///
618        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the value is truncated to a
619        /// <code>
620        /// uint64_t
621        /// </code>
622        /// .
623        #[unsafe(method(toUInt64))]
624        #[unsafe(method_family = none)]
625        pub unsafe fn toUInt64(&self) -> u64;
626
627        #[cfg(feature = "objc2-foundation")]
628        /// Convert a JSValue to a NSNumber.
629        ///
630        /// If the JSValue represents a boolean, a NSNumber value of YES or NO
631        /// will be returned. For all other types, the result is equivalent to
632        /// <code>
633        /// Number(value)
634        /// </code>
635        /// in JavaScript.
636        ///
637        /// Returns: The NSNumber result of the conversion.
638        #[unsafe(method(toNumber))]
639        #[unsafe(method_family = none)]
640        pub unsafe fn toNumber(&self) -> Option<Retained<NSNumber>>;
641
642        #[cfg(feature = "objc2-foundation")]
643        /// Convert a JSValue to a NSString.
644        ///
645        /// The JSValue is converted to a string according to the rules specified
646        /// by the JavaScript language.
647        ///
648        /// Returns: The NSString containing the result of the conversion.
649        #[unsafe(method(toString))]
650        #[unsafe(method_family = none)]
651        pub unsafe fn toString(&self) -> Option<Retained<NSString>>;
652
653        #[cfg(feature = "objc2-foundation")]
654        /// Convert a JSValue to a NSDate.
655        ///
656        /// The value is converted to a number representing a time interval
657        /// since 1970 which is then used to create a new NSDate instance.
658        ///
659        /// Returns: The NSDate created using the converted time interval.
660        #[unsafe(method(toDate))]
661        #[unsafe(method_family = none)]
662        pub unsafe fn toDate(&self) -> Option<Retained<NSDate>>;
663
664        #[cfg(feature = "objc2-foundation")]
665        /// Convert a JSValue to a NSArray.
666        ///
667        /// If the value is
668        /// <code>
669        /// null
670        /// </code>
671        /// or
672        /// <code>
673        /// undefined
674        /// </code>
675        /// then
676        /// <code>
677        /// nil
678        /// </code>
679        /// is returned.
680        /// If the value is not an object then a JavaScript TypeError will be thrown.
681        /// The property
682        /// <code>
683        /// length
684        /// </code>
685        /// is read from the object, converted to an unsigned
686        /// integer, and an NSArray of this size is allocated. Properties corresponding
687        /// to indices within the array bounds will be copied to the array, with
688        /// JSValues converted to equivalent Objective-C objects as specified.
689        ///
690        /// Returns: The NSArray containing the recursively converted contents of the
691        /// converted JavaScript array.
692        #[unsafe(method(toArray))]
693        #[unsafe(method_family = none)]
694        pub unsafe fn toArray(&self) -> Option<Retained<NSArray>>;
695
696        #[cfg(feature = "objc2-foundation")]
697        /// Convert a JSValue to a NSDictionary.
698        ///
699        /// If the value is
700        /// <code>
701        /// null
702        /// </code>
703        /// or
704        /// <code>
705        /// undefined
706        /// </code>
707        /// then
708        /// <code>
709        /// nil
710        /// </code>
711        /// is returned.
712        /// If the value is not an object then a JavaScript TypeError will be thrown.
713        /// All enumerable properties of the object are copied to the dictionary, with
714        /// JSValues converted to equivalent Objective-C objects as specified.
715        ///
716        /// Returns: The NSDictionary containing the recursively converted contents of
717        /// the converted JavaScript object.
718        #[unsafe(method(toDictionary))]
719        #[unsafe(method_family = none)]
720        pub unsafe fn toDictionary(&self) -> Option<Retained<NSDictionary>>;
721
722        /// Check if a JSValue corresponds to the JavaScript value
723        /// <code>
724        /// undefined
725        /// </code>
726        /// .
727        #[unsafe(method(isUndefined))]
728        #[unsafe(method_family = none)]
729        pub unsafe fn isUndefined(&self) -> bool;
730
731        /// Check if a JSValue corresponds to the JavaScript value
732        /// <code>
733        /// null
734        /// </code>
735        /// .
736        #[unsafe(method(isNull))]
737        #[unsafe(method_family = none)]
738        pub unsafe fn isNull(&self) -> bool;
739
740        /// Check if a JSValue is a boolean.
741        #[unsafe(method(isBoolean))]
742        #[unsafe(method_family = none)]
743        pub unsafe fn isBoolean(&self) -> bool;
744
745        /// Check if a JSValue is a number.
746        ///
747        /// In JavaScript, there is no differentiation between types of numbers.
748        /// Semantically all numbers behave like doubles except in special cases like bit
749        /// operations.
750        #[unsafe(method(isNumber))]
751        #[unsafe(method_family = none)]
752        pub unsafe fn isNumber(&self) -> bool;
753
754        /// Check if a JSValue is a string.
755        #[unsafe(method(isString))]
756        #[unsafe(method_family = none)]
757        pub unsafe fn isString(&self) -> bool;
758
759        /// Check if a JSValue is an object.
760        #[unsafe(method(isObject))]
761        #[unsafe(method_family = none)]
762        pub unsafe fn isObject(&self) -> bool;
763
764        /// Check if a JSValue is an array.
765        #[unsafe(method(isArray))]
766        #[unsafe(method_family = none)]
767        pub unsafe fn isArray(&self) -> bool;
768
769        /// Check if a JSValue is a date.
770        #[unsafe(method(isDate))]
771        #[unsafe(method_family = none)]
772        pub unsafe fn isDate(&self) -> bool;
773
774        /// Check if a JSValue is a symbol.
775        #[unsafe(method(isSymbol))]
776        #[unsafe(method_family = none)]
777        pub unsafe fn isSymbol(&self) -> bool;
778
779        /// Check if a JSValue is a BigInt.
780        #[unsafe(method(isBigInt))]
781        #[unsafe(method_family = none)]
782        pub unsafe fn isBigInt(&self) -> bool;
783
784        /// Check if a JSValue is an instance of another object.
785        ///
786        /// This method has the same function as the JavaScript operator
787        /// <code>
788        /// instanceof
789        /// </code>
790        /// .
791        /// If an object other than a JSValue is passed, it will first be converted according to
792        /// the aforementioned rules.
793        ///
794        /// # Safety
795        ///
796        /// - `value` should be of the correct type.
797        /// - `value` might not allow `None`.
798        #[unsafe(method(isInstanceOf:))]
799        #[unsafe(method_family = none)]
800        pub unsafe fn isInstanceOf(&self, value: Option<&AnyObject>) -> bool;
801
802        /// Compare two JSValues using JavaScript's
803        /// <code>
804        /// ===
805        /// </code>
806        /// operator.
807        ///
808        /// # Safety
809        ///
810        /// - `value` should be of the correct type.
811        /// - `value` might not allow `None`.
812        #[unsafe(method(isEqualToObject:))]
813        #[unsafe(method_family = none)]
814        pub unsafe fn isEqualToObject(&self, value: Option<&AnyObject>) -> bool;
815
816        /// Compare two JSValues using JavaScript's
817        /// <code>
818        /// ==
819        /// </code>
820        /// operator.
821        ///
822        /// # Safety
823        ///
824        /// - `value` should be of the correct type.
825        /// - `value` might not allow `None`.
826        #[unsafe(method(isEqualWithTypeCoercionToObject:))]
827        #[unsafe(method_family = none)]
828        pub unsafe fn isEqualWithTypeCoercionToObject(&self, value: Option<&AnyObject>) -> bool;
829
830        #[cfg(feature = "JSValueRef")]
831        /// Compare two JSValues.
832        /// The JSValue to compare with.
833        ///
834        /// Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
835        ///
836        /// The result is computed by comparing the results of JavaScript's
837        /// <code>
838        /// ==
839        /// </code>
840        /// ,
841        /// <code>
842        /// <
843        /// </code>
844        /// , and
845        /// <code>
846        /// >
847        /// </code>
848        /// operators. If either
849        /// <code>
850        /// self
851        /// </code>
852        /// or
853        /// <code>
854        /// other
855        /// </code>
856        /// is (or would coerce to)
857        /// <code>
858        /// NaN
859        /// </code>
860        /// in JavaScript, then the result is kJSRelationConditionUndefined.
861        #[unsafe(method(compareJSValue:))]
862        #[unsafe(method_family = none)]
863        pub unsafe fn compareJSValue(&self, other: &JSValue) -> JSRelationCondition;
864
865        #[cfg(feature = "JSValueRef")]
866        /// Compare a JSValue with a
867        /// <code>
868        /// int64_t
869        /// </code>
870        /// .
871        /// The
872        /// <code>
873        /// int64_t
874        /// </code>
875        /// to compare with.
876        ///
877        /// Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
878        ///
879        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language then compared with
880        /// <code>
881        /// other
882        /// </code>
883        /// .
884        #[unsafe(method(compareInt64:))]
885        #[unsafe(method_family = none)]
886        pub unsafe fn compareInt64(&self, other: i64) -> JSRelationCondition;
887
888        #[cfg(feature = "JSValueRef")]
889        /// Compare a JSValue with a
890        /// <code>
891        /// uint64_t
892        /// </code>
893        /// .
894        /// The
895        /// <code>
896        /// uint64_t
897        /// </code>
898        /// to compare with.
899        ///
900        /// Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
901        ///
902        /// The JSValue is converted to an integer according to the rules specified by the JavaScript language then compared with
903        /// <code>
904        /// other
905        /// </code>
906        /// .
907        #[unsafe(method(compareUInt64:))]
908        #[unsafe(method_family = none)]
909        pub unsafe fn compareUInt64(&self, other: u64) -> JSRelationCondition;
910
911        #[cfg(feature = "JSValueRef")]
912        /// Compare a JSValue with a double.
913        /// The double to compare with.
914        ///
915        /// Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
916        ///
917        /// The JSValue is converted to a double according to the rules specified by the JavaScript language then compared with
918        /// <code>
919        /// other
920        /// </code>
921        /// .
922        #[unsafe(method(compareDouble:))]
923        #[unsafe(method_family = none)]
924        pub unsafe fn compareDouble(&self, other: c_double) -> JSRelationCondition;
925
926        #[cfg(feature = "objc2-foundation")]
927        /// Invoke a JSValue as a function.
928        ///
929        /// In JavaScript, if a function doesn't explicitly return a value then it
930        /// implicitly returns the JavaScript value
931        /// <code>
932        /// undefined
933        /// </code>
934        /// .
935        ///
936        /// Parameter `arguments`: The arguments to pass to the function.
937        ///
938        /// Returns: The return value of the function call.
939        ///
940        /// # Safety
941        ///
942        /// - `arguments` generic should be of the correct type.
943        /// - `arguments` might not allow `None`.
944        #[unsafe(method(callWithArguments:))]
945        #[unsafe(method_family = none)]
946        pub unsafe fn callWithArguments(
947            &self,
948            arguments: Option<&NSArray>,
949        ) -> Option<Retained<JSValue>>;
950
951        #[cfg(feature = "objc2-foundation")]
952        /// Invoke a JSValue as a constructor.
953        ///
954        /// This is equivalent to using the
955        /// <code>
956        /// new
957        /// </code>
958        /// syntax in JavaScript.
959        ///
960        /// Parameter `arguments`: The arguments to pass to the constructor.
961        ///
962        /// Returns: The return value of the constructor call.
963        ///
964        /// # Safety
965        ///
966        /// - `arguments` generic should be of the correct type.
967        /// - `arguments` might not allow `None`.
968        #[unsafe(method(constructWithArguments:))]
969        #[unsafe(method_family = none)]
970        pub unsafe fn constructWithArguments(
971            &self,
972            arguments: Option<&NSArray>,
973        ) -> Option<Retained<JSValue>>;
974
975        #[cfg(feature = "objc2-foundation")]
976        /// Invoke a method on a JSValue.
977        ///
978        /// Accesses the property named
979        /// <code>
980        /// method
981        /// </code>
982        /// from this value and
983        /// calls the resulting value as a function, passing this JSValue as the
984        /// <code>
985        /// this
986        /// </code>
987        /// value along with the specified arguments.
988        ///
989        /// Parameter `method`: The name of the method to be invoked.
990        ///
991        /// Parameter `arguments`: The arguments to pass to the method.
992        ///
993        /// Returns: The return value of the method call.
994        ///
995        /// # Safety
996        ///
997        /// - `method` might not allow `None`.
998        /// - `arguments` generic should be of the correct type.
999        /// - `arguments` might not allow `None`.
1000        #[unsafe(method(invokeMethod:withArguments:))]
1001        #[unsafe(method_family = none)]
1002        pub unsafe fn invokeMethod_withArguments(
1003            &self,
1004            method: Option<&NSString>,
1005            arguments: Option<&NSArray>,
1006        ) -> Option<Retained<JSValue>>;
1007    );
1008}
1009
1010/// Methods declared on superclass `NSObject`.
1011#[cfg(feature = "objc2")]
1012impl JSValue {
1013    extern_methods!(
1014        #[unsafe(method(init))]
1015        #[unsafe(method_family = init)]
1016        pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
1017
1018        #[unsafe(method(new))]
1019        #[unsafe(method_family = new)]
1020        pub unsafe fn new() -> Retained<Self>;
1021    );
1022}
1023
1024/// StructSupport.
1025///
1026/// Objective-C methods exported to JavaScript may have argument and/or return
1027/// values of struct types, provided that conversion to and from the struct is
1028/// supported by JSValue. Support is provided for any types where JSValue
1029/// contains both a class method
1030/// <code>
1031/// valueWith
1032/// <Type
1033/// >:inContext:
1034/// </code>
1035/// , and and instance
1036/// method
1037/// <code>
1038/// to
1039/// <Type
1040/// >
1041/// </code>
1042/// - where the string
1043/// <code>
1044/// <Type
1045/// >
1046/// </code>
1047/// in these selector names match,
1048/// with the first argument to the former being of the same struct type as the
1049/// return type of the latter.
1050/// Support is provided for structs of type CGPoint, NSRange, CGRect and CGSize.
1051#[cfg(feature = "objc2")]
1052impl JSValue {
1053    extern_methods!(
1054        #[cfg(all(feature = "JSContext", feature = "objc2-core-foundation"))]
1055        /// Create a JSValue from a CGPoint.
1056        ///
1057        /// Returns: A newly allocated JavaScript object containing properties
1058        /// named
1059        /// <code>
1060        /// x
1061        /// </code>
1062        /// and
1063        /// <code>
1064        /// y
1065        /// </code>
1066        /// , with values from the CGPoint.
1067        ///
1068        /// # Safety
1069        ///
1070        /// `context` might not allow `None`.
1071        #[unsafe(method(valueWithPoint:inContext:))]
1072        #[unsafe(method_family = none)]
1073        pub unsafe fn valueWithPoint_inContext(
1074            point: CGPoint,
1075            context: Option<&JSContext>,
1076        ) -> Option<Retained<JSValue>>;
1077
1078        #[cfg(all(feature = "JSContext", feature = "objc2-foundation"))]
1079        /// Create a JSValue from a NSRange.
1080        ///
1081        /// Returns: A newly allocated JavaScript object containing properties
1082        /// named
1083        /// <code>
1084        /// location
1085        /// </code>
1086        /// and
1087        /// <code>
1088        /// length
1089        /// </code>
1090        /// , with values from the NSRange.
1091        ///
1092        /// # Safety
1093        ///
1094        /// `context` might not allow `None`.
1095        #[unsafe(method(valueWithRange:inContext:))]
1096        #[unsafe(method_family = none)]
1097        pub unsafe fn valueWithRange_inContext(
1098            range: NSRange,
1099            context: Option<&JSContext>,
1100        ) -> Option<Retained<JSValue>>;
1101
1102        #[cfg(all(feature = "JSContext", feature = "objc2-core-foundation"))]
1103        /// Create a JSValue from a CGRect.
1104        ///
1105        /// Returns: A newly allocated JavaScript object containing properties
1106        /// named
1107        /// <code>
1108        /// x
1109        /// </code>
1110        /// ,
1111        /// <code>
1112        /// y
1113        /// </code>
1114        /// ,
1115        /// <code>
1116        /// width
1117        /// </code>
1118        /// , and
1119        /// <code>
1120        /// height
1121        /// </code>
1122        /// , with values from the CGRect.
1123        ///
1124        /// # Safety
1125        ///
1126        /// `context` might not allow `None`.
1127        #[unsafe(method(valueWithRect:inContext:))]
1128        #[unsafe(method_family = none)]
1129        pub unsafe fn valueWithRect_inContext(
1130            rect: CGRect,
1131            context: Option<&JSContext>,
1132        ) -> Option<Retained<JSValue>>;
1133
1134        #[cfg(all(feature = "JSContext", feature = "objc2-core-foundation"))]
1135        /// Create a JSValue from a CGSize.
1136        ///
1137        /// Returns: A newly allocated JavaScript object containing properties
1138        /// named
1139        /// <code>
1140        /// width
1141        /// </code>
1142        /// and
1143        /// <code>
1144        /// height
1145        /// </code>
1146        /// , with values from the CGSize.
1147        ///
1148        /// # Safety
1149        ///
1150        /// `context` might not allow `None`.
1151        #[unsafe(method(valueWithSize:inContext:))]
1152        #[unsafe(method_family = none)]
1153        pub unsafe fn valueWithSize_inContext(
1154            size: CGSize,
1155            context: Option<&JSContext>,
1156        ) -> Option<Retained<JSValue>>;
1157
1158        #[cfg(feature = "objc2-core-foundation")]
1159        /// Convert a JSValue to a CGPoint.
1160        ///
1161        /// Reads the properties named
1162        /// <code>
1163        /// x
1164        /// </code>
1165        /// and
1166        /// <code>
1167        /// y
1168        /// </code>
1169        /// from
1170        /// this JSValue, and converts the results to double.
1171        ///
1172        /// Returns: The new CGPoint.
1173        #[unsafe(method(toPoint))]
1174        #[unsafe(method_family = none)]
1175        pub unsafe fn toPoint(&self) -> CGPoint;
1176
1177        #[cfg(feature = "objc2-foundation")]
1178        /// Convert a JSValue to an NSRange.
1179        ///
1180        /// Reads the properties named
1181        /// <code>
1182        /// location
1183        /// </code>
1184        /// and
1185        /// <code>
1186        /// length
1187        /// </code>
1188        /// from this JSValue and converts the results to double.
1189        ///
1190        /// Returns: The new NSRange.
1191        #[unsafe(method(toRange))]
1192        #[unsafe(method_family = none)]
1193        pub unsafe fn toRange(&self) -> NSRange;
1194
1195        #[cfg(feature = "objc2-core-foundation")]
1196        /// Convert a JSValue to a CGRect.
1197        ///
1198        /// Reads the properties named
1199        /// <code>
1200        /// x
1201        /// </code>
1202        /// ,
1203        /// <code>
1204        /// y
1205        /// </code>
1206        /// ,
1207        /// <code>
1208        /// width
1209        /// </code>
1210        /// , and
1211        /// <code>
1212        /// height
1213        /// </code>
1214        /// from this JSValue and converts the results to double.
1215        ///
1216        /// Returns: The new CGRect.
1217        #[unsafe(method(toRect))]
1218        #[unsafe(method_family = none)]
1219        pub unsafe fn toRect(&self) -> CGRect;
1220
1221        #[cfg(feature = "objc2-core-foundation")]
1222        /// Convert a JSValue to a CGSize.
1223        ///
1224        /// Reads the properties named
1225        /// <code>
1226        /// width
1227        /// </code>
1228        /// and
1229        /// <code>
1230        /// height
1231        /// </code>
1232        /// from this JSValue and converts the results to double.
1233        ///
1234        /// Returns: The new CGSize.
1235        #[unsafe(method(toSize))]
1236        #[unsafe(method_family = none)]
1237        pub unsafe fn toSize(&self) -> CGSize;
1238    );
1239}
1240
1241/// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jsvalueproperty?language=objc)
1242#[cfg(feature = "objc2-foundation")]
1243pub type JSValueProperty = NSString;
1244
1245/// PropertyAccess.
1246///
1247/// These methods enable querying properties on a JSValue.
1248#[cfg(feature = "objc2")]
1249impl JSValue {
1250    extern_methods!(
1251        #[cfg(feature = "objc2-foundation")]
1252        /// Access a property of a JSValue.
1253        ///
1254        /// Returns: The JSValue for the requested property or the JSValue
1255        /// <code>
1256        /// undefined
1257        /// </code>
1258        /// if the property does not exist.
1259        ///
1260        /// Corresponds to the JavaScript operation
1261        /// <code>
1262        /// object[property]
1263        /// </code>
1264        /// . Starting with macOS 10.15 and iOS 13, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of
1265        /// <code>
1266        /// valueWithObject:inContext:
1267        /// </code>
1268        /// . Prior to macOS 10.15 and iOS 13, 'property' was expected to be an NSString *.
1269        ///
1270        /// # Safety
1271        ///
1272        /// `property` might not allow `None`.
1273        #[unsafe(method(valueForProperty:))]
1274        #[unsafe(method_family = none)]
1275        pub unsafe fn valueForProperty(
1276            &self,
1277            property: Option<&JSValueProperty>,
1278        ) -> Option<Retained<JSValue>>;
1279
1280        #[cfg(feature = "objc2-foundation")]
1281        /// Set a property on a JSValue.
1282        ///
1283        /// Corresponds to the JavaScript operation
1284        /// <code>
1285        /// object[property] = value
1286        /// </code>
1287        /// . Starting with macOS 10.15 and iOS 13, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of
1288        /// <code>
1289        /// valueWithObject:inContext:
1290        /// </code>
1291        /// . Prior to macOS 10.15 and iOS 13, 'property' was expected to be an NSString *.
1292        ///
1293        /// # Safety
1294        ///
1295        /// - `value` should be of the correct type.
1296        /// - `value` might not allow `None`.
1297        /// - `property` might not allow `None`.
1298        #[unsafe(method(setValue:forProperty:))]
1299        #[unsafe(method_family = none)]
1300        pub unsafe fn setValue_forProperty(
1301            &self,
1302            value: Option<&AnyObject>,
1303            property: Option<&JSValueProperty>,
1304        );
1305
1306        #[cfg(feature = "objc2-foundation")]
1307        /// Delete a property from a JSValue.
1308        ///
1309        /// Returns: YES if deletion is successful, NO otherwise.
1310        ///
1311        /// Corresponds to the JavaScript operation
1312        /// <code>
1313        /// delete object[property]
1314        /// </code>
1315        /// . Starting with macOS 10.15 and iOS 13, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of
1316        /// <code>
1317        /// valueWithObject:inContext:
1318        /// </code>
1319        /// . Prior to macOS 10.15 and iOS 13, 'property' was expected to be an NSString *.
1320        ///
1321        /// # Safety
1322        ///
1323        /// `property` might not allow `None`.
1324        #[unsafe(method(deleteProperty:))]
1325        #[unsafe(method_family = none)]
1326        pub unsafe fn deleteProperty(&self, property: Option<&JSValueProperty>) -> bool;
1327
1328        #[cfg(feature = "objc2-foundation")]
1329        /// Check if a JSValue has a property.
1330        ///
1331        /// This method has the same function as the JavaScript operator
1332        /// <code>
1333        /// in
1334        /// </code>
1335        /// .
1336        ///
1337        /// Returns: Returns YES if property is present on the value.
1338        ///
1339        /// Corresponds to the JavaScript operation
1340        /// <code>
1341        /// property in object
1342        /// </code>
1343        /// . Starting with macOS 10.15 and iOS 13, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of
1344        /// <code>
1345        /// valueWithObject:inContext:
1346        /// </code>
1347        /// . Prior to macOS 10.15 and iOS 13, 'property' was expected to be an NSString *.
1348        ///
1349        /// # Safety
1350        ///
1351        /// `property` might not allow `None`.
1352        #[unsafe(method(hasProperty:))]
1353        #[unsafe(method_family = none)]
1354        pub unsafe fn hasProperty(&self, property: Option<&JSValueProperty>) -> bool;
1355
1356        #[cfg(feature = "objc2-foundation")]
1357        /// Define properties with custom descriptors on JSValues.
1358        ///
1359        /// This method may be used to create a data or accessor property on an object.
1360        /// This method operates in accordance with the Object.defineProperty method in the JavaScript language. Starting with macOS 10.15 and iOS 13, 'property' can be any 'id' and will be converted to a JSValue using the conversion rules of
1361        /// <code>
1362        /// valueWithObject:inContext:
1363        /// </code>
1364        /// . Prior to macOS 10.15 and iOS 13, 'property' was expected to be an NSString *.
1365        ///
1366        /// # Safety
1367        ///
1368        /// - `property` might not allow `None`.
1369        /// - `descriptor` should be of the correct type.
1370        /// - `descriptor` might not allow `None`.
1371        #[unsafe(method(defineProperty:descriptor:))]
1372        #[unsafe(method_family = none)]
1373        pub unsafe fn defineProperty_descriptor(
1374            &self,
1375            property: Option<&JSValueProperty>,
1376            descriptor: Option<&AnyObject>,
1377        );
1378
1379        /// Access an indexed (numerical) property on a JSValue.
1380        ///
1381        /// Returns: The JSValue for the property at the specified index.
1382        /// Returns the JavaScript value
1383        /// <code>
1384        /// undefined
1385        /// </code>
1386        /// if no property exists at that index.
1387        #[unsafe(method(valueAtIndex:))]
1388        #[unsafe(method_family = none)]
1389        pub unsafe fn valueAtIndex(&self, index: NSUInteger) -> Option<Retained<JSValue>>;
1390
1391        /// Set an indexed (numerical) property on a JSValue.
1392        ///
1393        /// For JSValues that are JavaScript arrays, indices greater than
1394        /// UINT_MAX - 1 will not affect the length of the array.
1395        ///
1396        /// # Safety
1397        ///
1398        /// - `value` should be of the correct type.
1399        /// - `value` might not allow `None`.
1400        #[unsafe(method(setValue:atIndex:))]
1401        #[unsafe(method_family = none)]
1402        pub unsafe fn setValue_atIndex(&self, value: Option<&AnyObject>, index: NSUInteger);
1403    );
1404}
1405
1406/// SubscriptSupport.
1407///
1408/// Instances of JSValue implement the following methods in order to enable
1409/// support for subscript access by key and index, for example:
1410///
1411/// ```text
1412///     JSValue *objectA, *objectB;
1413///     JSValue *v1 = object[@"X"]; // Get value for property "X" from 'object'.
1414///     JSValue *v2 = object[42];   // Get value for index 42 from 'object'.
1415///     object[@"Y"] = v1;          // Assign 'v1' to property "Y" of 'object'.
1416///     object[101] = v2;           // Assign 'v2' to index 101 of 'object'.
1417/// ```
1418///
1419/// An object key passed as a subscript will be converted to a JavaScript value,
1420/// and then the value using the same rules as
1421/// <code>
1422/// valueWithObject:inContext:
1423/// </code>
1424/// . In macOS
1425/// 10.14 and iOS 12 and below, the
1426/// <code>
1427/// key
1428/// </code>
1429/// argument of
1430/// <code>
1431/// setObject:object forKeyedSubscript:key
1432/// </code>
1433/// was restricted to an
1434/// <code>
1435/// NSObject
1436/// <NSCopying
1437/// > *
1438/// </code>
1439/// but that restriction was never used internally.
1440#[cfg(feature = "objc2")]
1441impl JSValue {
1442    extern_methods!(
1443        /// # Safety
1444        ///
1445        /// - `key` should be of the correct type.
1446        /// - `key` might not allow `None`.
1447        #[unsafe(method(objectForKeyedSubscript:))]
1448        #[unsafe(method_family = none)]
1449        pub unsafe fn objectForKeyedSubscript(
1450            &self,
1451            key: Option<&AnyObject>,
1452        ) -> Option<Retained<JSValue>>;
1453
1454        #[unsafe(method(objectAtIndexedSubscript:))]
1455        #[unsafe(method_family = none)]
1456        pub unsafe fn objectAtIndexedSubscript(
1457            &self,
1458            index: NSUInteger,
1459        ) -> Option<Retained<JSValue>>;
1460
1461        /// # Safety
1462        ///
1463        /// - `object` should be of the correct type.
1464        /// - `object` might not allow `None`.
1465        /// - `key` should be of the correct type.
1466        /// - `key` might not allow `None`.
1467        #[unsafe(method(setObject:forKeyedSubscript:))]
1468        #[unsafe(method_family = none)]
1469        pub unsafe fn setObject_forKeyedSubscript(
1470            &self,
1471            object: Option<&AnyObject>,
1472            key: Option<&AnyObject>,
1473        );
1474
1475        /// # Safety
1476        ///
1477        /// - `object` should be of the correct type.
1478        /// - `object` might not allow `None`.
1479        #[unsafe(method(setObject:atIndexedSubscript:))]
1480        #[unsafe(method_family = none)]
1481        pub unsafe fn setObject_atIndexedSubscript(
1482            &self,
1483            object: Option<&AnyObject>,
1484            index: NSUInteger,
1485        );
1486    );
1487}
1488
1489/// JSValueRefSupport.
1490///
1491/// These functions are for bridging between the C API and the Objective-C API.
1492#[cfg(feature = "objc2")]
1493impl JSValue {
1494    extern_methods!(
1495        #[cfg(all(feature = "JSBase", feature = "JSContext"))]
1496        /// Creates a JSValue, wrapping its C API counterpart.
1497        ///
1498        /// Returns: The Objective-C API equivalent of the specified JSValueRef.
1499        ///
1500        /// # Safety
1501        ///
1502        /// - `value` must be a valid pointer.
1503        /// - `context` might not allow `None`.
1504        #[unsafe(method(valueWithJSValueRef:inContext:))]
1505        #[unsafe(method_family = none)]
1506        pub unsafe fn valueWithJSValueRef_inContext(
1507            value: JSValueRef,
1508            context: Option<&JSContext>,
1509        ) -> Option<Retained<JSValue>>;
1510
1511        #[cfg(feature = "JSBase")]
1512        /// Returns the C API counterpart wrapped by a JSContext.
1513        ///
1514        /// Returns: The C API equivalent of this JSValue.
1515        #[unsafe(method(JSValueRef))]
1516        #[unsafe(method_family = none)]
1517        pub unsafe fn JSValueRef(&self) -> JSValueRef;
1518    );
1519}
1520
1521extern "C" {
1522    /// Property Descriptor Constants
1523    ///
1524    /// These keys may assist in creating a property descriptor for use with the
1525    /// defineProperty method on JSValue.
1526    /// Property descriptors must fit one of three descriptions:
1527    ///
1528    /// Data Descriptor:
1529    /// - A descriptor containing one or both of the keys
1530    /// <code>
1531    /// value
1532    /// </code>
1533    /// and
1534    /// <code>
1535    /// writable
1536    /// </code>
1537    /// ,
1538    /// and optionally containing one or both of the keys
1539    /// <code>
1540    /// enumerable
1541    /// </code>
1542    /// and
1543    /// <code>
1544    /// configurable
1545    /// </code>
1546    /// . A data descriptor may not contain either the
1547    /// <code>
1548    /// get
1549    /// </code>
1550    /// or
1551    /// <code>
1552    /// set
1553    /// </code>
1554    /// key.
1555    /// A data descriptor may be used to create or modify the attributes of a
1556    /// data property on an object (replacing any existing accessor property).
1557    ///
1558    /// Accessor Descriptor:
1559    /// - A descriptor containing one or both of the keys
1560    /// <code>
1561    /// get
1562    /// </code>
1563    /// and
1564    /// <code>
1565    /// set
1566    /// </code>
1567    /// , and
1568    /// optionally containing one or both of the keys
1569    /// <code>
1570    /// enumerable
1571    /// </code>
1572    /// and
1573    /// <code>
1574    /// configurable
1575    /// </code>
1576    /// . An accessor descriptor may not contain either the
1577    /// <code>
1578    /// value
1579    /// </code>
1580    /// or
1581    /// <code>
1582    /// writable
1583    /// </code>
1584    /// key.
1585    /// An accessor descriptor may be used to create or modify the attributes of
1586    /// an accessor property on an object (replacing any existing data property).
1587    ///
1588    /// Generic Descriptor:
1589    /// - A descriptor containing one or both of the keys
1590    /// <code>
1591    /// enumerable
1592    /// </code>
1593    /// and
1594    /// <code>
1595    /// configurable
1596    /// </code>
1597    /// . A generic descriptor may not contain any of the keys
1598    /// <code>
1599    /// value
1600    /// </code>
1601    /// ,
1602    /// <code>
1603    /// writable
1604    /// </code>
1605    /// ,
1606    /// <code>
1607    /// get
1608    /// </code>
1609    /// , or
1610    /// <code>
1611    /// set
1612    /// </code>
1613    /// .
1614    /// A generic descriptor may be used to modify the attributes of an existing
1615    /// data or accessor property, or to create a new data property.
1616    ///
1617    /// See also [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorwritablekey?language=objc)
1618    #[cfg(feature = "objc2-foundation")]
1619    pub static JSPropertyDescriptorWritableKey: Option<&'static NSString>;
1620}
1621
1622extern "C" {
1623    /// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorenumerablekey?language=objc)
1624    #[cfg(feature = "objc2-foundation")]
1625    pub static JSPropertyDescriptorEnumerableKey: Option<&'static NSString>;
1626}
1627
1628extern "C" {
1629    /// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorconfigurablekey?language=objc)
1630    #[cfg(feature = "objc2-foundation")]
1631    pub static JSPropertyDescriptorConfigurableKey: Option<&'static NSString>;
1632}
1633
1634extern "C" {
1635    /// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorvaluekey?language=objc)
1636    #[cfg(feature = "objc2-foundation")]
1637    pub static JSPropertyDescriptorValueKey: Option<&'static NSString>;
1638}
1639
1640extern "C" {
1641    /// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorgetkey?language=objc)
1642    #[cfg(feature = "objc2-foundation")]
1643    pub static JSPropertyDescriptorGetKey: Option<&'static NSString>;
1644}
1645
1646extern "C" {
1647    /// [Apple's documentation](https://developer.apple.com/documentation/javascriptcore/jspropertydescriptorsetkey?language=objc)
1648    #[cfg(feature = "objc2-foundation")]
1649    pub static JSPropertyDescriptorSetKey: Option<&'static NSString>;
1650}