mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
Improve op dispatch (#2088)
This commit is contained in:
parent
1034d9723d
commit
960ee5257a
5 changed files with 29 additions and 10 deletions
|
@ -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();
|
||||
|
|
|
@ -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(),
|
||||
|
|
18
cli/ops.rs
18
cli/ops.rs
|
@ -63,7 +63,7 @@ type OpCreator =
|
|||
fn(state: &ThreadSafeState, base: &msg::Base<'_>, data: deno_buf)
|
||||
-> Box<OpWithError>;
|
||||
|
||||
type OpSelector = fn(inner_type: msg::Any) -> Option<OpCreator>;
|
||||
pub type OpSelector = fn(inner_type: msg::Any) -> Option<OpCreator>;
|
||||
|
||||
#[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<OpCreator> {
|
||||
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<OpCreator> {
|
||||
match inner_type {
|
||||
|
@ -192,9 +203,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<OpCreator> {
|
|||
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);
|
||||
|
|
12
cli/state.rs
12
cli/state.rs
|
@ -60,6 +60,7 @@ pub struct State {
|
|||
pub workers: Mutex<UserWorkerTable>,
|
||||
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<Op>) {
|
||||
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<String>) -> Self {
|
||||
pub fn new(
|
||||
flags: flags::DenoFlags,
|
||||
argv_rest: Vec<String>,
|
||||
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::<Buf>(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(
|
||||
|
|
|
@ -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 =
|
||||
|
|
Loading…
Reference in a new issue