diff options
author | Bartek Iwańczuk <biwanczuk@gmail.com> | 2023-06-26 13:54:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-26 13:54:10 +0200 |
commit | ad3c494b46c97f0cf91098b7ec2afa576ea7a3dd (patch) | |
tree | 2996e36708b4c726c3b5e99b6fbbd8e63fcc121b /core/extensions.rs | |
parent | c7d38e593f7de66af47321e7463d8c15ae4176f2 (diff) |
Revert "Reland "refactor(core): cleanup feature flags for js source i… (#19611)
…nclusion" (#19519)"
This reverts commit 28a4f3d0f5383695b1d49ccdc8b0f799a715b2c2.
This change causes failures when used outside Deno repo:
```
============================================================
Deno has panicked. This is a bug in Deno. Please report this
at https://github.com/denoland/deno/issues/new.
If you can reliably reproduce this panic, include the
reproduction steps and re-run with the RUST_BACKTRACE=1 env
var set and include the backtrace in your report.
Platform: linux x86_64
Version: 1.34.3+b37b286
Args: ["/opt/hostedtoolcache/deno/0.0.0-b37b286f7fa68d5656f7c180f6127bdc38cf2cf5/x64/deno", "test", "--doc", "--unstable", "--allow-all", "--coverage=./cov"]
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Failed to read "/home/runner/work/deno/deno/core/00_primordials.js"
Caused by:
No such file or directory (os error 2)', core/runtime/jsruntime.rs:699:8
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```
Diffstat (limited to 'core/extensions.rs')
-rw-r--r-- | core/extensions.rs | 148 |
1 files changed, 83 insertions, 65 deletions
diff --git a/core/extensions.rs b/core/extensions.rs index 62390d054..fa6d7851e 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -17,10 +17,10 @@ pub enum ExtensionFileSourceCode { /// snapshot, the other the static string in the `Extension`. IncludedInBinary(&'static str), - /// Source code is loaded from a file on disk at 'runtime'. It's meant to be - /// used in build and dev scripts using the `runtime_js_sources` feature, - /// likely to create snapshots. - LoadAtRuntime(PathBuf), + // Source code is loaded from a file on disk. It's meant to be used if the + // embedder is creating snapshots. Files will be loaded from the filesystem + // during the build time and they will only be present in the V8 snapshot. + LoadedFromFsDuringSnapshot(PathBuf), } #[derive(Clone, Debug)] @@ -45,7 +45,7 @@ impl ExtensionFileSource { ); Ok(ModuleCode::from_static(code)) } - ExtensionFileSourceCode::LoadAtRuntime(path) => { + ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => { let msg = || format!("Failed to read \"{}\"", path.display()); let s = std::fs::read_to_string(path).with_context(msg)?; debug_assert!( @@ -168,6 +168,7 @@ macro_rules! ops { /// * bounds: a comma-separated list of additional type bounds, eg: `bounds = [ P::MyAssociatedType: MyTrait ]` /// * ops: a comma-separated list of [`OpDecl`]s to provide, eg: `ops = [ op_foo, op_bar ]` /// * esm: a comma-separated list of ESM module filenames (see [`include_js_files`]), eg: `esm = [ dir "dir", "my_file.js" ]` +/// * esm_setup_script: see [`ExtensionBuilder::esm_setup_script`] /// * js: a comma-separated list of JS filenames (see [`include_js_files`]), eg: `js = [ dir "dir", "my_file.js" ]` /// * config: a structure-like definition for configuration parameters which will be required when initializing this extension, eg: `config = { my_param: Option<usize> }` /// * middleware: an [`OpDecl`] middleware function with the signature `fn (OpDecl) -> OpDecl` @@ -184,8 +185,8 @@ macro_rules! extension { $(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )? ),+ $(,)? ] )? $(, esm_entry_point = $esm_entry_point:literal )? $(, esm = [ $( dir $dir_esm:literal , )? $( $esm:literal ),* $(,)? ] )? + $(, esm_setup_script = $esm_setup_script:expr )? $(, js = [ $( dir $dir_js:literal , )? $( $js:literal ),* $(,)? ] )? - $(, force_include_js_sources $($force_include_js_sources:block)? )? // dummy variable $(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )? $(, middleware = $middleware_fn:expr )? $(, state = $state_fn:expr )? @@ -210,15 +211,21 @@ macro_rules! extension { #[inline(always)] #[allow(unused_variables)] fn with_js(ext: &mut $crate::ExtensionBuilder) { - ext.esm( - $crate::include_js_files!( $name $( force_include_js_sources $($force_include_js_sources)?, )? $( $( dir $dir_esm , )? $( $esm , )* )? ) - ); + $( ext.esm( + $crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm , )* ) + ); )? + $( + ext.esm(vec![ExtensionFileSource { + specifier: "ext:setup", + code: ExtensionFileSourceCode::IncludedInBinary($esm_setup_script), + }]); + )? $( ext.esm_entry_point($esm_entry_point); )? - ext.js( - $crate::include_js_files!( $name $( force_include_js_sources $($force_include_js_sources)?, )? $( $( dir $dir_js , )? $( $js , )* )? ) - ); + $( ext.js( + $crate::include_js_files!( $name $( dir $dir_js , )? $( $js , )* ) + ); )? } // If ops were specified, add those ops to the extension. @@ -278,7 +285,7 @@ macro_rules! extension { } #[allow(dead_code)] - pub fn init_ext $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension + pub fn init_ops_and_esm $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension $( where $( $bound : $bound_type ),+ )? { let mut ext = Self::ext(); @@ -289,6 +296,17 @@ macro_rules! extension { Self::with_customizer(&mut ext); ext.take() } + + #[allow(dead_code)] + pub fn init_ops $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension + $( where $( $bound : $bound_type ),+ )? + { + let mut ext = Self::ext(); + Self::with_ops $( ::< $( $param ),+ > )?(&mut ext); + Self::with_state_and_middleware $( ::< $( $param ),+ > )?(&mut ext, $( $( $options_id , )* )? ); + Self::with_customizer(&mut ext); + ext.take() + } } }; @@ -331,8 +349,8 @@ macro_rules! extension { #[derive(Default)] pub struct Extension { pub(crate) name: &'static str, - js_files: Vec<ExtensionFileSource>, - esm_files: Vec<ExtensionFileSource>, + js_files: Option<Vec<ExtensionFileSource>>, + esm_files: Option<Vec<ExtensionFileSource>>, esm_entry_point: Option<&'static str>, ops: Option<Vec<OpDecl>>, opstate_fn: Option<Box<OpStateFn>>, @@ -388,12 +406,12 @@ impl Extension { /// returns JS source code to be loaded into the isolate (either at snapshotting, /// or at startup). as a vector of a tuple of the file name, and the source code. - pub fn get_js_sources(&self) -> &Vec<ExtensionFileSource> { - &self.js_files + pub fn get_js_sources(&self) -> Option<&Vec<ExtensionFileSource>> { + self.js_files.as_ref() } - pub fn get_esm_sources(&self) -> &Vec<ExtensionFileSource> { - &self.esm_files + pub fn get_esm_sources(&self) -> Option<&Vec<ExtensionFileSource>> { + self.esm_files.as_ref() } pub fn get_esm_entry_point(&self) -> Option<&'static str> { @@ -514,11 +532,13 @@ impl ExtensionBuilder { /// Consume the [`ExtensionBuilder`] and return an [`Extension`]. pub fn take(self) -> Extension { + let js_files = Some(self.js); + let esm_files = Some(self.esm); let ops = Some(self.ops); let deps = Some(self.deps); Extension { - js_files: self.js, - esm_files: self.esm, + js_files, + esm_files, esm_entry_point: self.esm_entry_point, ops, opstate_fn: self.state, @@ -533,11 +553,13 @@ impl ExtensionBuilder { } pub fn build(&mut self) -> Extension { + let js_files = Some(std::mem::take(&mut self.js)); + let esm_files = Some(std::mem::take(&mut self.esm)); let ops = Some(std::mem::take(&mut self.ops)); let deps = Some(std::mem::take(&mut self.deps)); Extension { - js_files: std::mem::take(&mut self.js), - esm_files: std::mem::take(&mut self.esm), + js_files, + esm_files, esm_entry_point: self.esm_entry_point.take(), ops, opstate_fn: self.state.take(), @@ -586,58 +608,54 @@ impl ExtensionBuilder { /// - "ext:my_extension/js/01_hello.js" /// - "ext:my_extension/js/02_goodbye.js" /// ``` -#[cfg(all( - feature = "exclude_js_sources", - not(feature = "runtime_js_sources"), -))] +#[cfg(not(feature = "include_js_files_for_snapshotting"))] #[macro_export] macro_rules! include_js_files { - ($name:ident $(dir $dir:literal,)? $($file:literal,)*) => { - vec![] - }; - - ($name:ident force_include_js_sources $($dummy:block)?, $($file:literal,)*) => { - vec![$($crate::include_js_file!($name $file)),*] + ($name:ident dir $dir:literal, $($file:literal,)+) => { + vec![ + $($crate::ExtensionFileSource { + specifier: concat!("ext:", stringify!($name), "/", $file), + code: $crate::ExtensionFileSourceCode::IncludedInBinary( + include_str!(concat!($dir, "/", $file) + )), + },)+ + ] }; - ($name:ident force_include_js_sources $($dummy:block)?, dir $dir:literal, $($file:literal,)*) => { - vec![$($crate::include_js_file!($name dir $dir, $file)),*] + ($name:ident $($file:literal,)+) => { + vec![ + $($crate::ExtensionFileSource { + specifier: concat!("ext:", stringify!($name), "/", $file), + code: $crate::ExtensionFileSourceCode::IncludedInBinary( + include_str!($file) + ), + },)+ + ] }; } -#[cfg(not(all( - feature = "exclude_js_sources", - not(feature = "runtime_js_sources"), -)))] +#[cfg(feature = "include_js_files_for_snapshotting")] #[macro_export] macro_rules! include_js_files { - ($name:ident $(force_include_js_sources $($dummy:block)?,)? $($file:literal,)*) => { - vec![$($crate::include_js_file!($name $file)),*] + ($name:ident dir $dir:literal, $($file:literal,)+) => { + vec![ + $($crate::ExtensionFileSource { + specifier: concat!("ext:", stringify!($name), "/", $file), + code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot( + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file) + ), + },)+ + ] }; - ($name:ident $(force_include_js_sources $($dummy:block)?,)? dir $dir:literal, $($file:literal,)*) => { - vec![$($crate::include_js_file!($name dir $dir, $file)),*] - }; -} - -#[cfg(not(feature = "runtime_js_sources"))] -#[macro_export] -macro_rules! include_js_file { - ($ext_name:ident $(dir $dir:literal,)? $file:literal) => { - $crate::ExtensionFileSource { - specifier: concat!("ext:", stringify!($ext_name), "/", $file), - code: $crate::ExtensionFileSourceCode::IncludedInBinary(include_str!(concat!($($dir, "/",)? $file))), - } - }; -} - -#[cfg(feature = "runtime_js_sources")] -#[macro_export] -macro_rules! include_js_file { - ($ext_name:ident $(dir $dir:literal,)? $file:literal) => { - $crate::ExtensionFileSource { - specifier: concat!("ext:", stringify!($ext_name), "/", $file), - code: $crate::ExtensionFileSourceCode::LoadAtRuntime(std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))$(.join($dir))?.join($file)), - } + ($name:ident $($file:literal,)+) => { + vec![ + $($crate::ExtensionFileSource { + specifier: concat!("ext:", stringify!($name), "/", $file), + code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot( + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($file) + ), + },)+ + ] }; } |