In the JSAPI, JSRuntime
is the top-level object that represents an instance of the JavaScript engine. A program typically has only one JSRuntime
, even if it has many threads. The JSRuntime
is the universe in which JavaScript objects live; they can't travel to other JSRuntime
s.
All JavaScript code and most JSAPI calls run within a JSContext
. The JSContext
is a child of the JSRuntime
. A context can run scripts. It contains the global object and the execution stack. Exception handling, error reporting, and some language options are per-JSContext
. Once created, a context can be used any number of times for different scripts or JSAPI queries. For example, a browser might create a separate context for each HTML page; every script in the page could use the same context.
Objects may be shared among JSContext
s within a JSRuntime
. There's no fixed association between an object and the context in which it is created. Sample code to set up and tear down a JSRuntime
and a JSContext
is at JSAPI User Guide.
ThreadsSection
Only one thread may use a JSContext
or JSRuntime
. Earlier versions allowed using JS_ClearContextThread
and other functions to move a JSContext
from one thread to another. This feature has since been removed.
在jsapi 中,JSRuntime 代表javascript执行引擎的顶级实例, 即便一个程序可以拥有多个线程,但比较典型的是只有一个JSRuntime 。JSRuntime 是 javascript对象的运行环境,这些对象不能在不同JSRuntime环境中使用。
所有的javascript代码和大多数的jsapi的调用运行在 jsContext(js上下文环境), jsContext 是 jsRuntime的子级(也许可理解为小型版本), 一个可以运行脚本的上下文环境,包含全局对象和执行堆栈,异常处理、错误报告和一些语言选项是在每一个JSContext中。一旦创建上下文环境,该环境可多次用于不同的脚本或JSAPI调用。例如,浏览器可以为每个HTML页面创建单独的上下文环境;页面中的每个脚本都可以使用相同的上下文。
对象可以在JSRuntime内的不同jscontext之间共享。对象和创建它的上下文之间没有固定的关联。JSAPI用户指南提供了设置和删除JSRuntime和JSContext的示例代码。
线程部分
只有一个线程可以使用JSContext或JSRuntime。早期版本允许使用JS_ClearContextThread和其他函数将JSContext从一个线程移动到另一个线程。此功能已被删除。
本人水平有限,若理解有误,请指正