1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-10 08:09:06 -05:00

refactor: Rename ProgramState to ProcState (#12204)

Move Arc into struct
This commit is contained in:
Ryan Dahl 2021-09-24 11:10:42 -04:00 committed by GitHub
parent 703ca905fd
commit 01da1d0adc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 281 additions and 331 deletions

View file

@ -14,7 +14,7 @@ use crate::flags::Flags;
use crate::http_cache; use crate::http_cache;
use crate::http_cache::HttpCache; use crate::http_cache::HttpCache;
use crate::module_graph::GraphBuilder; use crate::module_graph::GraphBuilder;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::specifier_handler::FetchHandler; use crate::specifier_handler::FetchHandler;
use crate::text_encoding; use crate::text_encoding;
use import_map::ImportMap; use import_map::ImportMap;
@ -40,15 +40,13 @@ pub async fn cache(
maybe_config_file: &Option<ConfigFile>, maybe_config_file: &Option<ConfigFile>,
maybe_cache_path: &Option<PathBuf>, maybe_cache_path: &Option<PathBuf>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = Arc::new( let ps = ProcState::build(Flags {
ProgramState::build(Flags {
cache_path: maybe_cache_path.clone(), cache_path: maybe_cache_path.clone(),
..Default::default() ..Default::default()
}) })
.await?, .await?;
);
let handler = Arc::new(Mutex::new(FetchHandler::new( let handler = Arc::new(Mutex::new(FetchHandler::new(
&program_state, &ps,
Permissions::allow_all(), Permissions::allow_all(),
Permissions::allow_all(), Permissions::allow_all(),
)?)); )?));

View file

@ -24,7 +24,7 @@ mod lsp;
mod module_graph; mod module_graph;
mod module_loader; mod module_loader;
mod ops; mod ops;
mod program_state; mod proc_state;
mod source_maps; mod source_maps;
mod specifier_handler; mod specifier_handler;
mod standalone; mod standalone;
@ -56,7 +56,7 @@ use crate::flags::TestFlags;
use crate::flags::UpgradeFlags; use crate::flags::UpgradeFlags;
use crate::fmt_errors::PrettyJsError; use crate::fmt_errors::PrettyJsError;
use crate::module_loader::CliModuleLoader; use crate::module_loader::CliModuleLoader;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::source_maps::apply_source_map; use crate::source_maps::apply_source_map;
use crate::specifier_handler::FetchHandler; use crate::specifier_handler::FetchHandler;
use crate::tools::installer::infer_name_from_url; use crate::tools::installer::infer_name_from_url;
@ -91,42 +91,36 @@ use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
fn create_web_worker_callback( fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
program_state: Arc<ProgramState>,
) -> Arc<CreateWebWorkerCb> {
Arc::new(move |args| { Arc::new(move |args| {
let global_state_ = program_state.clone(); let global_state_ = ps.clone();
let js_error_create_fn = Rc::new(move |core_js_error| { let js_error_create_fn = Rc::new(move |core_js_error| {
let source_mapped_error = let source_mapped_error =
apply_source_map(&core_js_error, global_state_.clone()); apply_source_map(&core_js_error, global_state_.clone());
PrettyJsError::create(source_mapped_error) PrettyJsError::create(source_mapped_error)
}); });
let maybe_inspector_server = program_state.maybe_inspector_server.clone(); let maybe_inspector_server = ps.maybe_inspector_server.clone();
let module_loader = CliModuleLoader::new_for_worker( let module_loader = CliModuleLoader::new_for_worker(
program_state.clone(), ps.clone(),
args.parent_permissions.clone(), args.parent_permissions.clone(),
); );
let create_web_worker_cb = let create_web_worker_cb = create_web_worker_callback(ps.clone());
create_web_worker_callback(program_state.clone());
let options = WebWorkerOptions { let options = WebWorkerOptions {
args: program_state.flags.argv.clone(), args: ps.flags.argv.clone(),
apply_source_maps: true, apply_source_maps: true,
debug_flag: program_state debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
.flags unstable: ps.flags.unstable,
.log_level enable_testing_features: ps.flags.enable_testing_features,
.map_or(false, |l| l == log::Level::Debug), unsafely_ignore_certificate_errors: ps
unstable: program_state.flags.unstable,
enable_testing_features: program_state.flags.enable_testing_features,
unsafely_ignore_certificate_errors: program_state
.flags .flags
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
.clone(), .clone(),
root_cert_store: program_state.root_cert_store.clone(), root_cert_store: ps.root_cert_store.clone(),
user_agent: version::get_user_agent(), user_agent: version::get_user_agent(),
seed: program_state.flags.seed, seed: ps.flags.seed,
module_loader, module_loader,
create_web_worker_cb, create_web_worker_cb,
js_error_create_fn: Some(js_error_create_fn), js_error_create_fn: Some(js_error_create_fn),
@ -137,11 +131,9 @@ fn create_web_worker_callback(
ts_version: version::TYPESCRIPT.to_string(), ts_version: version::TYPESCRIPT.to_string(),
no_color: !colors::use_color(), no_color: !colors::use_color(),
get_error_class_fn: Some(&crate::errors::get_error_class_name), get_error_class_fn: Some(&crate::errors::get_error_class_name),
blob_store: program_state.blob_store.clone(), blob_store: ps.blob_store.clone(),
broadcast_channel: program_state.broadcast_channel.clone(), broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some( shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
program_state.shared_array_buffer_store.clone(),
),
cpu_count: num_cpus::get(), cpu_count: num_cpus::get(),
}; };
@ -160,7 +152,7 @@ fn create_web_worker_callback(
js_runtime js_runtime
.op_state() .op_state()
.borrow_mut() .borrow_mut()
.put::<Arc<ProgramState>>(program_state.clone()); .put::<ProcState>(ps.clone());
// Applies source maps - works in conjuction with `js_error_create_fn` // Applies source maps - works in conjuction with `js_error_create_fn`
// above // above
ops::errors::init(js_runtime); ops::errors::init(js_runtime);
@ -176,14 +168,14 @@ fn create_web_worker_callback(
} }
pub fn create_main_worker( pub fn create_main_worker(
program_state: &Arc<ProgramState>, ps: &ProcState,
main_module: ModuleSpecifier, main_module: ModuleSpecifier,
permissions: Permissions, permissions: Permissions,
maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>, maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>,
) -> MainWorker { ) -> MainWorker {
let module_loader = CliModuleLoader::new(program_state.clone()); let module_loader = CliModuleLoader::new(ps.clone());
let global_state_ = program_state.clone(); let global_state_ = ps.clone();
let js_error_create_fn = Rc::new(move |core_js_error| { let js_error_create_fn = Rc::new(move |core_js_error| {
let source_mapped_error = let source_mapped_error =
@ -191,28 +183,24 @@ pub fn create_main_worker(
PrettyJsError::create(source_mapped_error) PrettyJsError::create(source_mapped_error)
}); });
let maybe_inspector_server = program_state.maybe_inspector_server.clone(); let maybe_inspector_server = ps.maybe_inspector_server.clone();
let should_break_on_first_statement = let should_break_on_first_statement = ps.flags.inspect_brk.is_some();
program_state.flags.inspect_brk.is_some();
let create_web_worker_cb = create_web_worker_callback(program_state.clone()); let create_web_worker_cb = create_web_worker_callback(ps.clone());
let options = WorkerOptions { let options = WorkerOptions {
apply_source_maps: true, apply_source_maps: true,
args: program_state.flags.argv.clone(), args: ps.flags.argv.clone(),
debug_flag: program_state debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
.flags unstable: ps.flags.unstable,
.log_level enable_testing_features: ps.flags.enable_testing_features,
.map_or(false, |l| l == log::Level::Debug), unsafely_ignore_certificate_errors: ps
unstable: program_state.flags.unstable,
enable_testing_features: program_state.flags.enable_testing_features,
unsafely_ignore_certificate_errors: program_state
.flags .flags
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
.clone(), .clone(),
root_cert_store: program_state.root_cert_store.clone(), root_cert_store: ps.root_cert_store.clone(),
user_agent: version::get_user_agent(), user_agent: version::get_user_agent(),
seed: program_state.flags.seed, seed: ps.flags.seed,
js_error_create_fn: Some(js_error_create_fn), js_error_create_fn: Some(js_error_create_fn),
create_web_worker_cb, create_web_worker_cb,
maybe_inspector_server, maybe_inspector_server,
@ -222,21 +210,18 @@ pub fn create_main_worker(
ts_version: version::TYPESCRIPT.to_string(), ts_version: version::TYPESCRIPT.to_string(),
no_color: !colors::use_color(), no_color: !colors::use_color(),
get_error_class_fn: Some(&crate::errors::get_error_class_name), get_error_class_fn: Some(&crate::errors::get_error_class_name),
location: program_state.flags.location.clone(), location: ps.flags.location.clone(),
origin_storage_dir: program_state.flags.location.clone().map(|loc| { origin_storage_dir: ps.flags.location.clone().map(|loc| {
program_state ps.dir
.dir
.root .root
.clone() .clone()
// TODO(@crowlKats): change to origin_data for 2.0 // TODO(@crowlKats): change to origin_data for 2.0
.join("location_data") .join("location_data")
.join(checksum::gen(&[loc.to_string().as_bytes()])) .join(checksum::gen(&[loc.to_string().as_bytes()]))
}), }),
blob_store: program_state.blob_store.clone(), blob_store: ps.blob_store.clone(),
broadcast_channel: program_state.broadcast_channel.clone(), broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some( shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
program_state.shared_array_buffer_store.clone(),
),
cpu_count: num_cpus::get(), cpu_count: num_cpus::get(),
}; };
@ -249,7 +234,7 @@ pub fn create_main_worker(
js_runtime js_runtime
.op_state() .op_state()
.borrow_mut() .borrow_mut()
.put::<Arc<ProgramState>>(program_state.clone()); .put::<ProcState>(ps.clone());
// Applies source maps - works in conjuction with `js_error_create_fn` // Applies source maps - works in conjuction with `js_error_create_fn`
// above // above
ops::errors::init(js_runtime); ops::errors::init(js_runtime);
@ -291,7 +276,7 @@ where
} }
fn print_cache_info( fn print_cache_info(
state: &Arc<ProgramState>, state: &ProcState,
json: bool, json: bool,
location: Option<deno_core::url::Url>, location: Option<deno_core::url::Url>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
@ -387,8 +372,8 @@ async fn compile_command(
)?; )?;
let module_specifier = resolve_url_or_path(&compile_flags.source_file)?; let module_specifier = resolve_url_or_path(&compile_flags.source_file)?;
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let deno_dir = &program_state.dir; let deno_dir = &ps.dir;
let output = compile_flags.output.or_else(|| { let output = compile_flags.output.or_else(|| {
infer_name_from_url(&module_specifier).map(PathBuf::from) infer_name_from_url(&module_specifier).map(PathBuf::from)
@ -398,7 +383,7 @@ async fn compile_command(
let module_graph = create_module_graph_and_maybe_check( let module_graph = create_module_graph_and_maybe_check(
module_specifier.clone(), module_specifier.clone(),
program_state.clone(), ps.clone(),
debug, debug,
) )
.await?; .await?;
@ -408,8 +393,7 @@ async fn compile_command(
colors::green("Bundle"), colors::green("Bundle"),
module_specifier.to_string() module_specifier.to_string()
); );
let bundle_str = let bundle_str = bundle_module_graph(module_graph, ps.clone(), flags, debug)?;
bundle_module_graph(module_graph, program_state.clone(), flags, debug)?;
info!( info!(
"{} {}", "{} {}",
@ -445,11 +429,11 @@ async fn info_command(
info_flags: InfoFlags, info_flags: InfoFlags,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let location = flags.location.clone(); let location = flags.location.clone();
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
if let Some(specifier) = info_flags.file { if let Some(specifier) = info_flags.file {
let specifier = resolve_url_or_path(&specifier)?; let specifier = resolve_url_or_path(&specifier)?;
let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new( let handler = Arc::new(Mutex::new(specifier_handler::FetchHandler::new(
&program_state, &ps,
// info accesses dynamically imported modules just for their information // info accesses dynamically imported modules just for their information
// so we allow access to all of them. // so we allow access to all of them.
Permissions::allow_all(), Permissions::allow_all(),
@ -457,13 +441,11 @@ async fn info_command(
)?)); )?));
let mut builder = module_graph::GraphBuilder::new( let mut builder = module_graph::GraphBuilder::new(
handler, handler,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
program_state.lockfile.clone(), ps.lockfile.clone(),
); );
builder.add(&specifier, false).await?; builder.add(&specifier, false).await?;
builder builder.analyze_config_file(&ps.maybe_config_file).await?;
.analyze_config_file(&program_state.maybe_config_file)
.await?;
let graph = builder.get_graph(); let graph = builder.get_graph();
let info = graph.info()?; let info = graph.info()?;
@ -475,7 +457,7 @@ async fn info_command(
} }
} else { } else {
// If it was just "deno info" print location of caches and exit // If it was just "deno info" print location of caches and exit
print_cache_info(&program_state, info_flags.json, location) print_cache_info(&ps, info_flags.json, location)
} }
} }
@ -487,10 +469,10 @@ async fn install_command(
preload_flags.inspect = None; preload_flags.inspect = None;
preload_flags.inspect_brk = None; preload_flags.inspect_brk = None;
let permissions = Permissions::from_options(&preload_flags.clone().into()); let permissions = Permissions::from_options(&preload_flags.clone().into());
let program_state = ProgramState::build(preload_flags).await?; let ps = ProcState::build(preload_flags).await?;
let main_module = resolve_url_or_path(&install_flags.module_url)?; let main_module = resolve_url_or_path(&install_flags.module_url)?;
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, None); create_main_worker(&ps, main_module.clone(), permissions, None);
// First, fetch and compile the module; this step ensures that the module exists. // First, fetch and compile the module; this step ensures that the module exists.
worker.preload_module(&main_module, true).await?; worker.preload_module(&main_module, true).await?;
tools::installer::install( tools::installer::install(
@ -517,9 +499,8 @@ async fn lint_command(
return Ok(()); return Ok(());
} }
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let maybe_lint_config = let maybe_lint_config = if let Some(config_file) = &ps.maybe_config_file {
if let Some(config_file) = &program_state.maybe_config_file {
config_file.to_lint_config()? config_file.to_lint_config()?
} else { } else {
None None
@ -546,18 +527,17 @@ async fn cache_command(
} else { } else {
module_graph::TypeLib::DenoWindow module_graph::TypeLib::DenoWindow
}; };
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
for file in cache_flags.files { for file in cache_flags.files {
let specifier = resolve_url_or_path(&file)?; let specifier = resolve_url_or_path(&file)?;
program_state ps.prepare_module_load(
.prepare_module_load(
specifier, specifier,
lib.clone(), lib.clone(),
Permissions::allow_all(), Permissions::allow_all(),
Permissions::allow_all(), Permissions::allow_all(),
false, false,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
) )
.await?; .await?;
} }
@ -572,9 +552,9 @@ async fn eval_command(
// Force TypeScript compile. // Force TypeScript compile.
let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap(); let main_module = resolve_url_or_path("./$deno$eval.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, None); create_main_worker(&ps, main_module.clone(), permissions, None);
// Create a dummy source file. // Create a dummy source file.
let source_code = if eval_flags.print { let source_code = if eval_flags.print {
format!("console.log({})", eval_flags.code) format!("console.log({})", eval_flags.code)
@ -602,7 +582,7 @@ async fn eval_command(
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
// to allow module access by TS compiler. // to allow module access by TS compiler.
program_state.file_fetcher.insert_cached(file); ps.file_fetcher.insert_cached(file);
debug!("main_module {}", &main_module); debug!("main_module {}", &main_module);
worker.execute_main_module(&main_module).await?; worker.execute_main_module(&main_module).await?;
worker.execute_script( worker.execute_script(
@ -619,11 +599,11 @@ async fn eval_command(
async fn create_module_graph_and_maybe_check( async fn create_module_graph_and_maybe_check(
module_specifier: ModuleSpecifier, module_specifier: ModuleSpecifier,
program_state: Arc<ProgramState>, ps: ProcState,
debug: bool, debug: bool,
) -> Result<module_graph::Graph, AnyError> { ) -> Result<module_graph::Graph, AnyError> {
let handler = Arc::new(Mutex::new(FetchHandler::new( let handler = Arc::new(Mutex::new(FetchHandler::new(
&program_state, &ps,
// when bundling, dynamic imports are only access for their type safety, // when bundling, dynamic imports are only access for their type safety,
// therefore we will allow the graph to access any module. // therefore we will allow the graph to access any module.
Permissions::allow_all(), Permissions::allow_all(),
@ -631,18 +611,16 @@ async fn create_module_graph_and_maybe_check(
)?)); )?));
let mut builder = module_graph::GraphBuilder::new( let mut builder = module_graph::GraphBuilder::new(
handler, handler,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
program_state.lockfile.clone(), ps.lockfile.clone(),
); );
builder.add(&module_specifier, false).await?; builder.add(&module_specifier, false).await?;
builder builder.analyze_config_file(&ps.maybe_config_file).await?;
.analyze_config_file(&program_state.maybe_config_file)
.await?;
let module_graph = builder.get_graph(); let module_graph = builder.get_graph();
if !program_state.flags.no_check { if !ps.flags.no_check {
// TODO(@kitsonk) support bundling for workers // TODO(@kitsonk) support bundling for workers
let lib = if program_state.flags.unstable { let lib = if ps.flags.unstable {
module_graph::TypeLib::UnstableDenoWindow module_graph::TypeLib::UnstableDenoWindow
} else { } else {
module_graph::TypeLib::DenoWindow module_graph::TypeLib::DenoWindow
@ -652,8 +630,8 @@ async fn create_module_graph_and_maybe_check(
debug, debug,
emit: false, emit: false,
lib, lib,
maybe_config_file: program_state.maybe_config_file.clone(), maybe_config_file: ps.maybe_config_file.clone(),
reload: program_state.flags.reload, reload: ps.flags.reload,
..Default::default() ..Default::default()
})?; })?;
@ -671,14 +649,14 @@ async fn create_module_graph_and_maybe_check(
fn bundle_module_graph( fn bundle_module_graph(
module_graph: module_graph::Graph, module_graph: module_graph::Graph,
program_state: Arc<ProgramState>, ps: ProcState,
flags: Flags, flags: Flags,
debug: bool, debug: bool,
) -> Result<String, AnyError> { ) -> Result<String, AnyError> {
let (bundle, stats, maybe_ignored_options) = let (bundle, stats, maybe_ignored_options) =
module_graph.bundle(module_graph::BundleOptions { module_graph.bundle(module_graph::BundleOptions {
debug, debug,
maybe_config_file: program_state.maybe_config_file.clone(), maybe_config_file: ps.maybe_config_file.clone(),
})?; })?;
match maybe_ignored_options { match maybe_ignored_options {
Some(ignored_options) if flags.no_check => { Some(ignored_options) if flags.no_check => {
@ -704,11 +682,11 @@ async fn bundle_command(
let module_specifier = resolve_url_or_path(&source_file1)?; let module_specifier = resolve_url_or_path(&source_file1)?;
debug!(">>>>> bundle START"); debug!(">>>>> bundle START");
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let module_graph = create_module_graph_and_maybe_check( let module_graph = create_module_graph_and_maybe_check(
module_specifier, module_specifier,
program_state.clone(), ps.clone(),
debug, debug,
) )
.await?; .await?;
@ -719,20 +697,18 @@ async fn bundle_command(
.filter_map(|specifier| specifier.to_file_path().ok()) .filter_map(|specifier| specifier.to_file_path().ok())
.collect(); .collect();
if let Some(import_map) = program_state.flags.import_map_path.as_ref() { if let Some(import_map) = ps.flags.import_map_path.as_ref() {
paths_to_watch paths_to_watch
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?); .push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
} }
Ok((paths_to_watch, module_graph, program_state)) Ok((paths_to_watch, module_graph, ps))
} }
.map(move |result| match result { .map(move |result| match result {
Ok((paths_to_watch, module_graph, program_state)) => { Ok((paths_to_watch, module_graph, ps)) => ResolutionResult::Restart {
ResolutionResult::Restart {
paths_to_watch, paths_to_watch,
result: Ok((program_state, module_graph)), result: Ok((ps, module_graph)),
} },
}
Err(e) => ResolutionResult::Restart { Err(e) => ResolutionResult::Restart {
paths_to_watch: vec![PathBuf::from(source_file2)], paths_to_watch: vec![PathBuf::from(source_file2)],
result: Err(e), result: Err(e),
@ -740,17 +716,13 @@ async fn bundle_command(
}) })
}; };
let operation = |(program_state, module_graph): ( let operation = |(ps, module_graph): (ProcState, module_graph::Graph)| {
Arc<ProgramState>,
module_graph::Graph,
)| {
let flags = flags.clone(); let flags = flags.clone();
let out_file = bundle_flags.out_file.clone(); let out_file = bundle_flags.out_file.clone();
async move { async move {
info!("{} {}", colors::green("Bundle"), module_graph.info()?.root); info!("{} {}", colors::green("Bundle"), module_graph.info()?.root);
let output = let output = bundle_module_graph(module_graph, ps, flags, debug)?;
bundle_module_graph(module_graph, program_state, flags, debug)?;
debug!(">>>>> bundle END"); debug!(">>>>> bundle END");
@ -805,9 +777,8 @@ async fn format_command(
flags: Flags, flags: Flags,
fmt_flags: FmtFlags, fmt_flags: FmtFlags,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let maybe_fmt_config = let maybe_fmt_config = if let Some(config_file) = &ps.maybe_config_file {
if let Some(config_file) = &program_state.maybe_config_file {
config_file.to_fmt_config()? config_file.to_fmt_config()?
} else { } else {
None None
@ -827,24 +798,20 @@ async fn format_command(
async fn run_repl(flags: Flags, repl_flags: ReplFlags) -> Result<(), AnyError> { async fn run_repl(flags: Flags, repl_flags: ReplFlags) -> Result<(), AnyError> {
let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap(); let main_module = resolve_url_or_path("./$deno$repl.ts").unwrap();
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, None); create_main_worker(&ps, main_module.clone(), permissions, None);
worker.run_event_loop(false).await?; worker.run_event_loop(false).await?;
tools::repl::run(&program_state, worker, repl_flags.eval).await tools::repl::run(&ps, worker, repl_flags.eval).await
} }
async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> { async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap(); let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
let mut worker = create_main_worker( let mut worker =
&program_state.clone(), create_main_worker(&ps.clone(), main_module.clone(), permissions, None);
main_module.clone(),
permissions,
None,
);
let mut source = Vec::new(); let mut source = Vec::new();
std::io::stdin().read_to_end(&mut source)?; std::io::stdin().read_to_end(&mut source)?;
@ -859,7 +826,7 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
}; };
// Save our fake file into file fetcher cache // Save our fake file into file fetcher cache
// to allow module access by TS compiler // to allow module access by TS compiler
program_state.file_fetcher.insert_cached(source_file); ps.file_fetcher.insert_cached(source_file);
debug!("main_module {}", main_module); debug!("main_module {}", main_module);
worker.execute_main_module(&main_module).await?; worker.execute_main_module(&main_module).await?;
@ -882,21 +849,19 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
let flags = flags.clone(); let flags = flags.clone();
async move { async move {
let main_module = resolve_url_or_path(&script1)?; let main_module = resolve_url_or_path(&script1)?;
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
let handler = Arc::new(Mutex::new(FetchHandler::new( let handler = Arc::new(Mutex::new(FetchHandler::new(
&program_state, &ps,
Permissions::allow_all(), Permissions::allow_all(),
Permissions::allow_all(), Permissions::allow_all(),
)?)); )?));
let mut builder = module_graph::GraphBuilder::new( let mut builder = module_graph::GraphBuilder::new(
handler, handler,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
program_state.lockfile.clone(), ps.lockfile.clone(),
); );
builder.add(&main_module, false).await?; builder.add(&main_module, false).await?;
builder builder.analyze_config_file(&ps.maybe_config_file).await?;
.analyze_config_file(&program_state.maybe_config_file)
.await?;
let module_graph = builder.get_graph(); let module_graph = builder.get_graph();
// Find all local files in graph // Find all local files in graph
@ -906,20 +871,18 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
.filter_map(|specifier| specifier.to_file_path().ok()) .filter_map(|specifier| specifier.to_file_path().ok())
.collect(); .collect();
if let Some(import_map) = program_state.flags.import_map_path.as_ref() { if let Some(import_map) = ps.flags.import_map_path.as_ref() {
paths_to_watch paths_to_watch
.push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?); .push(fs_util::resolve_from_cwd(std::path::Path::new(import_map))?);
} }
Ok((paths_to_watch, main_module, program_state)) Ok((paths_to_watch, main_module, ps))
} }
.map(move |result| match result { .map(move |result| match result {
Ok((paths_to_watch, module_info, program_state)) => { Ok((paths_to_watch, module_info, ps)) => ResolutionResult::Restart {
ResolutionResult::Restart {
paths_to_watch, paths_to_watch,
result: Ok((program_state, module_info)), result: Ok((ps, module_info)),
} },
}
Err(e) => ResolutionResult::Restart { Err(e) => ResolutionResult::Restart {
paths_to_watch: vec![PathBuf::from(script2)], paths_to_watch: vec![PathBuf::from(script2)],
result: Err(e), result: Err(e),
@ -986,15 +949,14 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
} }
} }
let operation = let operation = |(ps, main_module): (ProcState, ModuleSpecifier)| {
|(program_state, main_module): (Arc<ProgramState>, ModuleSpecifier)| {
let flags = flags.clone(); let flags = flags.clone();
let permissions = Permissions::from_options(&flags.into()); let permissions = Permissions::from_options(&flags.into());
async move { async move {
// We make use an module executor guard to ensure that unload is always fired when an // We make use an module executor guard to ensure that unload is always fired when an
// operation is called. // operation is called.
let mut executor = FileWatcherModuleExecutor::new(create_main_worker( let mut executor = FileWatcherModuleExecutor::new(create_main_worker(
&program_state, &ps,
main_module.clone(), main_module.clone(),
permissions, permissions,
None, None,
@ -1023,13 +985,13 @@ async fn run_command(
} }
let main_module = resolve_url_or_path(&run_flags.script)?; let main_module = resolve_url_or_path(&run_flags.script)?;
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let mut worker = let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, None); create_main_worker(&ps, main_module.clone(), permissions, None);
let mut maybe_coverage_collector = let mut maybe_coverage_collector =
if let Some(ref coverage_dir) = program_state.coverage_dir { if let Some(ref coverage_dir) = ps.coverage_dir {
let session = worker.create_inspector_session().await; let session = worker.create_inspector_session().await;
let coverage_dir = PathBuf::from(coverage_dir); let coverage_dir = PathBuf::from(coverage_dir);

View file

@ -1,7 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
use crate::module_graph::TypeLib; use crate::module_graph::TypeLib;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::futures::future::FutureExt; use deno_core::futures::future::FutureExt;
use deno_core::futures::Future; use deno_core::futures::Future;
@ -15,7 +15,6 @@ use std::cell::RefCell;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::str; use std::str;
use std::sync::Arc;
pub struct CliModuleLoader { pub struct CliModuleLoader {
/// When flags contains a `.import_map_path` option, the content of the /// When flags contains a `.import_map_path` option, the content of the
@ -26,32 +25,29 @@ pub struct CliModuleLoader {
/// worker. They are decoupled from the worker (dynamic) permissions since /// worker. They are decoupled from the worker (dynamic) permissions since
/// read access errors must be raised based on the parent thread permissions. /// read access errors must be raised based on the parent thread permissions.
pub root_permissions: Permissions, pub root_permissions: Permissions,
pub program_state: Arc<ProgramState>, pub ps: ProcState,
} }
impl CliModuleLoader { impl CliModuleLoader {
pub fn new(program_state: Arc<ProgramState>) -> Rc<Self> { pub fn new(ps: ProcState) -> Rc<Self> {
let lib = if program_state.flags.unstable { let lib = if ps.flags.unstable {
TypeLib::UnstableDenoWindow TypeLib::UnstableDenoWindow
} else { } else {
TypeLib::DenoWindow TypeLib::DenoWindow
}; };
let import_map = program_state.maybe_import_map.clone(); let import_map = ps.maybe_import_map.clone();
Rc::new(CliModuleLoader { Rc::new(CliModuleLoader {
import_map, import_map,
lib, lib,
root_permissions: Permissions::allow_all(), root_permissions: Permissions::allow_all(),
program_state, ps,
}) })
} }
pub fn new_for_worker( pub fn new_for_worker(ps: ProcState, permissions: Permissions) -> Rc<Self> {
program_state: Arc<ProgramState>, let lib = if ps.flags.unstable {
permissions: Permissions,
) -> Rc<Self> {
let lib = if program_state.flags.unstable {
TypeLib::UnstableDenoWorker TypeLib::UnstableDenoWorker
} else { } else {
TypeLib::DenoWorker TypeLib::DenoWorker
@ -61,7 +57,7 @@ impl CliModuleLoader {
import_map: None, import_map: None,
lib, lib,
root_permissions: permissions, root_permissions: permissions,
program_state, ps,
}) })
} }
} }
@ -75,7 +71,7 @@ impl ModuleLoader for CliModuleLoader {
is_main: bool, is_main: bool,
) -> Result<ModuleSpecifier, AnyError> { ) -> Result<ModuleSpecifier, AnyError> {
// FIXME(bartlomieju): hacky way to provide compatibility with repl // FIXME(bartlomieju): hacky way to provide compatibility with repl
let referrer = if referrer.is_empty() && self.program_state.flags.repl { let referrer = if referrer.is_empty() && self.ps.flags.repl {
deno_core::DUMMY_SPECIFIER deno_core::DUMMY_SPECIFIER
} else { } else {
referrer referrer
@ -102,13 +98,12 @@ impl ModuleLoader for CliModuleLoader {
_is_dynamic: bool, _is_dynamic: bool,
) -> Pin<Box<deno_core::ModuleSourceFuture>> { ) -> Pin<Box<deno_core::ModuleSourceFuture>> {
let module_specifier = module_specifier.clone(); let module_specifier = module_specifier.clone();
let program_state = self.program_state.clone(); let ps = self.ps.clone();
// NOTE: this block is async only because of `deno_core` // NOTE: this block is async only because of `deno_core`
// interface requirements; module was already loaded // interface requirements; module was already loaded
// when constructing module graph during call to `prepare_load`. // when constructing module graph during call to `prepare_load`.
async move { program_state.load(module_specifier, maybe_referrer) } async move { ps.load(module_specifier, maybe_referrer) }.boxed_local()
.boxed_local()
} }
fn prepare_load( fn prepare_load(
@ -120,7 +115,7 @@ impl ModuleLoader for CliModuleLoader {
is_dynamic: bool, is_dynamic: bool,
) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> { ) -> Pin<Box<dyn Future<Output = Result<(), AnyError>>>> {
let specifier = specifier.clone(); let specifier = specifier.clone();
let program_state = self.program_state.clone(); let ps = self.ps.clone();
let maybe_import_map = self.import_map.clone(); let maybe_import_map = self.import_map.clone();
let state = op_state.borrow(); let state = op_state.borrow();
@ -132,8 +127,7 @@ impl ModuleLoader for CliModuleLoader {
// TODO(bartlomieju): `prepare_module_load` should take `load_id` param // TODO(bartlomieju): `prepare_module_load` should take `load_id` param
async move { async move {
program_state ps.prepare_module_load(
.prepare_module_load(
specifier, specifier,
lib, lib,
root_permissions, root_permissions,

View file

@ -2,7 +2,7 @@
use crate::diagnostics::Diagnostics; use crate::diagnostics::Diagnostics;
use crate::fmt_errors::format_file_name; use crate::fmt_errors::format_file_name;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::source_maps::get_orig_position; use crate::source_maps::get_orig_position;
use crate::source_maps::CachedMaps; use crate::source_maps::CachedMaps;
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -12,7 +12,6 @@ use deno_core::serde_json::Value;
use deno_core::OpState; use deno_core::OpState;
use serde::Deserialize; use serde::Deserialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_sync(rt, "op_apply_source_map", op_apply_source_map); super::reg_sync(rt, "op_apply_source_map", op_apply_source_map);
@ -36,7 +35,7 @@ fn op_apply_source_map(
let args: ApplySourceMap = serde_json::from_value(args)?; let args: ApplySourceMap = serde_json::from_value(args)?;
let mut mappings_map: CachedMaps = HashMap::new(); let mut mappings_map: CachedMaps = HashMap::new();
let program_state = state.borrow::<Arc<ProgramState>>().clone(); let ps = state.borrow::<ProcState>().clone();
let (orig_file_name, orig_line_number, orig_column_number, _) = let (orig_file_name, orig_line_number, orig_column_number, _) =
get_orig_position( get_orig_position(
@ -44,7 +43,7 @@ fn op_apply_source_map(
args.line_number.into(), args.line_number.into(),
args.column_number.into(), args.column_number.into(),
&mut mappings_map, &mut mappings_map,
program_state, ps,
); );
Ok(json!({ Ok(json!({

View file

@ -3,7 +3,7 @@
use crate::module_graph::BundleType; use crate::module_graph::BundleType;
use crate::module_graph::EmitOptions; use crate::module_graph::EmitOptions;
use crate::module_graph::GraphBuilder; use crate::module_graph::GraphBuilder;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::specifier_handler::FetchHandler; use crate::specifier_handler::FetchHandler;
use crate::specifier_handler::MemoryHandler; use crate::specifier_handler::MemoryHandler;
use crate::specifier_handler::SpecifierHandler; use crate::specifier_handler::SpecifierHandler;
@ -58,7 +58,7 @@ async fn op_emit(
deno_runtime::ops::check_unstable2(&state, "Deno.emit"); deno_runtime::ops::check_unstable2(&state, "Deno.emit");
let args: EmitArgs = serde_json::from_value(args)?; let args: EmitArgs = serde_json::from_value(args)?;
let root_specifier = args.root_specifier; let root_specifier = args.root_specifier;
let program_state = state.borrow().borrow::<Arc<ProgramState>>().clone(); let ps = state.borrow().borrow::<ProcState>().clone();
let mut runtime_permissions = { let mut runtime_permissions = {
let state = state.borrow(); let state = state.borrow();
state.borrow::<Permissions>().clone() state.borrow::<Permissions>().clone()
@ -71,7 +71,7 @@ async fn op_emit(
Arc::new(Mutex::new(MemoryHandler::new(sources))) Arc::new(Mutex::new(MemoryHandler::new(sources)))
} else { } else {
Arc::new(Mutex::new(FetchHandler::new( Arc::new(Mutex::new(FetchHandler::new(
&program_state, &ps,
runtime_permissions.clone(), runtime_permissions.clone(),
runtime_permissions.clone(), runtime_permissions.clone(),
)?)) )?))
@ -82,7 +82,7 @@ async fn op_emit(
let import_map = if let Some(value) = args.import_map { let import_map = if let Some(value) = args.import_map {
ImportMap::from_json(import_map_specifier.as_str(), &value.to_string())? ImportMap::from_json(import_map_specifier.as_str(), &value.to_string())?
} else { } else {
let file = program_state let file = ps
.file_fetcher .file_fetcher
.fetch(&import_map_specifier, &mut runtime_permissions) .fetch(&import_map_specifier, &mut runtime_permissions)
.await .await
@ -117,7 +117,7 @@ async fn op_emit(
None => BundleType::None, None => BundleType::None,
}; };
let graph = builder.get_graph(); let graph = builder.get_graph();
let debug = program_state.flags.log_level == Some(log::Level::Debug); let debug = ps.flags.log_level == Some(log::Level::Debug);
let graph_errors = graph.get_errors(); let graph_errors = graph.get_errors();
let (files, mut result_info) = graph.emit(EmitOptions { let (files, mut result_info) = graph.emit(EmitOptions {
bundle_type, bundle_type,

View file

@ -41,12 +41,16 @@ use std::collections::HashSet;
use std::env; use std::env;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
/// This structure represents state of single "deno" program. /// This structure represents state of single "deno" program.
/// ///
/// It is shared by all created workers (thus V8 isolates). /// It is shared by all created workers (thus V8 isolates).
pub struct ProgramState { #[derive(Clone)]
pub struct ProcState(Arc<Inner>);
pub struct Inner {
/// Flags parsed from `argv` contents. /// Flags parsed from `argv` contents.
pub flags: flags::Flags, pub flags: flags::Flags,
pub dir: deno_dir::DenoDir, pub dir: deno_dir::DenoDir,
@ -64,8 +68,15 @@ pub struct ProgramState {
pub shared_array_buffer_store: SharedArrayBufferStore, pub shared_array_buffer_store: SharedArrayBufferStore,
} }
impl ProgramState { impl Deref for ProcState {
pub async fn build(flags: flags::Flags) -> Result<Arc<Self>, AnyError> { type Target = Arc<Inner>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ProcState {
pub async fn build(flags: flags::Flags) -> Result<Self, AnyError> {
let maybe_custom_root = flags let maybe_custom_root = flags
.cache_path .cache_path
.clone() .clone()
@ -200,7 +211,7 @@ impl ProgramState {
.clone() .clone()
.or_else(|| env::var("DENO_UNSTABLE_COVERAGE_DIR").ok()); .or_else(|| env::var("DENO_UNSTABLE_COVERAGE_DIR").ok());
let program_state = ProgramState { Ok(ProcState(Arc::new(Inner {
dir, dir,
coverage_dir, coverage_dir,
flags, flags,
@ -214,14 +225,12 @@ impl ProgramState {
blob_store, blob_store,
broadcast_channel, broadcast_channel,
shared_array_buffer_store, shared_array_buffer_store,
}; })))
Ok(Arc::new(program_state))
} }
/// Prepares a set of module specifiers for loading in one shot. /// Prepares a set of module specifiers for loading in one shot.
///
pub async fn prepare_module_graph( pub async fn prepare_module_graph(
self: &Arc<Self>, &self,
specifiers: Vec<ModuleSpecifier>, specifiers: Vec<ModuleSpecifier>,
lib: TypeLib, lib: TypeLib,
root_permissions: Permissions, root_permissions: Permissions,
@ -293,12 +302,11 @@ impl ProgramState {
Ok(()) Ok(())
} }
/// This function is called when new module load is /// This function is called when new module load is initialized by the JsRuntime. Its
/// initialized by the JsRuntime. Its resposibility is to collect /// resposibility is to collect all dependencies and if it is required then also perform TS
/// all dependencies and if it is required then also perform TS typecheck /// typecheck and traspilation.
/// and traspilation.
pub async fn prepare_module_load( pub async fn prepare_module_load(
self: &Arc<Self>, &self,
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
lib: TypeLib, lib: TypeLib,
root_permissions: Permissions, root_permissions: Permissions,
@ -448,7 +456,7 @@ impl ProgramState {
// TODO(@kitsonk) this is only temporary, but should be refactored to somewhere // TODO(@kitsonk) this is only temporary, but should be refactored to somewhere
// else, like a refactored file_fetcher. // else, like a refactored file_fetcher.
impl SourceMapGetter for ProgramState { impl SourceMapGetter for ProcState {
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> { fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
if let Ok(specifier) = resolve_url(file_name) { if let Ok(specifier) = resolve_url(file_name) {
if let Some((code, maybe_map)) = self.get_emit(&specifier) { if let Some((code, maybe_map)) = self.get_emit(&specifier) {

View file

@ -6,9 +6,8 @@ use deno_core::error::JsError;
use sourcemap::SourceMap; use sourcemap::SourceMap;
use std::collections::HashMap; use std::collections::HashMap;
use std::str; use std::str;
use std::sync::Arc;
pub trait SourceMapGetter: Sync + Send { pub trait SourceMapGetter: Sync + Send + Clone {
/// Returns the raw source map file. /// Returns the raw source map file.
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>>; fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>>;
fn get_source_line( fn get_source_line(
@ -27,7 +26,7 @@ pub type CachedMaps = HashMap<String, Option<SourceMap>>;
/// source, rather than the transpiled source code. /// source, rather than the transpiled source code.
pub fn apply_source_map<G: SourceMapGetter>( pub fn apply_source_map<G: SourceMapGetter>(
js_error: &JsError, js_error: &JsError,
getter: Arc<G>, getter: G,
) -> JsError { ) -> JsError {
// Note that js_error.frames has already been source mapped in // Note that js_error.frames has already been source mapped in
// prepareStackTrace(). // prepareStackTrace().
@ -97,7 +96,7 @@ fn get_maybe_orig_position<G: SourceMapGetter>(
line_number: Option<i64>, line_number: Option<i64>,
column_number: Option<i64>, column_number: Option<i64>,
mappings_map: &mut CachedMaps, mappings_map: &mut CachedMaps,
getter: Arc<G>, getter: G,
) -> (Option<String>, Option<i64>, Option<i64>, Option<String>) { ) -> (Option<String>, Option<i64>, Option<i64>, Option<String>) {
match (file_name, line_number, column_number) { match (file_name, line_number, column_number) {
(Some(file_name_v), Some(line_v), Some(column_v)) => { (Some(file_name_v), Some(line_v), Some(column_v)) => {
@ -119,7 +118,7 @@ pub fn get_orig_position<G: SourceMapGetter>(
line_number: i64, line_number: i64,
column_number: i64, column_number: i64,
mappings_map: &mut CachedMaps, mappings_map: &mut CachedMaps,
getter: Arc<G>, getter: G,
) -> (String, i64, i64, Option<String>) { ) -> (String, i64, i64, Option<String>) {
// Lookup expects 0-based line and column numbers, but ours are 1-based. // Lookup expects 0-based line and column numbers, but ours are 1-based.
let line_number = line_number - 1; let line_number = line_number - 1;
@ -173,7 +172,7 @@ pub fn get_orig_position<G: SourceMapGetter>(
fn get_mappings<'a, G: SourceMapGetter>( fn get_mappings<'a, G: SourceMapGetter>(
file_name: &str, file_name: &str,
mappings_map: &'a mut CachedMaps, mappings_map: &'a mut CachedMaps,
getter: Arc<G>, getter: G,
) -> &'a Option<SourceMap> { ) -> &'a Option<SourceMap> {
mappings_map mappings_map
.entry(file_name.to_string()) .entry(file_name.to_string())
@ -184,7 +183,7 @@ fn get_mappings<'a, G: SourceMapGetter>(
// the module meta data. // the module meta data.
fn parse_map_string<G: SourceMapGetter>( fn parse_map_string<G: SourceMapGetter>(
file_name: &str, file_name: &str,
getter: Arc<G>, getter: G,
) -> Option<SourceMap> { ) -> Option<SourceMap> {
getter getter
.get_source_map(file_name) .get_source_map(file_name)
@ -195,6 +194,7 @@ fn parse_map_string<G: SourceMapGetter>(
mod tests { mod tests {
use super::*; use super::*;
#[derive(Clone)]
struct MockSourceMapGetter {} struct MockSourceMapGetter {}
impl SourceMapGetter for MockSourceMapGetter { impl SourceMapGetter for MockSourceMapGetter {
@ -246,7 +246,7 @@ mod tests {
frames: vec![], frames: vec![],
stack: None, stack: None,
}; };
let getter = Arc::new(MockSourceMapGetter {}); let getter = MockSourceMapGetter {};
let actual = apply_source_map(&e, getter); let actual = apply_source_map(&e, getter);
assert_eq!(actual.source_line, Some("console.log('foo');".to_string())); assert_eq!(actual.source_line, Some("console.log('foo');".to_string()));
} }

View file

@ -3,7 +3,7 @@
use crate::ast::Location; use crate::ast::Location;
use crate::disk_cache::DiskCache; use crate::disk_cache::DiskCache;
use crate::file_fetcher::FileFetcher; use crate::file_fetcher::FileFetcher;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_core::error::custom_error; use deno_core::error::custom_error;
@ -219,18 +219,18 @@ pub struct FetchHandler {
root_permissions: Permissions, root_permissions: Permissions,
/// The set of permissions which are used for dynamic imports. /// The set of permissions which are used for dynamic imports.
dynamic_permissions: Permissions, dynamic_permissions: Permissions,
/// A clone of the `program_state` file fetcher. /// A clone of the `ps` file fetcher.
file_fetcher: FileFetcher, file_fetcher: FileFetcher,
} }
impl FetchHandler { impl FetchHandler {
pub fn new( pub fn new(
program_state: &Arc<ProgramState>, ps: &ProcState,
root_permissions: Permissions, root_permissions: Permissions,
dynamic_permissions: Permissions, dynamic_permissions: Permissions,
) -> Result<Self, AnyError> { ) -> Result<Self, AnyError> {
let disk_cache = program_state.dir.gen_cache.clone(); let disk_cache = ps.dir.gen_cache.clone();
let file_fetcher = program_state.file_fetcher.clone(); let file_fetcher = ps.file_fetcher.clone();
Ok(FetchHandler { Ok(FetchHandler {
disk_cache, disk_cache,

View file

@ -4,7 +4,7 @@ use crate::colors;
use crate::file_fetcher::get_source_from_data_url; use crate::file_fetcher::get_source_from_data_url;
use crate::flags::Flags; use crate::flags::Flags;
use crate::ops; use crate::ops;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::version; use crate::version;
use deno_core::error::anyhow; use deno_core::error::anyhow;
use deno_core::error::type_error; use deno_core::error::type_error;
@ -202,7 +202,7 @@ pub async fn run(
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let flags = metadata_to_flags(&metadata); let flags = metadata_to_flags(&metadata);
let main_module = resolve_url(SPECIFIER)?; let main_module = resolve_url(SPECIFIER)?;
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
let permissions = Permissions::from_options(&metadata.permissions); let permissions = Permissions::from_options(&metadata.permissions);
let blob_store = BlobStore::default(); let blob_store = BlobStore::default();
let broadcast_channel = InMemoryBroadcastChannel::default(); let broadcast_channel = InMemoryBroadcastChannel::default();
@ -218,7 +218,7 @@ pub async fn run(
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
); );
let mut root_cert_store = program_state let mut root_cert_store = ps
.root_cert_store .root_cert_store
.clone() .clone()
.unwrap_or_else(create_default_root_cert_store); .unwrap_or_else(create_default_root_cert_store);
@ -265,7 +265,7 @@ pub async fn run(
js_runtime js_runtime
.op_state() .op_state()
.borrow_mut() .borrow_mut()
.put::<Arc<ProgramState>>(program_state.clone()); .put::<ProcState>(ps.clone());
ops::errors::init(js_runtime); ops::errors::init(js_runtime);
ops::runtime_compiler::init(js_runtime); ops::runtime_compiler::init(js_runtime);
js_runtime.sync_ops_cache(); js_runtime.sync_ops_cache();

View file

@ -4,7 +4,7 @@ use crate::colors;
use crate::flags::Flags; use crate::flags::Flags;
use crate::fs_util::collect_files; use crate::fs_util::collect_files;
use crate::module_graph::TypeLib; use crate::module_graph::TypeLib;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::source_maps::SourceMapGetter; use crate::source_maps::SourceMapGetter;
use deno_ast::swc::common::Span; use deno_ast::swc::common::Span;
use deno_ast::MediaType; use deno_ast::MediaType;
@ -670,7 +670,7 @@ pub async fn cover_files(
exclude: Vec<String>, exclude: Vec<String>,
lcov: bool, lcov: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags).await?; let ps = ProcState::build(flags).await?;
let script_coverages = collect_coverages(files, ignore)?; let script_coverages = collect_coverages(files, ignore)?;
let script_coverages = filter_coverages(script_coverages, include, exclude); let script_coverages = filter_coverages(script_coverages, include, exclude);
@ -686,22 +686,21 @@ pub async fn cover_files(
for script_coverage in script_coverages { for script_coverage in script_coverages {
let module_specifier = let module_specifier =
deno_core::resolve_url_or_path(&script_coverage.url)?; deno_core::resolve_url_or_path(&script_coverage.url)?;
program_state ps.prepare_module_load(
.prepare_module_load(
module_specifier.clone(), module_specifier.clone(),
TypeLib::UnstableDenoWindow, TypeLib::UnstableDenoWindow,
Permissions::allow_all(), Permissions::allow_all(),
Permissions::allow_all(), Permissions::allow_all(),
false, false,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
) )
.await?; .await?;
let module_source = program_state.load(module_specifier.clone(), None)?; let module_source = ps.load(module_specifier.clone(), None)?;
let script_source = &module_source.code; let script_source = &module_source.code;
let maybe_source_map = program_state.get_source_map(&script_coverage.url); let maybe_source_map = ps.get_source_map(&script_coverage.url);
let maybe_cached_source = program_state let maybe_cached_source = ps
.file_fetcher .file_fetcher
.get_source(&module_specifier) .get_source(&module_specifier)
.map(|f| f.source); .map(|f| f.source);

View file

@ -4,7 +4,7 @@ use crate::colors;
use crate::file_fetcher::File; use crate::file_fetcher::File;
use crate::flags::Flags; use crate::flags::Flags;
use crate::get_types; use crate::get_types;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::write_json_to_stdout; use crate::write_json_to_stdout;
use crate::write_to_stdout_ignore_sigpipe; use crate::write_to_stdout_ignore_sigpipe;
use deno_ast::MediaType; use deno_ast::MediaType;
@ -61,7 +61,7 @@ impl Resolver for DocResolver {
} }
struct DocLoader { struct DocLoader {
program_state: Arc<ProgramState>, ps: ProcState,
} }
impl Loader for DocLoader { impl Loader for DocLoader {
@ -71,9 +71,9 @@ impl Loader for DocLoader {
_is_dynamic: bool, _is_dynamic: bool,
) -> LoadFuture { ) -> LoadFuture {
let specifier = specifier.clone(); let specifier = specifier.clone();
let program_state = self.program_state.clone(); let ps = self.ps.clone();
async move { async move {
let result = program_state let result = ps
.file_fetcher .file_fetcher
.fetch(&specifier, &mut Permissions::allow_all()) .fetch(&specifier, &mut Permissions::allow_all())
.await .await
@ -97,7 +97,7 @@ pub async fn print_docs(
maybe_filter: Option<String>, maybe_filter: Option<String>,
private: bool, private: bool,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let source_file = source_file.unwrap_or_else(|| "--builtin".to_string()); let source_file = source_file.unwrap_or_else(|| "--builtin".to_string());
let source_parser = deno_graph::DefaultSourceParser::new(); let source_parser = deno_graph::DefaultSourceParser::new();
@ -135,13 +135,11 @@ pub async fn print_docs(
}; };
// Save our fake file into file fetcher cache. // Save our fake file into file fetcher cache.
program_state.file_fetcher.insert_cached(root); ps.file_fetcher.insert_cached(root);
let mut loader = DocLoader { let mut loader = DocLoader { ps: ps.clone() };
program_state: program_state.clone(),
};
let resolver = DocResolver { let resolver = DocResolver {
import_map: program_state.maybe_import_map.clone(), import_map: ps.maybe_import_map.clone(),
}; };
let graph = create_graph( let graph = create_graph(
root_specifier.clone(), root_specifier.clone(),

View file

@ -3,7 +3,7 @@
use crate::ast::transpile; use crate::ast::transpile;
use crate::ast::ImportsNotUsedAsValues; use crate::ast::ImportsNotUsedAsValues;
use crate::colors; use crate::colors;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use deno_ast::swc::parser::error::SyntaxError; use deno_ast::swc::parser::error::SyntaxError;
use deno_ast::swc::parser::token::{Token, Word}; use deno_ast::swc::parser::token::{Token, Word};
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -731,7 +731,7 @@ async fn read_line_and_poll(
} }
pub async fn run( pub async fn run(
program_state: &ProgramState, ps: &ProcState,
worker: MainWorker, worker: MainWorker,
maybe_eval: Option<String>, maybe_eval: Option<String>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
@ -745,7 +745,7 @@ pub async fn run(
response_rx: RefCell::new(response_rx), response_rx: RefCell::new(response_rx),
}; };
let history_file_path = program_state.dir.root.join("deno_history.txt"); let history_file_path = ps.dir.root.join("deno_history.txt");
let editor = ReplEditor::new(helper, history_file_path); let editor = ReplEditor::new(helper, history_file_path);
if let Some(eval) = maybe_eval { if let Some(eval) = maybe_eval {

View file

@ -16,7 +16,7 @@ use crate::module_graph::GraphBuilder;
use crate::module_graph::Module; use crate::module_graph::Module;
use crate::module_graph::TypeLib; use crate::module_graph::TypeLib;
use crate::ops; use crate::ops;
use crate::program_state::ProgramState; use crate::proc_state::ProcState;
use crate::tokio_util; use crate::tokio_util;
use crate::tools::coverage::CoverageCollector; use crate::tools::coverage::CoverageCollector;
use crate::FetchHandler; use crate::FetchHandler;
@ -247,7 +247,7 @@ fn create_reporter(
/// Test a single specifier as documentation containing test programs, an executable test module or /// Test a single specifier as documentation containing test programs, an executable test module or
/// both. /// both.
async fn test_specifier( async fn test_specifier(
program_state: Arc<ProgramState>, ps: ProcState,
permissions: Permissions, permissions: Permissions,
specifier: ModuleSpecifier, specifier: ModuleSpecifier,
mode: TestMode, mode: TestMode,
@ -264,15 +264,11 @@ async fn test_specifier(
.put::<Sender<TestEvent>>(channel.clone()); .put::<Sender<TestEvent>>(channel.clone());
}; };
let mut worker = create_main_worker( let mut worker =
&program_state, create_main_worker(&ps, specifier.clone(), permissions, Some(&init_ops));
specifier.clone(),
permissions,
Some(&init_ops),
);
let mut maybe_coverage_collector = if let Some(ref coverage_dir) = let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
program_state.coverage_dir ps.coverage_dir
{ {
let session = worker.create_inspector_session().await; let session = worker.create_inspector_session().await;
let coverage_dir = PathBuf::from(coverage_dir); let coverage_dir = PathBuf::from(coverage_dir);
@ -468,13 +464,13 @@ fn extract_files_from_fenced_blocks(
} }
async fn fetch_inline_files( async fn fetch_inline_files(
program_state: Arc<ProgramState>, ps: ProcState,
specifiers: Vec<ModuleSpecifier>, specifiers: Vec<ModuleSpecifier>,
) -> Result<Vec<File>, AnyError> { ) -> Result<Vec<File>, AnyError> {
let mut files = Vec::new(); let mut files = Vec::new();
for specifier in specifiers { for specifier in specifiers {
let mut fetch_permissions = Permissions::allow_all(); let mut fetch_permissions = Permissions::allow_all();
let file = program_state let file = ps
.file_fetcher .file_fetcher
.fetch(&specifier, &mut fetch_permissions) .fetch(&specifier, &mut fetch_permissions)
.await?; .await?;
@ -501,13 +497,13 @@ async fn fetch_inline_files(
/// Type check a collection of module and document specifiers. /// Type check a collection of module and document specifiers.
async fn check_specifiers( async fn check_specifiers(
program_state: Arc<ProgramState>, ps: ProcState,
permissions: Permissions, permissions: Permissions,
specifiers: Vec<(ModuleSpecifier, TestMode)>, specifiers: Vec<(ModuleSpecifier, TestMode)>,
lib: TypeLib, lib: TypeLib,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let inline_files = fetch_inline_files( let inline_files = fetch_inline_files(
program_state.clone(), ps.clone(),
specifiers specifiers
.iter() .iter()
.filter_map(|(specifier, mode)| { .filter_map(|(specifier, mode)| {
@ -528,16 +524,15 @@ async fn check_specifiers(
.collect(); .collect();
for file in inline_files { for file in inline_files {
program_state.file_fetcher.insert_cached(file); ps.file_fetcher.insert_cached(file);
} }
program_state ps.prepare_module_graph(
.prepare_module_graph(
specifiers, specifiers,
lib.clone(), lib.clone(),
Permissions::allow_all(), Permissions::allow_all(),
permissions.clone(), permissions.clone(),
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
) )
.await?; .await?;
} }
@ -553,13 +548,12 @@ async fn check_specifiers(
}) })
.collect(); .collect();
program_state ps.prepare_module_graph(
.prepare_module_graph(
module_specifiers, module_specifiers,
lib, lib,
Permissions::allow_all(), Permissions::allow_all(),
permissions, permissions,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
) )
.await?; .await?;
@ -568,7 +562,7 @@ async fn check_specifiers(
/// Test a collection of specifiers with test modes concurrently. /// Test a collection of specifiers with test modes concurrently.
async fn test_specifiers( async fn test_specifiers(
program_state: Arc<ProgramState>, ps: ProcState,
permissions: Permissions, permissions: Permissions,
specifiers_with_mode: Vec<(ModuleSpecifier, TestMode)>, specifiers_with_mode: Vec<(ModuleSpecifier, TestMode)>,
fail_fast: Option<NonZeroUsize>, fail_fast: Option<NonZeroUsize>,
@ -576,7 +570,7 @@ async fn test_specifiers(
shuffle: Option<u64>, shuffle: Option<u64>,
concurrent_jobs: NonZeroUsize, concurrent_jobs: NonZeroUsize,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let log_level = program_state.flags.log_level; let log_level = ps.flags.log_level;
let specifiers_with_mode = if let Some(seed) = shuffle { let specifiers_with_mode = if let Some(seed) = shuffle {
let mut rng = SmallRng::seed_from_u64(seed); let mut rng = SmallRng::seed_from_u64(seed);
let mut specifiers_with_mode = specifiers_with_mode.clone(); let mut specifiers_with_mode = specifiers_with_mode.clone();
@ -591,7 +585,7 @@ async fn test_specifiers(
let join_handles = let join_handles =
specifiers_with_mode.iter().map(move |(specifier, mode)| { specifiers_with_mode.iter().map(move |(specifier, mode)| {
let program_state = program_state.clone(); let ps = ps.clone();
let permissions = permissions.clone(); let permissions = permissions.clone();
let specifier = specifier.clone(); let specifier = specifier.clone();
let mode = mode.clone(); let mode = mode.clone();
@ -601,7 +595,7 @@ async fn test_specifiers(
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
let join_handle = std::thread::spawn(move || { let join_handle = std::thread::spawn(move || {
let future = test_specifier( let future = test_specifier(
program_state, ps,
permissions, permissions,
specifier, specifier,
mode, mode,
@ -755,7 +749,7 @@ fn collect_specifiers_with_test_mode(
/// Specifiers that do not have a known media type that can be executed as a module are marked as /// Specifiers that do not have a known media type that can be executed as a module are marked as
/// `TestMode::Documentation`. /// `TestMode::Documentation`.
async fn fetch_specifiers_with_test_mode( async fn fetch_specifiers_with_test_mode(
program_state: Arc<ProgramState>, ps: ProcState,
include: Vec<String>, include: Vec<String>,
ignore: Vec<PathBuf>, ignore: Vec<PathBuf>,
include_inline: bool, include_inline: bool,
@ -763,7 +757,7 @@ async fn fetch_specifiers_with_test_mode(
let mut specifiers_with_mode = let mut specifiers_with_mode =
collect_specifiers_with_test_mode(include, ignore, include_inline)?; collect_specifiers_with_test_mode(include, ignore, include_inline)?;
for (specifier, mode) in &mut specifiers_with_mode { for (specifier, mode) in &mut specifiers_with_mode {
let file = program_state let file = ps
.file_fetcher .file_fetcher
.fetch(specifier, &mut Permissions::allow_all()) .fetch(specifier, &mut Permissions::allow_all())
.await?; .await?;
@ -789,10 +783,10 @@ pub async fn run_tests(
shuffle: Option<u64>, shuffle: Option<u64>,
concurrent_jobs: NonZeroUsize, concurrent_jobs: NonZeroUsize,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let specifiers_with_mode = fetch_specifiers_with_test_mode( let specifiers_with_mode = fetch_specifiers_with_test_mode(
program_state.clone(), ps.clone(),
include.unwrap_or_else(|| vec![".".to_string()]), include.unwrap_or_else(|| vec![".".to_string()]),
ignore.clone(), ignore.clone(),
doc, doc,
@ -810,7 +804,7 @@ pub async fn run_tests(
}; };
check_specifiers( check_specifiers(
program_state.clone(), ps.clone(),
permissions.clone(), permissions.clone(),
specifiers_with_mode.clone(), specifiers_with_mode.clone(),
lib, lib,
@ -822,7 +816,7 @@ pub async fn run_tests(
} }
test_specifiers( test_specifiers(
program_state, ps,
permissions, permissions,
specifiers_with_mode, specifiers_with_mode,
fail_fast, fail_fast,
@ -847,7 +841,7 @@ pub async fn run_tests_with_watch(
shuffle: Option<u64>, shuffle: Option<u64>,
concurrent_jobs: NonZeroUsize, concurrent_jobs: NonZeroUsize,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let lib = if flags.unstable { let lib = if flags.unstable {
@ -857,7 +851,7 @@ pub async fn run_tests_with_watch(
}; };
let handler = Arc::new(Mutex::new(FetchHandler::new( let handler = Arc::new(Mutex::new(FetchHandler::new(
&program_state, &ps,
Permissions::allow_all(), Permissions::allow_all(),
Permissions::allow_all(), Permissions::allow_all(),
)?)); )?));
@ -870,7 +864,7 @@ pub async fn run_tests_with_watch(
let paths_to_watch_clone = paths_to_watch.clone(); let paths_to_watch_clone = paths_to_watch.clone();
let handler = handler.clone(); let handler = handler.clone();
let program_state = program_state.clone(); let ps = ps.clone();
let files_changed = changed.is_some(); let files_changed = changed.is_some();
let include = include.clone(); let include = include.clone();
let ignore = ignore.clone(); let ignore = ignore.clone();
@ -894,15 +888,13 @@ pub async fn run_tests_with_watch(
let mut builder = GraphBuilder::new( let mut builder = GraphBuilder::new(
handler, handler,
program_state.maybe_import_map.clone(), ps.maybe_import_map.clone(),
program_state.lockfile.clone(), ps.lockfile.clone(),
); );
for specifier in test_modules.iter() { for specifier in test_modules.iter() {
builder.add(specifier, false).await?; builder.add(specifier, false).await?;
} }
builder builder.analyze_config_file(&ps.maybe_config_file).await?;
.analyze_config_file(&program_state.maybe_config_file)
.await?;
let graph = builder.get_graph(); let graph = builder.get_graph();
for specifier in test_modules { for specifier in test_modules {
@ -998,11 +990,11 @@ pub async fn run_tests_with_watch(
let ignore = ignore.clone(); let ignore = ignore.clone();
let lib = lib.clone(); let lib = lib.clone();
let permissions = permissions.clone(); let permissions = permissions.clone();
let program_state = program_state.clone(); let ps = ps.clone();
async move { async move {
let specifiers_with_mode = fetch_specifiers_with_test_mode( let specifiers_with_mode = fetch_specifiers_with_test_mode(
program_state.clone(), ps.clone(),
include.clone(), include.clone(),
ignore.clone(), ignore.clone(),
doc, doc,
@ -1014,7 +1006,7 @@ pub async fn run_tests_with_watch(
.collect::<Vec<(ModuleSpecifier, TestMode)>>(); .collect::<Vec<(ModuleSpecifier, TestMode)>>();
check_specifiers( check_specifiers(
program_state.clone(), ps.clone(),
permissions.clone(), permissions.clone(),
specifiers_with_mode.clone(), specifiers_with_mode.clone(),
lib, lib,
@ -1026,7 +1018,7 @@ pub async fn run_tests_with_watch(
} }
test_specifiers( test_specifiers(
program_state.clone(), ps.clone(),
permissions.clone(), permissions.clone(),
specifiers_with_mode, specifiers_with_mode,
fail_fast, fail_fast,