Skip to content

Commit

Permalink
Move create_main_worker to MainWorker::create (#6034)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry authored Jun 2, 2020
1 parent 40419c6 commit 8b1b476
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 43 deletions.
49 changes: 7 additions & 42 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ use crate::fs as deno_fs;
use crate::global_state::GlobalState;
use crate::msg::MediaType;
use crate::op_error::OpError;
use crate::ops::io::get_stdio;
use crate::permissions::Permissions;
use crate::state::State;
use crate::tsc::TargetLib;
use crate::worker::MainWorker;
use deno_core::v8_set_flags;
use deno_core::CoreIsolate;
use deno_core::ErrBox;
use deno_core::EsIsolate;
use deno_core::ModuleSpecifier;
Expand Down Expand Up @@ -152,38 +149,6 @@ fn write_lockfile(global_state: GlobalState) -> Result<(), std::io::Error> {
Ok(())
}

fn create_main_worker(
global_state: GlobalState,
main_module: ModuleSpecifier,
) -> Result<MainWorker, ErrBox> {
let state = State::new(
global_state.clone(),
None,
main_module,
global_state.maybe_import_map.clone(),
false,
)?;

let mut worker = MainWorker::new(
"main".to_string(),
startup_data::deno_isolate_init(),
state,
);

{
let (stdin, stdout, stderr) = get_stdio();
let state_rc = CoreIsolate::state(&worker.isolate);
let state = state_rc.borrow();
let mut t = state.resource_table.borrow_mut();
t.add("stdin", Box::new(stdin));
t.add("stdout", Box::new(stdout));
t.add("stderr", Box::new(stderr));
}

worker.execute("bootstrap.mainRuntime()")?;
Ok(worker)
}

fn print_cache_info(state: &GlobalState) {
println!(
"{} {:?}",
Expand Down Expand Up @@ -323,7 +288,7 @@ async fn info_command(
}

let main_module = ModuleSpecifier::resolve_url_or_path(&file.unwrap())?;
let mut worker = create_main_worker(global_state, main_module.clone())?;
let mut worker = MainWorker::create(global_state, main_module.clone())?;
worker.preload_module(&main_module).await?;
print_file_info(&worker, main_module.clone()).await
}
Expand All @@ -341,7 +306,7 @@ async fn install_command(
fetch_flags.reload = true;
let global_state = GlobalState::new(fetch_flags)?;
let main_module = ModuleSpecifier::resolve_url_or_path(&module_url)?;
let mut worker = create_main_worker(global_state, main_module.clone())?;
let mut worker = MainWorker::create(global_state, main_module.clone())?;
worker.preload_module(&main_module).await?;
installer::install(flags, &module_url, args, name, root, force)
.map_err(ErrBox::from)
Expand All @@ -352,7 +317,7 @@ async fn cache_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> {
ModuleSpecifier::resolve_url_or_path("./__$deno$fetch.ts").unwrap();
let global_state = GlobalState::new(flags)?;
let mut worker =
create_main_worker(global_state.clone(), main_module.clone())?;
MainWorker::create(global_state.clone(), main_module.clone())?;

for file in files {
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
Expand All @@ -373,7 +338,7 @@ async fn eval_command(
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$eval.ts").unwrap();
let global_state = GlobalState::new(flags)?;
let mut worker = create_main_worker(global_state, main_module.clone())?;
let mut worker = MainWorker::create(global_state, main_module.clone())?;
let main_module_url = main_module.as_url().to_owned();
// Create a dummy source file.
let source_file = SourceFile {
Expand Down Expand Up @@ -559,7 +524,7 @@ async fn run_repl(flags: Flags) -> Result<(), ErrBox> {
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$repl.ts").unwrap();
let global_state = GlobalState::new(flags)?;
let mut worker = create_main_worker(global_state, main_module)?;
let mut worker = MainWorker::create(global_state, main_module)?;
loop {
(&mut *worker).await?;
}
Expand All @@ -569,7 +534,7 @@ async fn run_command(flags: Flags, script: String) -> Result<(), ErrBox> {
let global_state = GlobalState::new(flags.clone())?;
let main_module = ModuleSpecifier::resolve_url_or_path(&script).unwrap();
let mut worker =
create_main_worker(global_state.clone(), main_module.clone())?;
MainWorker::create(global_state.clone(), main_module.clone())?;
debug!("main_module {}", main_module);
worker.execute_module(&main_module).await?;
write_lockfile(global_state)?;
Expand Down Expand Up @@ -608,7 +573,7 @@ async fn test_command(
let main_module =
ModuleSpecifier::resolve_url(&test_file_url.to_string()).unwrap();
let mut worker =
create_main_worker(global_state.clone(), main_module.clone())?;
MainWorker::create(global_state.clone(), main_module.clone())?;
// Create a dummy source file.
let source_file = SourceFile {
filename: test_file_url.to_file_path().unwrap(),
Expand Down
35 changes: 34 additions & 1 deletion cli/worker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::fmt_errors::JSError;
use crate::global_state::GlobalState;
use crate::inspector::DenoInspector;
use crate::ops;
use crate::ops::io::get_stdio;
use crate::startup_data;
use crate::state::State;
use deno_core::Buf;
use deno_core::CoreIsolate;
Expand Down Expand Up @@ -246,7 +249,8 @@ impl DerefMut for Worker {
pub struct MainWorker(Worker);

impl MainWorker {
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
// TODO(ry) combine MainWorker::new and MainWorker::create.
fn new(name: String, startup_data: StartupData, state: State) -> Self {
let state_ = state.clone();
let mut worker = Worker::new(name, startup_data, state_);
{
Expand Down Expand Up @@ -274,6 +278,35 @@ impl MainWorker {
}
Self(worker)
}

pub fn create(
global_state: GlobalState,
main_module: ModuleSpecifier,
) -> Result<MainWorker, ErrBox> {
let state = State::new(
global_state.clone(),
None,
main_module,
global_state.maybe_import_map.clone(),
false,
)?;
let mut worker = MainWorker::new(
"main".to_string(),
startup_data::deno_isolate_init(),
state,
);
{
let (stdin, stdout, stderr) = get_stdio();
let state_rc = CoreIsolate::state(&worker.isolate);
let state = state_rc.borrow();
let mut t = state.resource_table.borrow_mut();
t.add("stdin", Box::new(stdin));
t.add("stdout", Box::new(stdout));
t.add("stderr", Box::new(stderr));
}
worker.execute("bootstrap.mainRuntime()")?;
Ok(worker)
}
}

impl Deref for MainWorker {
Expand Down

0 comments on commit 8b1b476

Please sign in to comment.