JSValue
IntroductionA JSValue is a reference to a value within the JavaScript object space of a JSVirtualMachine. All instances of JSValue originate from a JSContext and hold a strong reference to this JSContext. As long as any value associated with a particular JSContext is retained, that JSContext will remain alive. Where an instance method is invoked upon a JSValue, and this returns another JSValue, the returned JSValue will originate from the same JSContext as the JSValue on which the method was invoked. All JavaScript values are associated with a particular JSVirtualMachine (the associated JSVirtualMachine is available indirectly via the context property). An instance of JSValue may only be passed as an argument to methods on instances of JSValue and JSContext that belong to the same JSVirtualMachine - passing a JSValue to a method on an object originating from a different JSVirtualMachine will result in an Objective-C exception being raised. GroupsCreating JavaScript ValuesGroup members:
Calling Functions and ConstructorsGroup members:
Checking JavaScript TypesGroup members:
Accessing PropertiesGroup members:
Converting to Objective-C TypesWhen 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. 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. Group members:
Methods
callWithArguments:Invoke a JSValue as a function. - (JSValue *)callWithArguments:(NSArray *)arguments; ParametersReturn ValueThe return value of the function call. DiscussionIn JavaScript, if a function doesn't explicitly return a value then it
implicitly returns the JavaScript value constructWithArguments:Invoke a JSValue as a constructor. - (JSValue *)constructWithArguments:(NSArray *)arguments; ParametersReturn ValueThe return value of the constructor call. DiscussionThis is equivalent to using the defineProperty:descriptor:Define properties with custom descriptors on JSValues. - (void)defineProperty:(NSString *)property descriptor:(id)descriptor; DiscussionThis 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. deleteProperty:Delete a property from a JSValue. - (BOOL)deleteProperty:(NSString *)property; Return ValueYES if deletion is successful, NO otherwise. hasProperty:Check if a JSValue has a property. - (BOOL)hasProperty:(NSString *)property; Return ValueReturns YES if property is present on the value. DiscussionThis method has the same function as the JavaScript operator invokeMethod:withArguments:Invoke a method on a JSValue. - (JSValue *)invokeMethod:(NSString *)method withArguments:(NSArray *)arguments; ParametersReturn ValueThe return value of the method call. DiscussionAccesses the property named isBooleanCheck if a JSValue is a boolean. - (BOOL)isBoolean; isEqualToObject:Compare two JSValues using JavaScript's - (BOOL)isEqualToObject:(id)value; isEqualWithTypeCoercionToObject:Compare two JSValues using JavaScript's - (BOOL)isEqualWithTypeCoercionToObject:(id)value; isInstanceOf:Check if a JSValue is an instance of another object. - (BOOL)isInstanceOf:(id)value; DiscussionThis method has the same function as the JavaScript operator isNullCheck if a JSValue corresponds to the JavaScript value - (BOOL)isNull; isNumberCheck if a JSValue is a number. - (BOOL)isNumber; DiscussionIn JavaScript, there is no differentiation between types of numbers. Semantically all numbers behave like doubles except in special cases like bit operations. isObjectCheck if a JSValue is an object. - (BOOL)isObject; isStringCheck if a JSValue is a string. - (BOOL)isString; isUndefinedCheck if a JSValue corresponds to the JavaScript value - (BOOL)isUndefined; setValue:atIndex:Set an indexed (numerical) property on a JSValue. - (void)setValue:(id)value atIndex:(NSUInteger)index; DiscussionFor JSValues that are JavaScript arrays, indices greater than UINT_MAX - 1 will not affect the length of the array. setValue:forProperty:Set a property on a JSValue. - (void)setValue:(id)value forProperty:(NSString *)property; toArrayConvert a JSValue to a NSArray. - (NSArray *)toArray; Return ValueThe NSArray containing the recursively converted contents of the converted JavaScript array. DiscussionIf the value is toBoolConvert a JSValue to a boolean. - (BOOL)toBool; Return ValueThe boolean result of the conversion. DiscussionThe JSValue is converted to a boolean according to the rules specified by the JavaScript language. toDateConvert a JSValue to a NSDate. - (NSDate *)toDate; Return ValueThe NSDate created using the converted time interval. DiscussionThe value is converted to a number representing a time interval since 1970 which is then used to create a new NSDate instance. toDictionaryConvert a JSValue to a NSDictionary. - (NSDictionary *)toDictionary; Return ValueThe NSDictionary containing the recursively converted contents of the converted JavaScript object. DiscussionIf the value is toDoubleConvert a JSValue to a double. - (double)toDouble; Return ValueThe double result of the conversion. DiscussionThe JSValue is converted to a number according to the rules specified by the JavaScript language. toInt32Convert a JSValue to an - (int32_t)toInt32; Return ValueThe DiscussionThe JSValue is converted to an integer according to the rules specified by the JavaScript language. toNumberConvert a JSValue to a NSNumber. - (NSNumber *)toNumber; Return ValueThe NSNumber result of the conversion. DiscussionIf 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. toObjectConvert this JSValue to an Objective-C object. - (id)toObject; Return ValueThe Objective-C representation of this JSValue. DiscussionThe JSValue is converted to an Objective-C object according to the conversion rules specified above. toObjectOfClass:Convert a JSValue to an Objective-C object of a specific class. - (id)toObjectOfClass:(Class)expectedClass; Return ValueAn Objective-C object of the specified Class or DiscussionThe JSValue is converted to an Objective-C object of the specified Class.
If the result is not of the specified Class then toStringConvert a JSValue to a NSString. - (NSString *)toString; Return ValueThe NSString containing the result of the conversion. DiscussionThe JSValue is converted to a string according to the rules specified by the JavaScript language. toUInt32Convert a JSValue to a - (uint32_t)toUInt32; Return ValueThe DiscussionThe JSValue is converted to an integer according to the rules specified by the JavaScript language. valueAtIndex:Access an indexed (numerical) property on a JSValue. - (JSValue *)valueAtIndex:(NSUInteger)index; Return ValueThe JSValue for the property at the specified index.
Returns the JavaScript value valueForProperty:Access a property of a JSValue. - (JSValue *)valueForProperty:(NSString *)property; Return ValueThe JSValue for the requested property or the JSValue valueWithBool:inContext:Create a JavaScript value from a BOOL primitive. + (JSValue *)valueWithBool:(BOOL)value inContext:(JSContext *)context; ParametersReturn ValueThe new JSValue representing the equivalent boolean value. valueWithDouble:inContext:Create a JavaScript value from a double primitive. + (JSValue *)valueWithDouble:(double)value inContext:(JSContext *)context; ParametersReturn ValueThe new JSValue representing the equivalent boolean value. valueWithInt32:inContext:Create a JavaScript value from an + (JSValue *)valueWithInt32:(int32_t)value inContext:(JSContext *)context; ParametersReturn ValueThe new JSValue representing the equivalent boolean value. valueWithNewArrayInContext:Create a new, empty JavaScript array. + (JSValue *)valueWithNewArrayInContext:(JSContext *)context; ParametersReturn ValueThe new JavaScript array. valueWithNewErrorFromMessage:inContext:Create a new JavaScript error object. + (JSValue *)valueWithNewErrorFromMessage:(NSString *)message inContext:(JSContext *)context; ParametersReturn ValueThe new JavaScript error object. valueWithNewObjectInContext:Create a new, empty JavaScript object. + (JSValue *)valueWithNewObjectInContext:(JSContext *)context; ParametersReturn ValueThe new JavaScript object. valueWithNewRegularExpressionFromPattern:flags:inContext:Create a new JavaScript regular expression object. + (JSValue *)valueWithNewRegularExpressionFromPattern:(NSString *)pattern flags:(NSString *)flags inContext:(JSContext *)context; ParametersReturn ValueThe new JavaScript regular expression object. valueWithNullInContext:Create the JavaScript value + (JSValue *)valueWithNullInContext:(JSContext *)context; ParametersReturn ValueThe JSValue representing the JavaScript value valueWithObject:inContext:Create a JSValue by converting an Objective-C object. + (JSValue *)valueWithObject:(id)value inContext:(JSContext *)context; ParametersReturn ValueThe new JSValue. DiscussionThe resulting JSValue retains the provided Objective-C object. valueWithUInt32:inContext:Create a JavaScript value from a + (JSValue *)valueWithUInt32:(uint32_t)value inContext:(JSContext *)context; ParametersReturn ValueThe new JSValue representing the equivalent boolean value. valueWithUndefinedInContext:Create the JavaScript value + (JSValue *)valueWithUndefinedInContext:(JSContext *)context; ParametersReturn ValueThe JSValue representing the JavaScript value Properties
contextThe JSContext that this value originates from. @property (readonly, strong) JSContext *context; |