From 0260b488fbba9a43c64641428d3603b8761067a4 Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Wed, 28 Apr 2021 18:41:50 +0200 Subject: core: introduce extensions (#9800) Extensions allow declarative extensions to "JsRuntime" (ops, state, JS or middleware). This allows for: - `op_crates` to be plug-and-play & self-contained, reducing complexity leaked to consumers - op middleware (like metrics_op) to be opt-in and for new middleware (unstable, tracing,...) - `MainWorker` and `WebWorker` to be composable, allowing users to extend workers with their ops whilst benefiting from the other infrastructure (inspector, etc...) In short extensions improve deno's modularity, reducing complexity and leaky abstractions for embedders and the internal codebase. --- runtime/web_worker.rs | 53 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'runtime/web_worker.rs') diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index f6f88f59b..5feb0212c 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -3,7 +3,7 @@ use crate::colors; use crate::inspector::DenoInspector; use crate::inspector::InspectorServer; use crate::js; -use crate::metrics::RuntimeMetrics; +use crate::metrics; use crate::ops; use crate::permissions::Permissions; use crate::tokio_util::create_basic_runtime; @@ -18,6 +18,7 @@ use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::url::Url; use deno_core::v8; +use deno_core::Extension; use deno_core::GetErrorClassFn; use deno_core::JsErrorCreateFn; use deno_core::JsRuntime; @@ -176,11 +177,37 @@ impl WebWorker { worker_id: u32, options: &WebWorkerOptions, ) -> Self { + let extensions: Vec = vec![ + // Web APIs + deno_webidl::init(), + deno_console::init(), + deno_url::init(), + deno_web::init(), + deno_file::init( + options.blob_url_store.clone(), + Some(main_module.clone()), + ), + deno_fetch::init::( + options.user_agent.clone(), + options.ca_data.clone(), + ), + deno_websocket::init::( + options.user_agent.clone(), + options.ca_data.clone(), + ), + deno_crypto::init(options.seed), + deno_webgpu::init(options.unstable), + deno_timers::init::(), + // Metrics + metrics::init(), + ]; + let mut js_runtime = JsRuntime::new(RuntimeOptions { module_loader: Some(options.module_loader.clone()), startup_snapshot: Some(js::deno_isolate_init()), js_error_create_fn: options.js_error_create_fn.clone(), get_error_class_fn: options.get_error_class_fn, + extensions, ..Default::default() }); @@ -220,21 +247,16 @@ impl WebWorker { { let op_state = js_runtime.op_state(); let mut op_state = op_state.borrow_mut(); - op_state.put(RuntimeMetrics::default()); op_state.put::(permissions); op_state.put(ops::UnstableChecker { unstable: options.unstable, }); } + js_runtime.init_extension_ops().unwrap(); + ops::web_worker::init(js_runtime, sender.clone(), handle); - ops::runtime::init(js_runtime, main_module.clone()); - ops::fetch::init( - js_runtime, - options.user_agent.clone(), - options.ca_data.clone(), - ); - ops::timers::init(js_runtime); + ops::runtime::init(js_runtime, main_module); ops::worker_host::init( js_runtime, Some(sender), @@ -242,20 +264,7 @@ impl WebWorker { ); ops::reg_sync(js_runtime, "op_close", deno_core::op_close); ops::reg_sync(js_runtime, "op_resources", deno_core::op_resources); - ops::url::init(js_runtime); - ops::file::init( - js_runtime, - options.blob_url_store.clone(), - Some(main_module), - ); ops::io::init(js_runtime); - ops::webgpu::init(js_runtime); - ops::websocket::init( - js_runtime, - options.user_agent.clone(), - options.ca_data.clone(), - ); - ops::crypto::init(js_runtime, options.seed); if options.use_deno_namespace { ops::fs_events::init(js_runtime); -- cgit v1.2.3