dispatch_minimal.rs 5.2 KB
Newer Older
R
Ry Dahl 已提交
1
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
R
Ryan Dahl 已提交
2 3 4 5 6
// 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.
7
use crate::op_error::OpError;
8
use byteorder::{LittleEndian, WriteBytesExt};
9
use deno_core::Buf;
10
use deno_core::CoreIsolateState;
11
use deno_core::Op;
R
Ryan Dahl 已提交
12
use deno_core::ZeroCopyBuf;
B
Bartek Iwańczuk 已提交
13 14 15
use futures::future::FutureExt;
use std::future::Future;
use std::pin::Pin;
R
Ryan Dahl 已提交
16

17 18 19 20
pub enum MinimalOp {
  Sync(Result<i32, OpError>),
  Async(Pin<Box<dyn Future<Output = Result<i32, OpError>>>>),
}
21

R
Ryan Dahl 已提交
22 23 24
#[derive(Copy, Clone, Debug, PartialEq)]
// This corresponds to RecordMinimal on the TS side.
pub struct Record {
25
  pub promise_id: i32,
R
Ryan Dahl 已提交
26 27 28 29 30 31
  pub arg: i32,
  pub result: i32,
}

impl Into<Buf> for Record {
  fn into(self) -> Buf {
R
Ryan Dahl 已提交
32
    let vec = vec![self.promise_id, self.arg, self.result];
R
Ryan Dahl 已提交
33
    let buf32 = vec.into_boxed_slice();
R
Ryan Dahl 已提交
34
    let ptr = Box::into_raw(buf32) as *mut [u8; 3 * 4];
R
Ryan Dahl 已提交
35 36 37 38
    unsafe { Box::from_raw(ptr) }
  }
}

39 40 41
pub struct ErrorRecord {
  pub promise_id: i32,
  pub arg: i32,
42 43
  pub error_len: i32,
  pub error_code: Vec<u8>,
44 45 46 47 48
  pub error_message: Vec<u8>,
}

impl Into<Buf> for ErrorRecord {
  fn into(self) -> Buf {
49
    let v32: Vec<i32> = vec![self.promise_id, self.arg, self.error_len];
50 51 52 53
    let mut v8: Vec<u8> = Vec::new();
    for n in v32 {
      v8.write_i32::<LittleEndian>(n).unwrap();
    }
54
    let mut code = self.error_code;
55
    let mut message = self.error_message;
56
    v8.append(&mut code);
57
    v8.append(&mut message);
58 59
    // Align to 32bit word, padding with the space character.
    v8.resize((v8.len() + 3usize) & !3usize, b' ');
60 61 62 63 64 65 66
    v8.into_boxed_slice()
  }
}

#[test]
fn test_error_record() {
  let expected = vec![
67 68
    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,
69 70 71 72
  ];
  let err_record = ErrorRecord {
    promise_id: 1,
    arg: -1,
73 74
    error_len: 11,
    error_code: "BadResource".to_string().as_bytes().to_owned(),
75 76 77 78 79 80
    error_message: "Error".to_string().as_bytes().to_owned(),
  };
  let buf: Buf = err_record.into();
  assert_eq!(buf, expected.into_boxed_slice());
}

R
Ryan Dahl 已提交
81 82 83 84 85 86 87
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;
88
  let s = unsafe { std::slice::from_raw_parts(p32, bytes.len() / 4) };
R
Ryan Dahl 已提交
89

R
Ryan Dahl 已提交
90
  if s.len() != 3 {
R
Ryan Dahl 已提交
91 92 93
    return None;
  }
  let ptr = s.as_ptr();
R
Ryan Dahl 已提交
94
  let ints = unsafe { std::slice::from_raw_parts(ptr, 3) };
R
Ryan Dahl 已提交
95
  Some(Record {
R
Ryan Dahl 已提交
96 97 98
    promise_id: ints[0],
    arg: ints[1],
    result: ints[2],
R
Ryan Dahl 已提交
99 100 101 102 103
  })
}

#[test]
fn test_parse_min_record() {
R
Ryan Dahl 已提交
104
  let buf = vec![1, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0];
R
Ryan Dahl 已提交
105 106 107
  assert_eq!(
    parse_min_record(&buf),
    Some(Record {
108 109 110
      promise_id: 1,
      arg: 3,
      result: 4,
R
Ryan Dahl 已提交
111 112 113 114 115 116 117 118 119 120
    })
  );

  let buf = vec![];
  assert_eq!(parse_min_record(&buf), None);

  let buf = vec![5];
  assert_eq!(parse_min_record(&buf), None);
}

121 122 123
pub fn minimal_op<D>(
  d: D,
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
124
where
125
  D: Fn(&mut CoreIsolateState, bool, i32, &mut [ZeroCopyBuf]) -> MinimalOp,
126
{
127 128 129
  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]) {
130 131
      Some(r) => r,
      None => {
132
        let e = OpError::type_error("Unparsable control buffer".to_string());
133 134 135
        let error_record = ErrorRecord {
          promise_id: 0,
          arg: -1,
136 137
          error_len: e.kind_str.len() as i32,
          error_code: e.kind_str.as_bytes().to_owned(),
138
          error_message: e.msg.as_bytes().to_owned(),
139 140 141 142
        };
        return Op::Sync(error_record.into());
      }
    };
143 144
    let is_sync = record.promise_id == 0;
    let rid = record.arg;
145
    let min_op = d(isolate_state, is_sync, rid, &mut zero_copy[1..]);
R
Ryan Dahl 已提交
146

147 148
    match min_op {
      MinimalOp::Sync(sync_result) => Op::Sync(match sync_result {
149 150
        Ok(r) => {
          record.result = r;
151
          record.into()
152 153 154 155 156
        }
        Err(err) => {
          let error_record = ErrorRecord {
            promise_id: record.promise_id,
            arg: -1,
157 158
            error_len: err.kind_str.len() as i32,
            error_code: err.kind_str.as_bytes().to_owned(),
159
            error_message: err.msg.as_bytes().to_owned(),
160
          };
161
          error_record.into()
162
        }
163 164
      }),
      MinimalOp::Async(min_fut) => {
R
Ryan Dahl 已提交
165
        let fut = async move {
166 167 168
          match min_fut.await {
            Ok(r) => {
              record.result = r;
R
Ryan Dahl 已提交
169
              record.into()
170 171 172 173 174
            }
            Err(err) => {
              let error_record = ErrorRecord {
                promise_id: record.promise_id,
                arg: -1,
175 176
                error_len: err.kind_str.len() as i32,
                error_code: err.kind_str.as_bytes().to_owned(),
177 178
                error_message: err.msg.as_bytes().to_owned(),
              };
R
Ryan Dahl 已提交
179
              error_record.into()
180 181 182
            }
          }
        };
R
Ryan Dahl 已提交
183
        Op::Async(fut.boxed_local())
B
Bartek Iwańczuk 已提交
184
      }
185
    }
186
  }
R
Ryan Dahl 已提交
187
}