天天看點

IOS7開發~JavaScriptCore (二)

IOS7開發~JavaScriptCore(一)

http://blog.csdn.net/zfpp25_/article/details/9232129

IOS7開發~JavaScriptCore(二)

http://blog.csdn.net/zfpp25_/article/details/9236357

三、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.csdn.net/detail/zfpp25_/8510931(其中 TestJS 項目為本文測試使用項目)

更多相關:

IOS7開發~JavaScriptCore (一)

iOS