JavaScriptCore框架在iOS7中的对象交互和管理

之前一篇的文章中已经简单入门了iOS7中新加的JavaScriptCore框架的基本用法,十分的简单方便而且高效,不过也仅限于数值型、布尔型、字符串、数组等这些基础类型。本文将扩展到更复杂的类型,介绍一下该强大的框架是如何让Objective-C对象和JavaScript对象进行直接互通的。

为了方便起见,以下所有代码中的JSContext对象都会添加如下的log方法和eventHandler

  1. JSContext *context = [[JSContext alloc] init];
  2. context.exceptionHandler = ^(JSContext *con, JSValue *exception) {
  3. NSLog(@"%@", exception);
  4. con.exception = exception;
  5. };
  6.  
  7. context[@"log"] = ^() {
  8. NSArray *args = [JSContext currentArguments];
  9. for (id obj in args) {
  10. NSLog(@"%@",obj);
  11. }
  12. };



 

键值对编程—Dictionary

JSContext并不能让Objective-C和JavaScript的对象直接转换,毕竟两者的面向对象的设计方式是不同的:前者基于class,后者基于prototype。但所有的对象其实可以视为一组键值对的集合,所以JavaScript中的对象可以返回到Objective-C中当做NSDictionary类型进行访问。

  1. JSValue *obj =[context evaluateScript:@"var jsObj = { number:7, name:'Ider' }; jsObj"];
  2. NSLog(@"%@, %@", obj[@"name"], obj[@"number"]);
  3. NSDictionary *dic = [obj toDictionary];
  4. NSLog(@"%@, %@", dic[@"name"], dic[@"number"]);
  5. //Output:
  6. // Ider, 7
  7. // Ider, 7

同样的,NSDicionaryNSMutableDictionary传入到JSContext之后也可以直接当对象来调用:

  1. NSDictionary *dic = @{@"name": @"Ider", @"#":@(21)};
  2. context[@"dic"] = dic;
  3. [context evaluateScript:@"log(dic.name, dic['#'])"];
  4. //OutPut:
  5. // Ider
  6. // 21

语言穿梭机—JSExport协议

JavaScript可以脱离prototype继承完全用JSON来定义对象,但是Objective-C编程里可不能脱离类和继承了写代码。所以JavaScriptCore就提供了JSExport作为两种语言的互通协议。JSExport中没有约定任何的方法,连可选的(@optional)都没有,但是所有继承了该协议(@protocol)的协议(注意不是Objective-C的类(@interface))中定义的方法,都可以在JSContext中被使用。语言表述起来有点绕,还是用例子来说明会更明确一点。

  1. @protocol PersonProtocol <JSExport>
  2.  
  3. @property (nonatomic, retain) NSDictionary *urls;
  4. - (NSString *)fullName;
  5.  
  6. @end
  7.  
  8. @interface Person :NSObject <PersonProtocol>
  9.  
  10. @property (nonatomic, copy) NSString *firstName;
  11. @property (nonatomic, copy) NSString *lastName;
  12.  
  13. @end;
  14.  
  15. @implementation Person
  16.  
  17. @synthesize firstName, lastName, urls;
  18.  
  19. - (NSString *)fullName {
  20. return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
  21. }
  22.  
  23. @end

在上边的代码中,定义了一个PersonProtocol,并让它继承了神秘的JSExport协议,在新定义的协议中约定urls属性和fullName方法。之后又定义了Person类,除了让它实现PersonProtocol外,还定义了firstName和lastName属性。而fullName方法返回的则是两部分名字的结合。

下边就来创建一个Person对象,然后传入到JSContext中并尝试使用JavaScript来访问和修改该对象。

  1. // initialize person object
  2. Person *person = [[Person alloc] init];
  3. context[@"p"] = person;
  4. person.firstName = @"Ider";
  5. person.lastName = @"Zheng";
  6. person.urls = @{@"site": @"http://www.iderzheng.com"};
  7.  
  8. // ok to get fullName
  9. [context evaluateScript:@"log(p.fullName());"];
  10. // cannot access firstName
  11. [context evaluateScript:@"log(p.firstName);"];
  12. // ok to access dictionary as object
  13. [context evaluateScript:@"log('site:', p.urls.site, 'blog:', p.urls.blog);"];
  14. // ok to change urls property
  15. [context evaluateScript:@"p.urls = {blog:'http://blog.iderzheng.com'}"];
  16. [context evaluateScript:@"log('-------AFTER CHANGE URLS-------')"];
  17. [context evaluateScript:@"log('site:', p.urls.site, 'blog:', p.urls.blog);"];
  18.  
  19. // affect on Objective-C side as well
  20. NSLog(@"%@", person.urls);
  21.  
  22. //Output:
  23. // Ider Zheng
  24. // undefined
  25. // undefined
  26. // site:
  27. // http://www.iderzheng.com
  28. // blog:
  29. // undefined
  30. // -------AFTER CHANGE URLS-------
  31. // site:
  32. // undefined
  33. // blog:
  34. // http://blog.iderzheng.com
  35. // {
  36. // blog = "http://blog.iderzheng.com";
  37. // }

