Class

JSValue

A JSValue instance is a reference to a JavaScript value. You use the JSValue class to convert basic values (such as numbers and strings) between JavaScript and Objective-C or Swift representations in order to pass data between native code and JavaScript code. You can also use this class to create JavaScript objects that wrap native objects of custom classes or JavaScript functions whose implementations are provided by native methods or blocks.

Declaration

@interface JSValue : NSObject

Overview

Each JSValue instance originates from a JSContext object that represents the JavaScript execution environment containing that value. The value holds a strong reference to its context object—as long as any value associated with a particular JSContext instance is retained, that context remains alive. When you invoke an instance method is invoked on a JSValue object, and that method returns another JSValue object, the returned value belongs to the same context as original value.

Each JavaScript value is also associated (indirectly via the context property) with a specific JSVirtualMachine object representing the underlying set of execution resources for its context. You can pass JSValue instances only to methods on JSValue and JSContext instances hosted by the same virtual machine—attempting to pass a value to a different virtual machine raises an Objective-C exception.

Converting Between JavaScript and Native Types

When you use the methods listed in Creating JavaScript Values and Reading and Converting JavaScript Values, JavaScriptCore automatically converts native values to JavaScript values and vice versa, using the rules summarized below.

  • NSDictionary objects or Swift dictionaries and the keys they contain become JavaScript objects with matching named properties and vice versa. Values for keys are recursively copied and converted.

  • NSArray objects or Swift arrays become JavaScript arrays and vice versa, with elements recursively copied and converted.

  • Objective-C blocks (or Swift closures with the @convention(block) attribute) become JavaScript Function objects, with parameter and return types converted using the same rules as values. Converting a JavaScript function backed by a native block or method returns that block or method; all other JavaScript functions convert as empty dictionaries.

  • For all other native object types (and class types or metatypes), JavaScriptCore creates a JavaScript wrapper object with a constructor prototype chain reflecting the native class hierarchy. By default, the JavaScript wrapper for a native object does not make that object’s properties and methods available in JavaScript. To choose properties and methods for export to JavaScript, see JSExport.

When you convert an object, method, or block, JavaScriptCore implicitly converts the types and values of object properties and method parameters using the rules summarized in Table 1.

Table 1

Type Conversions Between Objective-C or Swift and JavaScript

Objective-C (and Swift) Types

JavaScript Types

Notes

nil

undefined

NSNull

null

NSString (Swift String)

String

NSNumber and primitive numeric types

Number, Boolean

Conversion is consistent with the following methods:

valueWithInt32:inContext:/toInt32 for signed integer types

valueWithUInt32:inContext:/toUInt32 for unsigned integer types

valueWithBool:inContext:/toBool for Boolean types

valueWithDouble:inContext:/toBool for all other numeric types

NSDictionary (Swift Dictionary)

Object

Recursive conversion.

NSArray (Swift Array)

Array

Recursive conversion.

NSDate

Date

Objective-C or Swift object (id or AnyObject)

Objective-C or Swift class (Class or AnyClass)

Object

Converts with valueWithObject:inContext:/toObject.

Structure types:

NSRange, CGRect, CGPoint, CGSize

Object

Other structure types are not supported.

Objective-C block (Swift closure)

Function

Convert explicitly with valueWithObject:inContext:/toObject.

JavaScript functions do not convert to native blocks/closures unless already backed by a native block/closure.

Topics

Creating JavaScript Values

+ valueWithObject:inContext:

Creates a JavaScript value by converting the specified native object.

+ valueWithBool:inContext:

Creates a JavaScript representation of the specified Boolean value.

+ valueWithDouble:inContext:

Creates a JavaScript representation of the specified floating-point value.

+ valueWithInt32:inContext:

Creates a JavaScript representation of the specified signed integer value.

+ valueWithUInt32:inContext:

Creates a JavaScript representation of the specified unsigned integer value.

+ valueWithNewObjectInContext:

Creates a new, empty JavaScript object value.

+ valueWithNewArrayInContext:

Creates a new, empty JavaScript array value.

+ valueWithNewRegularExpressionFromPattern:flags:inContext:

Creates a JavaScript regular expression value from the specified pattern.

+ valueWithNewErrorFromMessage:inContext:

