JavaScriptCore初探

原:https://github.com/YanlongMa/SwiftJavaScriptCore

注:JavaScriptCore API也可以用Swift来调用,本文用Objective-C来介绍。

在iOS7之前,原生应用和Web应用之间很难通信。如果你想在iOS设备上渲染HTML或者运行JavaScript,你不得不使用UIWebView。iOS7引入了JavaScriptCore,功能更强大,使用更简单。

JavaScriptCore介绍

JavaScriptCore是封装了JavaScript和Objective-C桥接的Objective-C API,只要用很少的代码,就可以做到JavaScript调用Objective-C,或者Objective-C调用JavaScript。

在之前的iOS版本,你只能通过向UIWebView发送stringByEvaluatingJavaScriptFromString:消息来执行一段JavaScript脚本。并且如果想用JavaScript调用Objective-C,必须打开一个自定义的URL(例如:foo://),然后在UIWebView的delegate方法webView:shouldStartLoadWithRequest:navigationType中进行处理。

然而现在可以利用JavaScriptCore的先进功能了,它可以:

运行JavaScript脚本而不需要依赖UIWebView

使用现代Objective-C的语法(例如Blocks和下标)

在Objective-C和JavaScript之间无缝的传递值或者对象

创建混合对象(原生对象可以将JavaScript值或函数作为一个属性)

使用Objective-C和JavaScript结合开发的好处:

快速的开发和制作原型

如果某块区域的业务需求变化的非常频繁,那么可以用JavaScript来开发和制作原型,这比Objective-C效率更高。

团队职责划分

这部分参考原文吧

Since JavaScript is much easier to learn and use than Objective-C (especially if you develop a nice JavaScript sandbox), it can be handy to have one team of developers responsible for the Objective-C “engine/framework”, and another team of developers write the JavaScript that uses the “engine/framework”. Even non-developers can write JavaScript, so it’s great if you want to get designers or other folks on the team involved in certain areas of the app.

JavaScript是解释型语言

JavaScript是解释运行的,你可以实时的修改JavaScript代码并立即看到结果。

逻辑写一次,多平台运行

可以把逻辑用JavaScript实现,iOS端和Android端都可以调用

JavaScriptCore概述

JSValue: 代表一个JavaScript实体,一个JSValue可以表示很多JavaScript原始类型例如boolean, integers, doubles,甚至包括对象和函数。

JSManagedValue: 本质上是一个JSValue,但是可以处理内存管理中的一些特殊情形,它能帮助引用技术和垃圾回收这两种内存管理机制之间进行正确的转换。

JSContext: 代表JavaScript的运行环境,你需要用JSContext来执行JavaScript代码。所有的JSValue都是捆绑在一个JSContext上的。

JSExport: 这是一个协议,可以用这个协议来将原生对象导出给JavaScript,这样原生对象的属性或方法就成为了JavaScript的属性或方法,非常神奇。

JSVirtualMachine: 代表一个对象空间,拥有自己的堆结构和垃圾回收机制。大部分情况下不需要和它直接交互,除非要处理一些特殊的多线程或者内存管理问题。

JSContext / JSValue

JSVirtualMachine为JavaScript的运行提供了底层资源,JSContext为JavaScript提供运行环境,通过

- (JSValue *)evaluateScript:(NSString*)script;

方法就可以执行一段JavaScript脚本,并且如果其中有方法、变量等信息都会被存储在其中以便在需要的时候使用。 而JSContext的创建都是基于JSVirtualMachine:

- (id)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine;

如果是使用- (id)init;进行初始化,那么在其内部会自动创建一个新的JSVirtualMachine对象然后调用前边的初始化方法。

创建一个 JSContext 后,可以很容易地运行 JavaScript 代码来创建变量,做计算,甚至定义方法:

JSContext *context = [[JSContext alloc] init];

[context evaluateScript:@"var num = 5 + 5"];

[context evaluateScript:@"var names = ['Grace', 'Ada', 'Margaret']"];

[context evaluateScript:@"var triple = function(value) { return value * 3 }"];

JSValue *tripleNum = [context evaluateScript:@"triple(num)"];

任何出自 JSContext 的值都被可以被包裹在一个 JSValue 对象中,JSValue 包装了每一个可能的 JavaScript 值:字符串和数字;数组、对象和方法;甚至错误和特殊的 JavaScript 值诸如 null 和 undefined。

可以对JSValue调用toString、toBool、toDouble、toArray等等方法把它转换成合适的Objective-C值或对象。

Objective-C调用JavaScript

例如有一个"Hello.js"文件内容如下:

functionprintHello(){

}

在Objective-C中调用printHello方法:

NSString*scriptPath = [[NSBundlemainBundle] pathForResource:@"hello"ofType:@"js"];

NSString*scriptString = [NSStringstringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncodingerror:nil];

JSContext *context = [[JSContext alloc] init];

[context evaluateScript:scriptString];

JSValue *function =self.context[@"printHello"];

[function callWithArguments:@[]];

分析以上代码:

首先初始化了一个JSContext,并执行JavaScript脚本,此时printHello函数并没有被调用,只是被读取到了这个context中。

然后从context中取出对printHello函数的引用,并保存到一个JSValue中。

注意这里,从JSContext中取出一个JavaScript实体(值、函数、对象),和将一个实体保存到JSContext中,语法均与NSDictionary的取值存值类似,非常简单。

最后如果JSValue是一个JavaScript函数,可以用callWithArguments来调用,参数是一个数组,如果没有参数则传入空数组@[]。

JavaScript调用Objective-C

还是上面的例子,将"hello.js"的内容改为:

functionprintHello(){

print("Hello, World!");

}

这里的print函数用Objective-C代码来实现

NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"js"];