从输出结果不难看出,当访问firstNamelastName的时候给出的结果是undefined,因为它们跟JavaScript没有JSExport的联系。但这并不影响从fullName()中正确得到两个属性的值。和之前说过的一样,对于NSDictionary类型的urls,可以在JSContext中当做对象使用,而且还可以正确地给urls赋予新的值,并反映到实际的Objective-C的Person对象上。

JSExport不仅可以正确反映属性到JavaScript中,而且对属性的特性也会保证其正确,比如一个属性在协议中被声明成readonly,那么在JavaScript中也就只能读取属性值而不能赋予新的值。

对于多参数的方法,JavaScriptCore的转换方式将Objective-C的方法每个部分都合并在一起,冒号后的字母变为大写并移除冒号。比如下边协议中的方法,在JavaScript调用就是:doFooWithBar(foo, bar);

  1. @protocol MultiArgs <JSExport>
  2. - (void)doFoo:(id)foo withBar:(id)bar;
  3. @end

如果希望方法在JavaScript中有一个比较短的名字,就需要用的JSExport.h中提供的宏:JSExportAs(PropertyName, Selector)

  1. @protocol LongArgs <JSExport>
  2.  
  3. JSExportAs(testArgumentTypes,
  4. - (NSString *)testArgumentTypesWithInt:(int)i double:(double)d
  5. boolean:(BOOL)b string:(NSString *)s number:(NSNumber *)n
  6. array:(NSArray *)a dictionary:(NSDictionary *)o
  7. );
  8.  
  9. @end

比如上边定义的协议中的方法,在JavaScript就只要用testArgumentTypes(i, d, b, s, n, a, dic);来调用就可以了。

虽然JavaScriptCore框架还没有官方编程指南,但是在JSExport.h文件中对神秘协议的表述还是比较详细的,其中有一条是这样描述的:

By default no methods or properties of the Objective-C class will be exposed to JavaScript, however methods and properties may explicitly be exported. For each protocol that a class conforms to, if the protocol incorporates the protocol JSExport, then the protocol will be interpreted as a list of methods and properties to be exported to JavaScript.

这里面有个incorporate一词值得推敲,经过验证只有直接继承了JSExport的自定义协议(@protocol)才能在JSContext中访问到。也就是说比如有其它的协议继承了上边的PersonProtocol,其中的定义的方法并不会被引入到JSContext中。从源码中也能看出JavaScriptCore框架会通过class_copyProtocolList方法找到类所遵循的协议,然后再对每个协议通过protocol_copyProtocolList检查它是否遵循JSExport协议进而将方法反映到JavaScript之中。

对已定义类扩展协议— class_addProtocol

对于自定义的Objective-C类,可以通过之前的方式自定义继承了JSExport的协议来实现与JavaScript的交互。对于已经定义好的系统类或者从外部引入的库类,她们都不会预先定义协议提供与JavaScript的交互的。好在Objective-C是可以在运行时实行对类性质的修改的

比如下边的例子,就是为UITextField添加了协议,让其能在JavaScript中可以直接访问text属性。该接口如下:

  1. @protocol JSUITextFieldExport <JSExport>
  2.  
  3. @property(nonatomic,copy) NSString *text;
  4.  
  5. @end

之后在通过class_addProtocol为其添加上该协议:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. textField.text = @"7";
  5. class_addProtocol([UITextField class], @protocol(JSUITextFieldExport));
  6. }

为一个UIButton添加如下的事件,其方法只要是将textField传入到JSContext中然后读取其text值,自增1后重新赋值:

  1. - (IBAction)pressed:(id)sender {
  2. JSContext *context = [[JSContext alloc] init];
  3.  
  4. context[@"textField"] = textField;
  5.  
  6. NSString *script = @"var num = parseInt(textField.text, 10);"
  7. "++num;"
  8. "textField.text = num;";
  9. [context evaluateScript:script];
  10. }

