JSValue
A JavaScript value.
Declaration
class JSValueOverview
You use the JSValue class to convert basic values, such as numbers and strings, between JavaScript and Objective-C or Swift representations 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 with implementations that native methods or blocks provide.
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 it retains any value for a particular JSContext instance, that context remains alive. When you invoke an instance method on a JSValue object, and that method returns another JSValue object, the returned value belongs to the same context as the original value.
Each JavaScript value also has an association (indirectly via the context property) with a specific JSVirtualMachine object that represents the underlying set of execution resources for its context. You can pass JSValue instances only to methods on JSValue and JSContext instances on the same virtual machine — attempting to pass a value to a different virtual machine raises an Objective-C exception.
Convert Between JavaScript and Native Types
When you use the JSValue methods for creating, reading, and converting JavaScript values, JavaScriptCore automatically converts native values to JavaScript values and vice versa, using the rules below.
NSDictionary objects or Swift dictionaries and the keys they contain become JavaScript objects with matching named properties and vice versa. JavaScriptCore recursively copies and converts the values for keys.
NSArray objects or Swift arrays become JavaScript arrays and vice versa, with elements that JavaScriptCore recursively copies and converts.
Objective-C blocks (or Swift closures with the
@convention(block)attribute) become JavaScriptFunctionobjects, with parameter and return types that JavaScriptCore converts using the same rules as values. Converting a JavaScript function with a backing from 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 that reflects the native class hierarchy. By default, the JavaScript wrapper for a native object doesn’t 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 below:
Objective-C (and Swift) types | JavaScript types | Notes |
|---|---|---|
|
| |
| ||
| ||
Nsnumber and primitive numeric types |
| Conversion is consistent with the following methods: [Image] Init(int32:in:) / Toint32() for signed integer types [Image] Init(uint32:in:) / Touint32() for unsigned integer types [Image] Init(bool:in:) / Tobool() for Boolean types [Image] Init(double:in:) / Tobool() for all other numeric types |
Nsdictionary (Swift Dictionary) |
| Recursive conversion. |
| Recursive conversion. | |
| ||
Objective-C or Swift object (Objc_object or Anyobject) [Image] Objective-C or Swift class (Class or Anyclass) |
| Converts with Init(object:in:) / Toobject(). |
Structure types: [Image] Nsrange C.struct, Cgrect, Cgpoint, Cgsize |
| There isn’t support for other structure types. |
Objective-C block (Swift closure) |
| Convert explicitly with Init(object:in:) / Toobject(). [Image] JavaScript functions don’t convert to native blocks/closures unless they already have a backing from a native block/closure. |
Topics
Creating JavaScript Values
init(object:in:)init(bool:in:)init(double:in:)init(int32:in:)init(uInt32:in:)init(newObjectIn:)init(newArrayIn:)init(newRegularExpressionFromPattern:flags:in:)init(newErrorFromMessage:in:)init(undefinedIn:)init(nullIn:)init(point:inContext:)init(range:inContext:)init(rect:inContext:)init(size:inContext:)init(newSymbolFromDescription:in:)init(newPromiseIn:fromExecutor:)init(newPromiseRejectedWithReason:in:)init(newPromiseResolvedWithResult:in:)
Reading and Converting JavaScript Values
toObject()toObjectOf(_:)toBool()toDouble()toInt32()toUInt32()toNumber()toString()toDate()toArray()toDictionary()toPoint()toRange()toRect()toSize()
Determining the Type of a JavaScript Value
Comparing JavaScript Values
Working with Function and Constructor Values
Working with Container Values
defineProperty(_:descriptor:)hasProperty(_:)deleteProperty(_:)atIndex(_:)setValue(_:at:)forProperty(_:)setValue(_:forProperty:)JSValueProperty
Accessing a Value’s JavaScript Context
Accessing Values with Subscript Syntax
objectAtIndexedSubscript(_:)setObject(_:atIndexedSubscript:)objectForKeyedSubscript(_:)setObject(_:forKeyedSubscript:)
Working with the C JavaScriptCore API
Constants
Initializers
init(UInt32:inContext:)init(bool:inContext:)init(double:inContext:)init(int32:inContext:)init(jsValueRef:in:)init(newArrayInContext:)init(newBigIntFrom:in:)init(newBigIntFrom:in:)init(newBigIntFrom:in:)init(newBigIntFrom:in:)init(newBigIntFromDouble:inContext:)init(newBigIntFromInt64:inContext:)init(newBigIntFromString:inContext:)init(newBigIntFromUInt64:inContext:)init(newErrorFromMessage:inContext:)init(newObjectInContext:)init(newPromiseInContext:fromExecutor:)init(newPromiseRejectedWithReason:inContext:)init(newPromiseResolvedWithResult:inContext:)init(newRegularExpressionFromPattern:flags:inContext:)init(newSymbolFromDescription:inContext:)init(nullInContext:)init(object:inContext:)init(point:in:)init(range:in:)init(rect:in:)init(size:in:)init(undefinedInContext:)