Skip to content

Commit

Permalink
refactor(runtime): Worker bootstrap options (denoland#12299)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO committed Oct 5, 2021
1 parent 77a00ce commit 678a881
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 157 deletions.
61 changes: 38 additions & 23 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use deno_runtime::web_worker::WebWorker;
use deno_runtime::web_worker::WebWorkerOptions;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions;
use log::debug;
use log::info;
use std::env;
Expand Down Expand Up @@ -111,11 +112,21 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
let create_web_worker_cb = create_web_worker_callback(ps.clone());

let options = WebWorkerOptions {
args: ps.flags.argv.clone(),
apply_source_maps: true,
debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
unstable: ps.flags.unstable,
enable_testing_features: ps.flags.enable_testing_features,
bootstrap: BootstrapOptions {
args: ps.flags.argv.clone(),
apply_source_maps: true,
cpu_count: num_cpus::get(),
debug_flag: ps
.flags
.log_level
.map_or(false, |l| l == log::Level::Debug),
enable_testing_features: ps.flags.enable_testing_features,
location: Some(args.main_module.clone()),
no_color: !colors::use_color(),
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
unstable: ps.flags.unstable,
},
unsafely_ignore_certificate_errors: ps
.flags
.unsafely_ignore_certificate_errors
Expand All @@ -129,25 +140,26 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
use_deno_namespace: args.use_deno_namespace,
worker_type: args.worker_type,
maybe_inspector_server,
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
no_color: !colors::use_color(),
get_error_class_fn: Some(&crate::errors::get_error_class_name),
blob_store: ps.blob_store.clone(),
broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
cpu_count: num_cpus::get(),
};
let bootstrap_options = options.bootstrap.clone();

// TODO(@AaronO): switch to bootstrap_from_options() once ops below are an extension
// since it uses sync_ops_cache() which currently depends on the Deno namespace
// which can be nuked when bootstrapping workers (use_deno_namespace: false)
let (mut worker, external_handle) = WebWorker::from_options(
args.name,
args.permissions,
args.main_module,
args.worker_id,
&options,
options,
);

// TODO(@AaronO): move to a JsRuntime Extension passed into options
// This block registers additional ops and state that
// are only available in the CLI
{
Expand All @@ -164,7 +176,7 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
}
js_runtime.sync_ops_cache();
}
worker.bootstrap(&options);
worker.bootstrap(&bootstrap_options);

