chore(core): Small realm-related fixes (#17044)

- `JsRuntime::built_from_snapshot` was removed because it was redundant
with `JsRuntime::snapshot_options`.
- Updates to stale documentation of `JsRuntime::create_realm`.
- `JsRuntime::create_realm` now calls `JsRuntime::init_extension_js`
unconditionally, since if the runtime was built from a snapshot,
`init_extension_js` will be a no-op.
- Typo fix in the documentation for `JsRealm`.
This commit is contained in:
Andreu Botella 2022-12-17 00:54:00 +01:00 committed by GitHub
parent c39550fe52
commit bb8a49993b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -84,7 +84,6 @@ pub struct JsRuntime {
// a safety issue with SnapshotCreator. See JsRuntime::drop. // a safety issue with SnapshotCreator. See JsRuntime::drop.
v8_isolate: Option<v8::OwnedIsolate>, v8_isolate: Option<v8::OwnedIsolate>,
snapshot_options: SnapshotOptions, snapshot_options: SnapshotOptions,
built_from_snapshot: bool,
allocations: IsolateAllocations, allocations: IsolateAllocations,
extensions: Vec<Extension>, extensions: Vec<Extension>,
extensions_with_js: Vec<Extension>, extensions_with_js: Vec<Extension>,
@ -350,10 +349,8 @@ impl JsRuntime {
static DENO_INIT: Once = Once::new(); static DENO_INIT: Once = Once::new();
DENO_INIT.call_once(move || v8_init(v8_platform, options.will_snapshot)); DENO_INIT.call_once(move || v8_init(v8_platform, options.will_snapshot));
let has_startup_snapshot = options.startup_snapshot.is_some();
// Add builtins extension // Add builtins extension
if !has_startup_snapshot { if options.startup_snapshot.is_none() {
options options
.extensions_with_js .extensions_with_js
.insert(0, crate::ops_builtin::init_builtins()); .insert(0, crate::ops_builtin::init_builtins());
@ -557,7 +554,6 @@ impl JsRuntime {
let mut js_runtime = Self { let mut js_runtime = Self {
v8_isolate: Some(isolate), v8_isolate: Some(isolate),
built_from_snapshot: has_startup_snapshot,
snapshot_options, snapshot_options,
allocations: IsolateAllocations::default(), allocations: IsolateAllocations::default(),
event_loop_middlewares: Vec::with_capacity(options.extensions.len()), event_loop_middlewares: Vec::with_capacity(options.extensions.len()),
@ -623,37 +619,27 @@ impl JsRuntime {
/// Creates a new realm (V8 context) in this JS execution context, /// Creates a new realm (V8 context) in this JS execution context,
/// pre-initialized with all of the extensions that were passed in /// pre-initialized with all of the extensions that were passed in
/// [`RuntimeOptions::extensions`] when the [`JsRuntime`] was constructed. /// [`RuntimeOptions::extensions_with_js`] when the [`JsRuntime`] was
/// /// constructed.
/// If the [`JsRuntime`] was not built from a snapshot (see
/// [`RuntimeOptions::startup_snapshot`]), the JS code for the extensions will
/// be run in the call to this method. In contrast, if there is a snapshot,
/// that will be used instead, and the extensions' initialization will come
/// "for free".
pub fn create_realm(&mut self) -> Result<JsRealm, Error> { pub fn create_realm(&mut self) -> Result<JsRealm, Error> {
let realm = { let realm = {
// SAFETY: Having the scope tied to self's lifetime makes it impossible to // SAFETY: Having the scope tied to self's lifetime makes it impossible to
// reference self.ops while the scope is alive. Here we turn it into an // reference JsRuntimeState::op_ctxs while the scope is alive. Here we
// unbound lifetime, which is sound because 1. it only lives until the end // turn it into an unbound lifetime, which is sound because 1. it only
// of this block, and 2. the HandleScope only has access to the isolate, // lives until the end of this block, and 2. the HandleScope only has
// and nothing else we're accessing from self does. // access to the isolate, and nothing else we're accessing from self does.
let scope = &mut v8::HandleScope::new(unsafe { let scope = &mut v8::HandleScope::new(unsafe {
&mut *(self.v8_isolate() as *mut v8::OwnedIsolate) &mut *(self.v8_isolate() as *mut v8::OwnedIsolate)
}); });
let context = bindings::initialize_context( let context = bindings::initialize_context(
scope, scope,
&self.state.borrow().op_ctxs, &self.state.borrow().op_ctxs,
SnapshotOptions::from_bools( self.snapshot_options,
self.built_from_snapshot,
self.snapshot_options.will_snapshot(),
),
); );
JsRealm::new(v8::Global::new(scope, context)) JsRealm::new(v8::Global::new(scope, context))
}; };
if !self.built_from_snapshot { self.init_extension_js(&realm)?;
self.init_extension_js(&realm)?;
}
Ok(realm) Ok(realm)
} }
@ -2245,7 +2231,7 @@ impl JsRuntime {
/// ///
/// # Panics /// # Panics
/// ///
/// Every method of [`JsRealm`] will panic if you call if with a reference to a /// Every method of [`JsRealm`] will panic if you call it with a reference to a
/// [`v8::Isolate`] other than the one that corresponds to the current context. /// [`v8::Isolate`] other than the one that corresponds to the current context.
/// ///
/// # Lifetime of the realm /// # Lifetime of the realm