Skip to content

Commit 636f45f

Browse files
committed
Clean up read/write ops
1 parent a6cb984 commit 636f45f

File tree

4 files changed

+72
-71
lines changed

4 files changed

+72
-71
lines changed

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/io.rs

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

cli/ops/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use hyper::rt::Future;
1212
use tokio_threadpool;
1313

1414
mod dispatch_minimal;
15-
use dispatch_minimal::{dispatch_minimal, parse_min_record};
15+
mod io;
16+
1617
mod compiler;
1718
use compiler::{op_cache, op_fetch_source_file};
1819
mod errors;
@@ -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

js/files.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import * as msg from "gen/cli/msg_generated";
1616
import { assert } from "./util";
1717
import * as flatbuffers from "./flatbuffers";
1818

19-
// Warning: these constants defined in two places. Here and in
20-
// dispatch_minimal.rs
19+
// Warning: These constants defined in two places. Here and in cli/ops/mod.rs.
2120
const OP_READ = 1;
2221
const OP_WRITE = 2;
2322

0 commit comments

Comments
 (0)