Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Add struct Config and make Isolate not generic #2183

Merged
merged 1 commit into from Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions cli/state.rs
Expand Up @@ -10,7 +10,6 @@ use crate::resources::ResourceId;
use crate::worker::Worker;
use deno::deno_buf;
use deno::Buf;
use deno::Dispatch;
use deno::Op;
use futures::future::Shared;
use std;
Expand Down Expand Up @@ -74,9 +73,9 @@ impl Deref for ThreadSafeState {
}
}

impl Dispatch for ThreadSafeState {
fn dispatch(
&mut self,
impl ThreadSafeState {
pub fn dispatch(
&self,
control: &[u8],
zero_copy: deno_buf,
) -> (bool, Box<Op>) {
Expand Down
12 changes: 8 additions & 4 deletions cli/worker.rs
Expand Up @@ -9,6 +9,7 @@ use crate::msg;
use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno;
use deno::Config;
use deno::JSError;
use deno::Loader;
use deno::StartupData;
Expand All @@ -21,7 +22,7 @@ use url::Url;
/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
/// high-level module loading
pub struct Worker {
inner: deno::Isolate<ThreadSafeState>,
inner: deno::Isolate,
pub modules: deno::Modules,
pub state: ThreadSafeState,
}
Expand All @@ -33,8 +34,12 @@ impl Worker {
state: ThreadSafeState,
) -> Worker {
let state_ = state.clone();
let mut config = Config::default();
config.dispatch(move |control_buf, zero_copy_buf| {
state_.dispatch(control_buf, zero_copy_buf)
});
Self {
inner: deno::Isolate::new(startup_data, state_),
inner: deno::Isolate::new(startup_data, config),
modules: deno::Modules::new(),
state,
}
Expand Down Expand Up @@ -154,7 +159,6 @@ pub fn root_specifier_to_url(
}

impl Loader for Worker {
type Dispatch = ThreadSafeState;
type Error = DenoError;

fn resolve(specifier: &str, referrer: &str) -> Result<String, Self::Error> {
Expand Down Expand Up @@ -187,7 +191,7 @@ impl Loader for Worker {

fn isolate_and_modules<'a: 'b + 'c, 'b, 'c>(
&'a mut self,
) -> (&'b mut deno::Isolate<Self::Dispatch>, &'c mut deno::Modules) {
) -> (&'b mut deno::Isolate, &'c mut deno::Modules) {
(&mut self.inner, &mut self.modules)
}
}
Expand Down
108 changes: 51 additions & 57 deletions core/examples/http_bench.rs
Expand Up @@ -111,62 +111,54 @@ fn test_record_from() {

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

struct HttpBench();

impl Dispatch for HttpBench {
fn dispatch(
&mut self,
control: &[u8],
zero_copy_buf: deno_buf,
) -> (bool, Box<Op>) {
let record = Record::from(control);
let is_sync = record.promise_id == 0;
let http_bench_op = match record.op_id {
OP_LISTEN => {
assert!(is_sync);
op_listen()
}
OP_CLOSE => {
assert!(is_sync);
let rid = record.arg;
op_close(rid)
}
OP_ACCEPT => {
assert!(!is_sync);
let listener_rid = record.arg;
op_accept(listener_rid)
}
OP_READ => {
assert!(!is_sync);
let rid = record.arg;
op_read(rid, zero_copy_buf)
}
OP_WRITE => {
assert!(!is_sync);
let rid = record.arg;
op_write(rid, zero_copy_buf)
}
_ => panic!("bad op {}", record.op_id),
};
let mut record_a = record.clone();
let mut record_b = record.clone();

let op = Box::new(
http_bench_op
.and_then(move |result| {
record_a.result = result;
Ok(record_a)
}).or_else(|err| -> Result<Record, ()> {
eprintln!("unexpected err {}", err);
record_b.result = -1;
Ok(record_b)
}).then(|result| -> Result<Buf, ()> {
let record = result.unwrap();
Ok(record.into())
}),
);
(is_sync, op)
}
fn dispatch(control: &[u8], zero_copy_buf: deno_buf) -> (bool, Box<Op>) {
let record = Record::from(control);
let is_sync = record.promise_id == 0;
let http_bench_op = match record.op_id {
OP_LISTEN => {
assert!(is_sync);
op_listen()
}
OP_CLOSE => {
assert!(is_sync);
let rid = record.arg;
op_close(rid)
}
OP_ACCEPT => {
assert!(!is_sync);
let listener_rid = record.arg;
op_accept(listener_rid)
}
OP_READ => {
assert!(!is_sync);
let rid = record.arg;
op_read(rid, zero_copy_buf)
}
OP_WRITE => {
assert!(!is_sync);
let rid = record.arg;
op_write(rid, zero_copy_buf)
}
_ => panic!("bad op {}", record.op_id),
};
let mut record_a = record.clone();
let mut record_b = record.clone();

let op = Box::new(
http_bench_op
.and_then(move |result| {
record_a.result = result;
Ok(record_a)
}).or_else(|err| -> Result<Record, ()> {
eprintln!("unexpected err {}", err);
record_b.result = -1;
Ok(record_b)
}).then(|result| -> Result<Buf, ()> {
let record = result.unwrap();
Ok(record.into())
}),
);
(is_sync, op)
}

fn main() {
Expand All @@ -182,7 +174,9 @@ fn main() {
filename: "http_bench.js",
});

let isolate = deno::Isolate::new(startup_data, HttpBench());
let mut config = deno::Config::default();
config.dispatch(dispatch);
let isolate = deno::Isolate::new(startup_data, config);

isolate.then(|r| {
js_check(r);
Expand Down