2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-05-03 00:06:43 -04:00
|
|
|
// 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.
|
2020-08-25 18:22:15 -04:00
|
|
|
use crate::errors::get_error_class;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Buf;
|
2020-05-29 17:41:39 -04:00
|
|
|
use deno_core::CoreIsolateState;
|
2020-08-25 18:22:15 -04:00
|
|
|
use deno_core::ErrBox;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Op;
|
2020-01-24 15:10:49 -05:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
|
|
|
use std::future::Future;
|
2020-08-25 18:22:15 -04:00
|
|
|
use std::iter::repeat;
|
|
|
|
use std::mem::size_of_val;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-08-25 18:22:15 -04:00
|
|
|
use std::slice;
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2020-04-15 20:43:19 -04:00
|
|
|
pub enum MinimalOp {
|
2020-08-25 18:22:15 -04:00
|
|
|
Sync(Result<i32, ErrBox>),
|
|
|
|
Async(Pin<Box<dyn Future<Output = Result<i32, ErrBox>>>>),
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
2019-08-16 11:05:24 -04:00
|
|
|
|
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) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:22:31 -04:00
|
|
|
pub struct ErrorRecord {
|
|
|
|
pub promise_id: i32,
|
|
|
|
pub arg: i32,
|
2020-08-07 16:47:18 -04:00
|
|
|
pub error_len: i32,
|
2020-08-25 18:22:15 -04:00
|
|
|
pub error_class: &'static [u8],
|
2019-10-24 17:22:31 -04:00
|
|
|
pub error_message: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<Buf> for ErrorRecord {
|
|
|
|
fn into(self) -> Buf {
|
2020-08-25 18:22:15 -04:00
|
|
|
let Self {
|
|
|
|
promise_id,
|
|
|
|
arg,
|
|
|
|
error_len,
|
|
|
|
error_class,
|
|
|
|
error_message,
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
let header_i32 = [promise_id, arg, error_len];
|
|
|
|
let header_u8 = unsafe {
|
|
|
|
slice::from_raw_parts(
|
|
|
|
&header_i32 as *const _ as *const u8,
|
|
|
|
size_of_val(&header_i32),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
let padded_len =
|
|
|
|
(header_u8.len() + error_class.len() + error_message.len() + 3usize)
|
|
|
|
& !3usize;
|
|
|
|
header_u8
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.chain(error_class.iter().cloned())
|
|
|
|
.chain(error_message.into_iter())
|
|
|
|
.chain(repeat(b' '))
|
|
|
|
.take(padded_len)
|
|
|
|
.collect()
|
2019-10-24 17:22:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error_record() {
|
|
|
|
let expected = vec![
|
2020-08-07 16:47:18 -04:00
|
|
|
1, 0, 0, 0, 255, 255, 255, 255, 11, 0, 0, 0, 66, 97, 100, 82, 101, 115,
|
|
|
|
111, 117, 114, 99, 101, 69, 114, 114, 111, 114,
|
2019-10-24 17:22:31 -04:00
|
|
|
];
|
|
|
|
let err_record = ErrorRecord {
|
|
|
|
promise_id: 1,
|
|
|
|
arg: -1,
|
2020-08-07 16:47:18 -04:00
|
|
|
error_len: 11,
|
2020-08-25 18:22:15 -04:00
|
|
|
error_class: b"BadResource",
|
2020-08-25 18:48:25 -04:00
|
|
|
error_message: b"Error".to_vec(),
|
2019-10-24 17:22:31 -04:00
|
|
|
};
|
|
|
|
let buf: Buf = err_record.into();
|
|
|
|
assert_eq!(buf, expected.into_boxed_slice());
|
|
|
|
}
|
|
|
|
|
2019-05-03 00:06:43 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn minimal_op<D>(
|
|
|
|
d: D,
|
|
|
|
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
2019-11-14 12:10:25 -05:00
|
|
|
where
|
2020-06-01 14:20:47 -04:00
|
|
|
D: Fn(&mut CoreIsolateState, bool, i32, &mut [ZeroCopyBuf]) -> MinimalOp,
|
2019-11-14 12:10:25 -05:00
|
|
|
{
|
2020-07-08 11:23:50 -04:00
|
|
|
move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| {
|
|
|
|
assert!(!zero_copy.is_empty(), "Expected record at position 0");
|
|
|
|
let mut record = match parse_min_record(&zero_copy[0]) {
|
2019-10-25 13:23:16 -04:00
|
|
|
Some(r) => r,
|
|
|
|
None => {
|
2020-08-25 18:22:15 -04:00
|
|
|
let error = ErrBox::type_error("Unparsable control buffer");
|
|
|
|
let error_class = get_error_class(&error);
|
2019-10-25 13:23:16 -04:00
|
|
|
let error_record = ErrorRecord {
|
|
|
|
promise_id: 0,
|
|
|
|
arg: -1,
|
2020-08-25 18:22:15 -04:00
|
|
|
error_len: error_class.len() as i32,
|
|
|
|
error_class: error_class.as_bytes(),
|
|
|
|
error_message: error.to_string().as_bytes().to_owned(),
|
2019-10-25 13:23:16 -04:00
|
|
|
};
|
|
|
|
return Op::Sync(error_record.into());
|
|
|
|
}
|
|
|
|
};
|
2019-10-01 18:51:05 -04:00
|
|
|
let is_sync = record.promise_id == 0;
|
|
|
|
let rid = record.arg;
|
2020-07-08 11:23:50 -04:00
|
|
|
let min_op = d(isolate_state, is_sync, rid, &mut zero_copy[1..]);
|
2019-08-21 20:42:48 -04:00
|
|
|
|
2020-04-15 20:43:19 -04:00
|
|
|
match min_op {
|
|
|
|
MinimalOp::Sync(sync_result) => Op::Sync(match sync_result {
|
2020-01-04 05:20:52 -05:00
|
|
|
Ok(r) => {
|
|
|
|
record.result = r;
|
2020-04-15 20:43:19 -04:00
|
|
|
record.into()
|
2020-01-04 05:20:52 -05:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
let error_class = get_error_class(&err);
|
2020-01-04 05:20:52 -05:00
|
|
|
let error_record = ErrorRecord {
|
|
|
|
promise_id: record.promise_id,
|
|
|
|
arg: -1,
|
2020-08-25 18:22:15 -04:00
|
|
|
error_len: error_class.len() as i32,
|
|
|
|
error_class: error_class.as_bytes(),
|
|
|
|
error_message: err.to_string().as_bytes().to_owned(),
|
2020-01-04 05:20:52 -05:00
|
|
|
};
|
2020-04-15 20:43:19 -04:00
|
|
|
error_record.into()
|
2020-01-04 05:20:52 -05:00
|
|
|
}
|
2020-04-15 20:43:19 -04:00
|
|
|
}),
|
|
|
|
MinimalOp::Async(min_fut) => {
|
2020-04-18 20:05:13 -04:00
|
|
|
let fut = async move {
|
2020-04-15 20:43:19 -04:00
|
|
|
match min_fut.await {
|
|
|
|
Ok(r) => {
|
|
|
|
record.result = r;
|
2020-04-18 20:05:13 -04:00
|
|
|
record.into()
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2020-08-25 18:22:15 -04:00
|
|
|
let error_class = get_error_class(&err);
|
2020-04-15 20:43:19 -04:00
|
|
|
let error_record = ErrorRecord {
|
|
|
|
promise_id: record.promise_id,
|
|
|
|
arg: -1,
|
2020-08-25 18:22:15 -04:00
|
|
|
error_len: error_class.len() as i32,
|
|
|
|
error_class: error_class.as_bytes(),
|
|
|
|
error_message: err.to_string().as_bytes().to_owned(),
|
2020-04-15 20:43:19 -04:00
|
|
|
};
|
2020-04-18 20:05:13 -04:00
|
|
|
error_record.into()
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Async(fut.boxed_local())
|
2019-11-16 19:17:47 -05:00
|
|
|
}
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2019-06-14 13:58:20 -04:00
|
|
|
}
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|