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

@ -3,7 +3,7 @@ use crate::deno_error;
use crate::deno_error::bad_resource;
use crate::http_util::HttpBody;
use crate::ops::minimal_op;
use crate::state::ThreadSafeState;
use crate::state::State;
use deno_core::ErrBox;
use deno_core::*;
use futures::future::FutureExt;
@ -47,7 +47,7 @@ lazy_static! {
};
}
pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
pub fn init(i: &mut Isolate, s: &State) {
i.register_op(
"read",
s.core_op(minimal_op(s.stateful_minimal_op(op_read))),
@ -131,7 +131,7 @@ enum IoState {
///
/// The returned future will resolve to both the I/O stream and the buffer
/// as well as the number of bytes read once the read operation is completed.
pub fn read<T>(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Read<T>
pub fn read<T>(state: &State, rid: ResourceId, buf: T) -> Read<T>
where
T: AsMut<[u8]>,
{
@ -151,7 +151,7 @@ pub struct Read<T> {
rid: ResourceId,
buf: T,
io_state: IoState,
state: ThreadSafeState,
state: State,
}
impl<T> Future for Read<T>
@ -166,8 +166,9 @@ where
panic!("poll a Read after it's done");
}
let mut table = inner.state.lock_resource_table();
let resource = table
let mut state = inner.state.borrow_mut();
let resource = state
.resource_table
.get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?;
let nread = ready!(resource.poll_read(cx, &mut inner.buf.as_mut()[..]))?;
@ -177,7 +178,7 @@ where
}
pub fn op_read(
state: &ThreadSafeState,
state: &State,
rid: i32,
zero_copy: Option<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {
@ -257,7 +258,7 @@ pub struct Write<T> {
rid: ResourceId,
buf: T,
io_state: IoState,
state: ThreadSafeState,
state: State,
nwritten: i32,
}
@ -266,7 +267,7 @@ pub struct Write<T> {
///
/// Any error which happens during writing will cause both the stream and the
/// buffer to get destroyed.
pub fn write<T>(state: &ThreadSafeState, rid: ResourceId, buf: T) -> Write<T>
pub fn write<T>(state: &State, rid: ResourceId, buf: T) -> Write<T>
where
T: AsRef<[u8]>,
{
@ -294,8 +295,9 @@ where
}
if inner.io_state == IoState::Pending {
let mut table = inner.state.lock_resource_table();
let resource = table
let mut state = inner.state.borrow_mut();
let resource = state
.resource_table
.get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?;
@ -309,8 +311,9 @@ where
// Figure out why it's needed and preferably remove it.
// https://github.com/denoland/deno/issues/3565
if inner.io_state == IoState::Flush {
let mut table = inner.state.lock_resource_table();
let resource = table
let mut state = inner.state.borrow_mut();
let resource = state
.resource_table
.get_mut::<StreamResource>(inner.rid)
.ok_or_else(bad_resource)?;
ready!(resource.poll_flush(cx))?;
@ -322,7 +325,7 @@ where
}
pub fn op_write(
state: &ThreadSafeState,
state: &State,
rid: i32,
zero_copy: Option<ZeroCopyBuf>,
) -> Pin<Box<MinimalOp>> {