继承自:NSObject
遵守协议:NSObject
导入声明:@import JavaScriptCore;
适用范围:iOS 7.0 及以后
一、概述
一个 JSVirtualMachine 实例代表一个执行 JavaScript 的自包含(self-contained)的环境。你可以用这个类做两件事情:① JavaScript 的并发执行;② 桥接 JavaScript 和 Objective-C 或 Swift 的对象的内存管理。
1. 线程和 JavaScript 的并发执行(Threading and Concurrent JavaScript Execution)
每一个 JavaScript 上下文(也就是一个 JSContext 对象)归属于一个虚拟机。每一个虚拟机可以包含多个不同的上下文(context),而且可以在不同的上下文(context)之间传值(JSValue 对象)。但是,每一个虚拟机都是独立的——你不能将在一个虚拟机中创建的值传到另一个虚拟机的一个上下文中。
JavaScriptCore 的 API 是线程安全的。比如,你可以在任一线程中创建 JSValue 对象或者执行 scripts,但是,想要使用同一个虚拟机的所有其他线程都需要等待。如果要在多条不同线程上并发执行 JavaScript ,那么你就要确保每一条线程使用的 JSVirtualMachine 实例都是独立的。
2.导出对象的内存管理(Managing Memory for Exported Objects)
当你将一个 Objective-C 或者 Swift 对象转成 JavaScript时,你一定不要在那个对象中存储 JavaScript 值。否则,这将导致循环引用—— 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.创建一个 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.桥接值的内存管理(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
参数 | 含义 |
---|---|
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
参数 | 含义 |
---|---|
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.
参考(Reference)
https://developer.apple.com/library/ios/documentation/JavaScriptCore/Reference/JSVirtualMachine_Ref/index.html#//apple_ref/occ/cl/JSVirtualMachine
问题(Question)
JSVirtualMachine 在内存管理中扮演了什么样的角色?