From 6d2656fd56e8ac0f1b1443e28d474bf3ceca89bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20D=C3=BCrr?= <102963075+cd-work@users.noreply.github.com> Date: Sat, 15 Oct 2022 21:19:03 +0000 Subject: [PATCH] refactor: Add default implementation for WorkerOptions (#14860) This adds an implementation of `Default` for both `WorkerOptions` and `BootstrapOptions`. Since both of these structs are rather big, this should make it easier for people unfamiliar with the internals to focus on the options relevant to them. As a user of `deno_runtime` I feel like these should serve as good defaults, getting people them started without having to tweak the runtime. Additionally even if some changes are made, the usage of `..Default::default()` will significantly help with code clarity and verbosity. --- runtime/worker.rs | 38 ++++++++++++++++++++++++++++++++++++- runtime/worker_bootstrap.rs | 33 +++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/runtime/worker.rs b/runtime/worker.rs index 9b27559391..36833da329 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -18,6 +18,7 @@ use deno_core::serde_v8; use deno_core::v8; use deno_core::CompiledWasmModuleStore; use deno_core::Extension; +use deno_core::FsModuleLoader; use deno_core::GetErrorClassFn; use deno_core::JsRuntime; use deno_core::LocalInspectorSession; @@ -105,6 +106,41 @@ fn grab_cb( v8::Global::new(scope, cb) } +impl Default for WorkerOptions { + fn default() -> Self { + Self { + web_worker_preload_module_cb: Arc::new(|_| { + unimplemented!("web workers are not supported") + }), + web_worker_pre_execute_module_cb: Arc::new(|_| { + unimplemented!("web workers are not supported") + }), + create_web_worker_cb: Arc::new(|_| { + unimplemented!("web workers are not supported") + }), + module_loader: Rc::new(FsModuleLoader), + seed: None, + unsafely_ignore_certificate_errors: Default::default(), + should_break_on_first_statement: Default::default(), + compiled_wasm_module_store: Default::default(), + shared_array_buffer_store: Default::default(), + maybe_inspector_server: Default::default(), + format_js_error_fn: Default::default(), + get_error_class_fn: Default::default(), + origin_storage_dir: Default::default(), + cache_storage_dir: Default::default(), + broadcast_channel: Default::default(), + source_map_getter: Default::default(), + root_cert_store: Default::default(), + npm_resolver: Default::default(), + blob_store: Default::default(), + extensions: Default::default(), + bootstrap: Default::default(), + stdio: Default::default(), + } + } +} + impl MainWorker { pub fn bootstrap_from_options( main_module: ModuleSpecifier, @@ -533,7 +569,7 @@ mod tests { create_web_worker_cb: Arc::new(|_| unreachable!()), maybe_inspector_server: None, should_break_on_first_statement: false, - module_loader: Rc::new(deno_core::FsModuleLoader), + module_loader: Rc::new(FsModuleLoader), npm_resolver: None, get_error_class_fn: None, cache_storage_dir: None, diff --git a/runtime/worker_bootstrap.rs b/runtime/worker_bootstrap.rs index 31e7c43825..8af78c21a5 100644 --- a/runtime/worker_bootstrap.rs +++ b/runtime/worker_bootstrap.rs @@ -1,7 +1,12 @@ -use crate::ops::runtime::ppid; +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::ModuleSpecifier; +use std::thread; + +use crate::colors; +use crate::ops::runtime::ppid; /// Common bootstrap options for MainWorker & WebWorker #[derive(Clone)] @@ -24,6 +29,32 @@ pub struct BootstrapOptions { pub inspect: bool, } +impl Default for BootstrapOptions { + fn default() -> Self { + let cpu_count = thread::available_parallelism() + .map(|p| p.get()) + .unwrap_or(1); + + let runtime_version = env!("CARGO_PKG_VERSION").into(); + let user_agent = format!("Deno/{}", runtime_version); + + Self { + runtime_version, + user_agent, + cpu_count, + no_color: !colors::use_color(), + is_tty: colors::is_tty(), + enable_testing_features: Default::default(), + debug_flag: Default::default(), + ts_version: Default::default(), + location: Default::default(), + unstable: Default::default(), + inspect: Default::default(), + args: Default::default(), + } + } +} + impl BootstrapOptions { pub fn as_json(&self) -> String { let payload = json!({