diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2023-06-13 16:45:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-13 09:45:06 -0600 |
commit | ceb03cfb037cf7024a5048b17b508ddda59cfa05 (patch) | |
tree | 0c2df444e00f17131f9e2af708f80cdc4b032ce8 /core/extensions.rs | |
parent | 5348778666d2d8d7ce138bbdf75ac5aa9f7ed428 (diff) |
refactor(core): cleanup feature flags for js source inclusion (#19463)
Remove `ExtensionFileSourceCode::LoadedFromFsDuringSnapshot` and feature
`include_js_for_snapshotting` since they leak paths that are only
applicable in this repo to embedders. Replace with feature
`exclude_js_sources`. Additionally the feature
`force_include_js_sources` allows negating it, if both features are set.
We need both of these because features are additive and there must be a
way of force including sources for snapshot creation while still having
the `exclude_js_sources` feature. `force_include_js_sources` is only set
for build deps, so sources are still excluded from the final binary.
You can also specify `force_include_js_sources` on any extension to
override the above features for that extension. Towards #19398.
But there was still the snapshot-from-snapshot situation where code
could be executed twice, I addressed that by making `mod_evaluate()` and
scripts like `core/01_core.js` behave idempotently. This allowed
unifying `ext::init_ops()` and `ext::init_ops_and_esm()` into
`ext::init()`.
Diffstat (limited to 'core/extensions.rs')
-rw-r--r-- | core/extensions.rs | 133 |
1 files changed, 43 insertions, 90 deletions
diff --git a/core/extensions.rs b/core/extensions.rs index fa6d7851e..ab0578106 100644 --- a/core/extensions.rs +++ b/core/extensions.rs @@ -1,32 +1,15 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use crate::modules::ModuleCode; use crate::OpState; -use anyhow::Context as _; -use anyhow::Error; use std::cell::RefCell; -use std::path::PathBuf; use std::rc::Rc; use std::task::Context; use v8::fast_api::FastFunction; #[derive(Clone, Debug)] -pub enum ExtensionFileSourceCode { - /// Source code is included in the binary produced. Either by being defined - /// inline, or included using `include_str!()`. If you are snapshotting, this - /// will result in two copies of the source code being included - one in the - /// snapshot, the other the static string in the `Extension`. - IncludedInBinary(&'static str), - - // 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)] pub struct ExtensionFileSource { pub specifier: &'static str, - pub code: ExtensionFileSourceCode, + pub code: &'static str, } impl ExtensionFileSource { @@ -34,29 +17,14 @@ impl ExtensionFileSource { s.chars().filter(|c| !c.is_ascii()).collect::<String>() } - pub fn load(&self) -> Result<ModuleCode, Error> { - match &self.code { - ExtensionFileSourceCode::IncludedInBinary(code) => { - debug_assert!( - code.is_ascii(), - "Extension code must be 7-bit ASCII: {} (found {})", - self.specifier, - Self::find_non_ascii(code) - ); - Ok(ModuleCode::from_static(code)) - } - ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => { - let msg = || format!("Failed to read \"{}\"", path.display()); - let s = std::fs::read_to_string(path).with_context(msg)?; - debug_assert!( - s.is_ascii(), - "Extension code must be 7-bit ASCII: {} (found {})", - self.specifier, - Self::find_non_ascii(&s) - ); - Ok(s.into()) - } - } + pub fn load(&self) -> ModuleCode { + debug_assert!( + self.code.is_ascii(), + "Extension code must be 7-bit ASCII: {} (found {})", + self.specifier, + Self::find_non_ascii(self.code) + ); + ModuleCode::from_static(self.code) } } @@ -187,6 +155,7 @@ macro_rules! extension { $(, 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 )? @@ -211,9 +180,9 @@ macro_rules! extension { #[inline(always)] #[allow(unused_variables)] fn with_js(ext: &mut $crate::ExtensionBuilder) { - $( ext.esm( - $crate::include_js_files!( $name $( dir $dir_esm , )? $( $esm , )* ) - ); )? + ext.esm( + $crate::include_js_files!( $name $( force_include_js_sources $($force_include_js_sources)?, )? $( $( dir $dir_esm , )? $( $esm , )* )? ) + ); $( ext.esm(vec![ExtensionFileSource { specifier: "ext:setup", @@ -223,9 +192,9 @@ macro_rules! extension { $( ext.esm_entry_point($esm_entry_point); )? - $( ext.js( - $crate::include_js_files!( $name $( dir $dir_js , )? $( $js , )* ) - ); )? + ext.js( + $crate::include_js_files!( $name $( force_include_js_sources $($force_include_js_sources)?, )? $( $( dir $dir_js , )? $( $js , )* )? ) + ); } // If ops were specified, add those ops to the extension. @@ -285,7 +254,7 @@ macro_rules! extension { } #[allow(dead_code)] - pub fn init_ops_and_esm $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension + pub fn init $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension $( where $( $bound : $bound_type ),+ )? { let mut ext = Self::ext(); @@ -296,17 +265,6 @@ 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() - } } }; @@ -608,54 +566,49 @@ impl ExtensionBuilder { /// - "ext:my_extension/js/01_hello.js" /// - "ext:my_extension/js/02_goodbye.js" /// ``` -#[cfg(not(feature = "include_js_files_for_snapshotting"))] +#[cfg(not(all( + feature = "exclude_js_sources", + not(feature = "force_include_js_sources") +)))] #[macro_export] macro_rules! include_js_files { - ($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,)*) => { + $crate::force_include_js_files!($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(all( + feature = "exclude_js_sources", + not(feature = "force_include_js_sources") +))] +#[macro_export] +macro_rules! include_js_files { + ($name:ident force_include_js_sources $($dummy:block)?, $(dir $dir:literal,)? $($file:literal,)*) => { + $crate::force_include_js_files!($name $(dir $dir,)? $($file,)*) + }; + + ($name:ident $(dir $dir:literal,)? $($file:literal,)*) => { + vec![] }; } -#[cfg(feature = "include_js_files_for_snapshotting")] #[macro_export] -macro_rules! include_js_files { - ($name:ident dir $dir:literal, $($file:literal,)+) => { +macro_rules! force_include_js_files { + ($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) - ), - },)+ + code: include_str!(concat!($dir, "/", $file)), + },)* ] }; - ($name:ident $($file:literal,)+) => { + ($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) - ), - },)+ + code: include_str!($file), + },)* ] }; } |