diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-09 20:22:27 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 19:22:27 -0500 |
commit | d1685b120bf7da5ba384806153f65d90ef156b77 (patch) | |
tree | 155c87f1beae810f52fb2a9ef9ab7526fa1af990 /core/runtime.rs | |
parent | 78d430103a8f6931154ddbbe19d36f3b8630286d (diff) |
refactor(core): remove RuntimeOptions::extensions_with_js (#18099)
This commit removes "deno_core::RuntimeOptions::extensions_with_js".
Now it's embedders' responsibility to properly register extensions
that will not contains JavaScript sources when running from an existing
snapshot.
Prerequisite for https://github.com/denoland/deno/pull/18080
Diffstat (limited to 'core/runtime.rs')
-rw-r--r-- | core/runtime.rs | 107 |
1 files changed, 37 insertions, 70 deletions
diff --git a/core/runtime.rs b/core/runtime.rs index b9da06346..75bdb4519 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -89,7 +89,6 @@ pub struct JsRuntime { snapshot_options: SnapshotOptions, allocations: IsolateAllocations, extensions: Vec<Extension>, - extensions_with_js: Vec<Extension>, event_loop_middlewares: Vec<Box<OpEventLoopFn>>, // Marks if this is considered the top-level runtime. Used only be inspector. is_main: bool, @@ -253,20 +252,12 @@ pub struct RuntimeOptions { /// JsRuntime extensions, not to be confused with ES modules. /// Only ops registered by extensions will be initialized. If you need - /// to execute JS code from extensions, use `extensions_with_js` options - /// instead. - pub extensions: Vec<Extension>, - - /// JsRuntime extensions, not to be confused with ES modules. - /// Ops registered by extensions will be initialized and JS code will be - /// executed. If you don't need to execute JS code from extensions, use - /// `extensions` option instead. + /// to execute JS code from extensions, pass source files in `js` or `esm` + /// option on `ExtensionBuilder`. /// - /// This is useful when creating snapshots, in such case you would pass - /// extensions using `extensions_with_js`, later when creating a runtime - /// from the snapshot, you would pass these extensions using `extensions` - /// option. - pub extensions_with_js: Vec<Extension>, + /// If you are creating a runtime from a snapshot take care not to include + /// JavaScript sources in the extensions. + pub extensions: Vec<Extension>, /// V8 snapshot that should be loaded on startup. pub startup_snapshot: Option<Snapshot>, @@ -373,18 +364,15 @@ impl JsRuntime { let has_startup_snapshot = options.startup_snapshot.is_some(); if !has_startup_snapshot { options - .extensions_with_js - .insert(0, crate::ops_builtin::init_builtins()); + .extensions + .insert(0, crate::ops_builtin::init_builtin_ops_and_esm()); } else { options .extensions - .insert(0, crate::ops_builtin::init_builtins()); + .insert(0, crate::ops_builtin::init_builtin_ops()); } - let ops = Self::collect_ops( - &mut options.extensions, - &mut options.extensions_with_js, - ); + let ops = Self::collect_ops(&mut options.extensions); let mut op_state = OpState::new(ops.len()); if let Some(get_error_class_fn) = options.get_error_class_fn { @@ -624,9 +612,12 @@ impl JsRuntime { let loader = if snapshot_options != SnapshotOptions::Load { let esm_sources = options - .extensions_with_js + .extensions .iter() - .flat_map(|ext| ext.get_esm_sources().to_owned()) + .flat_map(|ext| match ext.get_esm_sources() { + Some(s) => s.to_owned(), + None => vec![], + }) .collect::<Vec<ExtensionFileSource>>(); #[cfg(feature = "include_js_files_for_snapshotting")] @@ -689,7 +680,6 @@ impl JsRuntime { allocations: IsolateAllocations::default(), event_loop_middlewares: Vec::with_capacity(options.extensions.len()), extensions: options.extensions, - extensions_with_js: options.extensions_with_js, state: state_rc, module_map: Some(module_map_rc), is_main: options.is_main, @@ -752,7 +742,7 @@ impl JsRuntime { /// Creates a new realm (V8 context) in this JS execution context, /// pre-initialized with all of the extensions that were passed in - /// [`RuntimeOptions::extensions_with_js`] when the [`JsRuntime`] was + /// [`RuntimeOptions::extensions`] when the [`JsRuntime`] was /// constructed. pub fn create_realm(&mut self) -> Result<JsRealm, Error> { let realm = { @@ -868,50 +858,45 @@ impl JsRuntime { } // Take extensions to avoid double-borrow - let extensions = std::mem::take(&mut self.extensions_with_js); + let extensions = std::mem::take(&mut self.extensions); for ext in &extensions { { - let esm_files = ext.get_esm_sources(); - if let Some(entry_point) = ext.get_esm_entry_point() { - let file_source = esm_files - .iter() - .find(|file| file.specifier == entry_point) - .unwrap(); - load_and_evaluate_module(self, file_source)?; - } else { - for file_source in esm_files { + if let Some(esm_files) = ext.get_esm_sources() { + if let Some(entry_point) = ext.get_esm_entry_point() { + let file_source = esm_files + .iter() + .find(|file| file.specifier == entry_point) + .unwrap(); load_and_evaluate_module(self, file_source)?; + } else { + for file_source in esm_files { + load_and_evaluate_module(self, file_source)?; + } } } } { - let js_files = ext.get_js_sources(); - for file_source in js_files { - // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap - realm.execute_script( - self.v8_isolate(), - &file_source.specifier, - &file_source.code.load()?, - )?; + if let Some(js_files) = ext.get_js_sources() { + for file_source in js_files { + // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap + realm.execute_script( + self.v8_isolate(), + &file_source.specifier, + &file_source.code.load()?, + )?; + } } } } // Restore extensions - self.extensions_with_js = extensions; + self.extensions = extensions; Ok(()) } /// Collects ops from extensions & applies middleware - fn collect_ops( - extensions: &mut [Extension], - extensions_with_js: &mut [Extension], - ) -> Vec<OpDecl> { - let mut exts = vec![]; - exts.extend(extensions); - exts.extend(extensions_with_js); - + fn collect_ops(exts: &mut [Extension]) -> Vec<OpDecl> { for (ext, previous_exts) in exts.iter().enumerate().map(|(i, ext)| (ext, &exts[..i])) { @@ -970,24 +955,6 @@ impl JsRuntime { // Restore extensions self.extensions = extensions; } - { - let mut extensions: Vec<Extension> = - std::mem::take(&mut self.extensions_with_js); - - // Setup state - for e in extensions.iter_mut() { - // ops are already registered during in bindings::initialize_context(); - e.init_state(&mut op_state.borrow_mut()); - - // Setup event-loop middleware - if let Some(middleware) = e.init_event_loop_middleware() { - self.event_loop_middlewares.push(middleware); - } - } - - // Restore extensions - self.extensions_with_js = extensions; - } Ok(()) } |