Skip to main content

Module context

Module context 

Source
Expand description

SpiderMonkey JSContext — always parasitic on servo’s Runtime.

铁律: Bao 始终寄生 servo 的 JSContext,不存在独立 JSContext。 所有模式(CLI/browser/CDP)共享 servo 的唯一 JSContext。

初始化路径:

  • CLI 模式: JsContext::init_runtime() → JSEngine + Runtime + JobQueue 返回 SmRuntimeGuard 持有所有权,BaoRuntime 持有 guard。
  • Browser 模式: servo 初始化 Runtime → JsContext::from_servo_runtime() 寄生 servo 拥有 Runtime 生命周期,不需要 guard。
  • 两者共享同一个 mozjs::rust::Runtime::get() TLS 全局

TLS 生命周期策略:

  • JSEngine 是进程级单例(JSEngine::init 只能成功一次,JS_ShutDown 后不可重启)
  • Engine 存储在 TLS 中,线程退出时 mem::forget(永不调 JS_ShutDown)
  • Runtime(JSContext)可安全创建/销毁多次
  • Runtime 在 TLS 中存储,线程退出时 mem::forget 避免在 __call_tls_dtors 中 执行 JS_DestroyContext(mozjs 的 GCRuntime::finishRoots 在 C++ TLS teardown 期间会 SIGSEGV)

Structs§

JsContext
Parasitic JSContext — borrows servo’s JSContext pointer. Does NOT own a mozjs::rust::Runtime; servo owns that lifetime.
RawValueRootGuard
RAII guard for one or more heap JSVal slots registered with the GC extra-roots table (AddRawValueRoot) — the general form of PersistentGlobal’s rooting, for values that must survive across an async window (pending fetch promises, fs/crypto callbacks, …).
SmRuntimeGuard
Owns the SM Runtime for CLI/test mode. Browser mode never constructs this — servo owns the lifetime there.

Functions§

ensure_engine_handle
Get or initialize the per-process JSEngine, returning a handle.
thread_realm_global
The current thread’s persistent realm global, if a JsContext on this thread has created its realm. Used by dispatch sites to AutoRealm.

Type Aliases§

GlobalSetupFn
Function pointer type for global setup during module evaluation.
PostEvalHook
Function pointer type for post-evaluation hooks.