refactor: rename ThreadSafeState, use RefCell for mutable state (#3931)

* rename ThreadSafeState to State
* State stores InnerState wrapped in Rc and RefCell
This commit is contained in:
Bartek Iwańczuk 2020-02-08 20:34:31 +01:00 committed by GitHub
parent 619a24390f
commit cdba5ab6fc
31 changed files with 454 additions and 464 deletions

View file

@ -4,12 +4,11 @@ use super::io::StreamResource;
use crate::deno_error::bad_resource;
use crate::ops::json_op;
use crate::signal::kill;
use crate::state::ThreadSafeState;
use crate::state::State;
use deno_core::*;
use futures;
use futures::future::FutureExt;
use futures::future::TryFutureExt;
use futures::task::SpawnExt;
use std;
use std::convert::From;
use std::future::Future;
@ -22,7 +21,7 @@ use tokio::process::Command;
#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
pub fn init(i: &mut Isolate, s: &State) {
i.register_op("run", s.core_op(json_op(s.stateful_op(op_run))));
i.register_op(
"run_status",
@ -31,12 +30,10 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
i.register_op("kill", s.core_op(json_op(s.stateful_op(op_kill))));
}
fn clone_file(
rid: u32,
state: &ThreadSafeState,
) -> Result<std::fs::File, ErrBox> {
let mut table = state.lock_resource_table();
let repr = table
fn clone_file(rid: u32, state: &State) -> Result<std::fs::File, ErrBox> {
let mut state = state.borrow_mut();
let repr = state
.resource_table
.get_mut::<StreamResource>(rid)
.ok_or_else(bad_resource)?;
let file = match repr {
@ -76,7 +73,7 @@ struct ChildResource {
}
fn op_run(
state: &ThreadSafeState,
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
@ -131,7 +128,8 @@ fn op_run(
let mut child = c.spawn()?;
let pid = child.id();
let mut table = state_.lock_resource_table();
let mut state = state_.borrow_mut();
let table = &mut state.resource_table;
let stdin_rid = match child.stdin.take() {
Some(child_stdin) => {
@ -180,7 +178,7 @@ fn op_run(
pub struct ChildStatus {
rid: ResourceId,
state: ThreadSafeState,
state: State,
}
impl Future for ChildStatus {
@ -188,8 +186,9 @@ impl Future for ChildStatus {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let inner = self.get_mut();
let mut table = inner.state.lock_resource_table();
let child_resource = table
let mut state = inner.state.borrow_mut();
let child_resource = state
.resource_table
.get_mut::<ChildResource>(inner.rid)
.ok_or_else(bad_resource)?;
let child = &mut child_resource.child;
@ -204,7 +203,7 @@ struct RunStatusArgs {
}
fn op_run_status(
state: &ThreadSafeState,
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {
@ -239,10 +238,7 @@ fn op_run_status(
}))
};
let pool = futures::executor::ThreadPool::new().unwrap();
let handle = pool.spawn_with_handle(future).unwrap();
Ok(JsonOp::Async(handle.boxed()))
Ok(JsonOp::Async(future.boxed_local()))
}
#[derive(Deserialize)]
@ -252,7 +248,7 @@ struct KillArgs {
}
fn op_kill(
state: &ThreadSafeState,
state: &State,
args: Value,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<JsonOp, ErrBox> {