mirror of
https://github.com/denoland/deno.git
synced 2024-12-25 08:39:09 -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:
parent
6d017d42aa
commit
4208199490
8 changed files with 87 additions and 137 deletions
80
cli/main.rs
80
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<CreateWebWorkerCb> {
|
|||
);
|
||||
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<CreateWebWorkerCb> {
|
|||
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<CreateWebWorkerCb> {
|
|||
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::<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,
|
||||
main_module: ModuleSpecifier,
|
||||
permissions: Permissions,
|
||||
maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>,
|
||||
mut custom_extensions: Vec<Extension>,
|
||||
) -> 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::<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
|
||||
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<i32, AnyError> {
|
|||
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<i32, AnyError> {
|
|||
// 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 {
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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<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()
|
||||
}
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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,
|
||||
pub fn init(sender: Sender<TestEvent>) -> Extension {
|
||||
Extension::builder()
|
||||
.ops(vec![
|
||||
(
|
||||
"op_pledge_test_permissions",
|
||||
op_sync(op_pledge_test_permissions),
|
||||
),
|
||||
(
|
||||
"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);
|
||||
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)]
|
||||
|
|
|
@ -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::<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.dispatch_load_event(&located_script_name!())?;
|
||||
worker.run_event_loop(true).await?;
|
||||
|
|
|
@ -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<u64>,
|
||||
channel: Sender<TestEvent>,
|
||||
) -> Result<(), AnyError> {
|
||||
let init_ops = |js_runtime: &mut JsRuntime| {
|
||||
ops::testing::init(js_runtime);
|
||||
|
||||
js_runtime
|
||||
.op_state()
|
||||
.borrow_mut()
|
||||
.put::<Sender<TestEvent>>(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
|
||||
|
|
|
@ -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<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`;
|
||||
/// 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
|
||||
|
|
Loading…
Reference in a new issue