2019-05-03 00:06:43 -04:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Do not add flatbuffer dependencies to this module.
|
|
|
|
//! Connects to js/dispatch_minimal.ts sendAsyncMinimal This acts as a faster
|
|
|
|
//! 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;
|
2019-06-17 21:02:08 -04:00
|
|
|
use deno::CoreOp;
|
2019-05-03 00:06:43 -04:00
|
|
|
use deno::Op;
|
2019-08-16 11:05:24 -04:00
|
|
|
use deno::OpId;
|
2019-05-03 00:06:43 -04:00
|
|
|
use deno::PinnedBuf;
|
|
|
|
use futures::Future;
|
|
|
|
|
2019-08-16 11:05:24 -04:00
|
|
|
const OP_READ: OpId = 1;
|
|
|
|
const OP_WRITE: OpId = 2;
|
|
|
|
|
2019-05-03 00:06:43 -04:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
// This corresponds to RecordMinimal on the TS side.
|
|
|
|
pub struct Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
pub promise_id: i32,
|
2019-05-03 00:06:43 -04:00
|
|
|
pub arg: i32,
|
|
|
|
pub result: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Buf> for Record {
|
|
|
|
fn into(self) -> Buf {
|
2019-08-07 14:02:29 -04:00
|
|
|
let vec = vec![self.promise_id, self.arg, self.result];
|
2019-05-03 00:06:43 -04:00
|
|
|
let buf32 = vec.into_boxed_slice();
|
2019-08-07 14:02:29 -04:00
|
|
|
let ptr = Box::into_raw(buf32) as *mut [u8; 3 * 4];
|
2019-05-03 00:06:43 -04:00
|
|
|
unsafe { Box::from_raw(ptr) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_min_record(bytes: &[u8]) -> Option<Record> {
|
|
|
|
if bytes.len() % std::mem::size_of::<i32>() != 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let p = bytes.as_ptr();
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
let p32 = p as *const i32;
|
2019-06-14 13:58:20 -04:00
|
|
|
let s = unsafe { std::slice::from_raw_parts(p32, bytes.len() / 4) };
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
if s.len() != 3 {
|
2019-05-03 00:06:43 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let ptr = s.as_ptr();
|
2019-08-07 14:02:29 -04:00
|
|
|
let ints = unsafe { std::slice::from_raw_parts(ptr, 3) };
|
2019-05-03 00:06:43 -04:00
|
|
|
Some(Record {
|
2019-08-07 14:02:29 -04:00
|
|
|
promise_id: ints[0],
|
|
|
|
arg: ints[1],
|
|
|
|
result: ints[2],
|
2019-05-03 00:06:43 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_min_record() {
|
2019-08-07 14:02:29 -04:00
|
|
|
let buf = vec![1, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0];
|
2019-05-03 00:06:43 -04:00
|
|
|
assert_eq!(
|
|
|
|
parse_min_record(&buf),
|
|
|
|
Some(Record {
|
2019-06-14 13:58:20 -04:00
|
|
|
promise_id: 1,
|
|
|
|
arg: 3,
|
|
|
|
result: 4,
|
2019-05-03 00:06:43 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
let buf = vec![];
|
|
|
|
assert_eq!(parse_min_record(&buf), None);
|
|
|
|
|
|
|
|
let buf = vec![5];
|
|
|
|
assert_eq!(parse_min_record(&buf), None);
|
|
|
|
}
|
|
|
|
|
2019-08-16 11:05:24 -04:00
|
|
|
pub fn dispatch_minimal(
|
2019-05-03 00:06:43 -04:00
|
|
|
state: &ThreadSafeState,
|
2019-08-16 11:05:24 -04:00
|
|
|
op_id: OpId,
|
|
|
|
mut record: Record,
|
2019-05-03 00:06:43 -04:00
|
|
|
zero_copy: Option<PinnedBuf>,
|
2019-06-17 21:02:08 -04:00
|
|
|
) -> CoreOp {
|
2019-06-14 13:58:20 -04:00
|
|
|
let is_sync = record.promise_id == 0;
|
2019-08-16 11:05:24 -04:00
|
|
|
let min_op = match op_id {
|
|
|
|
OP_READ => ops::read(record.arg, zero_copy),
|
|
|
|
OP_WRITE => ops::write(record.arg, zero_copy),
|
|
|
|
_ => unimplemented!(),
|
|
|
|
};
|
2019-05-03 00:06:43 -04:00
|
|
|
|
|
|
|
let state = state.clone();
|
|
|
|
|
|
|
|
let fut = Box::new(min_op.then(move |result| -> Result<Buf, ()> {
|
|
|
|
match result {
|
|
|
|
Ok(r) => {
|
|
|
|
record.result = r;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// TODO(ry) The dispatch_minimal doesn't properly pipe errors back to
|
|
|
|
// the caller.
|
|
|
|
debug!("swallowed err {}", err);
|
|
|
|
record.result = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let buf: Buf = record.into();
|
|
|
|
state.metrics_op_completed(buf.len());
|
|
|
|
Ok(buf)
|
|
|
|
}));
|
2019-06-14 13:58:20 -04:00
|
|
|
if is_sync {
|
|
|
|
Op::Sync(fut.wait().unwrap())
|
|
|
|
} else {
|
|
|
|
Op::Async(fut)
|
|
|
|
}
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
2019-08-16 11:05:24 -04:00
|
|
|
|
|
|
|
mod ops {
|
|
|
|
use crate::deno_error;
|
|
|
|
use crate::resources;
|
|
|
|
use crate::tokio_write;
|
|
|
|
use deno::ErrBox;
|
|
|
|
use deno::PinnedBuf;
|
|
|
|
use futures::Future;
|
|
|
|
|
|
|
|
type MinimalOp = dyn Future<Item = i32, Error = ErrBox> + Send;
|
|
|
|
|
|
|
|
pub fn read(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
|
|
|
|
debug!("read rid={}", rid);
|
|
|
|
let zero_copy = match zero_copy {
|
|
|
|
None => {
|
|
|
|
return Box::new(
|
|
|
|
futures::future::err(deno_error::no_buffer_specified()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(buf) => buf,
|
|
|
|
};
|
|
|
|
match resources::lookup(rid as u32) {
|
|
|
|
None => Box::new(futures::future::err(deno_error::bad_resource())),
|
|
|
|
Some(resource) => Box::new(
|
|
|
|
tokio::io::read(resource, zero_copy)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |(_resource, _buf, nread)| Ok(nread as i32)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(rid: i32, zero_copy: Option<PinnedBuf>) -> Box<MinimalOp> {
|
|
|
|
debug!("write rid={}", rid);
|
|
|
|
let zero_copy = match zero_copy {
|
|
|
|
None => {
|
|
|
|
return Box::new(
|
|
|
|
futures::future::err(deno_error::no_buffer_specified()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Some(buf) => buf,
|
|
|
|
};
|
|
|
|
match resources::lookup(rid as u32) {
|
|
|
|
None => Box::new(futures::future::err(deno_error::bad_resource())),
|
|
|
|
Some(resource) => Box::new(
|
|
|
|
tokio_write::write(resource, zero_copy)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(move |(_resource, _buf, nwritten)| Ok(nwritten as i32)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|