diff options
Diffstat (limited to 'cli/ops')
-rw-r--r-- | cli/ops/compiler.rs | 3 | ||||
-rw-r--r-- | cli/ops/errors.rs | 9 | ||||
-rw-r--r-- | cli/ops/fetch.rs | 14 | ||||
-rw-r--r-- | cli/ops/fs.rs | 52 | ||||
-rw-r--r-- | cli/ops/fs_events.rs | 7 | ||||
-rw-r--r-- | cli/ops/idna.rs | 5 | ||||
-rw-r--r-- | cli/ops/io.rs | 7 | ||||
-rw-r--r-- | cli/ops/net.rs | 17 | ||||
-rw-r--r-- | cli/ops/os.rs | 21 | ||||
-rw-r--r-- | cli/ops/permissions.rs | 18 | ||||
-rw-r--r-- | cli/ops/plugin.rs | 4 | ||||
-rw-r--r-- | cli/ops/process.rs | 9 | ||||
-rw-r--r-- | cli/ops/random.rs | 9 | ||||
-rw-r--r-- | cli/ops/repl.rs | 9 | ||||
-rw-r--r-- | cli/ops/resources.rs | 7 | ||||
-rw-r--r-- | cli/ops/runtime.rs | 15 | ||||
-rw-r--r-- | cli/ops/runtime_compiler.rs | 23 | ||||
-rw-r--r-- | cli/ops/signal.rs | 15 | ||||
-rw-r--r-- | cli/ops/timers.rs | 19 | ||||
-rw-r--r-- | cli/ops/tls.rs | 11 | ||||
-rw-r--r-- | cli/ops/tty.rs | 9 | ||||
-rw-r--r-- | cli/ops/web_worker.rs | 3 | ||||
-rw-r--r-- | cli/ops/worker_host.rs | 64 |
23 files changed, 178 insertions, 172 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index d41369855..7ad4aece3 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -6,12 +6,13 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::CoreIsolateState; use deno_core::ZeroCopyBuf; +use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; pub fn init( i: &mut CoreIsolate, - _s: &State, + _s: &Rc<State>, response: Arc<Mutex<Option<String>>>, ) { let custom_assets = std::collections::HashMap::new(); diff --git a/cli/ops/errors.rs b/cli/ops/errors.rs index ade125b1a..02295d7f8 100644 --- a/cli/ops/errors.rs +++ b/cli/ops/errors.rs @@ -8,8 +8,9 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use std::collections::HashMap; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op( "op_apply_source_map", s.stateful_json_op(op_apply_source_map), @@ -29,7 +30,7 @@ struct ApplySourceMap { } fn op_apply_source_map( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -42,7 +43,7 @@ fn op_apply_source_map( args.line_number.into(), args.column_number.into(), &mut mappings_map, - &state.borrow().global_state.ts_compiler, + &state.global_state.ts_compiler, ); Ok(JsonOp::Sync(json!({ @@ -53,7 +54,7 @@ fn op_apply_source_map( } fn op_format_diagnostic( - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/fetch.rs b/cli/ops/fetch.rs index 539316260..b320222ed 100644 --- a/cli/ops/fetch.rs +++ b/cli/ops/fetch.rs @@ -14,8 +14,9 @@ use http::Method; use reqwest::Client; use std::convert::From; use std::path::PathBuf; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_fetch", s.stateful_json_op2(op_fetch)); i.register_op( "op_create_http_client", @@ -34,22 +35,23 @@ struct FetchArgs { pub fn op_fetch( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, data: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: FetchArgs = serde_json::from_value(args)?; let url = args.url; let resource_table_ = isolate_state.resource_table.borrow(); - let state_ = state.borrow(); + let mut client_ref_mut; let client = if let Some(rid) = args.client_rid { let r = resource_table_ .get::<HttpClientResource>(rid) .ok_or_else(OpError::bad_resource_id)?; &r.client } else { - &state_.http_client + client_ref_mut = state.http_client.borrow_mut(); + &mut *client_ref_mut }; let method = match args.method { @@ -137,7 +139,7 @@ struct CreateHttpClientOptions { fn op_create_http_client( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -148,7 +150,7 @@ fn op_create_http_client( state.check_read(&PathBuf::from(ca_file))?; } - let client = create_http_client(args.ca_file).unwrap(); + let client = create_http_client(args.ca_file.as_deref()).unwrap(); let rid = resource_table.add("httpClient", Box::new(HttpClientResource::new(client))); diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index 2e6f076a6..0fd6ad5ef 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -23,7 +23,7 @@ use std::time::UNIX_EPOCH; use rand::{thread_rng, Rng}; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { let t = &CoreIsolate::state(i).borrow().resource_table.clone(); i.register_op("op_open_sync", s.stateful_json_op_sync(t, op_open_sync)); @@ -140,7 +140,7 @@ fn op_open_sync( } async fn op_open_async( - state: State, + state: Rc<State>, resource_table: Rc<RefCell<ResourceTable>>, args: Value, _zero_copy: BufVec, @@ -170,7 +170,7 @@ struct SeekArgs { fn op_seek( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -230,7 +230,7 @@ struct FdatasyncArgs { fn op_fdatasync( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -274,7 +274,7 @@ struct FsyncArgs { fn op_fsync( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -318,7 +318,7 @@ struct FstatArgs { fn op_fstat( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -360,7 +360,7 @@ struct UmaskArgs { } fn op_umask( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -398,7 +398,7 @@ struct ChdirArgs { } fn op_chdir( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -419,7 +419,7 @@ struct MkdirArgs { } fn op_mkdir( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -453,7 +453,7 @@ struct ChmodArgs { } fn op_chmod( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -493,7 +493,7 @@ struct ChownArgs { } fn op_chown( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -530,7 +530,7 @@ struct RemoveArgs { } fn op_remove( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -584,7 +584,7 @@ struct CopyFileArgs { } fn op_copy_file( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -678,7 +678,7 @@ struct StatArgs { } fn op_stat( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -708,7 +708,7 @@ struct RealpathArgs { } fn op_realpath( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -743,7 +743,7 @@ struct ReadDirArgs { } fn op_read_dir( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -786,7 +786,7 @@ struct RenameArgs { } fn op_rename( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -815,7 +815,7 @@ struct LinkArgs { } fn op_link( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -853,7 +853,7 @@ struct SymlinkOptions { } fn op_symlink( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -913,7 +913,7 @@ struct ReadLinkArgs { } fn op_read_link( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -941,7 +941,7 @@ struct FtruncateArgs { fn op_ftruncate( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -986,7 +986,7 @@ struct TruncateArgs { } fn op_truncate( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -1060,7 +1060,7 @@ struct MakeTempArgs { } fn op_make_temp_dir( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -1091,7 +1091,7 @@ fn op_make_temp_dir( } fn op_make_temp_file( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -1131,7 +1131,7 @@ struct UtimeArgs { } fn op_utime( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -1151,7 +1151,7 @@ fn op_utime( } fn op_cwd( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/fs_events.rs b/cli/ops/fs_events.rs index b9d54fc3e..fcaf138f8 100644 --- a/cli/ops/fs_events.rs +++ b/cli/ops/fs_events.rs @@ -17,9 +17,10 @@ use notify::Watcher; use serde::Serialize; use std::convert::From; use std::path::PathBuf; +use std::rc::Rc; use tokio::sync::mpsc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_fs_events_open", s.stateful_json_op2(op_fs_events_open)); i.register_op("op_fs_events_poll", s.stateful_json_op2(op_fs_events_poll)); } @@ -64,7 +65,7 @@ impl From<NotifyEvent> for FsEvent { pub fn op_fs_events_open( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -102,7 +103,7 @@ pub fn op_fs_events_open( pub fn op_fs_events_poll( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/idna.rs b/cli/ops/idna.rs index ee78307dc..c76ceba89 100644 --- a/cli/ops/idna.rs +++ b/cli/ops/idna.rs @@ -8,8 +8,9 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use idna::{domain_to_ascii, domain_to_ascii_strict}; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_domain_to_ascii", s.stateful_json_op(op_domain_to_ascii)); } @@ -21,7 +22,7 @@ struct DomainToAscii { } fn op_domain_to_ascii( - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/io.rs b/cli/ops/io.rs index c35e0d4b6..4d03e0ef7 100644 --- a/cli/ops/io.rs +++ b/cli/ops/io.rs @@ -11,6 +11,7 @@ use futures::future::FutureExt; use futures::ready; use std::collections::HashMap; use std::pin::Pin; +use std::rc::Rc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::task::Context; use std::task::Poll; @@ -84,7 +85,7 @@ lazy_static! { }; } -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_read", s.stateful_minimal_op2(op_read)); i.register_op("op_write", s.stateful_minimal_op2(op_write)); } @@ -236,7 +237,7 @@ impl DenoAsyncRead for StreamResource { pub fn op_read( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, is_sync: bool, rid: i32, zero_copy: &mut [ZeroCopyBuf], @@ -361,7 +362,7 @@ impl DenoAsyncWrite for StreamResource { pub fn op_write( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, is_sync: bool, rid: i32, zero_copy: &mut [ZeroCopyBuf], diff --git a/cli/ops/net.rs b/cli/ops/net.rs index 76abcb6c3..445c69106 100644 --- a/cli/ops/net.rs +++ b/cli/ops/net.rs @@ -13,6 +13,7 @@ use futures::future::FutureExt; use std::convert::From; use std::net::Shutdown; use std::net::SocketAddr; +use std::rc::Rc; use std::task::Context; use std::task::Poll; use tokio::net::TcpListener; @@ -22,7 +23,7 @@ use tokio::net::UdpSocket; #[cfg(unix)] use super::net_unix; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_accept", s.stateful_json_op2(op_accept)); i.register_op("op_connect", s.stateful_json_op2(op_connect)); i.register_op("op_shutdown", s.stateful_json_op2(op_shutdown)); @@ -102,7 +103,7 @@ fn accept_tcp( fn op_accept( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -126,7 +127,7 @@ struct ReceiveArgs { fn receive_udp( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: ReceiveArgs, zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -166,7 +167,7 @@ fn receive_udp( fn op_datagram_receive( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -196,7 +197,7 @@ struct SendArgs { fn op_datagram_send( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -266,7 +267,7 @@ struct ConnectArgs { fn op_connect( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -352,7 +353,7 @@ struct ShutdownArgs { fn op_shutdown( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -494,7 +495,7 @@ fn listen_udp( fn op_listen( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/os.rs b/cli/ops/os.rs index f084f213b..e46aff3e6 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -6,9 +6,10 @@ use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use std::collections::HashMap; use std::env; +use std::rc::Rc; use url::Url; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_exit", s.stateful_json_op(op_exit)); i.register_op("op_env", s.stateful_json_op(op_env)); i.register_op("op_exec_path", s.stateful_json_op(op_exec_path)); @@ -21,7 +22,7 @@ pub fn init(i: &mut CoreIsolate, s: &State) { } fn op_exec_path( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -41,7 +42,7 @@ struct SetEnv { } fn op_set_env( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -52,7 +53,7 @@ fn op_set_env( } fn op_env( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -67,7 +68,7 @@ struct GetEnv { } fn op_get_env( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -86,7 +87,7 @@ struct DeleteEnv { } fn op_delete_env( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -102,7 +103,7 @@ struct Exit { } fn op_exit( - _s: &State, + _s: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -111,7 +112,7 @@ fn op_exit( } fn op_loadavg( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -128,7 +129,7 @@ fn op_loadavg( } fn op_hostname( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -139,7 +140,7 @@ fn op_hostname( } fn op_os_release( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/permissions.rs b/cli/ops/permissions.rs index 571ea5b21..4cb998536 100644 --- a/cli/ops/permissions.rs +++ b/cli/ops/permissions.rs @@ -5,8 +5,9 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use std::path::Path; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op( "op_query_permission", s.stateful_json_op(op_query_permission), @@ -29,13 +30,12 @@ struct PermissionArgs { } pub fn op_query_permission( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: PermissionArgs = serde_json::from_value(args)?; - let state = state.borrow(); - let permissions = &state.permissions; + let permissions = state.permissions.borrow(); let path = args.path.as_deref(); let perm = match args.name.as_ref() { "read" => permissions.query_read(&path.as_deref().map(Path::new)), @@ -51,13 +51,12 @@ pub fn op_query_permission( } pub fn op_revoke_permission( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: PermissionArgs = serde_json::from_value(args)?; - let mut state = state.borrow_mut(); - let permissions = &mut state.permissions; + let mut permissions = state.permissions.borrow_mut(); let path = args.path.as_deref(); let perm = match args.name.as_ref() { "read" => permissions.revoke_read(&path.as_deref().map(Path::new)), @@ -73,13 +72,12 @@ pub fn op_revoke_permission( } pub fn op_request_permission( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: PermissionArgs = serde_json::from_value(args)?; - let mut state = state.borrow_mut(); - let permissions = &mut state.permissions; + let permissions = &mut state.permissions.borrow_mut(); let path = args.path.as_deref(); let perm = match args.name.as_ref() { "read" => permissions.request_read(&path.as_deref().map(Path::new)), diff --git a/cli/ops/plugin.rs b/cli/ops/plugin.rs index 16debac50..3fb149404 100644 --- a/cli/ops/plugin.rs +++ b/cli/ops/plugin.rs @@ -20,7 +20,7 @@ use std::rc::Rc; use std::task::Context; use std::task::Poll; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op( "op_open_plugin", s.core_op(json_op(s.stateful_op2(op_open_plugin))), @@ -35,7 +35,7 @@ struct OpenPluginArgs { pub fn op_open_plugin( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/process.rs b/cli/ops/process.rs index 384068e6a..bf733a78e 100644 --- a/cli/ops/process.rs +++ b/cli/ops/process.rs @@ -12,12 +12,13 @@ use futures::future::poll_fn; use futures::future::FutureExt; use futures::TryFutureExt; use std::convert::From; +use std::rc::Rc; use tokio::process::Command; #[cfg(unix)] use std::os::unix::process::ExitStatusExt; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_run", s.stateful_json_op2(op_run)); i.register_op("op_run_status", s.stateful_json_op2(op_run_status)); i.register_op("op_kill", s.stateful_json_op(op_kill)); @@ -62,7 +63,7 @@ struct ChildResource { fn op_run( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -173,7 +174,7 @@ struct RunStatusArgs { fn op_run_status( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -223,7 +224,7 @@ struct KillArgs { } fn op_kill( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/random.rs b/cli/ops/random.rs index b29c761b8..275f264f9 100644 --- a/cli/ops/random.rs +++ b/cli/ops/random.rs @@ -6,8 +6,9 @@ use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use rand::thread_rng; use rand::Rng; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op( "op_get_random_values", s.stateful_json_op(op_get_random_values), @@ -15,14 +16,14 @@ pub fn init(i: &mut CoreIsolate, s: &State) { } fn op_get_random_values( - state: &State, + state: &Rc<State>, _args: Value, zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { assert_eq!(zero_copy.len(), 1); - if let Some(ref mut seeded_rng) = state.borrow_mut().seeded_rng { - seeded_rng.fill(&mut *zero_copy[0]); + if let Some(seeded_rng) = &state.seeded_rng { + seeded_rng.borrow_mut().fill(&mut *zero_copy[0]); } else { let mut rng = thread_rng(); rng.fill(&mut *zero_copy[0]); diff --git a/cli/ops/repl.rs b/cli/ops/repl.rs index b8fd7ab8b..88b7de881 100644 --- a/cli/ops/repl.rs +++ b/cli/ops/repl.rs @@ -7,10 +7,11 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::CoreIsolateState; use deno_core::ZeroCopyBuf; +use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_repl_start", s.stateful_json_op2(op_repl_start)); i.register_op("op_repl_readline", s.stateful_json_op2(op_repl_readline)); } @@ -25,14 +26,14 @@ struct ReplStartArgs { fn op_repl_start( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: ReplStartArgs = serde_json::from_value(args)?; debug!("op_repl_start {}", args.history_file); let history_path = - repl::history_path(&state.borrow().global_state.dir, &args.history_file); + repl::history_path(&state.global_state.dir, &args.history_file); let repl = repl::Repl::new(history_path); let resource = ReplResource(Arc::new(Mutex::new(repl))); let mut resource_table = isolate_state.resource_table.borrow_mut(); @@ -48,7 +49,7 @@ struct ReplReadlineArgs { fn op_repl_readline( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/resources.rs b/cli/ops/resources.rs index a66a66170..a24b3b382 100644 --- a/cli/ops/resources.rs +++ b/cli/ops/resources.rs @@ -5,15 +5,16 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::CoreIsolateState; use deno_core::ZeroCopyBuf; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_resources", s.stateful_json_op2(op_resources)); i.register_op("op_close", s.stateful_json_op2(op_close)); } fn op_resources( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -24,7 +25,7 @@ fn op_resources( /// op_close removes a resource from the resource table. fn op_close( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/runtime.rs b/cli/ops/runtime.rs index 55f0d9f57..b4fb3c496 100644 --- a/cli/ops/runtime.rs +++ b/cli/ops/runtime.rs @@ -9,19 +9,19 @@ use deno_core::CoreIsolate; use deno_core::ModuleSpecifier; use deno_core::ZeroCopyBuf; use std::env; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_start", s.stateful_json_op(op_start)); i.register_op("op_main_module", s.stateful_json_op(op_main_module)); i.register_op("op_metrics", s.stateful_json_op(op_metrics)); } fn op_start( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { - let state = state.borrow(); let gs = &state.global_state; Ok(JsonOp::Sync(json!({ @@ -43,11 +43,11 @@ fn op_start( } fn op_main_module( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { - let main = &state.borrow().main_module.to_string(); + let main = &state.main_module.to_string(); let main_url = ModuleSpecifier::resolve_url_or_path(&main)?; if main_url.as_url().scheme() == "file" { let main_path = std::env::current_dir().unwrap().join(main_url.to_string()); @@ -57,12 +57,11 @@ fn op_main_module( } fn op_metrics( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { - let state = state.borrow(); - let m = &state.metrics; + let m = &state.metrics.borrow(); Ok(JsonOp::Sync(json!({ "opsDispatched": m.ops_dispatched, diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index e70b69de7..d77ab8f08 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -9,8 +9,9 @@ use crate::tsc::runtime_transpile; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use std::collections::HashMap; +use std::rc::Rc; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_compile", s.stateful_json_op(op_compile)); i.register_op("op_transpile", s.stateful_json_op(op_transpile)); } @@ -25,19 +26,18 @@ struct CompileArgs { } fn op_compile( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { state.check_unstable("Deno.compile"); let args: CompileArgs = serde_json::from_value(args)?; - let s = state.borrow(); - let global_state = s.global_state.clone(); - let permissions = s.permissions.clone(); + let global_state = state.global_state.clone(); + let permissions = state.permissions.borrow().clone(); let fut = async move { let fut = if args.bundle { runtime_bundle( - global_state, + &global_state, permissions, &args.root_name, &args.sources, @@ -46,7 +46,7 @@ fn op_compile( .boxed_local() } else { runtime_compile( - global_state, + &global_state, permissions, &args.root_name, &args.sources, @@ -68,17 +68,16 @@ struct TranspileArgs { } fn op_transpile( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { state.check_unstable("Deno.transpile"); let args: TranspileArgs = serde_json::from_value(args)?; - let s = state.borrow(); - let global_state = s.global_state.clone(); - let permissions = s.permissions.clone(); + let global_state = state.global_state.clone(); + let permissions = state.permissions.borrow().clone(); let fut = async move { - runtime_transpile(global_state, permissions, &args.sources, &args.options) + runtime_transpile(&global_state, permissions, &args.sources, &args.options) .await } .boxed_local(); diff --git a/cli/ops/signal.rs b/cli/ops/signal.rs index 6457ac42e..cb83427c8 100644 --- a/cli/ops/signal.rs +++ b/cli/ops/signal.rs @@ -5,6 +5,7 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::CoreIsolateState; use deno_core::ZeroCopyBuf; +use std::rc::Rc; #[cfg(unix)] use super::dispatch_json::Deserialize; @@ -15,7 +16,7 @@ use std::task::Waker; #[cfg(unix)] use tokio::signal::unix::{signal, Signal, SignalKind}; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_signal_bind", s.stateful_json_op2(op_signal_bind)); i.register_op("op_signal_unbind", s.stateful_json_op2(op_signal_unbind)); i.register_op("op_signal_poll", s.stateful_json_op2(op_signal_poll)); @@ -41,7 +42,7 @@ struct SignalArgs { #[cfg(unix)] fn op_signal_bind( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -63,7 +64,7 @@ fn op_signal_bind( #[cfg(unix)] fn op_signal_poll( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -90,7 +91,7 @@ fn op_signal_poll( #[cfg(unix)] pub fn op_signal_unbind( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -115,7 +116,7 @@ pub fn op_signal_unbind( #[cfg(not(unix))] pub fn op_signal_bind( _isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -125,7 +126,7 @@ pub fn op_signal_bind( #[cfg(not(unix))] fn op_signal_unbind( _isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -135,7 +136,7 @@ fn op_signal_unbind( #[cfg(not(unix))] fn op_signal_poll( _isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/timers.rs b/cli/ops/timers.rs index 21efa8cf9..e3fe14c2c 100644 --- a/cli/ops/timers.rs +++ b/cli/ops/timers.rs @@ -5,10 +5,11 @@ use crate::state::State; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use futures::future::FutureExt; +use std::rc::Rc; use std::time::Duration; use std::time::Instant; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op( "op_global_timer_stop", s.stateful_json_op(op_global_timer_stop), @@ -18,12 +19,11 @@ pub fn init(i: &mut CoreIsolate, s: &State) { } fn op_global_timer_stop( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { - let mut state = state.borrow_mut(); - state.global_timer.cancel(); + state.global_timer.borrow_mut().cancel(); Ok(JsonOp::Sync(json!({}))) } @@ -33,17 +33,17 @@ struct GlobalTimerArgs { } fn op_global_timer( - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: GlobalTimerArgs = serde_json::from_value(args)?; let val = args.timeout; - let mut state = state.borrow_mut(); let deadline = Instant::now() + Duration::from_millis(val); let f = state .global_timer + .borrow_mut() .new_timeout(deadline) .then(move |_| futures::future::ok(json!({}))); @@ -55,13 +55,12 @@ fn op_global_timer( // If the High precision flag is not set, the // nanoseconds are rounded on 2ms. fn op_now( - state: &State, + state: &Rc<State>, _args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { - let inner_state = state.borrow(); - let seconds = inner_state.start_time.elapsed().as_secs(); - let mut subsec_nanos = inner_state.start_time.elapsed().subsec_nanos(); + let seconds = state.start_time.elapsed().as_secs(); + let mut subsec_nanos = state.start_time.elapsed().subsec_nanos(); let reduced_time_precision = 2_000_000; // 2ms in nanoseconds // If the permission is not enabled diff --git a/cli/ops/tls.rs b/cli/ops/tls.rs index 3e22c71ea..76962f5c8 100644 --- a/cli/ops/tls.rs +++ b/cli/ops/tls.rs @@ -14,6 +14,7 @@ use std::fs::File; use std::io::BufReader; use std::net::SocketAddr; use std::path::Path; +use std::rc::Rc; use std::sync::Arc; use std::task::Context; use std::task::Poll; @@ -29,7 +30,7 @@ use tokio_rustls::{ }; use webpki::DNSNameRef; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_start_tls", s.stateful_json_op2(op_start_tls)); i.register_op("op_connect_tls", s.stateful_json_op2(op_connect_tls)); i.register_op("op_listen_tls", s.stateful_json_op2(op_listen_tls)); @@ -55,7 +56,7 @@ struct StartTLSArgs { pub fn op_start_tls( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -134,7 +135,7 @@ pub fn op_start_tls( pub fn op_connect_tls( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -308,7 +309,7 @@ struct ListenTlsArgs { fn op_listen_tls( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -358,7 +359,7 @@ struct AcceptTlsArgs { fn op_accept_tls( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/tty.rs b/cli/ops/tty.rs index d86100232..d6aaeb785 100644 --- a/cli/ops/tty.rs +++ b/cli/ops/tty.rs @@ -10,6 +10,7 @@ use deno_core::ZeroCopyBuf; use nix::sys::termios; use serde_derive::{Deserialize, Serialize}; use serde_json::Value; +use std::rc::Rc; #[cfg(windows)] use winapi::shared::minwindef::DWORD; @@ -35,7 +36,7 @@ fn get_windows_handle( Ok(handle) } -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_set_raw", s.stateful_json_op2(op_set_raw)); i.register_op("op_isatty", s.stateful_json_op2(op_isatty)); i.register_op("op_console_size", s.stateful_json_op2(op_console_size)); @@ -49,7 +50,7 @@ struct SetRawArgs { pub fn op_set_raw( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -218,7 +219,7 @@ struct IsattyArgs { pub fn op_isatty( isolate_state: &mut CoreIsolateState, - _state: &State, + _state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -265,7 +266,7 @@ struct ConsoleSize { pub fn op_console_size( isolate_state: &mut CoreIsolateState, - state: &State, + state: &Rc<State>, args: Value, _zero_copy: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { diff --git a/cli/ops/web_worker.rs b/cli/ops/web_worker.rs index 553278b07..8ad497d5c 100644 --- a/cli/ops/web_worker.rs +++ b/cli/ops/web_worker.rs @@ -10,6 +10,7 @@ use deno_core::CoreIsolateState; use deno_core::ZeroCopyBuf; use futures::channel::mpsc; use std::convert::From; +use std::rc::Rc; pub fn web_worker_op<D>( sender: mpsc::Sender<WorkerEvent>, @@ -59,7 +60,7 @@ where pub fn init( i: &mut CoreIsolate, - s: &State, + s: &Rc<State>, sender: &mpsc::Sender<WorkerEvent>, handle: WebWorkerHandle, ) { diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index e3571f713..47ebd9c7f 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -17,9 +17,11 @@ use deno_core::ModuleSpecifier; use deno_core::ZeroCopyBuf; use futures::future::FutureExt; use std::convert::From; +use std::rc::Rc; +use std::sync::Arc; use std::thread::JoinHandle; -pub fn init(i: &mut CoreIsolate, s: &State) { +pub fn init(i: &mut CoreIsolate, s: &Rc<State>) { i.register_op("op_create_worker", s.stateful_json_op(op_create_worker)); i.register_op( "op_host_terminate_worker", @@ -38,7 +40,7 @@ pub fn init(i: &mut CoreIsolate, s: &State) { fn create_web_worker( worker_id: u32, name: String, - global_state: GlobalState, + global_state: &Arc<GlobalState>, permissions: Permissions, specifier: ModuleSpecifier, has_deno_namespace: bool, @@ -49,7 +51,7 @@ fn create_web_worker( let mut worker = WebWorker::new( name.clone(), startup_data::deno_isolate_init(), - state, + &state, has_deno_namespace, ); @@ -84,12 +86,13 @@ fn create_web_worker( fn run_worker_thread( worker_id: u32, name: String, - global_state: GlobalState, + global_state: &Arc<GlobalState>, permissions: Permissions, specifier: ModuleSpecifier, has_deno_namespace: bool, maybe_source_code: Option<String>, ) -> Result<(JoinHandle<()>, WebWorkerHandle), ErrBox> { + let global_state = global_state.clone(); let (handle_sender, handle_receiver) = std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, ErrBox>>(1); @@ -103,7 +106,7 @@ fn run_worker_thread( let result = create_web_worker( worker_id, name, - global_state, + &global_state, permissions, specifier.clone(), has_deno_namespace, @@ -178,7 +181,7 @@ struct CreateWorkerArgs { /// Create worker as the host fn op_create_worker( - state: &State, + state: &Rc<State>, args: Value, _data: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -196,12 +199,10 @@ fn op_create_worker( state.check_unstable("Worker.deno"); } let parent_state = state.clone(); - let mut state = state.borrow_mut(); let global_state = state.global_state.clone(); - let permissions = state.permissions.clone(); - let worker_id = state.next_worker_id; - state.next_worker_id += 1; - drop(state); + let permissions = state.permissions.borrow().clone(); + let worker_id = state.next_worker_id.get(); + state.next_worker_id.set(worker_id + 1); let module_specifier = ModuleSpecifier::resolve_url(&specifier)?; let worker_name = args_name.unwrap_or_else(|| "".to_string()); @@ -209,7 +210,7 @@ fn op_create_worker( let (join_handle, worker_handle) = run_worker_thread( worker_id, worker_name, - global_state, + &global_state, permissions, module_specifier, use_deno_namespace, @@ -218,9 +219,9 @@ fn op_create_worker( .map_err(|e| OpError::other(e.to_string()))?; // At this point all interactions with worker happen using thread // safe handler returned from previous function call - let mut parent_state = parent_state.borrow_mut(); parent_state .workers + .borrow_mut() .insert(worker_id, (join_handle, worker_handle)); Ok(JsonOp::Sync(json!({ "id": worker_id }))) @@ -232,15 +233,17 @@ struct WorkerArgs { } fn op_host_terminate_worker( - state: &State, + state: &Rc<State>, args: Value, _data: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: WorkerArgs = serde_json::from_value(args)?; let id = args.id as u32; - let mut state = state.borrow_mut(); - let (join_handle, worker_handle) = - state.workers.remove(&id).expect("No worker handle found"); + let (join_handle, worker_handle) = state + .workers + .borrow_mut() + .remove(&id) + .expect("No worker handle found"); worker_handle.terminate(); join_handle.join().expect("Panic in worker thread"); Ok(JsonOp::Sync(json!({}))) @@ -298,27 +301,21 @@ fn serialize_worker_event(event: WorkerEvent) -> Value { /// Get message from guest worker as host fn op_host_get_message( - state: &State, + state: &Rc<State>, args: Value, _data: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { let args: WorkerArgs = serde_json::from_value(args)?; let id = args.id as u32; - let worker_handle = { - let state_ = state.borrow(); - let (_join_handle, worker_handle) = - state_.workers.get(&id).expect("No worker handle found"); - worker_handle.clone() - }; - let state_ = state.clone(); + let state = state.clone(); + let worker_handle = state.workers.borrow()[&id].1.clone(); let op = async move { let response = match worker_handle.get_event().await? { Some(event) => { // Terminal error means that worker should be removed from worker table. if let WorkerEvent::TerminalError(_) = &event { - let mut state_ = state_.borrow_mut(); if let Some((join_handle, mut worker_handle)) = - state_.workers.remove(&id) + state.workers.borrow_mut().remove(&id) { worker_handle.sender.close_channel(); join_handle.join().expect("Worker thread panicked"); @@ -328,12 +325,10 @@ fn op_host_get_message( } None => { // Worker shuts down - let mut state_ = state_.borrow_mut(); + let mut workers = state.workers.borrow_mut(); // Try to remove worker from workers table - NOTE: `Worker.terminate()` might have been called // already meaning that we won't find worker in table - in that case ignore. - if let Some((join_handle, mut worker_handle)) = - state_.workers.remove(&id) - { + if let Some((join_handle, mut worker_handle)) = workers.remove(&id) { worker_handle.sender.close_channel(); join_handle.join().expect("Worker thread panicked"); } @@ -347,7 +342,7 @@ fn op_host_get_message( /// Post message to guest worker as host fn op_host_post_message( - state: &State, + state: &Rc<State>, args: Value, data: &mut [ZeroCopyBuf], ) -> Result<JsonOp, OpError> { @@ -357,9 +352,8 @@ fn op_host_post_message( let msg = Vec::from(&*data[0]).into_boxed_slice(); debug!("post message to worker {}", id); - let state = state.borrow(); - let (_, worker_handle) = - state.workers.get(&id).expect("No worker handle found"); + let workers = state.workers.borrow(); + let worker_handle = workers[&id].1.clone(); worker_handle .post_message(msg) .map_err(|e| OpError::other(e.to_string()))?; |