Tuesday, January 20, 2015

JavaScriptCore

Nate Cook:

We can easily access any values we’ve created in our context using subscript notation on both JSContext and JSValue instances. JSContext requires a string subscript, while JSValue 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 the JSExport protocol.

[…]

Since blocks can capture references to variables and JSContexts maintain strong references to all their variables, some care needs to be taken to avoid strong reference cycles. Avoid capturing your JSContext or any JSValues inside a block. Instead, use [JSContext currentContext] to get the current context and pass any values you need as parameters.

Comments RSS · Twitter

Leave a Comment