(worker, external_handle)
})
Expand Down Expand Up @@ -192,11 +204,18 @@ pub fn create_main_worker(
let create_web_worker_cb = create_web_worker_callback(ps.clone());

let options = WorkerOptions {
apply_source_maps: true,
args: ps.flags.argv.clone(),
debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
unstable: ps.flags.unstable,
enable_testing_features: ps.flags.enable_testing_features,
bootstrap: BootstrapOptions {
apply_source_maps: true,
args: ps.flags.argv.clone(),
cpu_count: num_cpus::get(),
debug_flag: ps.flags.log_level.map_or(false, |l| l == log::Level::Debug),
enable_testing_features: ps.flags.enable_testing_features,
location: ps.flags.location.clone(),
no_color: !colors::use_color(),
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
unstable: ps.flags.unstable,
},
unsafely_ignore_certificate_errors: ps
.flags
.unsafely_ignore_certificate_errors
Expand All @@ -209,11 +228,7 @@ pub fn create_main_worker(
maybe_inspector_server,
should_break_on_first_statement,
module_loader,
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
no_color: !colors::use_color(),
get_error_class_fn: Some(&crate::errors::get_error_class_name),
location: ps.flags.location.clone(),
origin_storage_dir: ps.flags.location.clone().map(|loc| {
ps.dir
.root
Expand All @@ -226,11 +241,12 @@ pub fn create_main_worker(
broadcast_channel: ps.broadcast_channel.clone(),
shared_array_buffer_store: Some(ps.shared_array_buffer_store.clone()),
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
cpu_count: num_cpus::get(),
};

let mut worker = MainWorker::from_options(main_module, permissions, &options);
let mut worker =
MainWorker::bootstrap_from_options(main_module, permissions, options);

// TODO(@AaronO): move to a JsRuntime Extension passed into options
// This block registers additional ops and state that
// are only available in the CLI
{
Expand All @@ -250,7 +266,6 @@ pub fn create_main_worker(

js_runtime.sync_ops_cache();
}
worker.bootstrap(&options);

worker
}
Expand Down
32 changes: 19 additions & 13 deletions cli/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsOptions;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions;
use deno_tls::create_default_root_cert_store;
use log::Level;
use std::convert::TryInto;
Expand Down Expand Up @@ -228,12 +229,19 @@ pub async fn run(
}

let options = WorkerOptions {
apply_source_maps: false,
args: metadata.argv,
debug_flag: metadata.log_level.map_or(false, |l| l == log::Level::Debug),
bootstrap: BootstrapOptions {
apply_source_maps: false,
args: metadata.argv,
cpu_count: num_cpus::get(),
debug_flag: metadata.log_level.map_or(false, |l| l == log::Level::Debug),
enable_testing_features: false,
location: metadata.location,
no_color: !colors::use_color(),
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
unstable: metadata.unstable,
},
user_agent: version::get_user_agent(),
unstable: metadata.unstable,
enable_testing_features: false,
unsafely_ignore_certificate_errors: metadata
.unsafely_ignore_certificate_errors,
root_cert_store: Some(root_cert_store),
Expand All @@ -243,20 +251,19 @@ pub async fn run(
maybe_inspector_server: None,
should_break_on_first_statement: false,
module_loader,
runtime_version: version::deno(),
ts_version: version::TYPESCRIPT.to_string(),
no_color: !colors::use_color(),
get_error_class_fn: Some(&get_error_class_name),
location: metadata.location,
origin_storage_dir: None,
blob_store,
broadcast_channel,
shared_array_buffer_store: None,
compiled_wasm_module_store: None,
cpu_count: num_cpus::get(),
};
let mut worker =
MainWorker::from_options(main_module.clone(), permissions, &options);
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
permissions,
options,
);
// TODO(@AaronO): move to a JsRuntime Extension passed into options
{
let js_runtime = &mut worker.js_runtime;
js_runtime
Expand All @@ -267,7 +274,6 @@ pub async fn run(
ops::runtime_compiler::init(js_runtime);
js_runtime.sync_ops_cache();
}
worker.bootstrap(&options);
worker.execute_main_module(&main_module).await?;
worker.execute_script(
&located_script_name!(),
Expand Down
31 changes: 18 additions & 13 deletions runtime/examples/hello_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use deno_runtime::deno_web::BlobStore;
use deno_runtime::permissions::Permissions;
use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
Expand All @@ -23,11 +24,18 @@ async fn main() -> Result<(), AnyError> {
});

let options = WorkerOptions {
apply_source_maps: false,
args: vec![],
debug_flag: false,
unstable: false,
enable_testing_features: false,
bootstrap: BootstrapOptions {
apply_source_maps: false,
args: vec![],
cpu_count: 1,
debug_flag: false,
enable_testing_features: false,
location: None,
no_color: false,
runtime_version: "x".to_string(),
ts_version: "x".to_string(),
unstable: false,
},
unsafely_ignore_certificate_errors: None,
root_cert_store: None,
user_agent: "hello_runtime".to_string(),
Expand All @@ -37,27 +45,24 @@ async fn main() -> Result<(), AnyError> {
maybe_inspector_server: None,
should_break_on_first_statement: false,
module_loader,
runtime_version: "x".to_string(),
ts_version: "x".to_string(),
no_color: false,
get_error_class_fn: Some(&get_error_class_name),
location: None,
origin_storage_dir: None,
blob_store: BlobStore::default(),
broadcast_channel: InMemoryBroadcastChannel::default(),
shared_array_buffer_store: None,
compiled_wasm_module_store: None,
cpu_count: 1,
};

let js_path =
Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/hello_runtime.js");
let main_module = deno_core::resolve_path(&js_path.to_string_lossy())?;
let permissions = Permissions::allow_all();

let mut worker =
MainWorker::from_options(main_module.clone(), permissions, &options);
worker.bootstrap(&options);
let mut worker = MainWorker::bootstrap_from_options(
main_module.clone(),
permissions,
options,
);
worker.execute_main_module(&main_module).await?;
worker.run_event_loop(false).await?;
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions runtime/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ pub mod permissions;
pub mod tokio_util;
pub mod web_worker;
pub mod worker;

mod worker_bootstrap;
pub use worker_bootstrap::BootstrapOptions;
Loading

0 comments on commit 678a881

Please sign in to comment.