JSContext
继承自:NSObject
遵守协议:NSObject
导入声明:@import JavaScriptCore;
适用范围:iOS 7.0 及以后
一个 JSContext 对象代表一个 JavaScript 执行环境( execution environment)。你可以通过 Objective-C 或者 Swift 代码来创建和使用 JavaScript 上下文(contexts),这样你就可以去调用 JavaScript 的 scripts,获取 JavaScript 中定义的或计算的值,以及让原生的对象、方法、函数跟 JavaScript 交互。
一、创建 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
参数 | 含义 |
---|---|
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.
二、运行 JavaScript 脚本(Evaluating Scripts)
- evaluateScript:
Executes the specified JavaScript code.
Declaration
- (JSValue *)evaluateScript:(NSString *)script
Parameters
参数 | 含义 |
---|---|
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
参数 | 含义 |
---|---|
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.
三、监听 JavaScript 运行时的回调状态(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.
四、设置 JavaScript 全局状态(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:
参数 | 含义 |
---|---|
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.
五、通过下标来获取 JavaScript 全局状态(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
参数 | 含义 |
---|---|
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
参数 | 含义 |
---|---|
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 相关 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
参数 | 含义 |
---|---|
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/
问题:
- JSContext 对象的作用是什么?
- “JavaScript context” 是什么?
3.virtual machine 在 JavaScript 中指的是什么?virtual machine 与 context 的关系?
4.context’s global object 在 JavaScript 中指的是什么?
5.window 在 JavaScript 中指的是什么? - JavaScript Global State 是什么?
7.如何将 Objective-C 中的 block 转成 JavaScript 中的函数?