However, a JSValue can’t convert Objective C/Swift classes to JavaScript objects without help. JSExportĪ JSValue can represent and convert all of the JavaScript builtin types to Objective C/Swift and can convert them in the other direction to JavaScript types. Otherwise, the JSManagedValue gets set to nil, releasing the JSValue, and is free to be garbage-collected on the JavaScript side. A JSManagedValue will live on under two conditions: 1) its value is still part of the underlying JavaScript object graph or 2) It’s been added to the JSVirtualMachine using addManagedReference:withOwner method and has not been removed using the removeManagedReference:withOwner method. There is built in memory management to automatically release objects when they lose scope, which is similar to how ARC works for Objective C. JSManagedValueĪ JSManagedValue is a JSValue with additional logic to allow a native object to store the underlying JavaScript value without causing a retain cycle. You can also use them to create JavaScript functions that are written in Objective C/Swift. In addition to accessing underlying JavaScript objects, you can use a JSValue to create JavaScript objects that are wrappers to native objects in Swift/Objective C. As a word of caution, you need to remember that you can cause a retain cycle if you store the JSValue in Swift/Objective C object. A JSValue has a strong reference to the JSContext to which it belongs. Its the bridge that allows you to pass or share data between JavaScript and Objective C/Swift. JSValueĪ JSValue is the wrapper to an underlying JavaScript value. You can evaluate scripts written in JavaScript or Objective C/Swift, access values in the JavaScript environment, or send values and objects to JavaScript from Objective C/Swift using the JSContext. This, essentially, is your sandbox for controlling the scope of your JavaScript variables, functions, and objects. Everything that you add to this context is accessible by any other object in the same context. JSContextĪ JSContext is your JavaScript execution environment, and it is very similar to the window object for a web browser. The JavaScript garbage collector will only clean up objects after you remove references to all managed objects. In addition, you should use a JSManagedValue object when you need to conditionally store a JSValue on the native side. Therefore, you need to use JSVirtualMachine’s removeManagedReference:withOwner method to clean up memory when you’re done with your objects. Similarly, a JSContext maintains a strong reference to any Swift/Objective C object you pass to JavaScript. Retain cycles can occur because a JSValue maintains a strong reference in order to access the underlying JavaScript value on the native side. You also can get a retain cycle when storing JavaScript values in native objects (Swift/Objective C). You will create a retain cycle anytime you export a Swift/Objective C object to JavaScript and store the value, which is bad news. If you had created a background thread for this purpose, you would not get the results you expected.Īs for memory issues - the second purpose - the JSVirtualMachine gives you a mechanism to free memory when you are done with an object. Because the JavaScriptCore Api is thread safe, any attempts to access code running in a JSVirtualMachine from a separate thread will wait until the initial thread is finished. When you need to create separate threads, you also need to create a new JSVirtualMachine for each thread. It has two purposes: 1) to allow you to run code in separate threads concurrently and 2) to allow you to clean up memory you allocate when bridging between Objective C/Swift and JavaScript.Įverything you do in JavaScript ultimately executes in a JSVirtualMachine. JSVirtualMachineĪ JSVirtualMachine is the self-contained environment within which your JavaScript code executes. To keep this article reasonable in length, I will briefly cover the basic elements you need and then dive into some examples. As you might guess, I’m going to talk about how you can integrate JavaScript libraries into your Swift code and then how to can share data and call methods in JavaScript from Swift. There are so many libraries written in JavaScript that I frankly think it’s silly to have to recreate those for other languages. I love the web, and the language of the web is JavaScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |