Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove control slice from ops #6048

Merged
merged 9 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,9 @@ declare global {

interface DenoCore {
print(s: string, isErr?: boolean): void;
dispatch(
opId: number,
control: Uint8Array,
...zeroCopy: ArrayBufferView[]
): Uint8Array | null;
dispatch(opId: number, ...zeroCopy: ArrayBufferView[]): Uint8Array | null;
dispatchByName(
opName: string,
control: Uint8Array,
...zeroCopy: ArrayBufferView[]
): Uint8Array | null;
setAsyncHandler(opId: number, cb: (msg: Uint8Array) => void): void;
Expand All @@ -101,11 +96,7 @@ declare global {

recv(cb: (opId: number, msg: Uint8Array) => void): void;

send(
opId: number,
control: null | ArrayBufferView,
...data: ArrayBufferView[]
): null | Uint8Array;
send(opId: number, ...data: ArrayBufferView[]): null | Uint8Array;

setMacrotaskCallback(cb: () => boolean): void;

Expand Down
13 changes: 6 additions & 7 deletions cli/ops/dispatch_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ struct AsyncArgs {

pub fn json_op<D>(
d: D,
) -> impl Fn(&mut CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(
&mut CoreIsolateState,
Value,
&mut [ZeroCopyBuf],
) -> Result<JsonOp, OpError>,
{
move |isolate_state: &mut CoreIsolateState,
control: &[u8],
zero_copy: &mut [ZeroCopyBuf]| {
let async_args: AsyncArgs = match serde_json::from_slice(control) {
move |isolate_state: &mut CoreIsolateState, zero_copy: &mut [ZeroCopyBuf]| {
assert!(!zero_copy.is_empty(), "Expected JSON string at position 0");
let async_args: AsyncArgs = match serde_json::from_slice(&zero_copy[0]) {
Ok(args) => args,
Err(e) => {
let buf = serialize_result(None, Err(OpError::from(e)));
Expand All @@ -67,9 +66,9 @@ where
let promise_id = async_args.promise_id;
let is_sync = promise_id.is_none();

let result = serde_json::from_slice(control)
let result = serde_json::from_slice(&zero_copy[0])
.map_err(OpError::from)
.and_then(|args| d(isolate_state, args, zero_copy));
.and_then(|args| d(isolate_state, args, &mut zero_copy[1..]));

// Convert to Op
match result {
Expand Down
11 changes: 5 additions & 6 deletions cli/ops/dispatch_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ fn test_parse_min_record() {

pub fn minimal_op<D>(
d: D,
) -> impl Fn(&mut CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(&mut CoreIsolateState, bool, i32, &mut [ZeroCopyBuf]) -> MinimalOp,
{
move |isolate_state: &mut CoreIsolateState,
control: &[u8],
zero_copy: &mut [ZeroCopyBuf]| {
let mut record = match parse_min_record(control) {
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]) {
Some(r) => r,
None => {
let e = OpError::type_error("Unparsable control buffer".to_string());
Expand All @@ -138,7 +137,7 @@ where
};
let is_sync = record.promise_id == 0;
let rid = record.arg;
let min_op = d(isolate_state, is_sync, rid, zero_copy);
let min_op = d(isolate_state, is_sync, rid, &mut zero_copy[1..]);

match min_op {
MinimalOp::Sync(sync_result) => Op::Sync(match sync_result {
Expand Down
4 changes: 2 additions & 2 deletions cli/ops/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ impl<'a> plugin_api::Interface for PluginInterface<'a> {
let plugin_lib = self.plugin_lib.clone();
self.isolate_state.op_registry.register(
name,
move |isolate_state, control, zero_copy| {
move |isolate_state, zero_copy| {
let mut interface = PluginInterface::new(isolate_state, &plugin_lib);
let op = dispatch_op_fn(&mut interface, control, zero_copy);
let op = dispatch_op_fn(&mut interface, zero_copy);
match op {
sync_op @ Op::Sync(..) => sync_op,
Op::Async(fut) => {
Expand Down
18 changes: 9 additions & 9 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl State {
pub fn stateful_json_op<D>(
&self,
dispatcher: D,
) -> impl Fn(&mut deno_core::CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(&State, Value, &mut [ZeroCopyBuf]) -> Result<JsonOp, OpError>,
{
Expand All @@ -76,7 +76,7 @@ impl State {
pub fn stateful_json_op2<D>(
&self,
dispatcher: D,
) -> impl Fn(&mut deno_core::CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(
&mut deno_core::CoreIsolateState,
Expand All @@ -95,21 +95,21 @@ impl State {
pub fn core_op<D>(
&self,
dispatcher: D,
) -> impl Fn(&mut deno_core::CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(&mut deno_core::CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op,
D: Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op,
{
let state = self.clone();

move |isolate_state: &mut deno_core::CoreIsolateState,
control: &[u8],
zero_copy: &mut [ZeroCopyBuf]|
-> Op {
let bytes_sent_control = control.len() as u64;
let bytes_sent_control =
zero_copy.get(0).map(|s| s.len()).unwrap_or(0) as u64;
let bytes_sent_zero_copy =
zero_copy.iter().map(|b| b.len()).sum::<usize>() as u64;
zero_copy[1..].iter().map(|b| b.len()).sum::<usize>() as u64;

let op = dispatcher(isolate_state, control, zero_copy);
let op = dispatcher(isolate_state, zero_copy);

match op {
Op::Sync(buf) => {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl State {
pub fn stateful_minimal_op2<D>(
&self,
dispatcher: D,
) -> impl Fn(&mut deno_core::CoreIsolateState, &[u8], &mut [ZeroCopyBuf]) -> Op
) -> impl Fn(&mut deno_core::CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
where
D: Fn(
&mut deno_core::CoreIsolateState,
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ libc = "0.2.71"
log = "0.4.8"
rusty_v8 = "0.6.0"
serde_json = "1.0.55"
smallvec = "1.4.0"
url = "2.1.1"

[[example]]
Expand Down
53 changes: 10 additions & 43 deletions core/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::ZeroCopyBuf;
use rusty_v8 as v8;
use v8::MapFnTo;

use smallvec::SmallVec;
use std::cell::Cell;
use std::convert::TryFrom;
use std::option::Option;
Expand Down Expand Up @@ -388,24 +389,11 @@ fn send(
}
};

let control_backing_store: v8::SharedRef<v8::BackingStore>;
let control = match v8::Local::<v8::ArrayBufferView>::try_from(args.get(1)) {
Ok(view) => unsafe {
control_backing_store = view.buffer(scope).unwrap().get_backing_store();
get_backing_store_slice(
&control_backing_store,
view.byte_offset(),
view.byte_length(),
)
},
Err(_) => &[],
};

let state_rc = CoreIsolate::state(scope);
let mut state = state_rc.borrow_mut();
assert!(!state.global_context.is_empty());

let mut buf_iter = (2..args.length()).map(|idx| {
let buf_iter = (1..args.length()).map(|idx| {
v8::Local::<v8::ArrayBufferView>::try_from(args.get(idx))
.map(|view| ZeroCopyBuf::new(scope, view))
.map_err(|err| {
Expand All @@ -415,36 +403,15 @@ fn send(
})
});

let mut buf_one: ZeroCopyBuf;
let mut buf_vec: Vec<ZeroCopyBuf>;

// Collect all ArrayBufferView's
let buf_iter_result = match buf_iter.len() {
0 => Ok(&mut [][..]),
1 => match buf_iter.next().unwrap() {
Ok(buf) => {
buf_one = buf;
Ok(std::slice::from_mut(&mut buf_one))
// If response is empty then it's either async op or exception was thrown.
let maybe_response =
match buf_iter.collect::<Result<SmallVec<[ZeroCopyBuf; 2]>, _>>() {
Ok(mut bufs) => state.dispatch_op(scope, op_id, &mut bufs),
Err(exc) => {
scope.throw_exception(exc);
return;
}
Err(err) => Err(err),
},
_ => match buf_iter.collect::<Result<Vec<_>, _>>() {
Ok(v) => {
buf_vec = v;
Ok(&mut buf_vec[..])
}
Err(err) => Err(err),
},
};

// If response is empty then it's either async op or exception was thrown
let maybe_response = match buf_iter_result {
Ok(bufs) => state.dispatch_op(scope, op_id, control, bufs),
Err(exc) => {
scope.throw_exception(exc);
return;
}
};
};

if let Some(response) = maybe_response {
// Synchronous response.
Expand Down
Loading