Skip to content

Commit

Permalink
simplify cli implementation a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
afinch7 committed Aug 29, 2019
1 parent 25de668 commit 51a7c58
Show file tree
Hide file tree
Showing 21 changed files with 309 additions and 285 deletions.
2 changes: 0 additions & 2 deletions cli/ops/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl OpDispatcher for OpCache {

Ok(JsonOp::Sync(json!({})))
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -103,7 +102,6 @@ impl OpDispatcher for OpFetchSourceFile {
"sourceCode": String::from_utf8(out.source_code).unwrap(),
})))
},
&self.state,
control,
buf,
)
Expand Down
16 changes: 2 additions & 14 deletions cli/ops/dispatch_json.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno::*;
use futures::Future;
Expand Down Expand Up @@ -46,16 +45,12 @@ struct AsyncArgs {

pub fn wrap_json_op<D>(
d: D,
state: &ThreadSafeState,
control: &[u8],
zero_copy: Option<PinnedBuf>,
) -> CoreOp
where
D: FnOnce(Value, Option<PinnedBuf>) -> Result<JsonOp, ErrBox>,
{
let bytes_sent_control = control.len();
let bytes_sent_zero_copy = zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);

let async_args: AsyncArgs = serde_json::from_slice(control).unwrap();
let promise_id = async_args.promise_id;
let is_sync = promise_id.is_none();
Expand All @@ -64,27 +59,20 @@ where
.map_err(ErrBox::from)
.and_then(move |args| d(args, zero_copy));

let state = state.clone();
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);
match result {
Ok(JsonOp::Sync(sync_value)) => {
assert!(promise_id.is_none());
let buf = serialize_result(promise_id, Ok(sync_value));
state.metrics_op_completed(buf.len());
CoreOp::Sync(buf)
CoreOp::Sync(serialize_result(promise_id, Ok(sync_value)))
}
Ok(JsonOp::Async(fut)) => {
assert!(promise_id.is_some());
let fut2 = Box::new(fut.then(move |result| -> Result<Buf, ()> {
let buf = serialize_result(promise_id, result);
state.metrics_op_completed(buf.len());
Ok(buf)
Ok(serialize_result(promise_id, result))
}));
CoreOp::Async(fut2)
}
Err(sync_err) => {
let buf = serialize_result(promise_id, Err(sync_err));
state.metrics_op_completed(buf.len());
if is_sync {
CoreOp::Sync(buf)
} else {
Expand Down
12 changes: 1 addition & 11 deletions cli/ops/dispatch_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! alternative to flatbuffers using a very simple list of int32s to lay out
//! messages. The first i32 is used to determine if a message a flatbuffer
//! message or a "minimal" message.
use crate::state::ThreadSafeState;
use deno::Buf;
use deno::CoreOp;
use deno::ErrBox;
Expand Down Expand Up @@ -73,24 +72,17 @@ fn test_parse_min_record() {

pub fn wrap_minimal_op<D>(
d: D,
state: &ThreadSafeState,
control: &[u8],
zero_copy: Option<PinnedBuf>,
) -> CoreOp
where
D: FnOnce(i32, Option<PinnedBuf>) -> Box<MinimalOp>,
{
let bytes_sent_control = control.len();
let bytes_sent_zero_copy = zero_copy.as_ref().map(|b| b.len()).unwrap_or(0);

let mut record = parse_min_record(control).unwrap();
let is_sync = record.promise_id == 0;
let rid = record.arg;
let min_op = d(rid, zero_copy);

let state = state.clone();
state.metrics_op_dispatched(bytes_sent_control, bytes_sent_zero_copy);

let fut = Box::new(min_op.then(move |result| -> Result<Buf, ()> {
match result {
Ok(r) => {
Expand All @@ -103,9 +95,7 @@ where
record.result = -1;
}
}
let buf: Buf = record.into();
state.metrics_op_completed(buf.len());
Ok(buf)
Ok(record.into())
}));

if is_sync {
Expand Down
2 changes: 0 additions & 2 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl OpDispatcher for OpFormatError {
"error": error.to_string(),
})))
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -86,7 +85,6 @@ impl OpDispatcher for OpApplySourceMap {
"column": orig_column as u32,
})))
},
&self.state,
control,
buf,
)
Expand Down
1 change: 0 additions & 1 deletion cli/ops/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl OpDispatcher for OpFetch {

Ok(JsonOp::Async(Box::new(future)))
},
&self.state,
control,
buf,
)
Expand Down
23 changes: 2 additions & 21 deletions cli/ops/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl OpDispatcher for OpOpen {
Ok(JsonOp::Async(Box::new(op)))
}
},
&self.state,
control,
buf,
)
Expand All @@ -115,15 +114,7 @@ impl Named for OpOpen {

// Close

pub struct OpClose {
state: ThreadSafeState,
}

impl OpClose {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}
pub struct OpClose;

#[derive(Deserialize)]
struct CloseArgs {
Expand All @@ -144,7 +135,6 @@ impl OpDispatcher for OpClose {
}
}
},
&self.state,
control,
buf,
)
Expand All @@ -157,15 +147,7 @@ impl Named for OpClose {

// Seek

pub struct OpSeek {
state: ThreadSafeState,
}

impl OpSeek {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}
pub struct OpSeek;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -196,7 +178,6 @@ impl OpDispatcher for OpSeek {
}
}
},
&self.state,
control,
buf,
)
Expand Down
16 changes: 0 additions & 16 deletions cli/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl OpDispatcher for OpChdir {
std::env::set_current_dir(&args.directory)?;
Ok(JsonOp::Sync(json!({})))
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -89,7 +88,6 @@ impl OpDispatcher for OpMkdir {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -143,7 +141,6 @@ impl OpDispatcher for OpChmod {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -192,7 +189,6 @@ impl OpDispatcher for OpChown {
}
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -247,7 +243,6 @@ impl OpDispatcher for OpRemove {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -307,7 +302,6 @@ impl OpDispatcher for OpCopyFile {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -391,7 +385,6 @@ impl OpDispatcher for OpStat {
}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -457,7 +450,6 @@ impl OpDispatcher for OpReadDir {
Ok(json!({ "entries": entries }))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -510,7 +502,6 @@ impl OpDispatcher for OpRename {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -562,7 +553,6 @@ impl OpDispatcher for OpLink {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -620,7 +610,6 @@ impl OpDispatcher for OpSymlink {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -669,7 +658,6 @@ impl OpDispatcher for OpReadLink {
Ok(json!(path_str))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -720,7 +708,6 @@ impl OpDispatcher for OpTruncate {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -781,7 +768,6 @@ impl OpDispatcher for OpMakeTempDir {
Ok(json!(path_str))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -826,7 +812,6 @@ impl OpDispatcher for OpUtime {
Ok(json!({}))
})
},
&self.state,
control,
buf,
)
Expand Down Expand Up @@ -857,7 +842,6 @@ impl OpDispatcher for OpCwd {
let path_str = path.into_os_string().into_string().unwrap();
Ok(JsonOp::Sync(json!(path_str)))
},
&self.state,
control,
buf,
)
Expand Down
23 changes: 2 additions & 21 deletions cli/ops/io.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::dispatch_minimal::wrap_minimal_op;
use crate::deno_error;
use crate::resources;
use crate::state::ThreadSafeState;
use crate::tokio_write;
use deno::CoreOp;
use deno::ErrBox;
Expand All @@ -12,15 +11,7 @@ use futures::Future;

// Read

pub struct OpRead {
state: ThreadSafeState,
}

impl OpRead {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}
pub struct OpRead;

impl OpDispatcher for OpRead {
fn dispatch(&self, control: &[u8], buf: Option<PinnedBuf>) -> CoreOp {
Expand All @@ -44,7 +35,6 @@ impl OpDispatcher for OpRead {
),
}
},
&self.state,
control,
buf,
)
Expand All @@ -57,15 +47,7 @@ impl Named for OpRead {

// Write

pub struct OpWrite {
state: ThreadSafeState,
}

impl OpWrite {
pub fn new(state: ThreadSafeState) -> Self {
Self { state }
}
}
pub struct OpWrite;

impl OpDispatcher for OpWrite {
fn dispatch(&self, control: &[u8], buf: Option<PinnedBuf>) -> CoreOp {
Expand All @@ -89,7 +71,6 @@ impl OpDispatcher for OpWrite {
),
}
},
&self.state,
control,
buf,
)
Expand Down
Loading

0 comments on commit 51a7c58

Please sign in to comment.