1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

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.
This commit is contained in:
Christian Dürr 2022-10-15 21:19:03 +00:00 committed by GitHub
parent 872dc9b1df
commit 6d2656fd56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 2 deletions

View file

@ -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,

View file

@ -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!({