A JSContext object represents a JavaScript execution environment. You create and use JavaScript contexts to evaluate JavaScript scripts from Objective-C or Swift code, to access values defined in or calculated in JavaScript, and to make native objects, methods, or functions accessible to JavaScript.


class JSContext : NSObject


Creating JavaScript Contexts


Initializes a new JavaScript context.

init!(virtualMachine: JSVirtualMachine!)

Creates a new JavaScript context associated with a specific virtual machine.

Evaluating Scripts

func evaluateScript(String!) -> JSValue!

Executes the specified JavaScript code.

func evaluateScript(String!, withSourceURL: URL!) -> JSValue!

Executes the specified JavaScript code, treating the specified URL as its source location.

Inspecting Callback State in a Running Context

class func current() -> JSContext!

Returns the context currently executing JavaScript code.

class func currentCallee() -> JSValue!

Returns the currently executing JavaScript function.

class func currentThis() -> JSValue!

Returns the value of the this keyword in currently executing JavaScript code.

class func currentArguments() -> [Any]!

Returns the arguments to the current native callback from JavaScript code.

Working with JavaScript Global State

var globalObject: JSValue!

The JavaScript global object associated with the context.

var exception: JSValue!

A JavaScript exception to be thrown in evaluation of the script.

var exceptionHandler: ((JSContext?, JSValue?) -> Void)!

A block to be invoked should evaluating a script result in a JavaScript exception being thrown.

var virtualMachine: JSVirtualMachine!

The JavaScript virtual machine to which the context belongs.

var name: String!

A descriptive name for the context.

Accessing JavaScript Global State with Subscripts

func objectForKeyedSubscript(Any!) -> JSValue!

Returns the value of the specified JavaScript property in the context’s global object, allowing subscript getter syntax.

func setObject(Any!, forKeyedSubscript: (NSCopying & NSObjectProtocol)!)

Sets the specified JavaScript property of the context’s global object, allowing subscript setter syntax.

Working with the C JavaScriptCore API

var jsGlobalContextRef: JSGlobalContextRef!

Returns the C representation of the JavaScript context.

init!(jsGlobalContextRef: JSGlobalContextRef!)

Creates a JavaScript context object from the equivalent C representation.


Inherits From

Conforms To