Skip to content

Commit

Permalink
cleanup(cli): use op Extensions (#13223)
Browse files Browse the repository at this point in the history
Enabling op-middleware for overrides in lieu of imperative .replace_op() etc...

Impacts #13219,  #12938, #13122
  • Loading branch information
AaronO authored Dec 29, 2021
1 parent 6d017d4 commit 4208199
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 137 deletions.
80 changes: 18 additions & 62 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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
Expand All @@ -154,47 +156,22 @@ 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)
)
})
}

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());

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -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,
);

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 10 additions & 4 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
28 changes: 25 additions & 3 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 6 additions & 3 deletions cli/ops/runtime_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down
31 changes: 21 additions & 10 deletions cli/ops/testing.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TestEvent>) -> 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)]
Expand Down
13 changes: 1 addition & 12 deletions cli/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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?;
Expand Down
18 changes: 6 additions & 12 deletions cli/tools/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 4208199

Please sign in to comment.