JSContext (Objective-C)官方文檔翻譯

JSContext

繼承自:NSObject
遵守協(xié)議:NSObject
導(dǎo)入聲明:@import JavaScriptCore;
適用范圍:iOS 7.0 及以后

一個(gè) JSContext 對(duì)象代表一個(gè) JavaScript 執(zhí)行環(huán)境( execution environment)荡陷。你可以通過 Objective-C 或者 Swift 代碼來創(chuàng)建和使用 JavaScript 上下文(contexts)种吸,這樣你就可以去調(diào)用 JavaScript 的 scripts扭倾,獲取 JavaScript 中定義的或計(jì)算的值橡淆,以及讓原生的對(duì)象、方法普碎、函數(shù)跟 JavaScript 交互纸兔。

一姿锭、創(chuàng)建 JavaScript 上下文(Creating JavaScript Contexts)

- init

Initializes a new JavaScript context.

Declaration

- (instancetype)init

Return Value
A new JavaScript context.

Discussion
This initializer creates a context along with a new, independent virtual machine (a JSVirtualMachine object). You cannot pass JavaScript values (JSValue objects) between contexts in different virtual machines. To create contexts that share a virtual machine, use the initWithVirtualMachine: initializer.

Availability
Available in iOS 7.0 and later.


- initWithVirtualMachine:

Creates a new JavaScript context associated with a specific virtual machine.

Declaration

- (instancetype)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine

Parameters

參數(shù) 含義
virtualMachine The virtual machine with which to associate the new context.

Return Value
A new JavaScript context.

Discussion
By default, each context has an independent virtual machine (a JSVirtualMachine object). You cannot pass JavaScript values between contexts in different virtual machines. Use this initializer to create a context that shares its virtual machine with other JavaScript contexts to allow passing JSValue objects between those contexts.

Availability
Available in iOS 7.0 and later.

二、運(yùn)行 JavaScript 腳本(Evaluating Scripts)

- evaluateScript:

Executes the specified JavaScript code.

Declaration

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

Parameters

參數(shù) 含義
script The JavaScript source code to evaluate.

Return Value
The last value generated by the script. Note that a script can result in the JavaScript value undefined.

Discussion
Evaluating a script runs any top-level code and adds function and object definitions to the context’s global object.

Availability
Available in iOS 7.0 and later.


- evaluateScript:withSourceURL:

Executes the specified JavaScript code, treating the specified URL as its source location.

Declaration

- (JSValue *)evaluateScript:(NSString *)script
              withSourceURL:(NSURL *)sourceURL

Parameters

參數(shù) 含義
script The JavaScript source code to evaluate.
sourceURL A URL to be considered as the script’s origin.

Return Value
The last value generated by the script. Note that a script can result in the JavaScript value undefined.

Discussion
Evaluating a script runs any top-level code and adds function or object definitions to the context’s global object.
The sourceURL parameter is informative only; debuggers may use this URL when reporting exceptions.

Availability
Available in iOS 8.0 and later.

三壹粟、監(jiān)聽 JavaScript 運(yùn)行時(shí)的回調(diào)狀態(tài)(Evaluating Scripts)

+ currentContext

Returns the context currently executing JavaScript code.

Declaration

+ (JSContext *)currentContext

Return Value
The currently executing context, or nil if not within native code called from JavaScript.

Discussion
Call this method within an Objective-C or Swift block or method invoked from within JavaScript to obtain the JSContext object responsible for executing that Javascript code.
If not currently in code invoked as a callback from JavaScript, this method returns nil.

Availability
Available in iOS 7.0 and later.


+ currentCallee

Returns the currently executing JavaScript function.

Declaration

+ (JSValue *)currentCallee

Return Value
The currently executing JavaScript function, or nil if not within native code called from JavaScript.

Discussion
Call this method within an Objective-C or Swift block or method invoked from within JavaScript to obtain a JSValue object representing the JavaScript function responsible for executing that code.
If not currently in code invoked as a callback from JavaScript, this method returns nil.

Availability
Available in iOS 8.0 and later.


+ currentThis

Returns the value of the this keyword in currently executing JavaScript code.

Declaration

+ (JSValue *)currentThis

Return Value
The current value of the JavaScript this keyword, or nil if not within native code called from JavaScript.

Discussion
Call this method within an Objective-C or Swift block or method invoked from within JavaScript to obtain a JSValue object representing the current value of the this keyword in that JavaScript code.
If not currently in code invoked as a callback from JavaScript, this method returns nil.

Availability
Available in iOS 7.0 and later.


+ currentArguments

Returns the arguments to the current native callback from JavaScript code.

Declaration

+ (NSArray *)currentArguments

Return Value
The current callback arguments, or nil if not within native code called from JavaScript.

