summaryrefslogtreecommitdiff
path: root/cli/ops/files.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2019-11-14 18:10:25 +0100
committerRy Dahl <ry@tinyclouds.org>2019-11-14 12:10:25 -0500
commit8b90b8e88325d28fe41d8312ea91417b6e66a12e (patch)
treea80d85de86353e05a5e59fe8b9890635dcbf200c /cli/ops/files.rs
parent38ffe8886db104068af07cd5efe3d83f42da8ed6 (diff)
refactor: per-worker resource table, take 2 (#3342)
- removes global `RESOURCE_TABLE` - resource tables are now created per `Worker` in `State` - renames `CliResource` to `StreamResource` and moves all logic related to it to `cli/ops/io.rs` - removes `cli/resources.rs` - adds `state` argument to `op_read` and `op_write` and consequently adds `stateful_minimal_op` to `State` - IMPORTANT NOTE: workers don't have access to process stdio - this is caused by fact that dropping worker would close stdout for process (because it's constructed from raw handle, which closes underlying file descriptor on drop)
Diffstat (limited to 'cli/ops/files.rs')
-rw-r--r--cli/ops/files.rs28
1 files changed, 16 insertions, 12 deletions
diff --git a/cli/ops/files.rs b/cli/ops/files.rs
index 04b5f98bf..fc1b8e7d8 100644
--- a/cli/ops/files.rs
+++ b/cli/ops/files.rs
@@ -1,12 +1,11 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_json::{Deserialize, JsonOp, Value};
+use super::io::StreamResource;
use crate::deno_error::bad_resource;
use crate::deno_error::DenoError;
use crate::deno_error::ErrorKind;
use crate::fs as deno_fs;
use crate::ops::json_op;
-use crate::resources;
-use crate::resources::CliResource;
use crate::state::ThreadSafeState;
use deno::*;
use futures::Future;
@@ -38,7 +37,7 @@ fn op_open(
let args: OpenArgs = serde_json::from_value(args)?;
let (filename, filename_) = deno_fs::resolve_from_cwd(&args.filename)?;
let mode = args.mode.as_ref();
-
+ let state_ = state.clone();
let mut open_options = tokio::fs::OpenOptions::new();
match mode {
@@ -91,7 +90,8 @@ fn op_open(
let is_sync = args.promise_id.is_none();
let op = open_options.open(filename).map_err(ErrBox::from).and_then(
move |fs_file| {
- let rid = resources::add_fs_file(fs_file);
+ let mut table = state_.lock_resource_table();
+ let rid = table.add("fsFile", Box::new(StreamResource::FsFile(fs_file)));
futures::future::ok(json!(rid))
},
);
@@ -110,21 +110,21 @@ struct CloseArgs {
}
fn op_close(
- _state: &ThreadSafeState,
+ state: &ThreadSafeState,
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let args: CloseArgs = serde_json::from_value(args)?;
- let mut table = resources::lock_resource_table();
+ let mut table = state.lock_resource_table();
table.close(args.rid as u32).ok_or_else(bad_resource)?;
Ok(JsonOp::Sync(json!({})))
}
-#[derive(Debug)]
pub struct SeekFuture {
seek_from: SeekFrom,
rid: ResourceId,
+ state: ThreadSafeState,
}
impl Future for SeekFuture {
@@ -132,13 +132,13 @@ impl Future for SeekFuture {
type Error = ErrBox;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
- let mut table = resources::lock_resource_table();
+ let mut table = self.state.lock_resource_table();
let resource = table
- .get_mut::<CliResource>(self.rid)
+ .get_mut::<StreamResource>(self.rid)
.ok_or_else(bad_resource)?;
let tokio_file = match resource {
- CliResource::FsFile(ref mut file) => file,
+ StreamResource::FsFile(ref mut file) => file,
_ => return Err(bad_resource()),
};
@@ -156,7 +156,7 @@ struct SeekArgs {
}
fn op_seek(
- _state: &ThreadSafeState,
+ state: &ThreadSafeState,
args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
@@ -177,7 +177,11 @@ fn op_seek(
}
};
- let fut = SeekFuture { seek_from, rid };
+ let fut = SeekFuture {
+ state: state.clone(),
+ seek_from,
+ rid,
+ };
let op = fut.and_then(move |_| futures::future::ok(json!({})));
if args.promise_id.is_none() {