diff --git a/cli/main.rs b/cli/main.rs index 97b902ee6d..5730cba069 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -78,7 +78,7 @@ use deno_core::resolve_url_or_path; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::v8_set_flags; -use deno_core::JsRuntime; +use deno_core::Extension; use deno_core::ModuleSpecifier; use deno_runtime::colors; use deno_runtime::ops::worker_host::CreateWebWorkerCb; @@ -117,6 +117,8 @@ fn create_web_worker_callback(ps: ProcState) -> Arc { ); let create_web_worker_cb = create_web_worker_callback(ps.clone()); + let extensions = ops::cli_exts(ps.clone(), args.use_deno_namespace); + let options = WebWorkerOptions { bootstrap: BootstrapOptions { args: ps.flags.argv.clone(), @@ -133,7 +135,7 @@ fn create_web_worker_callback(ps: ProcState) -> Arc { ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, }, - extensions: vec![], + extensions, unsafely_ignore_certificate_errors: ps .flags .unsafely_ignore_certificate_errors @@ -154,39 +156,14 @@ fn create_web_worker_callback(ps: ProcState) -> Arc { compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()), maybe_exit_code: args.maybe_exit_code, }; - 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( + WebWorker::bootstrap_from_options( args.name, args.permissions, args.main_module, args.worker_id, 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 - { - let js_runtime = &mut worker.js_runtime; - js_runtime - .op_state() - .borrow_mut() - .put::(ps.clone()); - // Applies source maps - works in conjuction with `js_error_create_fn` - // above - ops::errors::init(js_runtime); - if args.use_deno_namespace { - ops::runtime_compiler::init(js_runtime); - } - js_runtime.sync_ops_cache(); - } - worker.bootstrap(&bootstrap_options); - - (worker, external_handle) + ) }) } @@ -194,7 +171,7 @@ pub fn create_main_worker( ps: &ProcState, main_module: ModuleSpecifier, permissions: Permissions, - maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>, + mut custom_extensions: Vec, ) -> MainWorker { let module_loader = CliModuleLoader::new(ps.clone()); @@ -237,6 +214,9 @@ pub fn create_main_worker( .join(checksum::gen(&[key.as_bytes()])) }); + let mut extensions = ops::cli_exts(ps.clone(), true); + extensions.append(&mut custom_extensions); + let options = WorkerOptions { bootstrap: BootstrapOptions { apply_source_maps: true, @@ -250,7 +230,7 @@ pub fn create_main_worker( ts_version: version::TYPESCRIPT.to_string(), unstable: ps.flags.unstable, }, - extensions: vec![], + extensions, unsafely_ignore_certificate_errors: ps .flags .unsafely_ignore_certificate_errors @@ -271,31 +251,7 @@ pub fn create_main_worker( compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()), }; - 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 - { - let js_runtime = &mut worker.js_runtime; - js_runtime - .op_state() - .borrow_mut() - .put::(ps.clone()); - // Applies source maps - works in conjuction with `js_error_create_fn` - // above - ops::errors::init(js_runtime); - ops::runtime_compiler::init(js_runtime); - - if let Some(op_init) = maybe_op_init { - op_init(js_runtime); - } - - js_runtime.sync_ops_cache(); - } - - worker + MainWorker::bootstrap_from_options(main_module, permissions, options) } pub fn write_to_stdout_ignore_sigpipe( @@ -538,7 +494,7 @@ async fn install_command( let ps = ProcState::build(preload_flags).await?; let main_module = resolve_url_or_path(&install_flags.module_url)?; let mut worker = - create_main_worker(&ps, main_module.clone(), permissions, None); + create_main_worker(&ps, main_module.clone(), permissions, vec![]); // First, fetch and compile the module; this step ensures that the module exists. worker.preload_module(&main_module, true).await?; tools::installer::install(flags, install_flags)?; @@ -616,7 +572,7 @@ async fn eval_command( let permissions = Permissions::from_options(&flags.clone().into()); let ps = ProcState::build(flags.clone()).await?; let mut worker = - create_main_worker(&ps, main_module.clone(), permissions, None); + create_main_worker(&ps, main_module.clone(), permissions, vec![]); // Create a dummy source file. let source_code = if eval_flags.print { format!("console.log({})", eval_flags.code) @@ -942,7 +898,7 @@ async fn repl_command( let permissions = Permissions::from_options(&flags.clone().into()); let ps = ProcState::build(flags.clone()).await?; let mut worker = - create_main_worker(&ps, main_module.clone(), permissions, None); + create_main_worker(&ps, main_module.clone(), permissions, vec![]); if flags.compat { worker.execute_side_module(&compat::GLOBAL_URL).await?; compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?; @@ -957,7 +913,7 @@ async fn run_from_stdin(flags: Flags) -> Result { let permissions = Permissions::from_options(&flags.clone().into()); let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap(); let mut worker = - create_main_worker(&ps.clone(), main_module.clone(), permissions, None); + create_main_worker(&ps.clone(), main_module.clone(), permissions, vec![]); let mut source = Vec::new(); std::io::stdin().read_to_end(&mut source)?; @@ -1140,7 +1096,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result { // We make use an module executor guard to ensure that unload is always fired when an // operation is called. let mut executor = FileWatcherModuleExecutor::new( - create_main_worker(&ps, main_module.clone(), permissions, None), + create_main_worker(&ps, main_module.clone(), permissions, vec![]), flags.compat, ); @@ -1176,7 +1132,7 @@ async fn run_command( let ps = ProcState::build(flags.clone()).await?; let permissions = Permissions::from_options(&flags.clone().into()); let mut worker = - create_main_worker(&ps, main_module.clone(), permissions, None); + create_main_worker(&ps, main_module.clone(), permissions, vec![]); let mut maybe_coverage_collector = if let Some(ref coverage_dir) = ps.coverage_dir { diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index 14d21ee84a..92843f577d 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -6,18 +6,24 @@ use crate::proc_state::ProcState; use crate::source_maps::get_orig_position; use crate::source_maps::CachedMaps; use deno_core::error::AnyError; +use deno_core::op_sync; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::serde_json::Value; +use deno_core::Extension; use deno_core::OpState; use serde::Deserialize; use serde::Serialize; use std::collections::HashMap; -pub fn init(rt: &mut deno_core::JsRuntime) { - super::reg_sync(rt, "op_apply_source_map", op_apply_source_map); - super::reg_sync(rt, "op_format_diagnostic", op_format_diagnostic); - super::reg_sync(rt, "op_format_file_name", op_format_file_name); +pub fn init() -> Extension { + Extension::builder() + .ops(vec![ + ("op_apply_source_map", op_sync(op_apply_source_map)), + ("op_format_diagnostic", op_sync(op_format_diagnostic)), + ("op_format_file_name", op_sync(op_format_file_name)), + ]) + .build() } #[derive(Deserialize)] diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index a3df77fac3..3584bdbefd 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -1,7 +1,29 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -pub mod errors; -pub mod runtime_compiler; +use crate::proc_state::ProcState; +use deno_core::Extension; + +mod errors; +mod runtime_compiler; pub mod testing; -pub use deno_runtime::ops::{reg_async, reg_sync}; +pub fn cli_exts(ps: ProcState, enable_compiler: bool) -> Vec { + if enable_compiler { + vec![ + init_proc_state(ps), + errors::init(), + runtime_compiler::init(), + ] + } else { + vec![init_proc_state(ps), errors::init()] + } +} + +fn init_proc_state(ps: ProcState) -> Extension { + Extension::builder() + .state(move |state| { + state.put(ps.clone()); + Ok(()) + }) + .build() +} diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index 46aaafd090..11504edd58 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -15,13 +15,14 @@ use deno_core::anyhow::Context; use deno_core::error::custom_error; use deno_core::error::generic_error; use deno_core::error::AnyError; +use deno_core::op_async; use deno_core::parking_lot::RwLock; use deno_core::resolve_url_or_path; use deno_core::serde_json; use deno_core::serde_json::Value; +use deno_core::Extension; use deno_core::ModuleSpecifier; use deno_core::OpState; -use deno_graph; use deno_runtime::permissions::Permissions; use import_map::ImportMap; use serde::Deserialize; @@ -32,8 +33,10 @@ use std::collections::HashSet; use std::rc::Rc; use std::sync::Arc; -pub fn init(rt: &mut deno_core::JsRuntime) { - super::reg_async(rt, "op_emit", op_emit); +pub fn init() -> Extension { + Extension::builder() + .ops(vec![("op_emit", op_async(op_emit))]) + .build() } #[derive(Debug, Deserialize)] diff --git a/cli/ops/testing.rs b/cli/ops/testing.rs index cf6d7b244f..928fb5a20a 100644 --- a/cli/ops/testing.rs +++ b/cli/ops/testing.rs @@ -1,7 +1,8 @@ use crate::tools::test::TestEvent; use deno_core::error::generic_error; use deno_core::error::AnyError; -use deno_core::JsRuntime; +use deno_core::op_sync; +use deno_core::Extension; use deno_core::ModuleSpecifier; use deno_core::OpState; use deno_runtime::permissions::create_child_permissions; @@ -10,15 +11,25 @@ use deno_runtime::permissions::Permissions; use std::sync::mpsc::Sender; use uuid::Uuid; -pub fn init(rt: &mut JsRuntime) { - super::reg_sync(rt, "op_pledge_test_permissions", op_pledge_test_permissions); - super::reg_sync( - rt, - "op_restore_test_permissions", - op_restore_test_permissions, - ); - super::reg_sync(rt, "op_get_test_origin", op_get_test_origin); - super::reg_sync(rt, "op_dispatch_test_event", op_dispatch_test_event); +pub fn init(sender: Sender) -> Extension { + Extension::builder() + .ops(vec![ + ( + "op_pledge_test_permissions", + op_sync(op_pledge_test_permissions), + ), + ( + "op_restore_test_permissions", + op_sync(op_restore_test_permissions), + ), + ("op_get_test_origin", op_sync(op_get_test_origin)), + ("op_dispatch_test_event", op_sync(op_dispatch_test_event)), + ]) + .state(move |state| { + state.put(sender.clone()); + Ok(()) + }) + .build() } #[derive(Clone)] diff --git a/cli/standalone.rs b/cli/standalone.rs index c7a5b2c142..f135e99465 100644 --- a/cli/standalone.rs +++ b/cli/standalone.rs @@ -249,7 +249,7 @@ pub async fn run( ts_version: version::TYPESCRIPT.to_string(), unstable: metadata.unstable, }, - extensions: vec![], + extensions: ops::cli_exts(ps.clone(), true), user_agent: version::get_user_agent(), unsafely_ignore_certificate_errors: metadata .unsafely_ignore_certificate_errors, @@ -272,17 +272,6 @@ pub async fn run( permissions, options, ); - // TODO(@AaronO): move to a JsRuntime Extension passed into options - { - let js_runtime = &mut worker.js_runtime; - js_runtime - .op_state() - .borrow_mut() - .put::(ps.clone()); - ops::errors::init(js_runtime); - ops::runtime_compiler::init(js_runtime); - js_runtime.sync_ops_cache(); - } worker.execute_main_module(&main_module).await?; worker.dispatch_load_event(&located_script_name!())?; worker.run_event_loop(true).await?; diff --git a/cli/tools/test.rs b/cli/tools/test.rs index d78363addd..9b82aa142e 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -33,7 +33,6 @@ use deno_core::futures::stream; use deno_core::futures::FutureExt; use deno_core::futures::StreamExt; use deno_core::serde_json::json; -use deno_core::JsRuntime; use deno_core::ModuleSpecifier; use deno_graph::Module; use deno_runtime::permissions::Permissions; @@ -453,17 +452,12 @@ async fn test_specifier( shuffle: Option, channel: Sender, ) -> Result<(), AnyError> { - let init_ops = |js_runtime: &mut JsRuntime| { - ops::testing::init(js_runtime); - - js_runtime - .op_state() - .borrow_mut() - .put::>(channel.clone()); - }; - - let mut worker = - create_main_worker(&ps, specifier.clone(), permissions, Some(&init_ops)); + let mut worker = create_main_worker( + &ps, + specifier.clone(), + permissions, + vec![ops::testing::init(channel.clone())], + ); let mut maybe_coverage_collector = if let Some(ref coverage_dir) = ps.coverage_dir diff --git a/runtime/ops/mod.rs b/runtime/ops/mod.rs index fa547f2edc..f2f1bdfade 100644 --- a/runtime/ops/mod.rs +++ b/runtime/ops/mod.rs @@ -14,41 +14,10 @@ mod utils; pub mod web_worker; pub mod worker_host; -use deno_core::error::AnyError; -use deno_core::op_async; -use deno_core::op_sync; -use deno_core::serde::de::DeserializeOwned; -use deno_core::serde::Serialize; -use deno_core::JsRuntime; use deno_core::OpState; use std::cell::RefCell; -use std::future::Future; use std::rc::Rc; -pub fn reg_async( - rt: &mut JsRuntime, - name: &'static str, - op_fn: F, -) where - F: Fn(Rc>, A, B) -> R + 'static, - A: DeserializeOwned, - B: DeserializeOwned, - R: Future> + 'static, - RV: Serialize + 'static, -{ - rt.register_op(name, op_async(op_fn)); -} - -pub fn reg_sync(rt: &mut JsRuntime, name: &'static str, op_fn: F) -where - F: Fn(&mut OpState, A, B) -> Result + 'static, - A: DeserializeOwned, - B: DeserializeOwned, - R: Serialize + 'static, -{ - rt.register_op(name, op_sync(op_fn)); -} - /// `UnstableChecker` is a struct so it can be placed inside `GothamState`; /// using type alias for a bool could work, but there's a high chance /// that there might be another type alias pointing to a bool, which