1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

cleanup(cli): use op Extensions (#13223)

Enabling op-middleware for overrides in lieu of imperative .replace_op() etc...

Impacts #13219,  #12938, #13122
This commit is contained in:
Aaron O'Mullan 2021-12-29 14:30:08 +01:00 committed by GitHub
parent 6d017d42aa
commit 4208199490
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 137 deletions

View file

@ -78,7 +78,7 @@ use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::v8_set_flags; use deno_core::v8_set_flags;
use deno_core::JsRuntime; use deno_core::Extension;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_runtime::colors; use deno_runtime::colors;
use deno_runtime::ops::worker_host::CreateWebWorkerCb; use deno_runtime::ops::worker_host::CreateWebWorkerCb;
@ -117,6 +117,8 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
); );
let create_web_worker_cb = create_web_worker_callback(ps.clone()); 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 { let options = WebWorkerOptions {
bootstrap: BootstrapOptions { bootstrap: BootstrapOptions {
args: ps.flags.argv.clone(), args: ps.flags.argv.clone(),
@ -133,7 +135,7 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
ts_version: version::TYPESCRIPT.to_string(), ts_version: version::TYPESCRIPT.to_string(),
unstable: ps.flags.unstable, unstable: ps.flags.unstable,
}, },
extensions: vec![], extensions,
unsafely_ignore_certificate_errors: ps unsafely_ignore_certificate_errors: ps
.flags .flags
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
@ -154,39 +156,14 @@ fn create_web_worker_callback(ps: ProcState) -> Arc<CreateWebWorkerCb> {
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()), compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
maybe_exit_code: args.maybe_exit_code, 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 WebWorker::bootstrap_from_options(
// 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.name,
args.permissions, args.permissions,
args.main_module, args.main_module,
args.worker_id, 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
{
let js_runtime = &mut worker.js_runtime;
js_runtime
.op_state()
.borrow_mut()
.put::<ProcState>(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, ps: &ProcState,
main_module: ModuleSpecifier, main_module: ModuleSpecifier,
permissions: Permissions, permissions: Permissions,
maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>, mut custom_extensions: Vec<Extension>,
) -> MainWorker { ) -> MainWorker {
let module_loader = CliModuleLoader::new(ps.clone()); let module_loader = CliModuleLoader::new(ps.clone());
@ -237,6 +214,9 @@ pub fn create_main_worker(
.join(checksum::gen(&[key.as_bytes()])) .join(checksum::gen(&[key.as_bytes()]))
}); });
let mut extensions = ops::cli_exts(ps.clone(), true);
extensions.append(&mut custom_extensions);
let options = WorkerOptions { let options = WorkerOptions {
bootstrap: BootstrapOptions { bootstrap: BootstrapOptions {
apply_source_maps: true, apply_source_maps: true,
@ -250,7 +230,7 @@ pub fn create_main_worker(
ts_version: version::TYPESCRIPT.to_string(), ts_version: version::TYPESCRIPT.to_string(),
unstable: ps.flags.unstable, unstable: ps.flags.unstable,
}, },
extensions: vec![], extensions,
unsafely_ignore_certificate_errors: ps unsafely_ignore_certificate_errors: ps
.flags .flags
.unsafely_ignore_certificate_errors .unsafely_ignore_certificate_errors
@ -271,31 +251,7 @@ pub fn create_main_worker(
compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()), compiled_wasm_module_store: Some(ps.compiled_wasm_module_store.clone()),
}; };
let mut worker = MainWorker::bootstrap_from_options(main_module, permissions, options)
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::<ProcState>(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
} }
pub fn write_to_stdout_ignore_sigpipe( pub fn write_to_stdout_ignore_sigpipe(
@ -538,7 +494,7 @@ async fn install_command(
let ps = ProcState::build(preload_flags).await?; let ps = ProcState::build(preload_flags).await?;
let main_module = resolve_url_or_path(&install_flags.module_url)?; let main_module = resolve_url_or_path(&install_flags.module_url)?;
let mut worker = 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. // First, fetch and compile the module; this step ensures that the module exists.
worker.preload_module(&main_module, true).await?; worker.preload_module(&main_module, true).await?;
tools::installer::install(flags, install_flags)?; tools::installer::install(flags, install_flags)?;
@ -616,7 +572,7 @@ async fn eval_command(
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let mut worker = 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. // Create a dummy source file.
let source_code = if eval_flags.print { let source_code = if eval_flags.print {
format!("console.log({})", eval_flags.code) format!("console.log({})", eval_flags.code)
@ -942,7 +898,7 @@ async fn repl_command(
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let ps = ProcState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let mut worker = let mut worker =
create_main_worker(&ps, main_module.clone(), permissions, None); create_main_worker(&ps, main_module.clone(), permissions, vec![]);
if flags.compat { if flags.compat {
worker.execute_side_module(&compat::GLOBAL_URL).await?; worker.execute_side_module(&compat::GLOBAL_URL).await?;
compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?; compat::add_global_require(&mut worker.js_runtime, main_module.as_str())?;
@ -957,7 +913,7 @@ async fn run_from_stdin(flags: Flags) -> Result<i32, AnyError> {
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap(); let main_module = resolve_url_or_path("./$deno$stdin.ts").unwrap();
let mut worker = 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(); let mut source = Vec::new();
std::io::stdin().read_to_end(&mut source)?; std::io::stdin().read_to_end(&mut source)?;
@ -1140,7 +1096,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<i32, AnyError> {
// We make use an module executor guard to ensure that unload is always fired when an // We make use an module executor guard to ensure that unload is always fired when an
// operation is called. // operation is called.
let mut executor = FileWatcherModuleExecutor::new( 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, flags.compat,
); );
@ -1176,7 +1132,7 @@ async fn run_command(
let ps = ProcState::build(flags.clone()).await?; let ps = ProcState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into()); let permissions = Permissions::from_options(&flags.clone().into());
let mut worker = 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 = let mut maybe_coverage_collector =
if let Some(ref coverage_dir) = ps.coverage_dir { if let Some(ref coverage_dir) = ps.coverage_dir {

View file

@ -6,18 +6,24 @@ use crate::proc_state::ProcState;
use crate::source_maps::get_orig_position; use crate::source_maps::get_orig_position;
use crate::source_maps::CachedMaps; use crate::source_maps::CachedMaps;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_sync;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
use deno_core::Extension;
use deno_core::OpState; use deno_core::OpState;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_sync(rt, "op_apply_source_map", op_apply_source_map); Extension::builder()
super::reg_sync(rt, "op_format_diagnostic", op_format_diagnostic); .ops(vec![
super::reg_sync(rt, "op_format_file_name", op_format_file_name); ("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)] #[derive(Deserialize)]

View file

@ -1,7 +1,29 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
pub mod errors; use crate::proc_state::ProcState;
pub mod runtime_compiler; use deno_core::Extension;
mod errors;
mod runtime_compiler;
pub mod testing; pub mod testing;
pub use deno_runtime::ops::{reg_async, reg_sync}; pub fn cli_exts(ps: ProcState, enable_compiler: bool) -> Vec<Extension> {
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()
}

View file

@ -15,13 +15,14 @@ use deno_core::anyhow::Context;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::generic_error; use deno_core::error::generic_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::op_async;
use deno_core::parking_lot::RwLock; use deno_core::parking_lot::RwLock;
use deno_core::resolve_url_or_path; use deno_core::resolve_url_or_path;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::serde_json::Value; use deno_core::serde_json::Value;
use deno_core::Extension;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
use deno_graph;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use import_map::ImportMap; use import_map::ImportMap;
use serde::Deserialize; use serde::Deserialize;
@ -32,8 +33,10 @@ use std::collections::HashSet;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
pub fn init(rt: &mut deno_core::JsRuntime) { pub fn init() -> Extension {
super::reg_async(rt, "op_emit", op_emit); Extension::builder()
.ops(vec![("op_emit", op_async(op_emit))])
.build()
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]

View file

@ -1,7 +1,8 @@
use crate::tools::test::TestEvent; use crate::tools::test::TestEvent;
use deno_core::error::generic_error; use deno_core::error::generic_error;
use deno_core::error::AnyError; 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::ModuleSpecifier;
use deno_core::OpState; use deno_core::OpState;
use deno_runtime::permissions::create_child_permissions; use deno_runtime::permissions::create_child_permissions;
@ -10,15 +11,25 @@ use deno_runtime::permissions::Permissions;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use uuid::Uuid; use uuid::Uuid;
pub fn init(rt: &mut JsRuntime) { pub fn init(sender: Sender<TestEvent>) -> Extension {
super::reg_sync(rt, "op_pledge_test_permissions", op_pledge_test_permissions); Extension::builder()
super::reg_sync( .ops(vec![
rt, (
"op_pledge_test_permissions",
op_sync(op_pledge_test_permissions),
),
(
"op_restore_test_permissions", "op_restore_test_permissions",
op_restore_test_permissions, op_sync(op_restore_test_permissions),
); ),
super::reg_sync(rt, "op_get_test_origin", op_get_test_origin); ("op_get_test_origin", op_sync(op_get_test_origin)),
super::reg_sync(rt, "op_dispatch_test_event", op_dispatch_test_event); ("op_dispatch_test_event", op_sync(op_dispatch_test_event)),
])
.state(move |state| {
state.put(sender.clone());
Ok(())
})
.build()
} }
#[derive(Clone)] #[derive(Clone)]

View file

@ -249,7 +249,7 @@ pub async fn run(
ts_version: version::TYPESCRIPT.to_string(), ts_version: version::TYPESCRIPT.to_string(),
unstable: metadata.unstable, unstable: metadata.unstable,
}, },
extensions: vec![], extensions: ops::cli_exts(ps.clone(), true),
user_agent: version::get_user_agent(), user_agent: version::get_user_agent(),
unsafely_ignore_certificate_errors: metadata unsafely_ignore_certificate_errors: metadata
.unsafely_ignore_certificate_errors, .unsafely_ignore_certificate_errors,
@ -272,17 +272,6 @@ pub async fn run(
permissions, permissions,
options, 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::<ProcState>(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.execute_main_module(&main_module).await?;
worker.dispatch_load_event(&located_script_name!())?; worker.dispatch_load_event(&located_script_name!())?;
worker.run_event_loop(true).await?; worker.run_event_loop(true).await?;

View file

@ -33,7 +33,6 @@ use deno_core::futures::stream;
use deno_core::futures::FutureExt; use deno_core::futures::FutureExt;
use deno_core::futures::StreamExt; use deno_core::futures::StreamExt;
use deno_core::serde_json::json; use deno_core::serde_json::json;
use deno_core::JsRuntime;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_graph::Module; use deno_graph::Module;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
@ -453,17 +452,12 @@ async fn test_specifier(
shuffle: Option<u64>, shuffle: Option<u64>,
channel: Sender<TestEvent>, channel: Sender<TestEvent>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let init_ops = |js_runtime: &mut JsRuntime| { let mut worker = create_main_worker(
ops::testing::init(js_runtime); &ps,
specifier.clone(),
js_runtime permissions,
.op_state() vec![ops::testing::init(channel.clone())],
.borrow_mut() );
.put::<Sender<TestEvent>>(channel.clone());
};
let mut worker =
create_main_worker(&ps, specifier.clone(), permissions, Some(&init_ops));
let mut maybe_coverage_collector = if let Some(ref coverage_dir) = let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
ps.coverage_dir ps.coverage_dir

View file

@ -14,41 +14,10 @@ mod utils;
pub mod web_worker; pub mod web_worker;
pub mod worker_host; 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 deno_core::OpState;
use std::cell::RefCell; use std::cell::RefCell;
use std::future::Future;
use std::rc::Rc; use std::rc::Rc;
pub fn reg_async<F, A, B, R, RV>(
rt: &mut JsRuntime,
name: &'static str,
op_fn: F,
) where
F: Fn(Rc<RefCell<OpState>>, A, B) -> R + 'static,
A: DeserializeOwned,
B: DeserializeOwned,
R: Future<Output = Result<RV, AnyError>> + 'static,
RV: Serialize + 'static,
{
rt.register_op(name, op_async(op_fn));
}
pub fn reg_sync<F, A, B, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
F: Fn(&mut OpState, A, B) -> Result<R, AnyError> + '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`; /// `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 /// 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 /// that there might be another type alias pointing to a bool, which