Creates a JavaScript error value with the specified error message.

+ valueWithUndefinedInContext:

Creates a JavaScript undefined value.

+ valueWithNullInContext:

Creates a JavaScript null value.

+ valueWithPoint:inContext:

Creates a JavaScript representation of the specified point.

+ valueWithRange:inContext:

Creates a JavaScript representation of the specified range.

+ valueWithRect:inContext:

Creates a JavaScript representation of the specified rectangle.

+ valueWithSize:inContext:

Creates a JavaScript representation of the specified width and height.

Reading and Converting JavaScript Values

- toObject

Converts the JavaScript value to a native object.

- toObjectOfClass:

Converts the JavaScript value to a native object of the specified class.

- toBool

Converts the JavaScript value to a native Boolean value.

- toDouble

Converts the JavaScript value to a native floating-point value.

- toInt32

Converts the JavaScript value to a native signed integer value.

- toUInt32

Converts the JavaScript value to a native unsigned integer value.

- toNumber

Converts the JavaScript value to a NSNumber object.

- toString

Converts the JavaScript value to a native string.

- toDate

Converts the JavaScript value to a date object.

- toArray

Converts the JavaScript value to an array.

- toDictionary

Converts the JavaScript value to a dictionary.

- toPoint

Converts the value to a point structure.

- toRange

Converts the value to a range.

- toRect

Converts the value to a rectangle structure.

- toSize

Converts the value to a size.

Determining the Type of a JavaScript Value

isUndefined

A Boolean value that indicates whether the instance corresponds to the JavaScript undefined value.

isNull

A Boolean value that indicates whether the instance corresponds to the JavaScript null value.

isBoolean

A Boolean value that indicates whether the instance is a JavaScript Boolean value.

isNumber

A Boolean value that indicates whether the instance is a JavaScript numeric value.

isString

A Boolean value that indicates whether the instance is a JavaScript String object.

isObject

A Boolean value that indicates whether the instance is a JavaScript object.

isArray

A Boolean value that indicates whether the instance is a JavaScript array value.

isDate

A Boolean value that indicates whether the instance is a JavaScript Date object.

Comparing JavaScript Values

- isEqualToObject:

Compares the value to another for strict equality.

- isEqualWithTypeCoercionToObject:

Compares the value to another for equivalence, allowing type conversion.

- isInstanceOf:

Returns a Boolean value indicating whether the value is an instance of another JavaScript object value.

Working with Function and Constructor Values

- callWithArguments:

Invokes the value as a JavaScript function.

- constructWithArguments:

Invokes the value as a JavaScript constructor.

- invokeMethod:withArguments:

Calls the named JavaScript method on the value.

Working with Container Values

- defineProperty:descriptor:

Defines a property on the JavaScript object value or modifies a property’s definition.

- hasProperty:

Returns a Boolean value indicating whether the JavaScript value has a defined property with the specified name.

- deleteProperty:

Deletes the named property from the JavaScript object value.

- valueAtIndex:

Returns the value at the specified numeric index in the JavaScript object value.

- setValue:atIndex:

Sets the value at the specified numeric index in the JavaScript object value.

- valueForProperty:

Returns the value of the named property in the JavaScript object value.

- setValue:forProperty:

Sets the value of the named property in the JavaScript object value.

Accessing a Value’s JavaScript Context

context

The JavaScript context hosting this value.

Accessing Values with Subscript Syntax

- objectAtIndexedSubscript:

Returns the value’s JavaScript property at the specified index, allowing subscript syntax.

- setObject:atIndexedSubscript:

Sets the value’s JavaScript property at the specified index, allowing subscript syntax.

- objectForKeyedSubscript:

Returns the value’s JavaScript property named with the specified key, allowing subscript syntax.

- setObject:forKeyedSubscript:

Sets the value’s JavaScript property named with the specified key, allowing subscript syntax.

Working with the C JavaScriptCore API

JSValueRef

Returns the C representation of the JavaScript value.

+ valueWithJSValueRef:inContext:

Creates a JavaScript value object from the equivalent C representation.

Constants

Property Descriptor Keys

Keys for the native dictionary representation of a JavaScript property descriptor, used with the defineProperty:descriptor: method.

Instance Properties

Relationships

Inherits From