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

JSContext

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

一個(gè) JSContext 對(duì)象代表一個(gè) JavaScript 執(zhí)行環(huán)境( execution environment)。你可以通過(guò) Objective-C 或者 Swift 代碼來(lái)創(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.

五、通過(guò)下標(biāo)來(lái)獲取 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/


問(wèn)題:

  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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,067評(píng)論 0 6
  • 今天看《愛(ài)自己,和誰(shuí)結(jié)婚都幸?!愤@本書,對(duì)這段話很有感觸:通過(guò)你們的弱點(diǎn),去發(fā)現(xiàn)你們的優(yōu)勢(shì);通過(guò)你們的痛苦,去發(fā)現(xiàn)...
    885d352dfbfc閱讀 217評(píng)論 0 0
  • 文/老顯 五月底,六月初,陽(yáng)光火辣,光陰飛梭無(wú)情,又是一年高考時(shí)。 高考前,莘莘學(xué)子在為了高考成功而低調(diào)地學(xué)習(xí)著,...
    老顯閱讀 1,385評(píng)論 15 10
  • —學(xué)員故事分享— 我接觸股票是因?yàn)槲覌?,我上中學(xué)那陣子,住在姥姥家,我媽在家沒(méi)什么事情,就開始跟著朋友學(xué)炒股,每天...
    壹園閱讀 305評(píng)論 0 0
  • 酒后勿忘吸支煙,云霧繚繞玉皇前。 倘若吸煙你忘卻,怎能駕云天庭玩。
    老槐樹閱讀 200評(píng)論 0 2

友情鏈接更多精彩內(nèi)容