Discussion
Call this method within an Objective-C or Swift block or method invoked from within JavaScript to obtain an array of JSValue objects representing the arguments to the JavaScript function responsible for that callback.
If not currently in code invoked as a callback from JavaScript, this method returns nil.

Availability
Available in iOS 7.0 and later.

四拜隧、設(shè)置 JavaScript 全局狀態(tài)(Working with JavaScript Global State)

globalObject Property

The JavaScript global object associated with the context. (read-only)

Declaration

@property(readonly, strong) JSValue *globalObject

Discussion
In a web browser, the global object of a JavaScript context is the browser window (the window object in JavaScript). Outside of web-browser use, a context’s global object serves a similar role, separating the JavaScript namespaces of different contexts. Global variables within a script appear as fields or subscripts in the global object—you can access them either through this JSValue object or through the methods listed in Accessing JavaScript Global State with Subscripts.

NOTE
For JSContext instances originating in WebKit, this method returns a reference to the WindowProxy object.

Availability
Available in iOS 7.0 and later.


exception Property

A JavaScript exception to be thrown in evaluation of the script.

Declaration

@property(strong) JSValue *exception

Discussion
Before performing a callback from JavaScript to an Objective-C or Swift block or method, the context preserves the prior value of this property and then sets its value to nil. After the callback has completed, the context reads the new value of the exception property—if this value is not nil, the context treats the value as an exception to be thrown in JavaScript as a result of the callback. After reading the property (and possibly throwing a JavaScript exception), the context restores the prior value of this property.
By default, JavaScriptCore assigns any uncaught exception to this property, so you can check this property’s value to find uncaught exceptions arising from JavaScript function calls. To change the exception handling behavior, use the exceptionHandler
property.

Availability
Available in iOS 7.0 and later.


exceptionHandler Property

A block to be invoked should evaluating a script result in a JavaScript exception being thrown.

Declaration

@property(copy) void (^exceptionHandler)( JSContext *context, JSValue *exception)

Discussion
The block takes the following parameters:

參數(shù) 含義
context The context in which the exception originates.
exception The JavaScript exception thrown.

The default value exception handler block stores its exception parameter value into the context’s exception property. As a consequence, the default behavior is that unhandled exceptions occurring within a callback from JavaScript to native code are thrown again upon return. Setting this value to nil results in all uncaught exceptions being silently consumed.

Availability
Available in iOS 7.0 and later.


virtualMachine Property

The JavaScript virtual machine to which the context belongs. (read-only)

Declaration

@property(readonly, strong) JSVirtualMachine *virtualMachine

Discussion
To create a context associated with a specific virtual machine, allowing JavaScript values to be passed between contexts that share the same virtual machine, use the initWithVirtualMachine: initializer.

Availability
Available in iOS 7.0 and later.


name Property

A descriptive name for the context.

Declaration

@property(copy) NSString *name

Discussion
This name appears when using remote debugging to examine the context.

Availability
Available in iOS 8.0 and later.

五、通過下標(biāo)來獲取 JavaScript 全局狀態(tài)(Accessing JavaScript Global State with Subscripts)

- objectForKeyedSubscript:

Returns the value of the specified JavaScript property in the context’s global object, allowing subscript getter syntax.

Declaration

- (JSValue *)objectForKeyedSubscript:(id)*key*

Parameters

參數(shù) 含義
key The name of a JavaScript property in the context’s global JavaScript object.

Return Value
The JavaScript property named by key, or nil if no such field or function exists.

Discussion
This method first constructs a JSValue object from the key parameter, then uses that value in JavaScript to look up the name of a property in the context’s global object.

Availability
Available in iOS 7.0 and later.


- setObject:forKeyedSubscript:

Sets the specified JavaScript property of the context’s global object, allowing subscript setter syntax.

Declaration

- (void)setObject:(id)object
forKeyedSubscript:(NSObject <NSCopying> *)key

Parameters

參數(shù) 含義
object The value to set for the JavaScript property.
key The JavaScript property name to use in the context’s global JavaScript object.

Discussion
This method first constructs a JSValue object from the key
parameter, then uses that value in JavaScript to set the property in the context’s global object.
Use this method (or Objective-C subscript syntax) to bridge native objects or functions for use in JavaScript. For example, the following code creates a JavaScript function whose implementation is an Objective-C block:

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

context[@"makeNSColor"] = ^(NSDictionary *rgb){

float r = rgb[@"red"].floatValue;

float g = rgb[@"green"].floatValue;

float b = rgb[@"blue"].floatValue;

return [NSColor colorWithRed:(r / 255.f) green:(g / 255.f) blue:(b / 255.f) alpha:1.0];

};

Availability
Available in iOS 7.0 and later.

