diff options
Diffstat (limited to 'cli/ops.rs')
-rw-r--r-- | cli/ops.rs | 382 |
1 files changed, 196 insertions, 186 deletions
diff --git a/cli/ops.rs b/cli/ops.rs index 862f92560..82f30c137 100644 --- a/cli/ops.rs +++ b/cli/ops.rs @@ -1,12 +1,11 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. use atty; use crate::ansi; -use crate::cli::Cli; use crate::errors; -use crate::errors::{permission_denied, DenoError, DenoResult, ErrorKind}; +use crate::errors::{op_not_implemented, DenoError, DenoResult, ErrorKind}; use crate::fs as deno_fs; use crate::http_util; -use crate::isolate_state::IsolateState; +use crate::isolate_state::{IsolateState, IsolateStateContainer}; use crate::js_errors::apply_source_map; use crate::js_errors::JSErrorColor; use crate::msg; @@ -59,7 +58,10 @@ pub type OpWithError = dyn Future<Item = Buf, Error = DenoError> + Send; // TODO Ideally we wouldn't have to box the OpWithError being returned. // The box is just to make it easier to get a prototype refactor working. type OpCreator = - fn(cli: &Cli, base: &msg::Base<'_>, data: deno_buf) -> Box<OpWithError>; + fn(sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf) + -> Box<OpWithError>; + +type OpSelector = fn(inner_type: msg::Any) -> DenoResult<Box<OpCreator>>; #[inline] fn empty_buf() -> Buf { @@ -70,10 +72,11 @@ fn empty_buf() -> Buf { /// This functions invoked every time libdeno.send() is called. /// control corresponds to the first argument of libdeno.send(). /// data corresponds to the second argument of libdeno.send(). -pub fn dispatch( - cli: &Cli, +pub fn dispatch_all( + sc: Box<&IsolateStateContainer>, control: &[u8], zero_copy: deno_buf, + op_selector: OpSelector, ) -> (bool, Box<Op>) { let bytes_sent_control = control.len(); let bytes_sent_zero_copy = zero_copy.len(); @@ -82,66 +85,19 @@ pub fn dispatch( let inner_type = base.inner_type(); let cmd_id = base.cmd_id(); - let op: Box<OpWithError> = { - // Handle regular ops. - let op_creator: OpCreator = match inner_type { - msg::Any::Accept => op_accept, - msg::Any::Chdir => op_chdir, - msg::Any::Chmod => op_chmod, - msg::Any::Close => op_close, - msg::Any::CopyFile => op_copy_file, - msg::Any::Cwd => op_cwd, - msg::Any::Dial => op_dial, - msg::Any::Environ => op_env, - msg::Any::Exit => op_exit, - msg::Any::Fetch => op_fetch, - msg::Any::FetchModuleMetaData => op_fetch_module_meta_data, - msg::Any::FormatError => op_format_error, - msg::Any::GlobalTimer => op_global_timer, - msg::Any::GlobalTimerStop => op_global_timer_stop, - msg::Any::IsTTY => op_is_tty, - msg::Any::Listen => op_listen, - msg::Any::MakeTempDir => op_make_temp_dir, - msg::Any::Metrics => op_metrics, - msg::Any::Mkdir => op_mkdir, - msg::Any::Now => op_now, - msg::Any::Open => op_open, - msg::Any::PermissionRevoke => op_revoke_permission, - msg::Any::Permissions => op_permissions, - msg::Any::Read => op_read, - msg::Any::ReadDir => op_read_dir, - msg::Any::ReadFile => op_read_file, - msg::Any::Readlink => op_read_link, - msg::Any::Remove => op_remove, - msg::Any::Rename => op_rename, - msg::Any::ReplReadline => op_repl_readline, - msg::Any::ReplStart => op_repl_start, - msg::Any::Resources => op_resources, - msg::Any::Run => op_run, - msg::Any::RunStatus => op_run_status, - msg::Any::Seek => op_seek, - msg::Any::SetEnv => op_set_env, - msg::Any::Shutdown => op_shutdown, - msg::Any::Start => op_start, - msg::Any::Stat => op_stat, - msg::Any::Symlink => op_symlink, - msg::Any::Truncate => op_truncate, - msg::Any::WorkerGetMessage => op_worker_get_message, - msg::Any::WorkerPostMessage => op_worker_post_message, - msg::Any::Write => op_write, - msg::Any::WriteFile => op_write_file, - _ => panic!(format!( - "Unhandled message {}", - msg::enum_name_any(inner_type) - )), - }; - op_creator(&cli, &base, zero_copy) + let op_func: Box<OpCreator> = match op_selector(inner_type) { + Ok(v) => v, + Err(_) => panic!(format!( + "Unhandled message {}", + msg::enum_name_any(inner_type) + )), }; - cli - .state - .metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy); - let state = cli.state.clone(); + let state = sc.state().clone(); + + let op: Box<OpWithError> = op_func(sc, &base, zero_copy); + + state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy); let boxed_op = Box::new( op.or_else(move |err: DenoError| -> Result<Buf, ()> { @@ -188,8 +144,72 @@ pub fn dispatch( (base.sync(), boxed_op) } +pub fn op_selector_compiler( + inner_type: msg::Any, +) -> DenoResult<Box<OpCreator>> { + let op_creator: Box<OpCreator> = match inner_type { + msg::Any::FetchModuleMetaData => Box::new(op_fetch_module_meta_data), + _ => match op_selector_std(inner_type) { + Ok(v) => v, + Err(e) => return Err(e), + }, + }; + Ok(op_creator) +} + +pub fn op_selector_std(inner_type: msg::Any) -> DenoResult<Box<OpCreator>> { + let op_creator: OpCreator = match inner_type { + msg::Any::Accept => op_accept, + msg::Any::Chdir => op_chdir, + msg::Any::Chmod => op_chmod, + msg::Any::Close => op_close, + msg::Any::CopyFile => op_copy_file, + msg::Any::Cwd => op_cwd, + msg::Any::Dial => op_dial, + msg::Any::Environ => op_env, + msg::Any::Exit => op_exit, + msg::Any::Fetch => op_fetch, + msg::Any::FormatError => op_format_error, + msg::Any::GlobalTimer => op_global_timer, + msg::Any::GlobalTimerStop => op_global_timer_stop, + msg::Any::IsTTY => op_is_tty, + msg::Any::Listen => op_listen, + msg::Any::MakeTempDir => op_make_temp_dir, + msg::Any::Metrics => op_metrics, + msg::Any::Mkdir => op_mkdir, + msg::Any::Now => op_now, + msg::Any::Open => op_open, + msg::Any::PermissionRevoke => op_revoke_permission, + msg::Any::Permissions => op_permissions, + msg::Any::Read => op_read, + msg::Any::ReadDir => op_read_dir, + msg::Any::ReadFile => op_read_file, + msg::Any::Readlink => op_read_link, + msg::Any::Remove => op_remove, + msg::Any::Rename => op_rename, + msg::Any::ReplReadline => op_repl_readline, + msg::Any::ReplStart => op_repl_start, + msg::Any::Resources => op_resources, + msg::Any::Run => op_run, + msg::Any::RunStatus => op_run_status, + msg::Any::Seek => op_seek, + msg::Any::SetEnv => op_set_env, + msg::Any::Shutdown => op_shutdown, + msg::Any::Start => op_start, + msg::Any::Stat => op_stat, + msg::Any::Symlink => op_symlink, + msg::Any::Truncate => op_truncate, + msg::Any::WorkerGetMessage => op_worker_get_message, + msg::Any::WorkerPostMessage => op_worker_post_message, + msg::Any::Write => op_write, + msg::Any::WriteFile => op_write_file, + _ => return Err(op_not_implemented()), + }; + Ok(Box::new(op_creator)) +} + fn op_now( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -213,7 +233,7 @@ fn op_now( } fn op_is_tty( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, _data: deno_buf, ) -> Box<OpWithError> { @@ -238,7 +258,7 @@ fn op_is_tty( } fn op_exit( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, _data: deno_buf, ) -> Box<OpWithError> { @@ -247,19 +267,15 @@ fn op_exit( } fn op_start( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { assert_eq!(data.len(), 0); let mut builder = FlatBufferBuilder::new(); - let argv = cli - .state - .argv - .iter() - .map(|s| s.as_str()) - .collect::<Vec<_>>(); + let state = sc.state(); + let argv = state.argv.iter().map(|s| s.as_str()).collect::<Vec<_>>(); let argv_off = builder.create_vector_of_strings(argv.as_slice()); let cwd_path = std::env::current_dir().unwrap(); @@ -275,7 +291,7 @@ fn op_start( let deno_version = version::DENO; let deno_version_off = builder.create_string(deno_version); - let main_module = cli.state.main_module().map(|m| builder.create_string(&m)); + let main_module = sc.state().main_module().map(|m| builder.create_string(&m)); let inner = msg::StartRes::create( &mut builder, @@ -284,9 +300,9 @@ fn op_start( pid: std::process::id(), argv: Some(argv_off), main_module, - debug_flag: cli.state.flags.log_debug, - types_flag: cli.state.flags.types, - version_flag: cli.state.flags.version, + debug_flag: sc.state().flags.log_debug, + types_flag: sc.state().flags.types, + version_flag: sc.state().flags.version, v8_version: Some(v8_version_off), deno_version: Some(deno_version_off), no_color: !ansi::use_color(), @@ -307,7 +323,7 @@ fn op_start( } fn op_format_error( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -316,7 +332,7 @@ fn op_format_error( let orig_error = String::from(inner.error().unwrap()); let js_error = JSError::from_v8_exception(&orig_error).unwrap(); - let js_error_mapped = apply_source_map(&js_error, &cli.state.dir); + let js_error_mapped = apply_source_map(&js_error, &sc.state().dir); let js_error_string = JSErrorColor(&js_error_mapped).to_string(); let mut builder = FlatBufferBuilder::new(); @@ -367,7 +383,7 @@ pub fn odd_future(err: DenoError) -> Box<OpWithError> { // https://github.com/denoland/deno/blob/golang/os.go#L100-L154 fn op_fetch_module_meta_data( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -377,33 +393,15 @@ fn op_fetch_module_meta_data( let specifier = inner.specifier().unwrap(); let referrer = inner.referrer().unwrap(); - // Check for allow read since this operation could be used to read from the file system. - if !cli.permissions.allows_read() { - debug!("No read permission for fetch_module_meta_data"); - return odd_future(permission_denied()); - } - - // Check for allow write since this operation could be used to write to the file system. - if !cli.permissions.allows_write() { - debug!("No network permission for fetch_module_meta_data"); - return odd_future(permission_denied()); - } - - // Check for allow net since this operation could be used to make https/http requests. - if !cli.permissions.allows_net() { - debug!("No network permission for fetch_module_meta_data"); - return odd_future(permission_denied()); - } - assert_eq!( - cli.state.dir.root.join("gen"), - cli.state.dir.gen, + sc.state().dir.root.join("gen"), + sc.state().dir.gen, "Sanity check" ); Box::new(futures::future::result(|| -> OpResult { let builder = &mut FlatBufferBuilder::new(); - let out = cli.state.dir.fetch_module_meta_data(specifier, referrer)?; + let out = sc.state().dir.fetch_module_meta_data(specifier, referrer)?; let data_off = builder.create_vector(out.source_code.as_slice()); let msg_args = msg::FetchModuleMetaDataResArgs { module_name: Some(builder.create_string(&out.module_name)), @@ -425,7 +423,7 @@ fn op_fetch_module_meta_data( } fn op_chdir( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -439,19 +437,20 @@ fn op_chdir( } fn op_global_timer_stop( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { assert!(base.sync()); assert_eq!(data.len(), 0); - let mut t = cli.state.global_timer.lock().unwrap(); + let state = sc.state(); + let mut t = state.global_timer.lock().unwrap(); t.cancel(); ok_future(empty_buf()) } fn op_global_timer( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -462,7 +461,8 @@ fn op_global_timer( let val = inner.timeout(); assert!(val >= 0); - let mut t = cli.state.global_timer.lock().unwrap(); + let state = sc.state(); + let mut t = state.global_timer.lock().unwrap(); let deadline = Instant::now() + Duration::from_millis(val as u64); let f = t.new_timeout(deadline); @@ -483,7 +483,7 @@ fn op_global_timer( } fn op_set_env( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -491,18 +491,22 @@ fn op_set_env( let inner = base.inner_as_set_env().unwrap(); let key = inner.key().unwrap(); let value = inner.value().unwrap(); - if let Err(e) = cli.check_env() { + if let Err(e) = sc.state().check_env() { return odd_future(e); } std::env::set_var(key, value); ok_future(empty_buf()) } -fn op_env(cli: &Cli, base: &msg::Base<'_>, data: deno_buf) -> Box<OpWithError> { +fn op_env( + sc: Box<&IsolateStateContainer>, + base: &msg::Base<'_>, + data: deno_buf, +) -> Box<OpWithError> { assert_eq!(data.len(), 0); let cmd_id = base.cmd_id(); - if let Err(e) = cli.check_env() { + if let Err(e) = sc.state().check_env() { return odd_future(e); } @@ -527,7 +531,7 @@ fn op_env(cli: &Cli, base: &msg::Base<'_>, data: deno_buf) -> Box<OpWithError> { } fn op_permissions( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -537,11 +541,11 @@ fn op_permissions( let inner = msg::PermissionsRes::create( builder, &msg::PermissionsResArgs { - run: cli.permissions.allows_run(), - read: cli.permissions.allows_read(), - write: cli.permissions.allows_write(), - net: cli.permissions.allows_net(), - env: cli.permissions.allows_env(), + run: sc.state().permissions.allows_run(), + read: sc.state().permissions.allows_read(), + write: sc.state().permissions.allows_write(), + net: sc.state().permissions.allows_net(), + env: sc.state().permissions.allows_env(), }, ); ok_future(serialize_response( @@ -556,7 +560,7 @@ fn op_permissions( } fn op_revoke_permission( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -564,11 +568,11 @@ fn op_revoke_permission( let inner = base.inner_as_permission_revoke().unwrap(); let permission = inner.permission().unwrap(); let result = match permission { - "run" => cli.permissions.revoke_run(), - "read" => cli.permissions.revoke_read(), - "write" => cli.permissions.revoke_write(), - "net" => cli.permissions.revoke_net(), - "env" => cli.permissions.revoke_env(), + "run" => sc.state().permissions.revoke_run(), + "read" => sc.state().permissions.revoke_read(), + "write" => sc.state().permissions.revoke_write(), + "net" => sc.state().permissions.revoke_net(), + "env" => sc.state().permissions.revoke_env(), _ => Ok(()), }; if let Err(e) = result { @@ -578,7 +582,7 @@ fn op_revoke_permission( } fn op_fetch( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -601,7 +605,7 @@ fn op_fetch( } let req = maybe_req.unwrap(); - if let Err(e) = cli.check_net(url) { + if let Err(e) = sc.state().check_net(url) { return odd_future(e); } @@ -665,7 +669,7 @@ where } fn op_make_temp_dir( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -675,7 +679,7 @@ fn op_make_temp_dir( let cmd_id = base.cmd_id(); // FIXME - if let Err(e) = cli.check_write("make_temp") { + if let Err(e) = sc.state().check_write("make_temp") { return odd_future(e); } @@ -714,7 +718,7 @@ fn op_make_temp_dir( } fn op_mkdir( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -724,7 +728,7 @@ fn op_mkdir( let recursive = inner.recursive(); let mode = inner.mode(); - if let Err(e) = cli.check_write(&path) { + if let Err(e) = sc.state().check_write(&path) { return odd_future(e); } @@ -736,7 +740,7 @@ fn op_mkdir( } fn op_chmod( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -745,7 +749,7 @@ fn op_chmod( let _mode = inner.mode(); let path = String::from(inner.path().unwrap()); - if let Err(e) = cli.check_write(&path) { + if let Err(e) = sc.state().check_write(&path) { return odd_future(e); } @@ -775,7 +779,7 @@ fn op_chmod( } fn op_open( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -824,20 +828,20 @@ fn op_open( match mode { "r" => { - if let Err(e) = cli.check_read(&filename_str) { + if let Err(e) = sc.state().check_read(&filename_str) { return odd_future(e); } } "w" | "a" | "x" => { - if let Err(e) = cli.check_write(&filename_str) { + if let Err(e) = sc.state().check_write(&filename_str) { return odd_future(e); } } &_ => { - if let Err(e) = cli.check_read(&filename_str) { + if let Err(e) = sc.state().check_read(&filename_str) { return odd_future(e); } - if let Err(e) = cli.check_write(&filename_str) { + if let Err(e) = sc.state().check_write(&filename_str) { return odd_future(e); } } @@ -865,7 +869,7 @@ fn op_open( } fn op_close( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -882,7 +886,7 @@ fn op_close( } fn op_shutdown( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -908,7 +912,7 @@ fn op_shutdown( } fn op_read( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -946,7 +950,7 @@ fn op_read( } fn op_write( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -983,7 +987,7 @@ fn op_write( } fn op_seek( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1005,7 +1009,7 @@ fn op_seek( } fn op_remove( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1015,7 +1019,7 @@ fn op_remove( let path = PathBuf::from(path_); let recursive = inner.recursive(); - if let Err(e) = cli.check_write(path.to_str().unwrap()) { + if let Err(e) = sc.state().check_write(path.to_str().unwrap()) { return odd_future(e); } @@ -1035,7 +1039,7 @@ fn op_remove( // Prototype https://github.com/denoland/deno/blob/golang/os.go#L171-L184 fn op_read_file( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1045,7 +1049,7 @@ fn op_read_file( let filename_ = inner.filename().unwrap(); let filename = PathBuf::from(filename_); debug!("op_read_file {}", filename.display()); - if let Err(e) = cli.check_read(&filename_) { + if let Err(e) = sc.state().check_read(&filename_) { return odd_future(e); } blocking(base.sync(), move || { @@ -1073,7 +1077,7 @@ fn op_read_file( } fn op_copy_file( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1084,10 +1088,10 @@ fn op_copy_file( let to_ = inner.to().unwrap(); let to = PathBuf::from(to_); - if let Err(e) = cli.check_read(&from_) { + if let Err(e) = sc.state().check_read(&from_) { return odd_future(e); } - if let Err(e) = cli.check_write(&to_) { + if let Err(e) = sc.state().check_write(&to_) { return odd_future(e); } @@ -1129,7 +1133,7 @@ fn get_mode(_perm: &fs::Permissions) -> u32 { } fn op_cwd( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1155,7 +1159,7 @@ fn op_cwd( } fn op_stat( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1166,7 +1170,7 @@ fn op_stat( let filename = PathBuf::from(filename_); let lstat = inner.lstat(); - if let Err(e) = cli.check_read(&filename_) { + if let Err(e) = sc.state().check_read(&filename_) { return odd_future(e); } @@ -1207,7 +1211,7 @@ fn op_stat( } fn op_read_dir( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1216,7 +1220,7 @@ fn op_read_dir( let cmd_id = base.cmd_id(); let path = String::from(inner.path().unwrap()); - if let Err(e) = cli.check_read(&path) { + if let Err(e) = sc.state().check_read(&path) { return odd_future(e); } @@ -1268,7 +1272,7 @@ fn op_read_dir( } fn op_write_file( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1279,7 +1283,7 @@ fn op_write_file( let is_create = inner.is_create(); let is_append = inner.is_append(); - if let Err(e) = cli.check_write(&filename) { + if let Err(e) = sc.state().check_write(&filename) { return odd_future(e); } @@ -1298,7 +1302,7 @@ fn op_write_file( } fn op_rename( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1307,7 +1311,7 @@ fn op_rename( let oldpath = PathBuf::from(inner.oldpath().unwrap()); let newpath_ = inner.newpath().unwrap(); let newpath = PathBuf::from(newpath_); - if let Err(e) = cli.check_write(&newpath_) { + if let Err(e) = sc.state().check_write(&newpath_) { return odd_future(e); } blocking(base.sync(), move || -> OpResult { @@ -1318,7 +1322,7 @@ fn op_rename( } fn op_symlink( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1328,7 +1332,7 @@ fn op_symlink( let newname_ = inner.newname().unwrap(); let newname = PathBuf::from(newname_); - if let Err(e) = cli.check_write(&newname_) { + if let Err(e) = sc.state().check_write(&newname_) { return odd_future(e); } // TODO Use type for Windows. @@ -1347,7 +1351,7 @@ fn op_symlink( } fn op_read_link( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1357,7 +1361,7 @@ fn op_read_link( let name_ = inner.name().unwrap(); let name = PathBuf::from(name_); - if let Err(e) = cli.check_read(&name_) { + if let Err(e) = sc.state().check_read(&name_) { return odd_future(e); } @@ -1385,7 +1389,7 @@ fn op_read_link( } fn op_repl_start( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1395,7 +1399,7 @@ fn op_repl_start( let history_file = String::from(inner.history_file().unwrap()); debug!("op_repl_start {}", history_file); - let history_path = repl::history_path(&cli.state.dir, &history_file); + let history_path = repl::history_path(&sc.state().dir, &history_file); let repl = repl::Repl::new(history_path); let resource = resources::add_repl(repl); @@ -1416,7 +1420,7 @@ fn op_repl_start( } fn op_repl_readline( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1452,7 +1456,7 @@ fn op_repl_readline( } fn op_truncate( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1462,7 +1466,7 @@ fn op_truncate( let filename = String::from(inner.name().unwrap()); let len = inner.len(); - if let Err(e) = cli.check_write(&filename) { + if let Err(e) = sc.state().check_write(&filename) { return odd_future(e); } @@ -1475,12 +1479,12 @@ fn op_truncate( } fn op_listen( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { assert_eq!(data.len(), 0); - if let Err(e) = cli.check_net("listen") { + if let Err(e) = sc.state().check_net("listen") { return odd_future(e); } @@ -1537,12 +1541,12 @@ fn new_conn(cmd_id: u32, tcp_stream: TcpStream) -> OpResult { } fn op_accept( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { assert_eq!(data.len(), 0); - if let Err(e) = cli.check_net("accept") { + if let Err(e) = sc.state().check_net("accept") { return odd_future(e); } let cmd_id = base.cmd_id(); @@ -1563,12 +1567,12 @@ fn op_accept( } fn op_dial( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { assert_eq!(data.len(), 0); - if let Err(e) = cli.check_net("dial") { + if let Err(e) = sc.state().check_net("dial") { return odd_future(e); } let cmd_id = base.cmd_id(); @@ -1589,7 +1593,7 @@ fn op_dial( } fn op_metrics( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1599,7 +1603,7 @@ fn op_metrics( let builder = &mut FlatBufferBuilder::new(); let inner = msg::MetricsRes::create( builder, - &msg::MetricsResArgs::from(&cli.state.metrics), + &msg::MetricsResArgs::from(&sc.state().metrics), ); ok_future(serialize_response( cmd_id, @@ -1613,7 +1617,7 @@ fn op_metrics( } fn op_resources( - _cli: &Cli, + _sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1664,11 +1668,15 @@ fn subprocess_stdio_map(v: msg::ProcessStdio) -> std::process::Stdio { } } -fn op_run(cli: &Cli, base: &msg::Base<'_>, data: deno_buf) -> Box<OpWithError> { +fn op_run( + sc: Box<&IsolateStateContainer>, + base: &msg::Base<'_>, + data: deno_buf, +) -> Box<OpWithError> { assert!(base.sync()); let cmd_id = base.cmd_id(); - if let Err(e) = cli.check_run() { + if let Err(e) = sc.state().check_run() { return odd_future(e); } @@ -1734,7 +1742,7 @@ fn op_run(cli: &Cli, base: &msg::Base<'_>, data: deno_buf) -> Box<OpWithError> { } fn op_run_status( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1743,7 +1751,7 @@ fn op_run_status( let inner = base.inner_as_run_status().unwrap(); let rid = inner.rid(); - if let Err(e) = cli.check_run() { + if let Err(e) = sc.state().check_run() { return odd_future(e); } @@ -1810,7 +1818,7 @@ impl Future for GetMessageFuture { } fn op_worker_get_message( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1818,7 +1826,7 @@ fn op_worker_get_message( let cmd_id = base.cmd_id(); let op = GetMessageFuture { - state: cli.state.clone(), + state: sc.state().clone(), }; let op = op.map_err(move |_| -> DenoError { unimplemented!() }); let op = op.and_then(move |maybe_buf| -> DenoResult<Buf> { @@ -1844,7 +1852,7 @@ fn op_worker_get_message( } fn op_worker_post_message( - cli: &Cli, + sc: Box<&IsolateStateContainer>, base: &msg::Base<'_>, data: deno_buf, ) -> Box<OpWithError> { @@ -1852,8 +1860,8 @@ fn op_worker_post_message( let d = Vec::from(data.as_ref()).into_boxed_slice(); - assert!(cli.state.worker_channels.is_some()); - let tx = match cli.state.worker_channels { + assert!(sc.state().worker_channels.is_some()); + let tx = match sc.state().worker_channels { None => panic!("expected worker_channels"), Some(ref wc) => { let wc = wc.lock().unwrap(); @@ -1876,6 +1884,7 @@ fn op_worker_post_message( Box::new(op) } +/* #[cfg(test)] mod tests { use super::*; @@ -1893,7 +1902,7 @@ mod tests { allow_run: PermissionAccessor::from(true), ..Default::default() }; - let cli = Cli::new(None, state, permissions); + let cli = Cli::new(None, state); let builder = &mut FlatBufferBuilder::new(); let fetch_msg_args = msg::FetchModuleMetaDataArgs { specifier: Some(builder.create_string("./somefile")), @@ -1910,7 +1919,7 @@ mod tests { let data = builder.finished_data(); let final_msg = msg::get_root_as_base(&data); let fetch_result = - op_fetch_module_meta_data(&cli, &final_msg, deno_buf::empty()).wait(); + op_fetch_module_meta_data(Box::new(&cli), &final_msg, deno_buf::empty()).wait(); match fetch_result { Ok(_) => assert!(true), Err(e) => assert_eq!(e.to_string(), permission_denied().to_string()), @@ -2018,3 +2027,4 @@ mod tests { } } } +*/ |