1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

refactor(cli): pass optional op_init to create_main_worker (#11707)

Replaces the testing flag in create_main_worker with a more 
general purpose hook to register additional ops.
This commit is contained in:
Casper Beyer 2021-08-17 18:08:39 +08:00 committed by GitHub
parent 7e5698fb2e
commit 4b800f5d26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 14 deletions

View file

@ -61,6 +61,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::ModuleSpecifier;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::permissions::Permissions;
@ -169,7 +170,7 @@ pub fn create_main_worker(
program_state: &Arc<ProgramState>,
main_module: ModuleSpecifier,
permissions: Permissions,
enable_testing: bool,
maybe_op_init: Option<&dyn Fn(&mut JsRuntime)>,
) -> MainWorker {
let module_loader = CliModuleLoader::new(program_state.clone());
@ -245,8 +246,8 @@ pub fn create_main_worker(
ops::errors::init(js_runtime);
ops::runtime_compiler::init(js_runtime);
if enable_testing {
ops::testing::init(js_runtime);
if let Some(op_init) = maybe_op_init {
op_init(js_runtime);
}
js_runtime.sync_ops_cache();
@ -483,7 +484,7 @@ async fn install_command(
let program_state = ProgramState::build(preload_flags).await?;
let main_module = resolve_url_or_path(&module_url)?;
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, false);
create_main_worker(&program_state, main_module.clone(), permissions, None);
// First, fetch and compile the module; this step ensures that the module exists.
worker.preload_module(&main_module).await?;
tools::installer::install(flags, &module_url, args, name, root, force)
@ -547,7 +548,7 @@ async fn eval_command(
let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?;
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, false);
create_main_worker(&program_state, main_module.clone(), permissions, None);
// Create a dummy source file.
let source_code = if print {
format!("console.log({})", code)
@ -793,7 +794,7 @@ async fn run_repl(
let permissions = Permissions::from_options(&flags.clone().into());
let program_state = ProgramState::build(flags).await?;
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, false);
create_main_worker(&program_state, main_module.clone(), permissions, None);
worker.run_event_loop(false).await?;
tools::repl::run(&program_state, worker, maybe_eval).await
@ -807,7 +808,7 @@ async fn run_from_stdin(flags: Flags) -> Result<(), AnyError> {
&program_state.clone(),
main_module.clone(),
permissions,
false,
None,
);
let mut source = Vec::new();
@ -900,7 +901,7 @@ async fn run_with_watch(flags: Flags, script: String) -> Result<(), AnyError> {
&program_state,
main_module.clone(),
permissions,
false,
None,
);
debug!("main_module {}", main_module);
worker.execute_module(&main_module).await?;
@ -934,7 +935,7 @@ async fn run_command(flags: Flags, script: String) -> Result<(), AnyError> {
let program_state = ProgramState::build(flags.clone()).await?;
let permissions = Permissions::from_options(&flags.clone().into());
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, false);
create_main_worker(&program_state, main_module.clone(), permissions, None);
let mut maybe_coverage_collector =
if let Some(ref coverage_dir) = program_state.coverage_dir {

View file

@ -7,6 +7,7 @@ use crate::create_main_worker;
use crate::file_fetcher::File;
use crate::media_type::MediaType;
use crate::module_graph;
use crate::ops;
use crate::program_state::ProgramState;
use crate::tokio_util;
use crate::tools::coverage::CoverageCollector;
@ -17,6 +18,7 @@ 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_runtime::permissions::Permissions;
use rand::rngs::SmallRng;
@ -243,16 +245,21 @@ pub async fn test_specifier(
program_state.file_fetcher.insert_cached(test_file);
let mut worker =
create_main_worker(&program_state, main_module.clone(), permissions, true);
let init_ops = |js_runtime: &mut JsRuntime| {
ops::testing::init(js_runtime);
{
let js_runtime = &mut worker.js_runtime;
js_runtime
.op_state()
.borrow_mut()
.put::<Sender<TestEvent>>(channel.clone());
}
};
let mut worker = create_main_worker(
&program_state,
main_module.clone(),
permissions,
Some(&init_ops),
);
let mut maybe_coverage_collector = if let Some(ref coverage_dir) =
program_state.coverage_dir