Skip to content

Commit

Permalink
refactor: use Arc instead of making copies of Flags struct (#13610)
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim authored Feb 11, 2022
1 parent 2fa0096 commit 65de5fb
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 83 deletions.
22 changes: 10 additions & 12 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,17 @@ impl Flags {
vec![]
}
}
}

impl From<Flags> for PermissionsOptions {
fn from(flags: Flags) -> Self {
Self {
allow_env: flags.allow_env,
allow_hrtime: flags.allow_hrtime,
allow_net: flags.allow_net,
allow_ffi: flags.allow_ffi,
allow_read: flags.allow_read,
allow_run: flags.allow_run,
allow_write: flags.allow_write,
prompt: flags.prompt,
pub fn permissions_options(&self) -> PermissionsOptions {
PermissionsOptions {
allow_env: self.allow_env.clone(),
allow_hrtime: self.allow_hrtime,
allow_net: self.allow_net.clone(),
allow_ffi: self.allow_ffi.clone(),
allow_read: self.allow_read.clone(),
allow_run: self.allow_run.clone(),
allow_write: self.allow_write.clone(),
prompt: self.prompt,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/lsp/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl CacheServer {
let _join_handle = thread::spawn(move || {
let runtime = create_basic_runtime();
runtime.block_on(async {
let ps = ProcState::build(Flags {
let ps = ProcState::build(Arc::new(Flags {
cache_path: maybe_cache_path,
ca_stores: maybe_ca_stores,
ca_file: maybe_ca_file,
unsafely_ignore_certificate_errors,
..Default::default()
})
}))
.await
.unwrap();
let maybe_import_map_resolver =
Expand Down
59 changes: 29 additions & 30 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,11 @@ async fn compile_command(
) -> Result<i32, AnyError> {
let debug = flags.log_level == Some(log::Level::Debug);

let run_flags = tools::standalone::compile_to_runtime_flags(
flags.clone(),
compile_flags.args,
)?;
let run_flags =
tools::standalone::compile_to_runtime_flags(&flags, compile_flags.args)?;

let module_specifier = resolve_url_or_path(&compile_flags.source_file)?;
let ps = ProcState::build(flags.clone()).await?;
let ps = ProcState::build(Arc::new(flags)).await?;
let deno_dir = &ps.dir;

let output = compile_flags.output.and_then(|output| {
Expand Down Expand Up @@ -432,7 +430,7 @@ async fn compile_command(
})
.flatten()
.unwrap_or_else(|| {
bundle_module_graph(graph.as_ref(), &ps, &flags).map(|r| r.0)
bundle_module_graph(graph.as_ref(), &ps, &ps.flags).map(|r| r.0)
})?;

info!(
Expand Down Expand Up @@ -468,7 +466,7 @@ async fn info_command(
flags: Flags,
info_flags: InfoFlags,
) -> Result<i32, AnyError> {
let ps = ProcState::build(flags).await?;
let ps = ProcState::build(Arc::new(flags)).await?;
if let Some(specifier) = info_flags.file {
let specifier = resolve_url_or_path(&specifier)?;
let mut cache = cache::FetchCacher::new(
Expand Down Expand Up @@ -526,8 +524,9 @@ async fn install_command(
let mut preload_flags = flags.clone();
preload_flags.inspect = None;
preload_flags.inspect_brk = None;
let permissions = Permissions::from_options(&preload_flags.clone().into());
let ps = ProcState::build(preload_flags).await?;
let permissions =
Permissions::from_options(&preload_flags.permissions_options());
let ps = ProcState::build(Arc::new(preload_flags)).await?;
let main_module = resolve_url_or_path(&install_flags.module_url)?;
let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
Expand Down Expand Up @@ -571,7 +570,7 @@ async fn cache_command(
} else {
emit::TypeLib::DenoWindow
};
let ps = ProcState::build(flags).await?;
let ps = ProcState::build(Arc::new(flags)).await?;

for file in cache_flags.files {
let specifier = resolve_url_or_path(&file)?;
Expand All @@ -597,8 +596,8 @@ async fn eval_command(
// type, and so our "fake" specifier needs to have the proper extension.
let main_module =
resolve_url_or_path(&format!("./$deno$eval.{}", eval_flags.ext)).unwrap();
let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.permissions_options());
let ps = ProcState::build(Arc::new(flags)).await?;
let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
// Create a dummy source file.
Expand All @@ -622,7 +621,7 @@ async fn eval_command(
// to allow module access by TS compiler.
ps.file_fetcher.insert_cached(file);
debug!("main_module {}", &main_module);
if flags.compat {
if ps.flags.compat {
worker.execute_side_module(&compat::GLOBAL_URL).await?;
}
worker.execute_main_module(&main_module).await?;
Expand Down Expand Up @@ -786,7 +785,7 @@ async fn bundle_command(
bundle_flags: BundleFlags,
) -> Result<i32, AnyError> {
let debug = flags.log_level == Some(log::Level::Debug);

let flags = Arc::new(flags);
let resolver = |_| {
let flags = flags.clone();
let source_file1 = bundle_flags.source_file.clone();
Expand All @@ -795,7 +794,7 @@ async fn bundle_command(
let module_specifier = resolve_url_or_path(&source_file1)?;

debug!(">>>>> bundle START");
let ps = ProcState::build(flags.clone()).await?;
let ps = ProcState::build(flags).await?;

let graph =
create_graph_and_maybe_check(module_specifier, &ps, debug).await?;
Expand Down Expand Up @@ -831,11 +830,10 @@ async fn bundle_command(
};

let operation = |(ps, graph): (ProcState, Arc<deno_graph::ModuleGraph>)| {
let flags = flags.clone();
let out_file = bundle_flags.out_file.clone();
async move {
let (bundle_emit, maybe_bundle_map) =
bundle_module_graph(graph.as_ref(), &ps, &flags)?;
bundle_module_graph(graph.as_ref(), &ps, &ps.flags)?;
debug!(">>>>> bundle END");

if let Some(out_file) = out_file.as_ref() {
Expand Down Expand Up @@ -908,7 +906,7 @@ async fn format_command(
flags: Flags,
fmt_flags: FmtFlags,
) -> Result<i32, AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let ps = ProcState::build(Arc::new(flags)).await?;
let maybe_fmt_config = if let Some(config_file) = &ps.maybe_config_file {
config_file.to_fmt_config()?
} else {
Expand All @@ -923,7 +921,7 @@ async fn format_command(
return Ok(0);
}

tools::fmt::format(flags, fmt_flags, maybe_fmt_config).await?;
tools::fmt::format(ps.flags.as_ref(), fmt_flags, maybe_fmt_config).await?;
Ok(0)
}

Expand All @@ -932,11 +930,11 @@ async fn repl_command(
repl_flags: ReplFlags,
) -> Result<i32, AnyError> {
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.permissions_options());
let ps = ProcState::build(Arc::new(flags)).await?;
let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, vec![]);
if flags.compat {
if ps.flags.compat {
worker.execute_side_module(&compat::GLOBAL_URL).await?;
compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?;
worker.run_event_loop(false).await?;
Expand All @@ -948,8 +946,8 @@ async fn repl_command(
}

async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(Arc::new(flags)).await?;
let permissions = Permissions::from_options(&ps.flags.permissions_options());
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
let mut worker =
create_main_worker(&ps.clone(), main_module.clone(), permissions, vec![]);
Expand All @@ -970,7 +968,7 @@ async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
ps.file_fetcher.insert_cached(source_file);

debug!("main_module {}", main_module);
if flags.compat {
if ps.flags.compat {
worker.execute_side_module(&compat::GLOBAL_URL).await?;
}
worker.execute_main_module(&main_module).await?;
Expand All @@ -983,6 +981,7 @@ async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
// TODO(bartlomieju): this function is not handling `exit_code` set by the runtime
// code properly.
async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
let flags = Arc::new(flags);
let resolver = |_| {
let script1 = script.clone();
let script2 = script.clone();
Expand Down Expand Up @@ -1131,7 +1130,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {

let operation = |(ps, main_module): (ProcState, ModuleSpecifier)| {
let flags = flags.clone();
let permissions = Permissions::from_options(&flags.clone().into());
let permissions = Permissions::from_options(&flags.permissions_options());
async move {
// We make use an module executor guard to ensure that unload is always fired when an
// operation is called.
Expand Down Expand Up @@ -1177,8 +1176,8 @@ async fn run_command(
// map specified and bare specifier is used on the command line - this should
// probably call `ProcState::resolve` instead
let main_module = resolve_url_or_path(&run_flags.script)?;
let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(Arc::new(flags)).await?;
let permissions = Permissions::from_options(&ps.flags.permissions_options());
let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, vec![]);

Expand All @@ -1199,7 +1198,7 @@ async fn run_command(

debug!("main_module {}", main_module);

if flags.compat {
if ps.flags.compat {
// TODO(bartlomieju): fix me
assert_eq!(main_module.scheme(), "file");

Expand Down Expand Up @@ -1326,7 +1325,7 @@ fn init_v8_flags(v8_flags: &[String]) {
fn get_subcommand(
flags: Flags,
) -> Pin<Box<dyn Future<Output = Result<i32, AnyError>>>> {
match flags.clone().subcommand {
match flags.subcommand.clone() {
DenoSubcommand::Bundle(bundle_flags) => {
bundle_command(flags, bundle_flags).boxed_local()
}
Expand Down
4 changes: 2 additions & 2 deletions cli/proc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub struct ProcState(Arc<Inner>);

pub struct Inner {
/// Flags parsed from `argv` contents.
pub flags: flags::Flags,
pub flags: Arc<flags::Flags>,
pub dir: deno_dir::DenoDir,
pub coverage_dir: Option<String>,
pub file_fetcher: FileFetcher,
Expand All @@ -90,7 +90,7 @@ impl Deref for ProcState {
}

impl ProcState {
pub async fn build(flags: flags::Flags) -> Result<Self, AnyError> {
pub async fn build(flags: Arc<flags::Flags>) -> Result<Self, AnyError> {
let maybe_custom_root = flags
.cache_path
.clone()
Expand Down
2 changes: 1 addition & 1 deletion cli/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub async fn run(
) -> Result<(), AnyError> {
let flags = metadata_to_flags(&metadata);
let main_module = resolve_url(SPECIFIER)?;
let ps = ProcState::build(flags).await?;
let ps = ProcState::build(Arc::new(flags)).await?;
let permissions = Permissions::from_options(&metadata.permissions);
let blob_store = BlobStore::default();
let broadcast_channel = InMemoryBroadcastChannel::default();
Expand Down
3 changes: 2 additions & 1 deletion cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::path::PathBuf;
use std::sync::Arc;
use text_lines::TextLines;
use uuid::Uuid;

Expand Down Expand Up @@ -559,7 +560,7 @@ pub async fn cover_files(
flags: Flags,
coverage_flags: CoverageFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags).await?;
let ps = ProcState::build(Arc::new(flags)).await?;

let script_coverages =
collect_coverages(coverage_flags.files, coverage_flags.ignore)?;
Expand Down
4 changes: 2 additions & 2 deletions cli/tools/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub async fn print_docs(
flags: Flags,
doc_flags: DocFlags,
) -> Result<(), AnyError> {
let ps = ProcState::build(flags.clone()).await?;
let ps = ProcState::build(Arc::new(flags)).await?;
let source_file = doc_flags
.source_file
.unwrap_or_else(|| "--builtin".to_string());
Expand All @@ -122,7 +122,7 @@ pub async fn print_docs(
doc_parser.parse_source(
&source_file_specifier,
MediaType::Dts,
Arc::new(get_types(flags.unstable)),
Arc::new(get_types(ps.flags.unstable)),
)
} else {
let module_specifier = resolve_url_or_path(&source_file)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::sync::{Arc, Mutex};

/// Format JavaScript/TypeScript files.
pub async fn format(
flags: Flags,
flags: &Flags,
fmt_flags: FmtFlags,
maybe_fmt_config: Option<FmtConfig>,
) -> Result<(), AnyError> {
Expand Down
1 change: 1 addition & 0 deletions cli/tools/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> {
}

pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> {
let flags = Arc::new(flags);
let LintFlags {
maybe_rules_tags,
maybe_rules_include,
Expand Down
Loading

0 comments on commit 65de5fb

Please sign in to comment.