六趁仙、C JavaScriptCore 相關(guān) API(Working with the C JavaScriptCore API)

JSGlobalContextRefProperty

Returns the C representation of the JavaScript context. (read-only)

Declaration

@property(readonly) JSGlobalContextRef JSGlobalContextRef

Discussion
See JSContextRef.h Reference for the C JavaScriptCore API.

Availability
Available in iOS 7.0 and later.


+ contextWithJSGlobalContextRef:

Creates a JavaScript context object from the equivalent C representation.

Declaration

+ (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)jsGlobalContextRef

Parameters

參數(shù) 含義
jsGlobalContextRef A C JavaScript context reference.

Return Value
A JavaScript context object representing the same context.

Discussion
See JSContextRef.h Reference for the C JavaScriptCore API.

Availability
Available in iOS 7.0 and later.

參考(Reference)
https://developer.apple.com/library/ios/documentation/JavaScriptCore/Reference/JSContext_Ref/


問題:

  1. JSContext 對(duì)象的作用是什么洪添?
  2. “JavaScript context” 是什么?
    3.virtual machine 在 JavaScript 中指的是什么雀费?virtual machine 與 context 的關(guān)系薇组?
    4.context’s global object 在 JavaScript 中指的是什么?
    5.window 在 JavaScript 中指的是什么坐儿?
  3. JavaScript Global State 是什么律胀?
    7.如何將 Objective-C 中的 block 轉(zhuǎn)成 JavaScript 中的函數(shù)?
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末貌矿,一起剝皮案震驚了整個(gè)濱河市炭菌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌逛漫,老刑警劉巖黑低,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異酌毡,居然都是意外死亡克握,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門枷踏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來菩暗,“玉大人,你說我怎么就攤上這事旭蠕⊥M牛” “怎么了?”我有些...
    開封第一講書人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵掏熬,是天一觀的道長(zhǎng)佑稠。 經(jīng)常有香客問我,道長(zhǎng)旗芬,這世上最難降的妖魔是什么舌胶? 我笑而不...
    開封第一講書人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮疮丛,結(jié)果婚禮上幔嫂,老公的妹妹穿的比我還像新娘漱办。我一直安慰自己,他們只是感情好婉烟,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開白布娩井。 她就那樣靜靜地躺著,像睡著了一般似袁。 火紅的嫁衣襯著肌膚如雪洞辣。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,190評(píng)論 1 299
  • 那天昙衅,我揣著相機(jī)與錄音扬霜,去河邊找鬼。 笑死而涉,一個(gè)胖子當(dāng)著我的面吹牛著瓶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播啼县,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼材原,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了季眷?” 一聲冷哼從身側(cè)響起余蟹,我...
    開封第一講書人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎子刮,沒想到半個(gè)月后威酒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡挺峡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年葵孤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片橱赠。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡尤仍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出病线,到底是詐尸還是另有隱情吓著,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布送挑,位于F島的核電站,受9級(jí)特大地震影響暖眼,放射性物質(zhì)發(fā)生泄漏惕耕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一诫肠、第九天 我趴在偏房一處隱蔽的房頂上張望司澎。 院中可真熱鬧欺缘,春花似錦、人聲如沸挤安。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蛤铜。三九已至嫩絮,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間围肥,已是汗流浹背剿干。 一陣腳步聲響...
    開封第一講書人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留穆刻,地道東北人置尔。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像氢伟,于是被迫代替她去往敵國(guó)和親榜轿。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,863評(píng)論 0 6
  • 今天看《愛自己朵锣,和誰(shuí)結(jié)婚都幸覆畹迹》這本書,對(duì)這段話很有感觸:通過你們的弱點(diǎn)猪勇,去發(fā)現(xiàn)你們的優(yōu)勢(shì)设褐;通過你們的痛苦,去發(fā)現(xiàn)...
    885d352dfbfc閱讀 169評(píng)論 0 0
  • 文/老顯 五月底,六月初椅您,陽(yáng)光火辣外冀,光陰飛梭無情,又是一年高考時(shí)掀泳。 高考前雪隧,莘莘學(xué)子在為了高考成功而低調(diào)地學(xué)習(xí)著,...
    老顯閱讀 1,265評(píng)論 15 10
  • —學(xué)員故事分享— 我接觸股票是因?yàn)槲覌屧倍妫疑现袑W(xué)那陣子脑沿,住在姥姥家,我媽在家沒什么事情马僻,就開始跟著朋友學(xué)炒股庄拇,每天...
    壹園閱讀 190評(píng)論 0 0
  • 酒后勿忘吸支煙,云霧繚繞玉皇前。 倘若吸煙你忘卻措近,怎能駕云天庭玩溶弟。
    老槐樹閱讀 165評(píng)論 0 2