当运行点击UIButton时就会看到UITextField的值在不断增加,也证明了对于已定义的类,也可以在运行时添加神奇的JSExport协议让它们可以在Objective-C和JavaScript直接实现友好互通。

iOS Simulator Screen shot Nov 3, 2013 2.57.19 PM iOS Simulator Screen shot Nov 3, 2013 2.57.29 PM

不同内存管理机制—Reference Counting vs. Garbage Collection

虽然Objetive-C和JavaScript都是面向对象的语言,而且它们都可以让程序员专心于业务逻辑,不用担心内存回收的问题。但是两者的内存回首机制全是不同的,Objective-C是基于引用计数,之后Xcode编译器又支持了自动引用计数(ARC, Automatic Reference Counting);JavaScript则如同Java/C#那样用的是垃圾回收机制(GC, Garbage Collection)。当两种不同的内存回收机制在同一个程序中被使用时就难免会产生冲突。

比如,在一个方法中创建了一个临时的Objective-C对象,然后将其加入到JSContext放在JavaScript中的变量中被使用。因为JavaScript中的变量有引用所以不会被释放回收,但是Objective-C上的对象可能在方法调用结束后,引用计数变0而被回收内存,因此JavaScript层面也会造成错误访问。

同样的,如果用JSContext创建了对象或者数组,返回JSValue到Objective-C,即使把JSValue变量retain下,但可能因为JavaScript中因为变量没有了引用而被释放内存,那么对应的JSValue也没有用了。

怎么在两种内存回收机制中处理好对象内存就成了问题。JavaScriptCore提供了JSManagedValue类型帮助开发人员更好地管理对象内存。

  1. @interface JSManagedValue : NSObject
  2.  
  3. // Convenience method for creating JSManagedValues from JSValues.
  4. + (JSManagedValue *)managedValueWithValue:(JSValue *)value;
  5.  
  6. // Create a JSManagedValue.
  7. - (id)initWithValue:(JSValue *)value;
  8.  
  9. // Get the JSValue to which this JSManagedValue refers. If the JavaScript value has been collected,
  10. // this method returns nil.
  11. - (JSValue *)value;
  12.  
  13. @end

在《iOS7新JavaScriptCore框架入门介绍》有提到JSVirtualMachine为整个JavaScriptCore的执行提供资源,所以当将一个JSValue转成JSManagedValue后,就可以添加到JSVirtualMachine中,这样在运行期间就可以保证在Objective-C和JavaScript两侧都可以正确访问对象而不会造成不必要的麻烦。

  1. @interface JSVirtualMachine : NSObject
  2.  
  3. // Create a new JSVirtualMachine.
  4. - (id)init;
  5.  
  6. // addManagedReference:withOwner and removeManagedReference:withOwner allow
  7. // clients of JSVirtualMachine to make the JavaScript runtime aware of
  8. // arbitrary external Objective-C object graphs. The runtime can then use
  9. // this information to retain any JavaScript values that are referenced
  10. // from somewhere in said object graph.
  11. //
  12. // For correct behavior clients must make their external object graphs
  13. // reachable from within the JavaScript runtime. If an Objective-C object is
  14. // reachable from within the JavaScript runtime, all managed references
  15. // transitively reachable from it as recorded with
  16. // addManagedReference:withOwner: will be scanned by the garbage collector.
  17. //
  18. - (void)addManagedReference:(id)object withOwner:(id)owner;
  19. - (void)removeManagedReference:(id)object withOwner:(id)owner;
  20.  
  21. @end

了解更多更多—Source Code

对于iOS7提供JavaScriptCore已经介绍的差不多了,之前也提到这其实是一个开源的框架,所以如果想要在低版本的iOS上使用,也可以很容易地自行添加源码进行编译和使用。

阅读源码也可以更加了解JavaScriptCore是怎么实现的,在开发时候也可以注意到更多的细节避免错误的发生,想要阅读框架的源码可以在这里(源码1源码2源码3)。

文章中的代码和例子都比较简单,如果想了解更多JavaScriptCore的使用方法,在这里有详细的测试案例可以提供一些线索。不过经验证并不是所有的测试案例在iOS7中都会通过,这大概是测试案例所用的JavaScriptCore是为chromium实现的而iOS7是webkit吧。

原文地址:https://www.cnblogs.com/luqinbin/p/5556222.html