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-08-20 09:45:59 -04:00
|
|
|
use deno_core::serde_json;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::CoreIsolate;
|
2020-05-29 17:41:39 -04:00
|
|
|
use deno_core::CoreIsolateState;
|
2020-08-20 09:45:59 -04:00
|
|
|
use deno_core::ErrBox;
|
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;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::env;
|
2019-10-28 20:42:44 -04:00
|
|
|
use std::io::Error;
|
|
|
|
use std::io::ErrorKind;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::net::SocketAddr;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
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;
|
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-08-17 17:29:01 -04:00
|
|
|
pub fn isolate_new() -> CoreIsolate {
|
|
|
|
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-08-17 17:29:01 -04:00
|
|
|
let mut isolate = CoreIsolate::new(startup_data, false);
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-08-20 09:45:59 -04:00
|
|
|
isolate.register_op_json_sync("listen", op_listen);
|
|
|
|
isolate.register_op_json_async("accept", op_accept);
|
2020-08-21 05:47:57 -04:00
|
|
|
isolate.register_op_json_async("read", op_read);
|
|
|
|
isolate.register_op_json_async("write", op_write);
|
|
|
|
isolate.register_op_json_sync("close", op_close);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-08-17 17:29:01 -04:00
|
|
|
isolate
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_close(
|
2020-08-21 05:47:57 -04:00
|
|
|
state: &mut CoreIsolateState,
|
|
|
|
args: serde_json::Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_buf: &mut [ZeroCopyBuf],
|
2020-08-21 05:47:57 -04:00
|
|
|
) -> Result<serde_json::Value, ErrBox> {
|
|
|
|
let rid = args.get("rid").unwrap().as_u64().unwrap() as u32;
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("close rid={}", rid);
|
2020-08-21 05:47:57 -04:00
|
|
|
|
|
|
|
let resource_table = &mut state.resource_table.borrow_mut();
|
2020-04-18 20:05:13 -04:00
|
|
|
resource_table
|
|
|
|
.close(rid)
|
2020-08-21 05:47:57 -04:00
|
|
|
.map(|_| serde_json::json!(()))
|
2020-04-18 20:05:13 -04:00
|
|
|
.ok_or_else(bad_resource)
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
fn op_listen(
|
2020-08-20 09:45:59 -04:00
|
|
|
state: &mut CoreIsolateState,
|
|
|
|
_args: serde_json::Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_buf: &mut [ZeroCopyBuf],
|
2020-08-20 09:45:59 -04:00
|
|
|
) -> Result<serde_json::Value, ErrBox> {
|
2019-03-11 17:57:36 -04:00
|
|
|
debug!("listen");
|
2020-04-18 20:05:13 -04:00
|
|
|
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)?;
|
2020-08-20 09:45:59 -04:00
|
|
|
let resource_table = &mut state.resource_table.borrow_mut();
|
2020-04-18 20:05:13 -04:00
|
|
|
let rid = resource_table.add("tcpListener", Box::new(listener));
|
2020-08-20 09:45:59 -04:00
|
|
|
Ok(serde_json::json!({ "rid": rid }))
|
2019-12-15 05:47:26 -05:00
|
|
|
}
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
fn op_accept(
|
2020-08-20 09:45:59 -04:00
|
|
|
state: &mut CoreIsolateState,
|
|
|
|
args: serde_json::Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_buf: &mut [ZeroCopyBuf],
|
2020-08-20 09:45:59 -04:00
|
|
|
) -> impl Future<Output = Result<serde_json::Value, ErrBox>> {
|
|
|
|
let rid = args.get("rid").unwrap().as_u64().unwrap() as u32;
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("accept rid={}", rid);
|
|
|
|
|
2020-08-20 09:45:59 -04:00
|
|
|
let resource_table = state.resource_table.clone();
|
2020-02-06 18:34:40 -05:00
|
|
|
poll_fn(move |cx| {
|
2020-08-17 17:29:01 -04:00
|
|
|
let resource_table = &mut resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let listener = resource_table
|
|
|
|
.get_mut::<TcpListener>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
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-08-21 05:47:57 -04:00
|
|
|
state: &mut CoreIsolateState,
|
|
|
|
args: serde_json::Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
bufs: &mut [ZeroCopyBuf],
|
2020-08-21 05:47:57 -04:00
|
|
|
) -> impl Future<Output = Result<serde_json::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
|
|
|
let rid = args.get("rid").unwrap().as_u64().unwrap() as u32;
|
2019-02-26 17:36:05 -05:00
|
|
|
debug!("read rid={}", rid);
|
|
|
|
|
2020-08-21 05:47:57 -04:00
|
|
|
let mut buf = bufs[0].clone();
|
|
|
|
let resource_table = state.resource_table.clone();
|
|
|
|
|
|
|
|
poll_fn(move |cx| -> Poll<Result<serde_json::Value, ErrBox>> {
|
2020-08-17 17:29:01 -04:00
|
|
|
let resource_table = &mut resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
2020-08-21 05:47:57 -04:00
|
|
|
Pin::new(stream)
|
|
|
|
.poll_read(cx, &mut buf)?
|
|
|
|
.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-08-21 05:47:57 -04:00
|
|
|
state: &mut CoreIsolateState,
|
|
|
|
args: serde_json::Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
bufs: &mut [ZeroCopyBuf],
|
2020-08-21 05:47:57 -04:00
|
|
|
) -> impl Future<Output = Result<serde_json::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
|
|
|
|
|
|
|
let rid = args.get("rid").unwrap().as_u64().unwrap() as u32;
|
2020-02-06 18:34:40 -05:00
|
|
|
debug!("write rid={}", rid);
|
2019-12-15 05:47:26 -05:00
|
|
|
|
2020-08-21 05:47:57 -04:00
|
|
|
let buf = bufs[0].clone();
|
|
|
|
let resource_table = state.resource_table.clone();
|
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
poll_fn(move |cx| {
|
2020-08-17 17:29:01 -04:00
|
|
|
let resource_table = &mut resource_table.borrow_mut();
|
2020-02-06 18:34:40 -05:00
|
|
|
let stream = resource_table
|
|
|
|
.get_mut::<TcpStream>(rid)
|
|
|
|
.ok_or_else(bad_resource)?;
|
2020-08-21 05:47:57 -04:00
|
|
|
Pin::new(stream)
|
|
|
|
.poll_write(cx, &buf)?
|
|
|
|
.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-08-21 05:47:57 -04:00
|
|
|
fn bad_resource() -> ErrBox {
|
|
|
|
Error::new(ErrorKind::NotFound, "bad resource id").into()
|
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-08-17 17:29:01 -04:00
|
|
|
let isolate = isolate_new();
|
2020-02-06 18:34:40 -05:00
|
|
|
let mut runtime = tokio::runtime::Builder::new()
|
|
|
|
.basic_scheduler()
|
|
|
|
.enable_all()
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2020-08-20 09:45:59 -04:00
|
|
|
deno_core::js_check(runtime.block_on(isolate));
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|