-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Start porting flatbuffer ops to JSON #2799
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
846d91b
Add ability to dispatch ops using JSON
ry d67c332
fix op dispatch/completed metrics
ry aa4e396
cleanup
ry 6f158fe
cleanup
ry 0947ece
cleanup
ry 50a1426
Merge branch 'master' into json_ops
ry 25e24fc
Merge branch 'master' into json_ops
ry dabf778
add todo
ry fd9de8b
Merge remote-tracking branch 'piscisaureus/derive_feature' into json_ops
ry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
use crate::state::ThreadSafeState; | ||
use crate::tokio_util; | ||
use deno::*; | ||
use futures::Future; | ||
use futures::Poll; | ||
pub use serde_derive::Deserialize; | ||
use serde_json::json; | ||
pub use serde_json::Value; | ||
|
||
pub type AsyncJsonOp = Box<dyn Future<Item = Value, Error = ErrBox> + Send>; | ||
|
||
pub enum JsonOp { | ||
Sync(Value), | ||
Async(AsyncJsonOp), | ||
} | ||
|
||
fn json_err(err: ErrBox) -> Value { | ||
use crate::deno_error::GetErrorKind; | ||
json!({ | ||
"message": err.to_string(), | ||
"kind": err.kind() as u32, | ||
}) | ||
} | ||
|
||
pub type Dispatcher = fn( | ||
state: &ThreadSafeState, | ||
args: Value, | ||
zero_copy: Option<PinnedBuf>, | ||
) -> Result<JsonOp, ErrBox>; | ||
|
||
fn serialize_result( | ||
promise_id: Option<u64>, | ||
result: Result<Value, ErrBox>, | ||
) -> Buf { | ||
let value = match result { | ||
Ok(v) => json!({ "ok": v, "promiseId": promise_id }), | ||
Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }), | ||
}; | ||
let vec = serde_json::to_vec(&value).unwrap(); | ||
vec.into_boxed_slice() | ||
} | ||
|
||
#[derive(Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
struct AsyncArgs { | ||
promise_id: Option<u64>, | ||
} | ||
|
||
pub fn dispatch( | ||
d: Dispatcher, | ||
state: &ThreadSafeState, | ||
control: &[u8], | ||
zero_copy: Option<PinnedBuf>, | ||
) -> CoreOp { | ||
let async_args: AsyncArgs = serde_json::from_slice(control).unwrap(); | ||
ry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let promise_id = async_args.promise_id; | ||
let is_sync = promise_id.is_none(); | ||
|
||
let result = serde_json::from_slice(control) | ||
.map_err(ErrBox::from) | ||
.and_then(move |args| d(state, args, zero_copy)); | ||
match result { | ||
Ok(JsonOp::Sync(sync_value)) => { | ||
assert!(promise_id.is_none()); | ||
CoreOp::Sync(serialize_result(promise_id, Ok(sync_value))) | ||
} | ||
Ok(JsonOp::Async(fut)) => { | ||
assert!(promise_id.is_some()); | ||
let fut2 = Box::new(fut.then(move |result| -> Result<Buf, ()> { | ||
Ok(serialize_result(promise_id, result)) | ||
})); | ||
CoreOp::Async(fut2) | ||
} | ||
Err(sync_err) => { | ||
let buf = serialize_result(promise_id, Err(sync_err)); | ||
if is_sync { | ||
CoreOp::Sync(buf) | ||
} else { | ||
CoreOp::Async(Box::new(futures::future::ok(buf))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// This is just type conversion. Implement From trait? | ||
// See https://github.com/tokio-rs/tokio/blob/ffd73a64e7ec497622b7f939e38017afe7124dc4/tokio-fs/src/lib.rs#L76-L85 | ||
fn convert_blocking_json<F>(f: F) -> Poll<Value, ErrBox> | ||
where | ||
F: FnOnce() -> Result<Value, ErrBox>, | ||
{ | ||
use futures::Async::*; | ||
match tokio_threadpool::blocking(f) { | ||
Ok(Ready(Ok(v))) => Ok(Ready(v)), | ||
Ok(Ready(Err(err))) => Err(err), | ||
Ok(NotReady) => Ok(NotReady), | ||
Err(err) => panic!("blocking error {}", err), | ||
} | ||
} | ||
|
||
pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, ErrBox> | ||
where | ||
F: 'static + Send + FnOnce() -> Result<Value, ErrBox>, | ||
{ | ||
if is_sync { | ||
Ok(JsonOp::Sync(f()?)) | ||
} else { | ||
Ok(JsonOp::Async(Box::new(futures::sync::oneshot::spawn( | ||
tokio_util::poll_fn(move || convert_blocking_json(f)), | ||
&tokio_executor::DefaultExecutor::current(), | ||
)))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do this as a separate PR, so we can keep the cargo an gn builds in sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2808