Class JSValue

  • All Implemented Interfaces:
    NSObject

    public class JSValue
    extends NSObject
    [@interface] 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.
    • Constructor Detail

      • JSValue

        protected JSValue​(org.moe.natj.general.Pointer peer)
    • Method Detail

      • accessInstanceVariablesDirectly

        public static boolean accessInstanceVariablesDirectly()
      • alloc

        public static JSValue alloc()
      • allocWithZone

        public static java.lang.Object allocWithZone​(org.moe.natj.general.ptr.VoidPtr zone)
      • automaticallyNotifiesObserversForKey

        public static boolean automaticallyNotifiesObserversForKey​(java.lang.String key)
      • cancelPreviousPerformRequestsWithTarget

        public static void cancelPreviousPerformRequestsWithTarget​(java.lang.Object aTarget)
      • cancelPreviousPerformRequestsWithTargetSelectorObject

        public static void cancelPreviousPerformRequestsWithTargetSelectorObject​(java.lang.Object aTarget,
                                                                                 org.moe.natj.objc.SEL aSelector,
                                                                                 java.lang.Object anArgument)
      • classFallbacksForKeyedArchiver

        public static NSArray<java.lang.String> classFallbacksForKeyedArchiver()
      • classForKeyedUnarchiver

        public static org.moe.natj.objc.Class classForKeyedUnarchiver()
      • debugDescription_static

        public static java.lang.String debugDescription_static()
      • description_static

        public static java.lang.String description_static()
      • hash_static

        public static long hash_static()
      • instanceMethodSignatureForSelector

        public static NSMethodSignature instanceMethodSignatureForSelector​(org.moe.natj.objc.SEL aSelector)
      • instancesRespondToSelector

        public static boolean instancesRespondToSelector​(org.moe.natj.objc.SEL aSelector)
      • isSubclassOfClass

        public static boolean isSubclassOfClass​(org.moe.natj.objc.Class aClass)
      • keyPathsForValuesAffectingValueForKey

        public static NSSet<java.lang.String> keyPathsForValuesAffectingValueForKey​(java.lang.String key)
      • new_objc

        public static java.lang.Object new_objc()
      • resolveClassMethod

        public static boolean resolveClassMethod​(org.moe.natj.objc.SEL sel)
      • resolveInstanceMethod

        public static boolean resolveInstanceMethod​(org.moe.natj.objc.SEL sel)
      • setVersion_static

        public static void setVersion_static​(long aVersion)
      • superclass_static

        public static org.moe.natj.objc.Class superclass_static()
      • valueWithBoolInContext

        public static JSValue valueWithBoolInContext​(boolean value,
                                                     JSContext context)
        Create a JavaScript value from a BOOL primitive.
        Parameters:
        context - The JSContext in which the resulting JSValue will be created.
        Returns:
        The new JSValue representing the equivalent boolean value.
      • valueWithDoubleInContext

        public static JSValue valueWithDoubleInContext​(double value,
                                                       JSContext context)
        Create a JavaScript value from a double primitive.
        Parameters:
        context - The JSContext in which the resulting JSValue will be created.
        Returns:
        The new JSValue representing the equivalent boolean value.
      • valueWithInt32InContext

        public static JSValue valueWithInt32InContext​(int value,
                                                      JSContext context)
        Create a JavaScript value from an int32_t primitive.
        Parameters:
        context - The JSContext in which the resulting JSValue will be created.
        Returns:
        The new JSValue representing the equivalent boolean value.
      • valueWithJSValueRefInContext

        public static JSValue valueWithJSValueRefInContext​(JSValueRef value,
                                                           JSContext context)
        Creates a JSValue, wrapping its C API counterpart.
        Returns:
        The Objective-C API equivalent of the specified JSValueRef.
      • valueWithNewArrayInContext

        public static JSValue valueWithNewArrayInContext​(JSContext context)
        Create a new, empty JavaScript array.
        Parameters:
        context - The JSContext in which the resulting array will be created.
        Returns:
        The new JavaScript array.
      • valueWithNewErrorFromMessageInContext

        public static JSValue valueWithNewErrorFromMessageInContext​(java.lang.String message,
                                                                    JSContext context)
        Create a new JavaScript error object.
        Parameters:
        message - The error message.
        context - The JSContext in which the resulting error object will be created.
        Returns:
        The new JavaScript error object.
      • valueWithNewObjectInContext

        public static JSValue valueWithNewObjectInContext​(JSContext context)
        Create a new, empty JavaScript object.
        Parameters:
        context - The JSContext in which the resulting object will be created.
        Returns:
        The new JavaScript object.
      • valueWithNewRegularExpressionFromPatternFlagsInContext

        public static JSValue valueWithNewRegularExpressionFromPatternFlagsInContext​(java.lang.String pattern,
                                                                                     java.lang.String flags,
                                                                                     JSContext context)
        Create a new JavaScript regular expression object.
        Parameters:
        pattern - The regular expression pattern.
        flags - The regular expression flags.
        context - The JSContext in which the resulting regular expression object will be created.
        Returns:
        The new JavaScript regular expression object.
      • valueWithNullInContext

        public static JSValue valueWithNullInContext​(JSContext context)
        Create the JavaScript value null.
        Parameters:
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing the JavaScript value null.
      • valueWithObjectInContext

        public static JSValue valueWithObjectInContext​(java.lang.Object value,
                                                       JSContext context)
        [@methodgroup] Creating JavaScript Values Create a JSValue by converting an Objective-C object. The resulting JSValue retains the provided Objective-C object.
        Parameters:
        value - The Objective-C object to be converted.
        Returns:
        The new JSValue.
      • valueWithPointInContext

        public static JSValue valueWithPointInContext​(CGPoint point,
                                                      JSContext context)
        Create a JSValue from a CGPoint.
        Returns:
        A newly allocated JavaScript object containing properties named x and y, with values from the CGPoint.
      • valueWithRangeInContext

        public static JSValue valueWithRangeInContext​(NSRange range,
                                                      JSContext context)
        Create a JSValue from a NSRange.
        Returns:
        A newly allocated JavaScript object containing properties named location and length, with values from the NSRange.
      • valueWithRectInContext

        public static JSValue valueWithRectInContext​(CGRect rect,
                                                     JSContext context)
        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.
      • valueWithSizeInContext

        public static JSValue valueWithSizeInContext​(CGSize size,
                                                     JSContext context)
        Create a JSValue from a CGSize.
        Returns:
        A newly allocated JavaScript object containing properties named width and height, with values from the CGSize.
      • valueWithUInt32InContext

        public static JSValue valueWithUInt32InContext​(int value,
                                                       JSContext context)
        Create a JavaScript value from a uint32_t primitive.
        Parameters:
        context - The JSContext in which the resulting JSValue will be created.
        Returns:
        The new JSValue representing the equivalent boolean value.
      • valueWithUndefinedInContext

        public static JSValue valueWithUndefinedInContext​(JSContext context)
        Create the JavaScript value undefined.
        Parameters:
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing the JavaScript value undefined.
      • version_static

        public static long version_static()
      • JSValueRef

        public JSValueRef JSValueRef()
        [@property] Returns the C API counterpart wrapped by a JSContext.
        Returns:
        The C API equivalent of this JSValue.
      • callWithArguments

        public JSValue callWithArguments​(NSArray<?> arguments)
        [@methodgroup] Calling Functions and Constructors 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.
        Parameters:
        arguments - The arguments to pass to the function.
        Returns:
        The return value of the function call.
      • constructWithArguments

        public JSValue constructWithArguments​(NSArray<?> arguments)
        Invoke a JSValue as a constructor. This is equivalent to using the new syntax in JavaScript.
        Parameters:
        arguments - The arguments to pass to the constructor.
        Returns:
        The return value of the constructor call.
      • context

        public JSContext context()
        [@property] The JSContext that this value originates from.
      • definePropertyDescriptor

        public void definePropertyDescriptor​(java.lang.String property,
                                             java.lang.Object descriptor)
        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 *.
      • deleteProperty

        public boolean deleteProperty​(java.lang.String property)
        Delete a property from a JSValue. 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 *.
        Returns:
        YES if deletion is successful, NO otherwise.
      • hasProperty

        public boolean hasProperty​(java.lang.String property)
        Check if a JSValue has a property. This method has the same function as the JavaScript operator in. 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 *.
        Returns:
        Returns YES if property is present on the value.
      • invokeMethodWithArguments

        public JSValue invokeMethodWithArguments​(java.lang.String method,
                                                 NSArray<?> arguments)
        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.
        Parameters:
        method - The name of the method to be invoked.
        arguments - The arguments to pass to the method.
        Returns:
        The return value of the method call.
      • isArray

        public boolean isArray()
        [@property] Check if a JSValue is an array.
      • isBoolean

        public boolean isBoolean()
        [@property] Check if a JSValue is a boolean.
      • isDate

        public boolean isDate()
        [@property] Check if a JSValue is a date.
      • isEqualToObject

        public boolean isEqualToObject​(java.lang.Object value)
        Compare two JSValues using JavaScript's === operator.
      • isEqualWithTypeCoercionToObject

        public boolean isEqualWithTypeCoercionToObject​(java.lang.Object value)
        Compare two JSValues using JavaScript's == operator.
      • isInstanceOf

        public boolean isInstanceOf​(java.lang.Object value)
        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.
      • isNull

        public boolean isNull()
        [@property] Check if a JSValue corresponds to the JavaScript value null.
      • isNumber

        public boolean isNumber()
        [@property] 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.
      • isObject

        public boolean isObject()
        [@property] Check if a JSValue is an object.
      • isString

        public boolean isString()
        [@property] Check if a JSValue is a string.
      • isUndefined

        public boolean isUndefined()
        [@property] Check if a JSValue corresponds to the JavaScript value undefined.
      • objectAtIndexedSubscript

        public JSValue objectAtIndexedSubscript​(long index)
      • objectForKeyedSubscript

        public JSValue objectForKeyedSubscript​(java.lang.Object key)
      • setObjectAtIndexedSubscript

        public void setObjectAtIndexedSubscript​(java.lang.Object object,
                                                long index)
      • setObjectForKeyedSubscript

        public void setObjectForKeyedSubscript​(java.lang.Object object,
                                               java.lang.Object key)
      • setValueAtIndex

        public void setValueAtIndex​(java.lang.Object value,
                                    long index)
        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.
      • setValueForProperty

        public void setValueForProperty​(java.lang.Object value,
                                        java.lang.String property)
        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 *.
      • toArray

        public NSArray<?> toArray()
        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 indicies 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.
      • toBool

        public boolean toBool()
        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.
      • toDate

        public NSDate toDate()
        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.
      • toDictionary

        public NSDictionary<?,​?> toDictionary()
        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.
      • toDouble

        public double toDouble()
        Convert a JSValue to a double. The JSValue is converted to a number according to the rules specified by the JavaScript language.
        Returns:
        The double result of the conversion.
      • toInt32

        public int toInt32()
        Convert a JSValue to an int32_t. The JSValue is converted to an integer according to the rules specified by the JavaScript language.
        Returns:
        The int32_t result of the conversion.
      • toNumber

        public NSNumber toNumber()
        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 value will be converted to a number according to the rules specified by the JavaScript language.
        Returns:
        The NSNumber result of the conversion.
      • toObject

        public java.lang.Object toObject()
        [@methodgroup] Converting to Objective-C Types 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.
        Returns:
        The Objective-C representation of this JSValue.
      • toObjectOfClass

        public java.lang.Object toObjectOfClass​(org.moe.natj.objc.Class expectedClass)
        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.
      • toPoint

        public CGPoint toPoint()
        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.
      • toRange

        public NSRange toRange()
        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.
      • toRect

        public CGRect toRect()
        Convert a JSValue to a CGRect. Reads the properties named x, y, width, and height from this JSValue and converts the results to double.
        Returns:
        The new CGRect.
      • toSize

        public CGSize toSize()
        Convert a JSValue to a CGSize. Reads the properties named width and height from this JSValue and converts the results to double.
        Returns:
        The new CGSize.
      • toString

        public java.lang.String toString()
        Convert a JSValue to a NSString. The JSValue is converted to a string according to the rules specified by the JavaScript language.
        Overrides:
        toString in class org.moe.natj.objc.ObjCObject
        Returns:
        The NSString containing the result of the conversion.
      • toUInt32

        public int toUInt32()
        Convert a JSValue to a uint32_t. The JSValue is converted to an integer according to the rules specified by the JavaScript language.
        Returns:
        The uint32_t result of the conversion.
      • valueAtIndex

        public JSValue valueAtIndex​(long index)
        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.
      • valueForProperty

        public JSValue valueForProperty​(java.lang.String property)
        Access a property of a JSValue. 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 *.
        Returns:
        The JSValue for the requested property or the JSValue undefined if the property does not exist.
      • isSymbol

        public boolean isSymbol()
        [@property] Check if a JSValue is a symbol.
      • valueWithNewPromiseInContextFromExecutor

        public static JSValue valueWithNewPromiseInContextFromExecutor​(JSContext context,
                                                                       JSValue.Block_valueWithNewPromiseInContextFromExecutor callback)
        Create a new promise object using the provided executor callback. 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].
        Parameters:
        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.
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing a new promise JavaScript object.
      • valueWithNewPromiseRejectedWithReasonInContext

        public static JSValue valueWithNewPromiseRejectedWithReasonInContext​(java.lang.Object reason,
                                                                             JSContext context)
        Create a new rejected promise object with the provided value. This method is equivalent to calling [JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [reject callWithArguments:@[reason]]; } inContext:context]
        Parameters:
        reason - The result value to be passed to any reactions.
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing a new promise JavaScript object.
      • valueWithNewPromiseResolvedWithResultInContext

        public static JSValue valueWithNewPromiseResolvedWithResultInContext​(java.lang.Object result,
                                                                             JSContext context)
        Create a new resolved promise object with the provided value. This method is equivalent to calling [JSValue valueWithNewPromiseFromExecutor:^(JSValue *resolve, JSValue *reject) { [resolve callWithArguments:@[result]]; } inContext:context]
        Parameters:
        result - The result value to be passed to any reactions.
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing a new promise JavaScript object.
      • valueWithNewSymbolFromDescriptionInContext

        public static JSValue valueWithNewSymbolFromDescriptionInContext​(java.lang.String description,
                                                                         JSContext context)
        Create a new, unique, symbol object.
        Parameters:
        description - The description of the symbol object being created.
        context - The JSContext to which the resulting JSValue belongs.
        Returns:
        The JSValue representing a unique JavaScript value with type symbol.