From 960ee5257a7b38c6b3b59a3dca670c7c1771996f Mon Sep 17 00:00:00 2001 From: andy finch Date: Thu, 11 Apr 2019 10:58:31 -0400 Subject: [PATCH] Improve op dispatch (#2088) --- cli/compiler.rs | 2 ++ cli/main.rs | 2 +- cli/ops.rs | 18 ++++++++++++++---- cli/state.rs | 12 +++++++++--- cli/worker.rs | 5 +++-- 5 files changed, 29 insertions(+), 10 deletions(-) diff --git a/cli/compiler.rs b/cli/compiler.rs index 4fb2105a9f..fa59e8a8cf 100644 --- a/cli/compiler.rs +++ b/cli/compiler.rs @@ -2,6 +2,7 @@ use crate::js_errors; use crate::js_errors::JSErrorColor; use crate::msg; +use crate::ops::op_selector_compiler; use crate::resources; use crate::resources::ResourceId; use crate::startup_data; @@ -93,6 +94,7 @@ fn lazy_start(parent_state: ThreadSafeState) -> ResourceId { let child_state = ThreadSafeState::new( parent_state.flags.clone(), parent_state.argv.clone(), + op_selector_compiler, ); let rid = child_state.resource.rid; let resource = child_state.resource.clone(); diff --git a/cli/main.rs b/cli/main.rs index 6178dafeee..5314e4149d 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -101,7 +101,7 @@ fn main() { let should_prefetch = flags.prefetch || flags.info; let should_display_info = flags.info; - let state = ThreadSafeState::new(flags, rest_argv); + let state = ThreadSafeState::new(flags, rest_argv, ops::op_selector_std); let mut main_worker = Worker::new( "main".to_string(), startup_data::deno_isolate_init(), diff --git a/cli/ops.rs b/cli/ops.rs index ce82991361..0d752e2266 100644 --- a/cli/ops.rs +++ b/cli/ops.rs @@ -63,7 +63,7 @@ type OpCreator = fn(state: &ThreadSafeState, base: &msg::Base<'_>, data: deno_buf) -> Box; -type OpSelector = fn(inner_type: msg::Any) -> Option; +pub type OpSelector = fn(inner_type: msg::Any) -> Option; #[inline] fn empty_buf() -> Buf { @@ -142,6 +142,17 @@ pub fn dispatch_all( (base.sync(), boxed_op) } +pub fn op_selector_compiler(inner_type: msg::Any) -> Option { + match inner_type { + msg::Any::FetchModuleMetaData => Some(op_fetch_module_meta_data), + msg::Any::WorkerGetMessage => Some(op_worker_get_message), + msg::Any::WorkerPostMessage => Some(op_worker_post_message), + msg::Any::Exit => Some(op_exit), + msg::Any::Start => Some(op_start), + _ => None, + } +} + /// Standard ops set for most isolates pub fn op_selector_std(inner_type: msg::Any) -> Option { match inner_type { @@ -192,9 +203,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option { msg::Any::Write => Some(op_write), // TODO(ry) split these out so that only the appropriate Workers can access - // them. Only the compiler worker should be able to access - // FetchModuleMetaData. - msg::Any::FetchModuleMetaData => Some(op_fetch_module_meta_data), + // them. msg::Any::WorkerGetMessage => Some(op_worker_get_message), msg::Any::WorkerPostMessage => Some(op_worker_post_message), @@ -1860,6 +1869,7 @@ fn op_create_worker( let child_state = ThreadSafeState::new( parent_state.flags.clone(), parent_state.argv.clone(), + op_selector_std, ); let rid = child_state.resource.rid; let name = format!("USER-WORKER-{}", specifier); diff --git a/cli/state.rs b/cli/state.rs index 09a57d985c..3434566fa2 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -60,6 +60,7 @@ pub struct State { pub workers: Mutex, pub start_time: Instant, pub resource: resources::Resource, + pub dispatch_selector: ops::OpSelector, } impl Clone for ThreadSafeState { @@ -81,12 +82,16 @@ impl Dispatch for ThreadSafeState { control: &[u8], zero_copy: deno_buf, ) -> (bool, Box) { - ops::dispatch_all(self, control, zero_copy, ops::op_selector_std) + ops::dispatch_all(self, control, zero_copy, self.dispatch_selector) } } impl ThreadSafeState { - pub fn new(flags: flags::DenoFlags, argv_rest: Vec) -> Self { + pub fn new( + flags: flags::DenoFlags, + argv_rest: Vec, + dispatch_selector: ops::OpSelector, + ) -> Self { let custom_root = env::var("DENO_DIR").map(|s| s.into()).ok(); let (worker_in_tx, worker_in_rx) = async_mpsc::channel::(1); @@ -107,6 +112,7 @@ impl ThreadSafeState { workers: Mutex::new(UserWorkerTable::new()), start_time: Instant::now(), resource, + dispatch_selector, })) } @@ -157,7 +163,7 @@ impl ThreadSafeState { let argv = vec![String::from("./deno"), String::from("hello.js")]; // For debugging: argv.push_back(String::from("-D")); let (flags, rest_argv) = flags::set_flags(argv).unwrap(); - ThreadSafeState::new(flags, rest_argv) + ThreadSafeState::new(flags, rest_argv, ops::op_selector_std) } pub fn metrics_op_dispatched( diff --git a/cli/worker.rs b/cli/worker.rs index 325ec46eba..5271831824 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -250,6 +250,7 @@ fn fetch_module_meta_data_and_maybe_compile( mod tests { use super::*; use crate::flags; + use crate::ops::op_selector_std; use crate::resources; use crate::startup_data; use crate::state::ThreadSafeState; @@ -269,7 +270,7 @@ mod tests { let argv = vec![String::from("./deno"), filename.clone()]; let (flags, rest_argv) = flags::set_flags(argv).unwrap(); - let state = ThreadSafeState::new(flags, rest_argv); + let state = ThreadSafeState::new(flags, rest_argv, op_selector_std); let state_ = state.clone(); tokio_util::run(lazy(move || { let mut worker = @@ -292,7 +293,7 @@ mod tests { let argv = vec![String::from("./deno"), filename.clone()]; let (flags, rest_argv) = flags::set_flags(argv).unwrap(); - let state = ThreadSafeState::new(flags, rest_argv); + let state = ThreadSafeState::new(flags, rest_argv, op_selector_std); let state_ = state.clone(); tokio_util::run(lazy(move || { let mut worker =