2019-02-26 17:36:05 -05:00
|
|
|
#[macro_use]
|
2020-02-06 18:34:40 -05:00
|
|
|
extern crate log;
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::js_check;
|
|
|
|
use deno_core::BasicState;
|
|
|
|
use deno_core::BufVec;
|
2020-08-20 09:45:59 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-09-06 15:44:29 -04:00
|
|
|
use deno_core::JsRuntime;
|
2020-09-05 20:34:02 -04:00
|
|
|
use deno_core::OpRegistry;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::Script;
|
|
|
|
use deno_core::StartupData;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-02-06 18:34:40 -05:00
|
|
|
use futures::future::poll_fn;
|
2020-08-20 09:45:59 -04:00
|
|
|
use futures::future::Future;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_json::Value;
|
|
|
|
use std::convert::TryInto;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::env;
|
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-09-05 20:34:02 -04:00
|
|
|
use std::rc::Rc;
|
2020-08-21 05:47:57 -04:00
|
|
|
use std::task::Poll;
|
2019-12-15 05:47:26 -05:00
|
|
|
use tokio::io::AsyncRead;
|
|
|
|
use tokio::io::AsyncWrite;
|
2020-02-06 18:34:40 -05:00
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tokio::net::TcpStream;
|
2020-09-05 20:34:02 -04:00
|
|
|
use tokio::runtime;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
struct Logger;
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
impl log::Log for Logger {
|
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
|
|
metadata.level() <= log::max_level()
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn log(&self, record: &log::Record) {
|
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
println!("{} - {}", record.level(), record.args());
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 18:34:40 -05:00
|
|
|
|
2019-04-07 22:40:58 -04:00
|
|
|
fn flush(&self) {}
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
fn create_isolate() -> JsRuntime {
|
2020-09-05 20:34:02 -04:00
|
|
|
let state = BasicState::new();
|
|
|
|
state.register_op_json_sync("listen", op_listen);
|
|
|
|
state.register_op_json_sync("close", op_close);
|
|
|
|
state.register_op_json_async("accept", op_accept);
|
|
|
|
state.register_op_json_async("read", op_read);
|
|
|
|
state.register_op_json_async("write", op_write);
|
|
|
|
|
2020-08-17 17:29:01 -04:00
|
|
|
let startup_data = StartupData::Script(Script {
|
2020-08-21 11:14:47 -04:00
|
|
|
source: include_str!("http_bench_json_ops.js"),
|
|
|
|
filename: "http_bench_json_ops.js",
|
2020-08-17 17:29:01 -04:00
|
|
|
});
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
JsRuntime::new(state, startup_data, false)
|
2020-09-05 20:34:02 -04:00
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
fn op_listen(
|
|
|
|
state: &BasicState,
|
|
|
|
_args: Value,
|
|
|
|
_bufs: &mut [ZeroCopyBuf],
|
|
|
|
) -> Result<Value, ErrBox> {
|
|
|
|
debug!("listen");
|
|
|
|
let addr = "127.0.0.1:4544".parse::<SocketAddr>().unwrap();
|
|
|
|
let std_listener = std::net::TcpListener::bind(&addr)?;
|
|
|
|
let listener = TcpListener::from_std(std_listener)?;
|
|
|
|
let rid = state
|
|
|
|
.resource_table
|
|
|
|
.borrow_mut()
|
|
|
|
.add("tcpListener", Box::new(listener));
|
|
|
|
Ok(serde_json::json!({ "rid": rid }))
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_close(
|
2020-09-05 20:34:02 -04:00
|
|
|
state: &BasicState,
|
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_buf: &mut [ZeroCopyBuf],
|
2020-09-05 20:34:02 -04:00
|
|
|
) -> Result<Value, ErrBox> {
|
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("close rid={}", rid);
|
2020-08-21 05:47:57 -04:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
state
|
|
|
|
.resource_table
|
|
|
|
.borrow_mut()
|
2020-04-18 20:05:13 -04:00
|
|
|
.close(rid)
|
2020-08-21 05:47:57 -04:00
|
|
|
.map(|_| serde_json::json!(()))
|
2020-09-05 20:34:02 -04:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_accept(
|
2020-09-05 20:34:02 -04:00
|
|
|
state: Rc<BasicState>,
|
|
|
|
args: Value,
|
|
|
|
_bufs: BufVec,
|
|
|
|
) -> impl Future<Output = Result<Value, ErrBox>> {
|
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("accept rid={}", rid);
|
|
|
|
|
|
|
|
poll_fn(move |cx| {
|
2020-09-05 20:34:02 -04:00
|
|
|
let resource_table = &mut state.resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let listener = resource_table
|
|
|
|
.get_mut::<TcpListener>(rid)
|
2020-09-05 20:34:02 -04:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
2020-08-21 05:47:57 -04:00
|
|
|
listener.poll_accept(cx)?.map(|(stream, _addr)| {
|
|
|
|
let rid = resource_table.add("tcpStream", Box::new(stream));
|
|
|
|
Ok(serde_json::json!({ "rid": rid }))
|
|
|
|
})
|
2020-02-06 18:34:40 -05:00
|
|
|
})
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn op_read(
|
2020-09-05 20:34:02 -04:00
|
|
|
state: Rc<BasicState>,
|
|
|
|
args: Value,
|
|
|
|
mut bufs: BufVec,
|
|
|
|
) -> impl Future<Output = Result<Value, ErrBox>> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("read rid={}", rid);
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
poll_fn(move |cx| -> Poll<Result<Value, ErrBox>> {
|
|
|
|
let resource_table = &mut state.resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
2020-09-05 20:34:02 -04:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
2020-08-21 05:47:57 -04:00
|
|
|
Pin::new(stream)
|
2020-09-05 20:34:02 -04:00
|
|
|
.poll_read(cx, &mut bufs[0])?
|
2020-08-21 05:47:57 -04:00
|
|
|
.map(|nread| Ok(serde_json::json!({ "nread": nread })))
|
2020-02-06 18:34:40 -05:00
|
|
|
})
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_write(
|
2020-09-05 20:34:02 -04:00
|
|
|
state: Rc<BasicState>,
|
|
|
|
args: Value,
|
|
|
|
bufs: BufVec,
|
|
|
|
) -> impl Future<Output = Result<Value, ErrBox>> {
|
2020-06-01 14:20:47 -04:00
|
|
|
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
2020-08-21 05:47:57 -04:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let rid: u32 = args
|
|
|
|
.get("rid")
|
|
|
|
.unwrap()
|
|
|
|
.as_u64()
|
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("write rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
poll_fn(move |cx| {
|
2020-09-05 20:34:02 -04:00
|
|
|
let resource_table = &mut state.resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
2020-09-05 20:34:02 -04:00
|
|
|
.ok_or_else(ErrBox::bad_resource_id)?;
|
2020-08-21 05:47:57 -04:00
|
|
|
Pin::new(stream)
|
2020-09-05 20:34:02 -04:00
|
|
|
.poll_write(cx, &bufs[0])?
|
2020-08-21 05:47:57 -04:00
|
|
|
.map(|nwritten| Ok(serde_json::json!({ "nwritten": nwritten })))
|
2020-02-06 18:34:40 -05:00
|
|
|
})
|
|
|
|
}
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn main() {
|
|
|
|
log::set_logger(&Logger).unwrap();
|
|
|
|
log::set_max_level(
|
|
|
|
env::args()
|
|
|
|
.find(|a| a == "-D")
|
|
|
|
.map(|_| log::LevelFilter::Debug)
|
|
|
|
.unwrap_or(log::LevelFilter::Warn),
|
|
|
|
);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
// NOTE: `--help` arg will display V8 help and exit
|
|
|
|
deno_core::v8_set_flags(env::args().collect());
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let isolate = create_isolate();
|
|
|
|
let mut runtime = runtime::Builder::new()
|
2020-02-06 18:34:40 -05:00
|
|
|
.basic_scheduler()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-09-05 20:34:02 -04:00
|
|
|
js_check(runtime.block_on(isolate));
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|