NSString *scriptString = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil];

JSContext *context = [[JSContext alloc] init];

[context evaluateScript:scriptString];

self.context[@"print"] = ^(NSString *text) {

NSLog(@"%@", text");

};

JSValue *function = self.context[@"printHello"];

[function callWithArguments:@[]];

这里将一个Block以"print"为名传递给JavaScript上下文,JavaScript中调用print函数就可以执行这个Objective-C Block。

注意这里JavaScript中的字符串可以无缝的桥接为NSString,实参"Hello, World!"被传递给了NSString类型的text形参。

异常处理

当JavaScript运行时出现异常,会回调JSContext的exceptionHandler中设置的Block

context.exceptionHandler= ^(JSContext *context, JSValue *exception) {

NSLog(@"JS Error: %@", exception);

};

[context evaluateScript:@"function multiply(value1, value2) { return value1 * value2 "];

// 此时会打印Log "JS Error: SyntaxError: Unexpected end of script"

JSExport

JSExport是一个协议,可以让原生类的属性或方法称为JavaScript的属性或方法。

看下面的例子:

@protocolItemExport

@property(strong,nonatomic)NSString*name;

@property(strong,nonatomic)NSString*description;

@end

@interfaceItem:NSObject

@property(strong,nonatomic)NSString*name;

@property(strong,nonatomic)NSString*description;

@end

注意Item类不去直接符合JSExport,而是符合一个自己的协议,这个协议去继承JSExport协议。

例如有如下JavaScript代码

functionItem(name, description){

this.name = name;

this.description = description;

}

varitems = [];

functionaddItem(item){

items.push(item);

}

可以在Objective-C中把Item对象传递给addItem函数

Item *item = [[Item alloc] init];

item.name=@"itemName";

item.description=@"itemDescription";

JSValue *function = context[@"addItem"];

[function callWithArguments:@[item]];

或者把Item类导出到JavaScript环境,等待稍后使用

[self.contextsetObject:Item.selfforKeyedSubscript:@"Item"];

内存管理陷阱

Objective-C的内存管理机制是引用计数,JavaScript的内存管理机制是垃圾回收。在大部分情况下,JavaScriptCore能做到在这两种内存管理机制之间无缝无错转换,但也有少数情况需要特别注意。

在block内捕获JSContext

Block会为默认为所有被它捕获的对象创建一个强引用。JSContext为它管理的所有JSValue也都拥有一个强引用。并且,JSValue会为它保存的值和它所在的Context都维持一个强引用。这样JSContext和JSValue看上去是循环引用的,然而并不会,垃圾回收机制会打破这个循环引用。

看下面的例子:

self.context[@"getVersion"] = ^{

NSString*versionString = [[NSBundlemainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

versionString = [@"version "stringByAppendingString:versionString];

JSContext *context = [JSContext currentContext];// 这里不要用self.context

JSValue *version = [JSValue valueWithObject:versionString inContext:context];

returnversion;

};

使用[JSContext currentContext]而不是self.context来在block中使用JSContext,来防止循环引用。

JSManagedValue

当把一个JavaScript值保存到一个本地实例变量上时,需要尤其注意内存管理陷阱。 用实例变量保存一个JSValue非常容易引起循环引用。

看以下下例子,自定义一个UIAlertView,当点击按钮时调用一个JavaScript函数:

#import

#import

@interfaceMyAlertView:UIAlertView

- (id)initWithTitle:(NSString*)title

message:(NSString*)message

success:(JSValue *)successHandler

failure:(JSValue *)failureHandler

context:(JSContext *)context;

@end

按照一般自定义AlertView的实现方法,MyAlertView需要持有successHandler,failureHandler这两个JSValue对象

向JavaScript环境注入一个function

self.context[@"presentNativeAlert"] = ^(NSString*title,

NSString*message,

JSValue *success,

JSValue *failure) {

JSContext *context = [JSContext currentContext];

MyAlertView *alertView = [[MyAlertView alloc] initWithTitle:title

message:message

success:success

failure:failure

context:context];

[alertView show];

};

因为JavaScript环境中都是“强引用”(相对Objective-C的概念来说)的,这时JSContext强引用了一个presentNativeAlert函数,这个函数中又强引用了MyAlertView 等于说JSContext强引用了MyAlertView,而MyAlertView为了持有两个回调强引用了successHandler和failureHandler这两个JSValue,这样MyAlertView和JavaScript环境互相引用了。

所以苹果提供了一个JSMagagedValue类来解决这个问题。

看MyAlertView.m的正确实现:

#import"MyAlertView.h"

@interfaceXorkAlertView()

@property(strong,nonatomic) JSContext *ctxt;

@property(strong,nonatomic) JSMagagedValue *successHandler;

@property(strong,nonatomic) JSMagagedValue *failureHandler;

@end

@implementationMyAlertView

- (id)initWithTitle:(NSString*)title

message:(NSString*)message

success:(JSValue *)successHandler

failure:(JSValue *)failureHandler

context:(JSContext *)context {

self= [superinitWithTitle:title

message:message

delegate:self

cancelButtonTitle:@"No"

otherButtonTitles:@"Yes",nil];

if(self) {

_ctxt = context;

_successHandler = [JSManagedValue managedValueWithValue:successHandler];

// A JSManagedValue by itself is a weak reference. You convert it into a conditionally retained

// reference, by inserting it to the JSVirtualMachine using addManagedReference:withOwner:

[context.virtualMachineaddManagedReference:_successHandler withOwner:self];

_failureHandler = [JSManagedValue managedValueWithValue:failureHandler];

[context.virtualMachineaddManagedReference:_failureHandler withOwner:self];

}

returnself;

}

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if(buttonIndex ==self.cancelButtonIndex) {

JSValue *function = [self.failureHandlervalue];

[function callWithArguments:@[]];

}else{

JSValue *function = [self.successHandlervalue];

[function callWithArguments:@[]];

}

[self.ctxt.virtualMachineremoveManagedReference:_failureHandler withOwner:self];

[self.ctxt.virtualMachineremoveManagedReference:_successHandler withOwner:self];

}

@end

分析上面例子,从外部传入的JSValue对象在类内部使用JSManagedValue来保存。

JSManagedValue本身是一个弱引用对象,需要调用JSVirtualMachine的addManagedReference:withOwner:把它添加到JSVirtualMachine对象中,确保使用过程中JSValue不会被释放

当用户点击AlertView上的按钮时,根据用户点击哪一个按钮,来执行对应的处理函数,这时AlertView也随即被销毁。 这时需要手动调用removeManagedReference:withOwner:来移除JSManagedValue。

参考资料

《iOS 7 by tutorials》

https://www.bignerdranch.com/blog/javascriptcore-and-ios-7/

http://nshipster.com/javascriptcore/

https://github.com/Jobot/JSCoreByExample

https://github.com/TheHolyGrail/Zoot

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容