Skip to content

Commit 498f6ad

Browse files
authored
Remove dead code: legacy read/write ops (#2776)
readSync and writeSync use dispatch_minimal now.
1 parent e6c349a commit 498f6ad

File tree

9 files changed

+106
-223
lines changed

9 files changed

+106
-223
lines changed

cli/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub mod deno_dir;
2121
pub mod deno_error;
2222
pub mod diagnostics;
2323
mod disk_cache;
24-
mod dispatch_minimal;
2524
mod file_fetcher;
2625
pub mod flags;
2726
pub mod fmt_errors;

cli/msg.fbs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ union Any {
4848
PermissionRevoke,
4949
Permissions,
5050
PermissionsRes,
51-
Read,
5251
ReadDir,
5352
ReadDirRes,
54-
ReadRes,
5553
Readlink,
5654
ReadlinkRes,
5755
Remove,
@@ -83,8 +81,6 @@ union Any {
8381
WorkerGetMessage,
8482
WorkerGetMessageRes,
8583
WorkerPostMessage,
86-
Write,
87-
WriteRes,
8884
}
8985

9086
enum ErrorKind: byte {
@@ -491,24 +487,6 @@ table OpenRes {
491487
rid: uint32;
492488
}
493489

494-
table Read {
495-
rid: uint32;
496-
// (ptr, len) is passed as second parameter to Deno.core.send().
497-
}
498-
499-
table ReadRes {
500-
nread: uint;
501-
eof: bool;
502-
}
503-
504-
table Write {
505-
rid: uint32;
506-
}
507-
508-
table WriteRes {
509-
nbyte: uint;
510-
}
511-
512490
table Close {
513491
rid: uint32;
514492
}

cli/dispatch_minimal.rs renamed to cli/ops/dispatch_minimal.rs

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
use crate::state::ThreadSafeState;
88
use deno::Buf;
99
use deno::CoreOp;
10+
use deno::ErrBox;
1011
use deno::Op;
11-
use deno::OpId;
1212
use deno::PinnedBuf;
1313
use futures::Future;
1414

15-
const OP_READ: OpId = 1;
16-
const OP_WRITE: OpId = 2;
17-
1815
#[derive(Copy, Clone, Debug, PartialEq)]
1916
// This corresponds to RecordMinimal on the TS side.
2017
pub struct Record {
@@ -72,21 +69,23 @@ fn test_parse_min_record() {
7269
assert_eq!(parse_min_record(&buf), None);
7370
}
7471

75-
pub fn dispatch_minimal(
72+
pub type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;
73+
pub type Dispatcher = fn(i32, Option<PinnedBuf>) -> Box<MinimalOp>;
74+
75+
pub fn dispatch(
76+
d: Dispatcher,
7677
state: &ThreadSafeState,
77-
op_id: OpId,
78-
mut record: Record,
78+
control: &[u8],
7979
zero_copy: Option<PinnedBuf>,
8080
) -> CoreOp {
81+
let mut record = parse_min_record(control).unwrap();
8182
let is_sync = record.promise_id == 0;
82-
let min_op = match op_id {
83-
OP_READ => ops::read(record.arg, zero_copy),
84-
OP_WRITE => ops::write(record.arg, zero_copy),
85-
_ => unimplemented!(),
86-
};
8783

8884
let state = state.clone();
8985

86+
let rid = record.arg;
87+
let min_op = d(rid, zero_copy);
88+
9089
let fut = Box::new(min_op.then(move |result| -> Result<Buf, ()> {
9190
match result {
9291
Ok(r) => {
@@ -109,54 +108,3 @@ pub fn dispatch_minimal(
109108
Op::Async(fut)
110109
}
111110
}
112-
113-
mod ops {
114-
use crate::deno_error;
115-
use crate::resources;
116-
use crate::tokio_write;
117-
use deno::ErrBox;
118-
use deno::PinnedBuf;
119-
use futures::Future;
120-
121-
type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;
122-
123-
pub fn read(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
124-
debug!("read rid={}", rid);
125-
let zero_copy = match zero_copy {
126-
None => {
127-
return Box::new(
128-
futures::future::err(deno_error::no_buffer_specified()),
129-
)
130-
}
131-
Some(buf) => buf,
132-
};
133-
match resources::lookup(rid as u32) {
134-
None => Box::new(futures::future::err(deno_error::bad_resource())),
135-
Some(resource) => Box::new(
136-
tokio::io::read(resource, zero_copy)
137-
.map_err(ErrBox::from)
138-
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
139-
),
140-
}
141-
}
142-
143-
pub fn write(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
144-
debug!("write rid={}", rid);
145-
let zero_copy = match zero_copy {
146-
None => {
147-
return Box::new(
148-
futures::future::err(deno_error::no_buffer_specified()),
149-
)
150-
}
151-
Some(buf) => buf,
152-
};
153-
match resources::lookup(rid as u32) {
154-
None => Box::new(futures::future::err(deno_error::bad_resource())),
155-
Some(resource) => Box::new(
156-
tokio_write::write(resource, zero_copy)
157-
.map_err(ErrBox::from)
158-
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
159-
),
160-
}
161-
}
162-
}

cli/ops/files.rs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::ops::serialize_response;
88
use crate::ops::CliOpResult;
99
use crate::resources;
1010
use crate::state::ThreadSafeState;
11-
use crate::tokio_write;
1211
use deno::*;
1312
use flatbuffers::FlatBufferBuilder;
1413
use futures::Future;
@@ -119,91 +118,6 @@ pub fn op_close(
119118
}
120119
}
121120

122-
pub fn op_read(
123-
_state: &ThreadSafeState,
124-
base: &msg::Base<'_>,
125-
data: Option<PinnedBuf>,
126-
) -> CliOpResult {
127-
let cmd_id = base.cmd_id();
128-
let inner = base.inner_as_read().unwrap();
129-
let rid = inner.rid();
130-
131-
match resources::lookup(rid) {
132-
None => Err(deno_error::bad_resource()),
133-
Some(resource) => {
134-
let op = tokio::io::read(resource, data.unwrap())
135-
.map_err(ErrBox::from)
136-
.and_then(move |(_resource, _buf, nread)| {
137-
let builder = &mut FlatBufferBuilder::new();
138-
let inner = msg::ReadRes::create(
139-
builder,
140-
&msg::ReadResArgs {
141-
nread: nread as u32,
142-
eof: nread == 0,
143-
},
144-
);
145-
Ok(serialize_response(
146-
cmd_id,
147-
builder,
148-
msg::BaseArgs {
149-
inner: Some(inner.as_union_value()),
150-
inner_type: msg::Any::ReadRes,
151-
..Default::default()
152-
},
153-
))
154-
});
155-
if base.sync() {
156-
let buf = op.wait()?;
157-
Ok(Op::Sync(buf))
158-
} else {
159-
Ok(Op::Async(Box::new(op)))
160-
}
161-
}
162-
}
163-
}
164-
165-
pub fn op_write(
166-
_state: &ThreadSafeState,
167-
base: &msg::Base<'_>,
168-
data: Option<PinnedBuf>,
169-
) -> CliOpResult {
170-
let cmd_id = base.cmd_id();
171-
let inner = base.inner_as_write().unwrap();
172-
let rid = inner.rid();
173-
174-
match resources::lookup(rid) {
175-
None => Err(deno_error::bad_resource()),
176-
Some(resource) => {
177-
let op = tokio_write::write(resource, data.unwrap())
178-
.map_err(ErrBox::from)
179-
.and_then(move |(_resource, _buf, nwritten)| {
180-
let builder = &mut FlatBufferBuilder::new();
181-
let inner = msg::WriteRes::create(
182-
builder,
183-
&msg::WriteResArgs {
184-
nbyte: nwritten as u32,
185-
},
186-
);
187-
Ok(serialize_response(
188-
cmd_id,
189-
builder,
190-
msg::BaseArgs {
191-
inner: Some(inner.as_union_value()),
192-
inner_type: msg::Any::WriteRes,
193-
..Default::default()
194-
},
195-
))
196-
});
197-
if base.sync() {
198-
let buf = op.wait()?;
199-
Ok(Op::Sync(buf))
200-
} else {
201-
Ok(Op::Async(Box::new(op)))
202-
}
203-
}
204-
}
205-
}
206-
207121
pub fn op_seek(
208122
_state: &ThreadSafeState,
209123
base: &msg::Base<'_>,

cli/ops/io.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use super::dispatch_minimal::MinimalOp;
2+
use crate::deno_error;
3+
use crate::resources;
4+
use crate::tokio_write;
5+
use deno::ErrBox;
6+
use deno::PinnedBuf;
7+
use futures::Future;
8+
9+
pub fn op_read(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
10+
debug!("read rid={}", rid);
11+
let zero_copy = match zero_copy {
12+
None => {
13+
return Box::new(futures::future::err(deno_error::no_buffer_specified()))
14+
}
15+
Some(buf) => buf,
16+
};
17+
match resources::lookup(rid as u32) {
18+
None => Box::new(futures::future::err(deno_error::bad_resource())),
19+
Some(resource) => Box::new(
20+
tokio::io::read(resource, zero_copy)
21+
.map_err(ErrBox::from)
22+
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
23+
),
24+
}
25+
}
26+
27+
pub fn op_write(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
28+
debug!("write rid={}", rid);
29+
let zero_copy = match zero_copy {
30+
None => {
31+
return Box::new(futures::future::err(deno_error::no_buffer_specified()))
32+
}
33+
Some(buf) => buf,
34+
};
35+
match resources::lookup(rid as u32) {
36+
None => Box::new(futures::future::err(deno_error::bad_resource())),
37+
Some(resource) => Box::new(
38+
tokio_write::write(resource, zero_copy)
39+
.map_err(ErrBox::from)
40+
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
41+
),
42+
}
43+
}

cli/ops/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
22
use crate::deno_error::GetErrorKind;
3-
use crate::dispatch_minimal::dispatch_minimal;
4-
use crate::dispatch_minimal::parse_min_record;
53
use crate::msg;
64
use crate::state::ThreadSafeState;
75
use crate::tokio_util;
@@ -13,12 +11,15 @@ use hyper;
1311
use hyper::rt::Future;
1412
use tokio_threadpool;
1513

14+
mod dispatch_minimal;
15+
mod io;
16+
1617
mod compiler;
1718
use compiler::{op_cache, op_fetch_source_file};
1819
mod errors;
1920
use errors::{op_apply_source_map, op_format_error};
2021
mod files;
21-
use files::{op_close, op_open, op_read, op_seek, op_write};
22+
use files::{op_close, op_open, op_seek};
2223
mod fetch;
2324
use fetch::op_fetch;
2425
mod fs;
@@ -71,6 +72,8 @@ fn empty_buf() -> Buf {
7172
}
7273

7374
const FLATBUFFER_OP_ID: OpId = 44;
75+
const OP_READ: OpId = 1;
76+
const OP_WRITE: OpId = 2;
7477

7578
pub fn dispatch_all(
7679
state: &ThreadSafeState,
@@ -81,11 +84,18 @@ pub fn dispatch_all(
8184
) -> CoreOp {
8285
let bytes_sent_control = control.len();
8386
let bytes_sent_zero_copy = zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);
84-
let op = if op_id != FLATBUFFER_OP_ID {
85-
let min_record = parse_min_record(control).unwrap();
86-
dispatch_minimal(state, op_id, min_record, zero_copy)
87-
} else {
88-
dispatch_all_legacy(state, control, zero_copy, op_selector)
87+
88+
let op = match op_id {
89+
OP_READ => {
90+
dispatch_minimal::dispatch(io::op_read, state, control, zero_copy)
91+
}
92+
OP_WRITE => {
93+
dispatch_minimal::dispatch(io::op_write, state, control, zero_copy)
94+
}
95+
FLATBUFFER_OP_ID => {
96+
dispatch_all_legacy(state, control, zero_copy, op_selector)
97+
}
98+
_ => panic!("bad op_id"),
8999
};
90100
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
91101
op
@@ -226,7 +236,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
226236
msg::Any::Open => Some(op_open),
227237
msg::Any::PermissionRevoke => Some(op_revoke_permission),
228238
msg::Any::Permissions => Some(op_permissions),
229-
msg::Any::Read => Some(op_read),
230239
msg::Any::ReadDir => Some(op_read_dir),
231240
msg::Any::Readlink => Some(op_read_link),
232241
msg::Any::Remove => Some(op_remove),
@@ -245,7 +254,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
245254
msg::Any::Truncate => Some(op_truncate),
246255
msg::Any::HomeDir => Some(op_home_dir),
247256
msg::Any::Utime => Some(op_utime),
248-
msg::Any::Write => Some(op_write),
249257

250258
// TODO(ry) split these out so that only the appropriate Workers can access
251259
// them.

js/dispatch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ function flatbufferRecordFromBuf(buf: Uint8Array): FlatbufferRecord {
3232
}
3333

3434
export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void {
35-
const buf32 = new Int32Array(ui8.buffer, ui8.byteOffset, ui8.byteLength / 4);
3635
if (opId !== FLATBUFFER_OP_ID) {
3736
// Fast and new
37+
const buf32 = new Int32Array(
38+
ui8.buffer,
39+
ui8.byteOffset,
40+
ui8.byteLength / 4
41+
);
3842
const recordMin = recordFromBufMinimal(opId, buf32);
3943
handleAsyncMsgFromRustMinimal(ui8, recordMin);
4044
} else {

0 commit comments

Comments
 (0)