三、Memory management
1• Objective-C uses ARC(OC 使用ARC机制)
2• JavaScriptCore uses garbage collection (JS 使用垃圾回收机制)
■ All references are strong (JS中全部都是“强引用”)
3• API memory management is mostly automatic
4• Two situations that require extra attention: (几乎SDK已经做好了很多事情,所以开发者只需要重点掌握以下亮点)
■ Storing JavaScript values in Objective-C objects
■ Adding JavaScript fields to Objective-C objects
(英文文档只要被翻译了,就难免失去原有的含义,尽量不翻译~)
根据官方文档关于JS-OC内存管理总结:由于JS中全部都是强引用,如果JS 与 OC互相引用时,就要防止OC也强引用JS,这样会形成引用循环,所以OC要想办法弱引用,但弱引用会被系统释放,所以把可能被释放的对象放到一个容器中来防止对象被被错误释放。
看代码(一):
JS:
function ClickHandler(button, callback) {
this.button = button;
this.button.onClickHandler = this;
this.handleEvent = callback;
};
OC:
@implementation MyButton
- (void)setOnClickHandler:(JSValue *)handler
{
_onClickHandler = handler; // Retain cycle
}
@end
如果直接保存 handler,就会出现内存泄露,因为 JS 中引用 button 对象是强引用,如果 Button 也用强引用来保存 JS 中的 handler,这就导致了 循环引用。我们没法改变 JavaScript 中的强引用机制,只能在 Objective-C 中弱引用 handler,为了防止 onclick handler 被错误释放, JavaScriptCore 给出的解决方案如下:
- (void)setOnClickHandler:(JSValue *)handler
{
_onClickHandler = [JSManagedValue managedValueWithValue:handler];
[_context.virtualMachine addManagedReference:_onClickHandler
withOwner:self]
}
代码(二):
- (void)loadColorsPlugin
{
// Load the plugin script from the bundle.
NSString *path = [[NSBundlemainBundle]pathForResource:@"colors"ofType:@"js"];
NSString *pluginScript = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
_context = [[JSContextalloc]init];
// We insert the AppDelegate into the global object so that when we call
// -addManagedReference:withOwner: for the plugin object we're about to load
// and pass the AppDelegate as the owner, the AppDelegate itself is reachable from
// within JavaScript. If we didn't do this, the AppDelegate wouldn't be reachable
// from JavaScript, and there wouldn't be anything keeping the plugin object alive.
_context[@"AppDelegate"] =self;
// Insert a block so that the plugin can create NSColors to return to us later.
_context[@"makeNSColor"] = ^(NSDictionary *rgb){
return [NSColorcolorWithRed:[rgb[@"red"]floatValue] / 255.0f
green:[rgb[@"green"]floatValue] /255.0f
blue:[rgb[@"blue"]floatValue] /255.0f
alpha:1.0f];
};
JSValue *plugin = [_contextevaluateScript:pluginScript];
_colorPlugin = [JSManagedValuemanagedValueWithValue:plugin];
[_context.virtualMachineaddManagedReference:_colorPluginwithOwner:self];
[self.windowsetDelegate:self];
}
注意:
JSManagedValue:
The primary use case for JSManagedValue is for safely referencing JSValues
from the Objective-C heap. It is incorrect to store a JSValue into an
Objective-C heap object, as this can very easily create a reference cycle,
keeping the entire JSContext alive.
(将 JSValue 转为 JSManagedValue 类型后,可以添加到 JSVirtualMachine 对象中,这样能够保证你在使用过程中 JSValue 对象不会被释放掉,当你不再需要该 JSValue 对象后,从 JSVirtualMachine 中移除该 JSManagedValue 对象,JSValue 对象就会被释放并置空。)
JSVirtualMachine:
All instances of JSContext are associated with a single JSVirtualMachine. The
virtual machine provides an "object space" or set of execution resources.(JSVirtualMachine就是一个用于保存弱引用对象的数组,加入该数组的弱引用对象因为会被该数组 retain,所以保证了使用时不会被释放,当数组里的对象不再需要时,就从数组中移除,没有了引用的对象就会被系统释放。)
四、Threading
• API is thread safe• Locking granularity is JSVirtualMachine
■ Use separate JSVirtualMachines for concurrency/parallelism
五、JavaScriptCore C API
JSValue ↔ JSValueRef :
JSValueRef valueRef = XXX;
JSValue *value = [JSValue valueWithJSValueRef:valueRef inContext:context];
JSValue *value = XXX;
JSValueRef valueRef = [value JSValueRef];
JSContext ↔ JSGlobalContextRef :
JSGlobalContextRef ctx = XXX;
JSContext *context = [JSContext contextWithJSGlobalContextRef:ctx];
JSContext *context = XXX;
JSGlobalContextRef ctx = [context JSGlobalContextRef];
五、JavaScriptCore with a WebView (Mac)
暂不讨论,Demos下载中有项目源码。
参考资料:
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/ WebKit_JavaScriptCore_Ref/
https://developer.apple.com/wwdc/schedule/details.php?id=615
https://github.com/zynga/jsbindings
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Protocols/WebScripting_Protocol/Reference/Reference.html
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/WebKit/Classes/WebScriptObject_Class/Reference/Reference.html#//apple_ref/doc/c_ref/WebScriptObject
Demos下载直通车:http://download.youkuaiyun.com/detail/zfpp25_/8510931(其中 TestJS 项目为本文测试使用项目)