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從一個線程移動到另一個線程。此功能已被刪除。
本人水平有限,若理解有誤,請指正