JavaScriptCore
We can easily access any values we’ve created in our
context
using subscript notation on bothJSContext
andJSValue
instances.JSContext
requires a string subscript, whileJSValue
allows either string or integer subscripts for delving down into objects and arrays:
Swift doesn’t support the subscript notation, though.
With a
JSValue
that wraps a JavaScript function, we can call that function directly from our Objective-C/Swift code using Foundation types as parameters. Once again, JavaScriptCore handles the bridging without any trouble[…][…]
There are two main ways of giving a
JSContext
access to our native client code: blocks and theJSExport
protocol.[…]
Since blocks can capture references to variables and
JSContext
s maintain strong references to all their variables, some care needs to be taken to avoid strong reference cycles. Avoid capturing yourJSContext
or anyJSValue
s inside a block. Instead, use[JSContext currentContext]
to get the current context and pass any values you need as parameters.