繼承自:NSObject
遵守協(xié)議:NSObject
導入聲明:@import JavaScriptCore;
適用范圍:iOS 7.0 及以后
一瘦赫、概述
一個 JSVirtualMachine 實例代表一個執(zhí)行 JavaScript 的自包含(self-contained)的環(huán)境史隆。你可以用這個類做兩件事情:① JavaScript 的并發(fā)執(zhí)行夹纫;② 橋接 JavaScript 和 Objective-C 或 Swift 的對象的內(nèi)存管理。
1. 線程和 JavaScript 的并發(fā)執(zhí)行(Threading and Concurrent JavaScript Execution)
每一個 JavaScript 上下文(也就是一個 JSContext 對象)歸屬于一個虛擬機篙议。每一個虛擬機可以包含多個不同的上下文(context),而且可以在不同的上下文(context)之間傳值(JSValue 對象)。但是翘地,每一個虛擬機都是獨立的——你不能將在一個虛擬機中創(chuàng)建的值傳到另一個虛擬機的一個上下文中。
JavaScriptCore 的 API 是線程安全的癌幕。比如衙耕,你可以在任一線程中創(chuàng)建 JSValue 對象或者執(zhí)行 scripts,但是勺远,想要使用同一個虛擬機的所有其他線程都需要等待橙喘。如果要在多條不同線程上并發(fā)執(zhí)行 JavaScript ,那么你就要確保每一條線程使用的 JSVirtualMachine 實例都是獨立的胶逢。
2.導出對象的內(nèi)存管理(Managing Memory for Exported Objects)
當你將一個 Objective-C 或者 Swift 對象轉成 JavaScript時厅瞎,你一定不要在那個對象中存儲 JavaScript 值。否則初坠,這將導致循環(huán)引用—— JSValue 對象對它們的封閉的 JavaScript 上下文進行了強引用和簸, 而 JSContext 又對要被轉成 JavaScript 的原生對象進行了強引用。 你應該用 JSManagedValue 類有條件地持有(retain)一個 JavaScript 值碟刺,并且為 managed value向 JavaScriptCore 虛擬機說明原生的擁有關系鏈(ownership chain)锁保。使用 addManagedReference:withOwner: 和 removeManagedReference:withOwner: 方法向 JavaScriptCore 描述你的原生對象圖(object graph)。在你移除了一個對象的最后一個 managed reference 后半沽,那個對象將會被 JavaScript 垃圾回收器(garbage collector)安全銷毀爽柒。
二、功能(Tasks)
1.創(chuàng)建一個 JavaScript 虛擬機(Creating a JavaScript Virtual Machine)
- init
Initializes a JavaScript virtual machine.
Declaration
- (instancetype)init
Return Value
A new, independent JavaScript virtual machine.
Discussion
Use this initializer to create a virtual machine for use with more than one JavaScript context. By default, creating a JSContext object automatically creates an independent virtual machine—to share a virtual machine between contexts, obtain a JSVirtualMachine instance and then create contexts using the initWithVirtualMachine: initializer.
Availability
Available in iOS 7.0 and later.
2.橋接值的內(nèi)存管理(Managing Memory for Bridged Values)
- addManagedReference:withOwner:
Notifies the JavaScriptCore virtual machine of an external object relationship.
Declaration
- (void)addManagedReference:(id)object
withOwner:(id)owner
Parameters
參數(shù) | 含義 |
---|---|
object | The object to be referenced by the JavaScript memory management graph. |
owner | The other object responsible for the lifetime of the reference. |
Discussion
Use this method to make the JavaScript runtime aware of arbitrary external Objective-C or Swift object graphs. The runtime can then use this information to retain any JavaScript values that are referenced from somewhere in said object graph.
For correct behavior, clients must make their external object graphs reachable from within the JavaScript runtime. If an Objective-C or Swift object is reachable from within the JavaScript runtime, all managed references transitively reachable from it as recorded using the addManagedReference:withOwner: method are scanned by the garbage collector.
Availability
Available in iOS 7.0 and later.
- removeManagedReference:withOwner:
Notifies the JavaScriptCore virtual machine that a previously registered object relationship no longer exists.
Declaration
- (void)removeManagedReference:(id)object
withOwner:(id)owner
Parameters
參數(shù) | 含義 |
---|---|
object | The object formerly referenced by the JavaScript memory management graph. |
owner | The other object responsible for the lifetime of the reference. |
Discussion
Use this method to deregister object relationships recorded using the removeManagedReference:withOwner: method.
The JavaScript garbage collector continues to scan any references that were reported to it until you use this method to remove those references.
Availability
Available in iOS 7.0 and later.
問題(Question)
JSVirtualMachine 在內(nèi)存管理中扮演了什么樣的角色者填?