Skip to content

Commit

Permalink
review part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 25, 2019
1 parent 86f53b2 commit 5436220
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
2 changes: 1 addition & 1 deletion core/examples/http_bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async function main() {
const op = opRegistry.get(name);

if (!op) {
continue;
throw new Error(`Unknown op: ${name}`);
}

op.setOpId(opId);
Expand Down
51 changes: 13 additions & 38 deletions core/examples/http_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ fn test_record_from() {

pub type HttpOp = dyn Future<Item = i32, Error = std::io::Error> + Send;

pub type HttpOpHandler = fn(
is_sync: bool,
record: Record,
zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp>;
pub type HttpOpHandler =
fn(record: Record, zero_copy_buf: Option<PinnedBuf>) -> Box<HttpOp>;

fn serialize_http_op(handler: HttpOpHandler) -> Box<CoreOpHandler> {
fn http_op(handler: HttpOpHandler) -> Box<CoreOpHandler> {
let serialized_op =
move |control: &[u8], zero_copy_buf: Option<PinnedBuf>| -> CoreOp {
let record = Record::from(control);
let is_sync = record.promise_id == 0;
let op = handler(is_sync, record.clone(), zero_copy_buf);
let op = handler(record.clone(), zero_copy_buf);

let mut record_a = record.clone();
let mut record_b = record.clone();
Expand Down Expand Up @@ -156,11 +153,11 @@ fn main() {
});

let mut isolate = deno::Isolate::new(startup_data, false);
isolate.register_op("listen", serialize_http_op(op_listen));
isolate.register_op("accept", serialize_http_op(op_accept));
isolate.register_op("read", serialize_http_op(op_read));
isolate.register_op("write", serialize_http_op(op_write));
isolate.register_op("close", serialize_http_op(op_close));
isolate.register_op("listen", http_op(op_listen));
isolate.register_op("accept", http_op(op_accept));
isolate.register_op("read", http_op(op_read));
isolate.register_op("write", http_op(op_write));
isolate.register_op("close", http_op(op_close));

isolate.then(|r| {
js_check(r);
Expand Down Expand Up @@ -204,12 +201,7 @@ fn new_rid() -> i32 {
rid as i32
}

fn op_accept(
is_sync: bool,
record: Record,
_zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp> {
assert!(!is_sync);
fn op_accept(record: Record, _zero_copy_buf: Option<PinnedBuf>) -> Box<HttpOp> {
let listener_rid = record.arg;
debug!("accept {}", listener_rid);
Box::new(
Expand All @@ -234,11 +226,9 @@ fn op_accept(
}

fn op_listen(
is_sync: bool,
_record: Record,
_zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp> {
assert!(is_sync);
debug!("listen");
Box::new(lazy(move || {
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
Expand All @@ -251,12 +241,7 @@ fn op_listen(
}))
}

fn op_close(
is_sync: bool,
record: Record,
_zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp> {
assert!(is_sync);
fn op_close(record: Record, _zero_copy_buf: Option<PinnedBuf>) -> Box<HttpOp> {
debug!("close");
let rid = record.arg;
Box::new(lazy(move || {
Expand All @@ -267,12 +252,7 @@ fn op_close(
}))
}

fn op_read(
is_sync: bool,
record: Record,
zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp> {
assert!(!is_sync);
fn op_read(record: Record, zero_copy_buf: Option<PinnedBuf>) -> Box<HttpOp> {
let rid = record.arg;
debug!("read rid={}", rid);
let mut zero_copy_buf = zero_copy_buf.unwrap();
Expand All @@ -294,12 +274,7 @@ fn op_read(
)
}

fn op_write(
is_sync: bool,
record: Record,
zero_copy_buf: Option<PinnedBuf>,
) -> Box<HttpOp> {
assert!(!is_sync);
fn op_write(record: Record, zero_copy_buf: Option<PinnedBuf>) -> Box<HttpOp> {
let rid = record.arg;
debug!("write rid={}", rid);
let zero_copy_buf = zero_copy_buf.unwrap();
Expand Down
5 changes: 4 additions & 1 deletion core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ impl OpRegistry {
}

pub fn get_op_map(&self) -> HashMap<String, OpId> {
self.op_map.clone()
let mut op_map = self.op_map.clone();
// Don't send "get_op_map" to JS, if JS encounters unknown op it should throw.
op_map.remove("get_op_map");
op_map
}

pub fn register_op(
Expand Down

0 comments on commit 5436220

Please sign in to comment.