#[repr(C)]pub struct JSValue { /* private fields */ }
JSValue
and objc2
only.Expand description
A JSValue is a reference to a JavaScript value. Every JSValue originates from a JSContext and holds a strong reference to it. When a JSValue instance method creates a new JSValue, the new value originates from the same JSContext.
All JSValues values also originate from a JSVirtualMachine (available indirectly via the context property). It is an error to pass a JSValue to a method or property of a JSValue or JSContext originating from a different JSVirtualMachine. Doing so will raise an Objective-C exception.
See also Apple’s documentation
Implementations§
Source§impl JSValue
impl JSValue
Sourcepub unsafe fn context(&self) -> Option<Retained<JSContext>>
Available on crate feature JSContext
only.
pub unsafe fn context(&self) -> Option<Retained<JSContext>>
JSContext
only.The JSContext that this value originates from.
Sourcepub unsafe fn valueWithObject_inContext(
value: Option<&AnyObject>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithObject_inContext( value: Option<&AnyObject>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a JSValue by converting an Objective-C object.
The resulting JSValue retains the provided Objective-C object.
Parameter value
: The Objective-C object to be converted.
Returns: The new JSValue.
Sourcepub unsafe fn valueWithBool_inContext(
value: bool,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithBool_inContext( value: bool, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a JavaScript value from a BOOL primitive.
Parameter context
: The JSContext in which the resulting JSValue will be created.
Returns: The new JSValue representing the equivalent boolean value.
Sourcepub unsafe fn valueWithDouble_inContext(
value: c_double,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithDouble_inContext( value: c_double, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a JavaScript value from a double primitive.
Parameter context
: The JSContext in which the resulting JSValue will be created.
Returns: The new JSValue representing the equivalent boolean value.
Sourcepub unsafe fn valueWithInt32_inContext(
value: i32,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithInt32_inContext( value: i32, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a JavaScript value from an
int32_t
primitive.
Parameter context
: The JSContext in which the resulting JSValue will be created.
Returns: The new JSValue representing the equivalent boolean value.
Sourcepub unsafe fn valueWithUInt32_inContext(
value: u32,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithUInt32_inContext( value: u32, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a JavaScript value from a
uint32_t
primitive.
Parameter context
: The JSContext in which the resulting JSValue will be created.
Returns: The new JSValue representing the equivalent boolean value.
Sourcepub unsafe fn valueWithNewObjectInContext(
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewObjectInContext( context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new, empty JavaScript object.
Parameter context
: The JSContext in which the resulting object will be created.
Returns: The new JavaScript object.
Sourcepub unsafe fn valueWithNewArrayInContext(
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewArrayInContext( context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new, empty JavaScript array.
Parameter context
: The JSContext in which the resulting array will be created.
Returns: The new JavaScript array.
Sourcepub unsafe fn valueWithNewRegularExpressionFromPattern_flags_inContext(
pattern: Option<&NSString>,
flags: Option<&NSString>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-foundation
only.
pub unsafe fn valueWithNewRegularExpressionFromPattern_flags_inContext( pattern: Option<&NSString>, flags: Option<&NSString>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-foundation
only.Create a new JavaScript regular expression object.
Parameter pattern
: The regular expression pattern.
Parameter flags
: The regular expression flags.
Parameter context
: The JSContext in which the resulting regular expression object will be created.
Returns: The new JavaScript regular expression object.
Sourcepub unsafe fn valueWithNewErrorFromMessage_inContext(
message: Option<&NSString>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-foundation
only.
pub unsafe fn valueWithNewErrorFromMessage_inContext( message: Option<&NSString>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-foundation
only.Create a new JavaScript error object.
Parameter message
: The error message.
Parameter context
: The JSContext in which the resulting error object will be created.
Returns: The new JavaScript error object.
Sourcepub unsafe fn valueWithNewPromiseInContext_fromExecutor(
context: Option<&JSContext>,
callback: Option<&DynBlock<dyn Fn(*mut JSValue, *mut JSValue)>>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and block2
only.
pub unsafe fn valueWithNewPromiseInContext_fromExecutor( context: Option<&JSContext>, callback: Option<&DynBlock<dyn Fn(*mut JSValue, *mut JSValue)>>, ) -> Option<Retained<JSValue>>
JSContext
and block2
only.Create a new promise object using the provided executor callback.
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.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a new promise JavaScript object.
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
nil
. This also means means the new promise object may be accessed via
[context thisValue]
.
Sourcepub unsafe fn valueWithNewPromiseResolvedWithResult_inContext(
result: Option<&AnyObject>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewPromiseResolvedWithResult_inContext( result: Option<&AnyObject>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new resolved promise object with the provided value.
Parameter result
: The result value to be passed to any reactions.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a new promise JavaScript object.
This method is equivalent to calling
[JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [resolve callWithArguments:
@
[result]]; } inContext:context]
Sourcepub unsafe fn valueWithNewPromiseRejectedWithReason_inContext(
reason: Option<&AnyObject>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewPromiseRejectedWithReason_inContext( reason: Option<&AnyObject>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new rejected promise object with the provided value.
Parameter reason
: The result value to be passed to any reactions.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a new promise JavaScript object.
This method is equivalent to calling
[JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [reject callWithArguments:
@
[reason]]; } inContext:context]
Sourcepub unsafe fn valueWithNewSymbolFromDescription_inContext(
description: Option<&NSString>,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-foundation
only.
pub unsafe fn valueWithNewSymbolFromDescription_inContext( description: Option<&NSString>, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-foundation
only.Create a new, unique, symbol object.
Parameter description
: The description of the symbol object being created.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a unique JavaScript value with type symbol.
Sourcepub unsafe fn valueWithNewBigIntFromString_inContext(
string: &NSString,
context: &JSContext,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-foundation
only.
pub unsafe fn valueWithNewBigIntFromString_inContext( string: &NSString, context: &JSContext, ) -> Option<Retained<JSValue>>
JSContext
and objc2-foundation
only.Create a new BigInt value from a numeric string.
Parameter string
: The string representation of the BigInt JavaScript value being created.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a JavaScript value with type BigInt.
This is equivalent to calling the
BigInt
constructor from JavaScript with a string argument.
Sourcepub unsafe fn valueWithNewBigIntFromInt64_inContext(
int64: i64,
context: &JSContext,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewBigIntFromInt64_inContext( int64: i64, context: &JSContext, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new BigInt value from a
int64_t
.
Parameter int64
: The signed 64-bit integer of the BigInt JavaScript value being created.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a JavaScript value with type BigInt.
Sourcepub unsafe fn valueWithNewBigIntFromUInt64_inContext(
uint64: u64,
context: &JSContext,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewBigIntFromUInt64_inContext( uint64: u64, context: &JSContext, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new BigInt value from a
uint64_t
.
Parameter uint64
: The unsigned 64-bit integer of the BigInt JavaScript value being created.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a JavaScript value with type BigInt.
Sourcepub unsafe fn valueWithNewBigIntFromDouble_inContext(
value: c_double,
context: &JSContext,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNewBigIntFromDouble_inContext( value: c_double, context: &JSContext, ) -> Option<Retained<JSValue>>
JSContext
only.Create a new BigInt value from a double.
Parameter value
: The value of the BigInt JavaScript value being created.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing a JavaScript value with type BigInt.
If the value is not an integer, an exception is thrown.
Sourcepub unsafe fn valueWithNullInContext(
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithNullInContext( context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create the JavaScript value
null
.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing the JavaScript value
null
.
Sourcepub unsafe fn valueWithUndefinedInContext(
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate feature JSContext
only.
pub unsafe fn valueWithUndefinedInContext( context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
only.Create the JavaScript value
undefined
.
Parameter context
: The JSContext to which the resulting JSValue belongs.
Returns: The JSValue representing the JavaScript value
undefined
.
Sourcepub unsafe fn toObject(&self) -> Option<Retained<AnyObject>>
pub unsafe fn toObject(&self) -> Option<Retained<AnyObject>>
When converting between JavaScript values and Objective-C objects a copy is performed. Values of types listed below are copied to the corresponding types on conversion in each direction. For NSDictionaries, entries in the dictionary that are keyed by strings are copied onto a JavaScript object. For dictionaries and arrays, conversion is recursive, with the same object conversion being applied to all entries in the collection.
```text Objective-C type | JavaScript type --------------------+--------------------- nil | undefined NSNull | null NSString | string NSNumber | number, boolean NSDictionary | Object object NSArray | Array object NSDate | Date object NSBlock (1) | Function object (1) id (2) | Wrapper object (2) Class (3) | Constructor object (3) ```
(1) Instances of NSBlock with supported arguments types will be presented to JavaScript as a callable Function object. For more information on supported argument types see JSExport.h. If a JavaScript Function originating from an Objective-C block is converted back to an Objective-C object the block will be returned. All other JavaScript functions will be converted in the same manner as a JavaScript object of type Object.
(2) For Objective-C instances that do not derive from the set of types listed above, a wrapper object to provide a retaining handle to the Objective-C instance from JavaScript. For more information on these wrapper objects, see JSExport.h. When a JavaScript wrapper object is converted back to Objective-C the Objective-C instance being retained by the wrapper is returned.
(3) For Objective-C Class objects a constructor object containing exported class methods will be returned. See JSExport.h for more information on constructor objects.
For all methods taking arguments of type id, arguments will be converted into a JavaScript value according to the above conversion.
Convert this JSValue to an Objective-C object.
The JSValue is converted to an Objective-C object according to the conversion rules specified above.
Returns: The Objective-C representation of this JSValue.
Sourcepub unsafe fn toObjectOfClass(
&self,
expected_class: Option<&AnyClass>,
) -> Option<Retained<AnyObject>>
pub unsafe fn toObjectOfClass( &self, expected_class: Option<&AnyClass>, ) -> Option<Retained<AnyObject>>
Convert a JSValue to an Objective-C object of a specific class.
The JSValue is converted to an Objective-C object of the specified Class.
If the result is not of the specified Class then
nil
will be returned.
Returns: An Objective-C object of the specified Class or
nil
.
Sourcepub unsafe fn toBool(&self) -> bool
pub unsafe fn toBool(&self) -> bool
Convert a JSValue to a boolean.
The JSValue is converted to a boolean according to the rules specified by the JavaScript language.
Returns: The boolean result of the conversion.
Sourcepub unsafe fn toDouble(&self) -> c_double
pub unsafe fn toDouble(&self) -> c_double
Convert a JSValue to a double.
Returns: The double result of the conversion.
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
Number(value)
in JavaScript.
Sourcepub unsafe fn toInt32(&self) -> i32
pub unsafe fn toInt32(&self) -> i32
Convert a JSValue to an
int32_t
.
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
int32_t
.
Returns: The
int32_t
result of the conversion.
Sourcepub unsafe fn toUInt32(&self) -> u32
pub unsafe fn toUInt32(&self) -> u32
Convert a JSValue to a
uint32_t
.
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
uint32_t
.
Returns: The
uint32_t
result of the conversion.
Sourcepub unsafe fn toInt64(&self) -> i64
pub unsafe fn toInt64(&self) -> i64
Convert a JSValue to a
int64_t
.
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
int64_t
.
Sourcepub unsafe fn toUInt64(&self) -> u64
pub unsafe fn toUInt64(&self) -> u64
Convert a JSValue to a
uint64_t
.
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
uint64_t
.
Sourcepub unsafe fn toNumber(&self) -> Option<Retained<NSNumber>>
Available on crate feature objc2-foundation
only.
pub unsafe fn toNumber(&self) -> Option<Retained<NSNumber>>
objc2-foundation
only.Convert a JSValue to a NSNumber.
If the JSValue represents a boolean, a NSNumber value of YES or NO
will be returned. For all other types, the result is equivalent to
Number(value)
in JavaScript.
Returns: The NSNumber result of the conversion.
Sourcepub unsafe fn toString(&self) -> Option<Retained<NSString>>
Available on crate feature objc2-foundation
only.
pub unsafe fn toString(&self) -> Option<Retained<NSString>>
objc2-foundation
only.Convert a JSValue to a NSString.
The JSValue is converted to a string according to the rules specified by the JavaScript language.
Returns: The NSString containing the result of the conversion.
Sourcepub unsafe fn toDate(&self) -> Option<Retained<NSDate>>
Available on crate feature objc2-foundation
only.
pub unsafe fn toDate(&self) -> Option<Retained<NSDate>>
objc2-foundation
only.Convert a JSValue to a NSDate.
The value is converted to a number representing a time interval since 1970 which is then used to create a new NSDate instance.
Returns: The NSDate created using the converted time interval.
Sourcepub unsafe fn toArray(&self) -> Option<Retained<NSArray>>
Available on crate feature objc2-foundation
only.
pub unsafe fn toArray(&self) -> Option<Retained<NSArray>>
objc2-foundation
only.Convert a JSValue to a NSArray.
If the value is
null
or
undefined
then
nil
is returned.
If the value is not an object then a JavaScript TypeError will be thrown.
The property
length
is read from the object, converted to an unsigned
integer, and an NSArray of this size is allocated. Properties corresponding
to indices within the array bounds will be copied to the array, with
JSValues converted to equivalent Objective-C objects as specified.
Returns: The NSArray containing the recursively converted contents of the converted JavaScript array.
Sourcepub unsafe fn toDictionary(&self) -> Option<Retained<NSDictionary>>
Available on crate feature objc2-foundation
only.
pub unsafe fn toDictionary(&self) -> Option<Retained<NSDictionary>>
objc2-foundation
only.Convert a JSValue to a NSDictionary.
If the value is
null
or
undefined
then
nil
is returned.
If the value is not an object then a JavaScript TypeError will be thrown.
All enumerable properties of the object are copied to the dictionary, with
JSValues converted to equivalent Objective-C objects as specified.
Returns: The NSDictionary containing the recursively converted contents of the converted JavaScript object.
Sourcepub unsafe fn isUndefined(&self) -> bool
pub unsafe fn isUndefined(&self) -> bool
Check if a JSValue corresponds to the JavaScript value
undefined
.
Sourcepub unsafe fn isNull(&self) -> bool
pub unsafe fn isNull(&self) -> bool
Check if a JSValue corresponds to the JavaScript value
null
.
Sourcepub unsafe fn isNumber(&self) -> bool
pub unsafe fn isNumber(&self) -> bool
Check if a JSValue is a number.
In JavaScript, there is no differentiation between types of numbers. Semantically all numbers behave like doubles except in special cases like bit operations.
Sourcepub unsafe fn isInstanceOf(&self, value: Option<&AnyObject>) -> bool
pub unsafe fn isInstanceOf(&self, value: Option<&AnyObject>) -> bool
Check if a JSValue is an instance of another object.
This method has the same function as the JavaScript operator
instanceof
.
If an object other than a JSValue is passed, it will first be converted according to
the aforementioned rules.
Sourcepub unsafe fn isEqualToObject(&self, value: Option<&AnyObject>) -> bool
pub unsafe fn isEqualToObject(&self, value: Option<&AnyObject>) -> bool
§Compare two JSValues using JavaScript’s
operator.Sourcepub unsafe fn isEqualWithTypeCoercionToObject(
&self,
value: Option<&AnyObject>,
) -> bool
pub unsafe fn isEqualWithTypeCoercionToObject( &self, value: Option<&AnyObject>, ) -> bool
§Compare two JSValues using JavaScript’s
operator.Sourcepub unsafe fn compareJSValue(&self, other: &JSValue) -> JSRelationCondition
Available on crate feature JSValueRef
only.
pub unsafe fn compareJSValue(&self, other: &JSValue) -> JSRelationCondition
JSValueRef
only.Compare two JSValues. The JSValue to compare with.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
§The result is computed by comparing the results of JavaScript’s
,
<
, and
>
operators. If either
self
or
other
is (or would coerce to)
NaN
in JavaScript, then the result is kJSRelationConditionUndefined.Sourcepub unsafe fn compareInt64(&self, other: i64) -> JSRelationCondition
Available on crate feature JSValueRef
only.
pub unsafe fn compareInt64(&self, other: i64) -> JSRelationCondition
JSValueRef
only.Compare a JSValue with a
int64_t
.
The
int64_t
to compare with.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
The JSValue is converted to an integer according to the rules specified by the JavaScript language then compared with
other
.
Sourcepub unsafe fn compareUInt64(&self, other: u64) -> JSRelationCondition
Available on crate feature JSValueRef
only.
pub unsafe fn compareUInt64(&self, other: u64) -> JSRelationCondition
JSValueRef
only.Compare a JSValue with a
uint64_t
.
The
uint64_t
to compare with.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
The JSValue is converted to an integer according to the rules specified by the JavaScript language then compared with
other
.
Sourcepub unsafe fn compareDouble(&self, other: c_double) -> JSRelationCondition
Available on crate feature JSValueRef
only.
pub unsafe fn compareDouble(&self, other: c_double) -> JSRelationCondition
JSValueRef
only.Compare a JSValue with a double. The double to compare with.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
The JSValue is converted to a double according to the rules specified by the JavaScript language then compared with
other
.
Sourcepub unsafe fn callWithArguments(
&self,
arguments: Option<&NSArray>,
) -> Option<Retained<JSValue>>
Available on crate feature objc2-foundation
only.
pub unsafe fn callWithArguments( &self, arguments: Option<&NSArray>, ) -> Option<Retained<JSValue>>
objc2-foundation
only.Invoke a JSValue as a function.
In JavaScript, if a function doesn’t explicitly return a value then it
implicitly returns the JavaScript value
undefined
.
Parameter arguments
: The arguments to pass to the function.
Returns: The return value of the function call.
Sourcepub unsafe fn constructWithArguments(
&self,
arguments: Option<&NSArray>,
) -> Option<Retained<JSValue>>
Available on crate feature objc2-foundation
only.
pub unsafe fn constructWithArguments( &self, arguments: Option<&NSArray>, ) -> Option<Retained<JSValue>>
objc2-foundation
only.Invoke a JSValue as a constructor.
This is equivalent to using the
new
syntax in JavaScript.
Parameter arguments
: The arguments to pass to the constructor.
Returns: The return value of the constructor call.
Sourcepub unsafe fn invokeMethod_withArguments(
&self,
method: Option<&NSString>,
arguments: Option<&NSArray>,
) -> Option<Retained<JSValue>>
Available on crate feature objc2-foundation
only.
pub unsafe fn invokeMethod_withArguments( &self, method: Option<&NSString>, arguments: Option<&NSArray>, ) -> Option<Retained<JSValue>>
objc2-foundation
only.Invoke a method on a JSValue.
Accesses the property named
method
from this value and
calls the resulting value as a function, passing this JSValue as the
this
value along with the specified arguments.
Parameter method
: The name of the method to be invoked.
Parameter arguments
: The arguments to pass to the method.
Returns: The return value of the method call.
Source§impl JSValue
StructSupport.
Objective-C methods exported to JavaScript may have argument and/or return
values of struct types, provided that conversion to and from the struct is
supported by JSValue. Support is provided for any types where JSValue
contains both a class method
valueWith
<Type
impl JSValue
StructSupport.
Objective-C methods exported to JavaScript may have argument and/or return
values of struct types, provided that conversion to and from the struct is
supported by JSValue. Support is provided for any types where JSValue
contains both a class method
valueWith
<Type
- where the string:inContext: , and and instance method
to <Type
in these selector names match,
with the first argument to the former being of the same struct type as the
return type of the latter.
Support is provided for structs of type CGPoint, NSRange, CGRect and CGSize.Sourcepub unsafe fn valueWithPoint_inContext(
point: CGPoint,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-core-foundation
only.
pub unsafe fn valueWithPoint_inContext( point: CGPoint, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-core-foundation
only.Create a JSValue from a CGPoint.
Returns: A newly allocated JavaScript object containing properties
named
x
and
y
, with values from the CGPoint.
Sourcepub unsafe fn valueWithRange_inContext(
range: NSRange,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-foundation
only.
pub unsafe fn valueWithRange_inContext( range: NSRange, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-foundation
only.Create a JSValue from a NSRange.
Returns: A newly allocated JavaScript object containing properties
named
location
and
length
, with values from the NSRange.
Sourcepub unsafe fn valueWithRect_inContext(
rect: CGRect,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-core-foundation
only.
pub unsafe fn valueWithRect_inContext( rect: CGRect, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-core-foundation
only.Create a JSValue from a CGRect.
Returns: A newly allocated JavaScript object containing properties
named
x
,
y
,
width
, and
height
, with values from the CGRect.
Sourcepub unsafe fn valueWithSize_inContext(
size: CGSize,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSContext
and objc2-core-foundation
only.
pub unsafe fn valueWithSize_inContext( size: CGSize, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSContext
and objc2-core-foundation
only.Create a JSValue from a CGSize.
Returns: A newly allocated JavaScript object containing properties
named
width
and
height
, with values from the CGSize.
Sourcepub unsafe fn toPoint(&self) -> CGPoint
Available on crate feature objc2-core-foundation
only.
pub unsafe fn toPoint(&self) -> CGPoint
objc2-core-foundation
only.Convert a JSValue to a CGPoint.
Reads the properties named
x
and
y
from
this JSValue, and converts the results to double.
Returns: The new CGPoint.
Sourcepub unsafe fn toRange(&self) -> NSRange
Available on crate feature objc2-foundation
only.
pub unsafe fn toRange(&self) -> NSRange
objc2-foundation
only.Convert a JSValue to an NSRange.
Reads the properties named
location
and
length
from this JSValue and converts the results to double.
Returns: The new NSRange.
Source§impl JSValue
PropertyAccess.
These methods enable querying properties on a JSValue.
impl JSValue
PropertyAccess. These methods enable querying properties on a JSValue.
Sourcepub unsafe fn valueForProperty(
&self,
property: Option<&JSValueProperty>,
) -> Option<Retained<JSValue>>
Available on crate feature objc2-foundation
only.
pub unsafe fn valueForProperty( &self, property: Option<&JSValueProperty>, ) -> Option<Retained<JSValue>>
objc2-foundation
only.Access a property of a JSValue.
Returns: The JSValue for the requested property or the JSValue
undefined
if the property does not exist.
Corresponds to the JavaScript operation
object[property]
. 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
valueWithObject:inContext:
. Prior to macOS 10.15 and iOS 13, ‘property’ was expected to be an NSString *.
Sourcepub unsafe fn setValue_forProperty(
&self,
value: Option<&AnyObject>,
property: Option<&JSValueProperty>,
)
Available on crate feature objc2-foundation
only.
pub unsafe fn setValue_forProperty( &self, value: Option<&AnyObject>, property: Option<&JSValueProperty>, )
objc2-foundation
only.Set a property on a JSValue.
Corresponds to the JavaScript operation
object[property] = value
. 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
valueWithObject:inContext:
. Prior to macOS 10.15 and iOS 13, ‘property’ was expected to be an NSString *.
Sourcepub unsafe fn deleteProperty(&self, property: Option<&JSValueProperty>) -> bool
Available on crate feature objc2-foundation
only.
pub unsafe fn deleteProperty(&self, property: Option<&JSValueProperty>) -> bool
objc2-foundation
only.Delete a property from a JSValue.
Returns: YES if deletion is successful, NO otherwise.
Corresponds to the JavaScript operation
delete object[property]
. 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
valueWithObject:inContext:
. Prior to macOS 10.15 and iOS 13, ‘property’ was expected to be an NSString *.
Sourcepub unsafe fn hasProperty(&self, property: Option<&JSValueProperty>) -> bool
Available on crate feature objc2-foundation
only.
pub unsafe fn hasProperty(&self, property: Option<&JSValueProperty>) -> bool
objc2-foundation
only.Check if a JSValue has a property.
This method has the same function as the JavaScript operator
in
.
Returns: Returns YES if property is present on the value.
Corresponds to the JavaScript operation
property in object
. 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
valueWithObject:inContext:
. Prior to macOS 10.15 and iOS 13, ‘property’ was expected to be an NSString *.
Sourcepub unsafe fn defineProperty_descriptor(
&self,
property: Option<&JSValueProperty>,
descriptor: Option<&AnyObject>,
)
Available on crate feature objc2-foundation
only.
pub unsafe fn defineProperty_descriptor( &self, property: Option<&JSValueProperty>, descriptor: Option<&AnyObject>, )
objc2-foundation
only.Define properties with custom descriptors on JSValues.
This method may be used to create a data or accessor property on an object.
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
valueWithObject:inContext:
. Prior to macOS 10.15 and iOS 13, ‘property’ was expected to be an NSString *.
Sourcepub unsafe fn valueAtIndex(
&self,
index: NSUInteger,
) -> Option<Retained<JSValue>>
pub unsafe fn valueAtIndex( &self, index: NSUInteger, ) -> Option<Retained<JSValue>>
Access an indexed (numerical) property on a JSValue.
Returns: The JSValue for the property at the specified index.
Returns the JavaScript value
undefined
if no property exists at that index.
Sourcepub unsafe fn setValue_atIndex(
&self,
value: Option<&AnyObject>,
index: NSUInteger,
)
pub unsafe fn setValue_atIndex( &self, value: Option<&AnyObject>, index: NSUInteger, )
Set an indexed (numerical) property on a JSValue.
For JSValues that are JavaScript arrays, indices greater than UINT_MAX - 1 will not affect the length of the array.
Source§impl JSValue
SubscriptSupport.
Instances of JSValue implement the following methods in order to enable
support for subscript access by key and index, for example:
impl JSValue
SubscriptSupport. Instances of JSValue implement the following methods in order to enable support for subscript access by key and index, for example:
JSValue *objectA, *objectB;
JSValue *v1 = object[@"X"]; // Get value for property "X" from 'object'.
JSValue *v2 = object[42]; // Get value for index 42 from 'object'.
object[@"Y"] = v1; // Assign 'v1' to property "Y" of 'object'.
object[101] = v2; // Assign 'v2' to index 101 of 'object'.
An object key passed as a subscript will be converted to a JavaScript value,
and then the value using the same rules as
valueWithObject:inContext:
. In macOS
10.14 and iOS 12 and below, the
key
argument of
setObject:object forKeyedSubscript:key
was restricted to an
NSObject
<NSCopying
but that restriction was never used internally.
pub unsafe fn objectForKeyedSubscript( &self, key: Option<&AnyObject>, ) -> Option<Retained<JSValue>>
pub unsafe fn objectAtIndexedSubscript( &self, index: NSUInteger, ) -> Option<Retained<JSValue>>
pub unsafe fn setObject_forKeyedSubscript( &self, object: Option<&AnyObject>, key: Option<&AnyObject>, )
pub unsafe fn setObject_atIndexedSubscript( &self, object: Option<&AnyObject>, index: NSUInteger, )
Source§impl JSValue
JSValueRefSupport.
These functions are for bridging between the C API and the Objective-C API.
impl JSValue
JSValueRefSupport. These functions are for bridging between the C API and the Objective-C API.
Sourcepub unsafe fn valueWithJSValueRef_inContext(
value: JSValueRef,
context: Option<&JSContext>,
) -> Option<Retained<JSValue>>
Available on crate features JSBase
and JSContext
only.
pub unsafe fn valueWithJSValueRef_inContext( value: JSValueRef, context: Option<&JSContext>, ) -> Option<Retained<JSValue>>
JSBase
and JSContext
only.Creates a JSValue, wrapping its C API counterpart.
Returns: The Objective-C API equivalent of the specified JSValueRef.
Sourcepub unsafe fn JSValueRef(&self) -> JSValueRef
Available on crate feature JSBase
only.
pub unsafe fn JSValueRef(&self) -> JSValueRef
JSBase
only.Returns the C API counterpart wrapped by a JSContext.
Returns: The C API equivalent of this JSValue.
Source§impl JSValue
impl JSValue
Sourcepub unsafe fn type(ctx: JSContextRef, value: JSValueRef) -> JSType
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn type(ctx: JSContextRef, value: JSValueRef) -> JSType
JSValueRef
and JSBase
only.Returns a JavaScript value’s type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue whose type you want to obtain.
Returns: A value of type JSType that identifies value’s type.
Sourcepub unsafe fn is_undefined(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_undefined(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the undefined type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the undefined type, otherwise false.
Sourcepub unsafe fn is_null(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_null(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the null type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the null type, otherwise false.
Sourcepub unsafe fn is_boolean(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_boolean(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the boolean type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the boolean type, otherwise false.
Sourcepub unsafe fn is_number(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_number(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the number type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the number type, otherwise false.
Sourcepub unsafe fn is_string(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_string(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the string type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the string type, otherwise false.
Sourcepub unsafe fn is_symbol(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_symbol(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the symbol type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the symbol type, otherwise false.
Sourcepub unsafe fn is_big_int(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_big_int(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the BigInt type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the BigInt type, otherwise false.
Sourcepub unsafe fn is_object(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_object(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value’s type is the object type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value’s type is the object type, otherwise false.
Sourcepub unsafe fn is_object_of_class(
ctx: JSContextRef,
value: JSValueRef,
js_class: JSClassRef,
) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_object_of_class( ctx: JSContextRef, value: JSValueRef, js_class: JSClassRef, ) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value is an object with a given class in its class chain.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Parameter jsClass
: The JSClass to test against.
Returns: true if value is an object and has jsClass in its class chain, otherwise false.
Sourcepub unsafe fn is_array(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_array(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value is an array.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value is an array, otherwise false.
Sourcepub unsafe fn is_date(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_date(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value is a date.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Returns: true if value is a date, otherwise false.
Sourcepub unsafe fn typed_array_type(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> JSTypedArrayType
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn typed_array_type( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> JSTypedArrayType
JSValueRef
and JSBase
only.Returns a JavaScript value’s Typed Array type.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue whose Typed Array type to return.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A value of type JSTypedArrayType that identifies value’s Typed Array type, or kJSTypedArrayTypeNone if the value is not a Typed Array object.
Sourcepub unsafe fn is_equal(
ctx: JSContextRef,
a: JSValueRef,
b: JSValueRef,
exception: *mut JSValueRef,
) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_equal( ctx: JSContextRef, a: JSValueRef, b: JSValueRef, exception: *mut JSValueRef, ) -> bool
JSValueRef
and JSBase
only.Tests whether two JavaScript values are equal, as compared by the JS == operator.
Parameter ctx
: The execution context to use.
Parameter a
: The first value to test.
Parameter b
: The second value to test.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: true if the two values are equal, false if they are not equal or an exception is thrown.
Sourcepub unsafe fn is_strict_equal(
ctx: JSContextRef,
a: JSValueRef,
b: JSValueRef,
) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_strict_equal( ctx: JSContextRef, a: JSValueRef, b: JSValueRef, ) -> bool
JSValueRef
and JSBase
only.Tests whether two JavaScript values are strict equal, as compared by the JS === operator.
Parameter ctx
: The execution context to use.
Parameter a
: The first value to test.
Parameter b
: The second value to test.
Returns: true if the two values are strict equal, otherwise false.
Sourcepub unsafe fn is_instance_of_constructor(
ctx: JSContextRef,
value: JSValueRef,
constructor: JSObjectRef,
exception: *mut JSValueRef,
) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn is_instance_of_constructor( ctx: JSContextRef, value: JSValueRef, constructor: JSObjectRef, exception: *mut JSValueRef, ) -> bool
JSValueRef
and JSBase
only.Tests whether a JavaScript value is an object constructed by a given constructor, as compared by the JS instanceof operator.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to test.
Parameter constructor
: The constructor to test against.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: true if value is an object constructed by constructor, as compared by the JS instanceof operator, otherwise false.
Sourcepub unsafe fn compare(
ctx: JSContextRef,
left: JSValueRef,
right: JSValueRef,
exception: *mut JSValueRef,
) -> JSRelationCondition
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn compare( ctx: JSContextRef, left: JSValueRef, right: JSValueRef, exception: *mut JSValueRef, ) -> JSRelationCondition
JSValueRef
and JSBase
only.Compares two JSValues.
Parameter ctx
: The execution context to use.
Parameter left
: The JSValue as the left operand.
Parameter right
: The JSValue as the right operand.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
The result is computed by comparing the results of JavaScript’s ==
, <
, and >
operators. If either left
or right
is (or would coerce to) NaN
in JavaScript, then the result is kJSRelationConditionUndefined.
Sourcepub unsafe fn compare_int64(
ctx: JSContextRef,
left: JSValueRef,
right: i64,
exception: *mut JSValueRef,
) -> JSRelationCondition
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn compare_int64( ctx: JSContextRef, left: JSValueRef, right: i64, exception: *mut JSValueRef, ) -> JSRelationCondition
JSValueRef
and JSBase
only.Compares a JSValue with a signed 64-bit integer.
Parameter ctx
: The execution context to use.
Parameter left
: The JSValue as the left operand.
Parameter right
: The int64_t as the right operand.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
left
is converted to an integer according to the rules specified by the JavaScript language then compared with right
.
Sourcepub unsafe fn compare_u_int64(
ctx: JSContextRef,
left: JSValueRef,
right: u64,
exception: *mut JSValueRef,
) -> JSRelationCondition
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn compare_u_int64( ctx: JSContextRef, left: JSValueRef, right: u64, exception: *mut JSValueRef, ) -> JSRelationCondition
JSValueRef
and JSBase
only.Compares a JSValue with an unsigned 64-bit integer.
Parameter ctx
: The execution context to use.
Parameter left
: The JSValue as the left operand.
Parameter right
: The uint64_t as the right operand.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
left
is converted to an integer according to the rules specified by the JavaScript language then compared with right
.
Sourcepub unsafe fn compare_double(
ctx: JSContextRef,
left: JSValueRef,
right: c_double,
exception: *mut JSValueRef,
) -> JSRelationCondition
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn compare_double( ctx: JSContextRef, left: JSValueRef, right: c_double, exception: *mut JSValueRef, ) -> JSRelationCondition
JSValueRef
and JSBase
only.Compares a JSValue with a double.
Parameter ctx
: The execution context to use.
Parameter left
: The JSValue as the left operand.
Parameter right
: The double as the right operand.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A value of JSRelationCondition, a kJSRelationConditionUndefined is returned if an exception is thrown.
left
is converted to a double according to the rules specified by the JavaScript language then compared with right
.
Sourcepub unsafe fn new_undefined(ctx: JSContextRef) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_undefined(ctx: JSContextRef) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the undefined type.
Parameter ctx
: The execution context to use.
Returns: The unique undefined value.
Sourcepub unsafe fn new_null(ctx: JSContextRef) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_null(ctx: JSContextRef) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the null type.
Parameter ctx
: The execution context to use.
Returns: The unique null value.
Sourcepub unsafe fn new_boolean(ctx: JSContextRef, boolean: bool) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_boolean(ctx: JSContextRef, boolean: bool) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the boolean type.
Parameter ctx
: The execution context to use.
Parameter boolean
: The bool to assign to the newly created JSValue.
Returns: A JSValue of the boolean type, representing the value of boolean.
Sourcepub unsafe fn new_number(ctx: JSContextRef, number: c_double) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_number(ctx: JSContextRef, number: c_double) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the number type.
Parameter ctx
: The execution context to use.
Parameter number
: The double to assign to the newly created JSValue.
Returns: A JSValue of the number type, representing the value of number.
Sourcepub unsafe fn new_string(ctx: JSContextRef, string: JSStringRef) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_string(ctx: JSContextRef, string: JSStringRef) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the string type.
Parameter ctx
: The execution context to use.
Parameter string
: The JSString to assign to the newly created JSValue. The
newly created JSValue retains string, and releases it upon garbage collection.
Returns: A JSValue of the string type, representing the value of string.
Sourcepub unsafe fn new_symbol(
ctx: JSContextRef,
description: JSStringRef,
) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn new_symbol( ctx: JSContextRef, description: JSStringRef, ) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value of the symbol type.
Parameter ctx
: The execution context to use.
Parameter description
: A description of the newly created symbol value.
Returns: A unique JSValue of the symbol type, whose description matches the one provided.
Source§impl JSValue
impl JSValue
Sourcepub unsafe fn from_json_string(
ctx: JSContextRef,
string: JSStringRef,
) -> JSValueRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn from_json_string( ctx: JSContextRef, string: JSStringRef, ) -> JSValueRef
JSValueRef
and JSBase
only.Creates a JavaScript value from a JSON formatted string.
Parameter ctx
: The execution context to use.
Parameter string
: The JSString containing the JSON string to be parsed.
Returns: A JSValue containing the parsed value, or NULL if the input is invalid.
Sourcepub unsafe fn create_json_string(
ctx: JSContextRef,
value: JSValueRef,
indent: c_uint,
exception: *mut JSValueRef,
) -> JSStringRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn create_json_string( ctx: JSContextRef, value: JSValueRef, indent: c_uint, exception: *mut JSValueRef, ) -> JSStringRef
JSValueRef
and JSBase
only.Creates a JavaScript string containing the JSON serialized representation of a JS value.
Parameter ctx
: The execution context to use.
Parameter value
: The value to serialize.
Parameter indent
: The number of spaces to indent when nesting. If 0, the resulting JSON will not contains newlines. The size of the indent is clamped to 10 spaces.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A JSString with the result of serialization, or NULL if an exception is thrown.
Sourcepub unsafe fn to_boolean(ctx: JSContextRef, value: JSValueRef) -> bool
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_boolean(ctx: JSContextRef, value: JSValueRef) -> bool
JSValueRef
and JSBase
only.Converts a JavaScript value to boolean and returns the resulting boolean.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Returns: The boolean result of conversion.
Sourcepub unsafe fn to_number(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> c_double
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_number( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> c_double
JSValueRef
and JSBase
only.Converts a JavaScript value to number and returns the resulting number.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: The numeric result of conversion, or NaN if an exception is thrown.
The result is equivalent to Number(value)
in JavaScript.
Sourcepub unsafe fn to_int32(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> i32
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_int32( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> i32
JSValueRef
and JSBase
only.Converts a JSValue to a singed 32-bit integer and returns the resulting integer.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: An int32_t with the result of conversion, or 0 if an exception is thrown. Since 0 is valid value, exception
must be checked after the call.
The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the JSValue is truncated to an int32_t.
Sourcepub unsafe fn to_u_int32(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> u32
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_u_int32( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> u32
JSValueRef
and JSBase
only.Converts a JSValue to an unsigned 32-bit integer and returns the resulting integer.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A uint32_t with the result of conversion, or 0 if an exception is thrown. Since 0 is valid value, exception
must be checked after the call.
The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the JSValue is truncated to a uint32_t.
Sourcepub unsafe fn to_int64(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> i64
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_int64( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> i64
JSValueRef
and JSBase
only.Converts a JSValue to a singed 64-bit integer and returns the resulting integer.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: An int64_t with the result of conversion, or 0 if an exception is thrown. Since 0 is valid value, exception
must be checked after the call.
The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the JSValue is truncated to an int64_t.
Sourcepub unsafe fn to_u_int64(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> u64
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_u_int64( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> u64
JSValueRef
and JSBase
only.Converts a JSValue to an unsigned 64-bit integer and returns the resulting integer.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A uint64_t with the result of conversion, or 0 if an exception is thrown. Since 0 is valid value, exception
must be checked after the call.
The JSValue is converted to an integer according to the rules specified by the JavaScript language. If the value is a BigInt, then the JSValue is truncated to a uint64_t.
Sourcepub unsafe fn to_string_copy(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> JSStringRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_string_copy( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> JSStringRef
JSValueRef
and JSBase
only.Converts a JavaScript value to string and copies the result into a JavaScript string.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: A JSString with the result of conversion, or NULL if an exception is thrown. Ownership follows the Create Rule.
Sourcepub unsafe fn to_object(
ctx: JSContextRef,
value: JSValueRef,
exception: *mut JSValueRef,
) -> JSObjectRef
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn to_object( ctx: JSContextRef, value: JSValueRef, exception: *mut JSValueRef, ) -> JSObjectRef
JSValueRef
and JSBase
only.Converts a JavaScript value to object and returns the resulting object.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to convert.
Parameter exception
: A pointer to a JSValueRef in which to store an exception, if any. To reliable detect exception, initialize this to null before the call. Pass NULL if you do not care to store an exception.
Returns: The JSObject result of conversion, or NULL if an exception is thrown.
Sourcepub unsafe fn protect(ctx: JSContextRef, value: JSValueRef)
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn protect(ctx: JSContextRef, value: JSValueRef)
JSValueRef
and JSBase
only.Protects a JavaScript value from garbage collection.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to protect.
Use this method when you want to store a JSValue in a global or on the heap, where the garbage collector will not be able to discover your reference to it.
A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection.
Sourcepub unsafe fn unprotect(ctx: JSContextRef, value: JSValueRef)
Available on crate features JSValueRef
and JSBase
only.
pub unsafe fn unprotect(ctx: JSContextRef, value: JSValueRef)
JSValueRef
and JSBase
only.Unprotects a JavaScript value from garbage collection.
Parameter ctx
: The execution context to use.
Parameter value
: The JSValue to unprotect.
A value may be protected multiple times and must be unprotected an equal number of times before becoming eligible for garbage collection.
Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
pub fn class(&self) -> &'static AnyClass
Dynamically find the class of this object.
§Panics
May panic if the object is invalid (which may be the case for objects
returned from unavailable init
/new
methods).
§Example
Check that an instance of NSObject
has the precise class NSObject
.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load
instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load
instead.Use Ivar::load
instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
See Ivar::load_ptr
for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Attempt to downcast the object to a class of type T
.
This is the reference-variant. Use Retained::downcast
if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString
.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString
to a NSMutableString
,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass:
for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject
.
§Panics
This works internally by calling isKindOfClass:
. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject
and
NSProxy
implement this method.
§Examples
Cast an NSString
back and forth from NSObject
.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();
Try (and fail) to cast an NSObject
to an NSString
.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());
Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();
This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}
Trait Implementations§
Source§impl ClassType for JSValue
impl ClassType for JSValue
Source§const NAME: &'static str = "JSValue"
const NAME: &'static str = "JSValue"
Source§type ThreadKind = <<JSValue as ClassType>::Super as ClassType>::ThreadKind
type ThreadKind = <<JSValue as ClassType>::Super as ClassType>::ThreadKind
Source§impl NSObjectProtocol for JSValue
impl NSObjectProtocol for JSValue
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref