Contents

JSValue

A JavaScript value.

Declaration

class JSValue

Overview

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 JavaScript Function objects, 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

nil

undefined

Nsnull

null

Nsstring (Swift String)

String

Nsnumber and primitive numeric types

Number, Boolean

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)

Object

Recursive conversion.

Nsarray (Swift Array)

Array

Recursive conversion.

Nsdate

Date

Objective-C or Swift object (Objc_object or Anyobject) [Image] Objective-C or Swift class (Class or Anyclass)

Object

Converts with Init(object:in:) / Toobject().

Structure types: [Image] Nsrange C.struct, Cgrect, Cgpoint, Cgsize

Object

There isn’t support for other structure types.

Objective-C block (Swift closure)

Function

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

Reading and Converting JavaScript Values

Determining the Type of a JavaScript Value

Comparing JavaScript Values

Working with Function and Constructor Values

Working with Container Values

Accessing a Value’s JavaScript Context

Accessing Values with Subscript Syntax

Working with the C JavaScriptCore API

Constants

Initializers

Instance Properties

Instance Methods

Default Implementations

See Also

JavaScript Code