Skip to content

Commit fd62379

Browse files
bartlomiejury
authored andcommitted
refactor: per-worker resource table (denoland#3306)
- 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)
1 parent af448e8 commit fd62379

14 files changed

+358
-336
lines changed

cli/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub mod permissions;
4343
mod progress;
4444
mod repl;
4545
pub mod resolve_addr;
46-
pub mod resources;
4746
mod shell;
4847
mod signal;
4948
pub mod source_maps;
@@ -57,6 +56,7 @@ pub mod worker;
5756
use crate::deno_error::js_check;
5857
use crate::deno_error::print_err_and_exit;
5958
use crate::global_state::ThreadSafeGlobalState;
59+
use crate::ops::io::get_stdio;
6060
use crate::progress::Progress;
6161
use crate::state::ThreadSafeState;
6262
use crate::worker::Worker;
@@ -128,6 +128,15 @@ fn create_worker_and_state(
128128
.map_err(deno_error::print_err_and_exit)
129129
.unwrap();
130130

131+
let state_ = state.clone();
132+
{
133+
let mut resource_table = state_.lock_resource_table();
134+
let (stdin, stdout, stderr) = get_stdio();
135+
resource_table.add("stdin", Box::new(stdin));
136+
resource_table.add("stdout", Box::new(stdout));
137+
resource_table.add("stderr", Box::new(stderr));
138+
}
139+
131140
let worker = Worker::new(
132141
"main".to_string(),
133142
startup_data::deno_isolate_init(),

cli/ops/dispatch_minimal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use deno::PinnedBuf;
1515
use futures::Future;
1616

1717
pub type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;
18-
pub type Dispatcher = fn(i32, Option<PinnedBuf>) -> Box<MinimalOp>;
1918

2019
#[derive(Copy, Clone, Debug, PartialEq)]
2120
// This corresponds to RecordMinimal on the TS side.
@@ -112,9 +111,10 @@ fn test_parse_min_record() {
112111
assert_eq!(parse_min_record(&buf), None);
113112
}
114113

115-
pub fn minimal_op(
116-
d: Dispatcher,
117-
) -> impl Fn(&[u8], Option<PinnedBuf>) -> CoreOp {
114+
pub fn minimal_op<D>(d: D) -> impl Fn(&[u8], Option<PinnedBuf>) -> CoreOp
115+
where
116+
D: Fn(i32, Option<PinnedBuf>) -> Box<MinimalOp>,
117+
{
118118
move |control: &[u8], zero_copy: Option<PinnedBuf>| {
119119
let mut record = match parse_min_record(control) {
120120
Some(r) => r,

cli/ops/fetch.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
use super::dispatch_json::{Deserialize, JsonOp, Value};
3+
use super::io::StreamResource;
4+
use crate::http_body::HttpBody;
35
use crate::http_util::get_client;
46
use crate::ops::json_op;
5-
use crate::resources;
67
use crate::state::ThreadSafeState;
78
use deno::*;
89
use http::header::HeaderName;
@@ -54,15 +55,17 @@ pub fn op_fetch(
5455
request = request.header(name, v);
5556
}
5657
debug!("Before fetch {}", url);
58+
let state_ = state.clone();
5759
let future = request.send().map_err(ErrBox::from).and_then(move |res| {
5860
let status = res.status();
5961
let mut res_headers = Vec::new();
6062
for (key, val) in res.headers().iter() {
6163
res_headers.push((key.to_string(), val.to_str().unwrap().to_owned()));
6264
}
6365

64-
let body = res.into_body();
65-
let rid = resources::add_reqwest_body(body);
66+
let body = HttpBody::from(res.into_body());
67+
let mut table = state_.lock_resource_table();
68+
let rid = table.add("httpBody", Box::new(StreamResource::HttpBody(body)));
6669

6770
let json_res = json!({
6871
"bodyRid": rid,

cli/ops/files.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
use super::dispatch_json::{Deserialize, JsonOp, Value};
3+
use super::io::StreamResource;
34
use crate::deno_error::bad_resource;
45
use crate::deno_error::DenoError;
56
use crate::deno_error::ErrorKind;
67
use crate::fs as deno_fs;
78
use crate::ops::json_op;
8-
use crate::resources;
9-
use crate::resources::CliResource;
109
use crate::state::ThreadSafeState;
1110
use deno::*;
1211
use futures::Future;
@@ -38,7 +37,7 @@ fn op_open(
3837
let args: OpenArgs = serde_json::from_value(args)?;
3938
let (filename, filename_) = deno_fs::resolve_from_cwd(&args.filename)?;
4039
let mode = args.mode.as_ref();
41-
40+
let state_ = state.clone();
4241
let mut open_options = tokio::fs::OpenOptions::new();
4342

4443
match mode {
@@ -91,7 +90,8 @@ fn op_open(
9190
let is_sync = args.promise_id.is_none();
9291
let op = open_options.open(filename).map_err(ErrBox::from).and_then(
9392
move |fs_file| {
94-
let rid = resources::add_fs_file(fs_file);
93+
let mut table = state_.lock_resource_table();
94+
let rid = table.add("fsFile", Box::new(StreamResource::FsFile(fs_file)));
9595
futures::future::ok(json!(rid))
9696
},
9797
);
@@ -110,35 +110,35 @@ struct CloseArgs {
110110
}
111111

112112
fn op_close(
113-
_state: &ThreadSafeState,
113+
state: &ThreadSafeState,
114114
args: Value,
115115
_zero_copy: Option<PinnedBuf>,
116116
) -> Result<JsonOp, ErrBox> {
117117
let args: CloseArgs = serde_json::from_value(args)?;
118118

119-
let mut table = resources::lock_resource_table();
119+
let mut table = state.lock_resource_table();
120120
table.close(args.rid as u32).ok_or_else(bad_resource)?;
121121
Ok(JsonOp::Sync(json!({})))
122122
}
123123

124-
#[derive(Debug)]
125124
pub struct SeekFuture {
126125
seek_from: SeekFrom,
127126
rid: ResourceId,
127+
state: ThreadSafeState,
128128
}
129129

130130
impl Future for SeekFuture {
131131
type Item = u64;
132132
type Error = ErrBox;
133133

134134
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
135-
let mut table = resources::lock_resource_table();
135+
let mut table = self.state.lock_resource_table();
136136
let resource = table
137-
.get_mut::<CliResource>(self.rid)
137+
.get_mut::<StreamResource>(self.rid)
138138
.ok_or_else(bad_resource)?;
139139

140140
let tokio_file = match resource {
141-
CliResource::FsFile(ref mut file) => file,
141+
StreamResource::FsFile(ref mut file) => file,
142142
_ => return Err(bad_resource()),
143143
};
144144

@@ -156,7 +156,7 @@ struct SeekArgs {
156156
}
157157

158158
fn op_seek(
159-
_state: &ThreadSafeState,
159+
state: &ThreadSafeState,
160160
args: Value,
161161
_zero_copy: Option<PinnedBuf>,
162162
) -> Result<JsonOp, ErrBox> {
@@ -177,7 +177,11 @@ fn op_seek(
177177
}
178178
};
179179

180-
let fut = SeekFuture { seek_from, rid };
180+
let fut = SeekFuture {
181+
state: state.clone(),
182+
seek_from,
183+
rid,
184+
};
181185

182186
let op = fut.and_then(move |_| futures::future::ok(json!({})));
183187
if args.promise_id.is_none() {

0 commit comments

Comments
 (0)