2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-23 14:51:29 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::Buf;
|
2020-05-29 17:41:39 -04:00
|
|
|
use deno_core::CoreIsolateState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::Op;
|
|
|
|
use deno_core::ZeroCopyBuf;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-23 01:30:14 -04:00
|
|
|
pub use serde_derive::Deserialize;
|
|
|
|
use serde_json::json;
|
|
|
|
pub use serde_json::Value;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-08-23 01:30:14 -04:00
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
pub type JsonResult = Result<Value, OpError>;
|
2020-02-03 18:08:44 -05:00
|
|
|
|
|
|
|
pub type AsyncJsonOp = Pin<Box<dyn Future<Output = JsonResult>>>;
|
2019-08-23 01:30:14 -04:00
|
|
|
|
|
|
|
pub enum JsonOp {
|
|
|
|
Sync(Value),
|
|
|
|
Async(AsyncJsonOp),
|
2020-01-21 12:01:10 -05:00
|
|
|
/// AsyncUnref is the variation of Async, which doesn't block the program
|
|
|
|
/// exiting.
|
|
|
|
AsyncUnref(AsyncJsonOp),
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
fn json_err(err: OpError) -> Value {
|
2019-08-23 01:30:14 -04:00
|
|
|
json!({
|
2020-02-23 14:51:29 -05:00
|
|
|
"message": err.msg,
|
2020-08-07 16:47:18 -04:00
|
|
|
"kind": err.kind_str,
|
2019-08-23 01:30:14 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 08:07:57 -04:00
|
|
|
pub fn serialize_result(promise_id: Option<u64>, result: JsonResult) -> Buf {
|
2019-08-23 01:30:14 -04:00
|
|
|
let value = match result {
|
|
|
|
Ok(v) => json!({ "ok": v, "promiseId": promise_id }),
|
|
|
|
Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }),
|
|
|
|
};
|
2020-02-09 13:54:16 -05:00
|
|
|
serde_json::to_vec(&value).unwrap().into_boxed_slice()
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct AsyncArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
}
|
|
|
|
|
2020-08-12 10:44:58 -04:00
|
|
|
pub fn json_op<D>(
|
|
|
|
d: D,
|
|
|
|
) -> impl Fn(&mut CoreIsolateState, &mut [ZeroCopyBuf]) -> Op
|
2019-10-01 18:51:05 -04:00
|
|
|
where
|
2020-08-12 10:44:58 -04:00
|
|
|
D: Fn(
|
2020-05-29 17:41:39 -04:00
|
|
|
&mut CoreIsolateState,
|
|
|
|
Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
&mut [ZeroCopyBuf],
|
2020-05-29 17:41:39 -04:00
|
|
|
) -> Result<JsonOp, OpError>,
|
2019-10-01 18:51:05 -04:00
|
|
|
{
|
2020-07-08 11:23:50 -04:00
|
|
|
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]) {
|
2019-10-25 13:23:16 -04:00
|
|
|
Ok(args) => args,
|
|
|
|
Err(e) => {
|
2020-02-23 14:51:29 -05:00
|
|
|
let buf = serialize_result(None, Err(OpError::from(e)));
|
2020-04-18 20:05:13 -04:00
|
|
|
return Op::Sync(buf);
|
2019-10-25 13:23:16 -04:00
|
|
|
}
|
|
|
|
};
|
2019-10-01 18:51:05 -04:00
|
|
|
let promise_id = async_args.promise_id;
|
|
|
|
let is_sync = promise_id.is_none();
|
2019-08-23 01:30:14 -04:00
|
|
|
|
2020-07-08 11:23:50 -04:00
|
|
|
let result = serde_json::from_slice(&zero_copy[0])
|
2020-02-23 14:51:29 -05:00
|
|
|
.map_err(OpError::from)
|
2020-08-12 10:44:58 -04:00
|
|
|
.and_then(|args| d(isolate_state, args, &mut zero_copy[1..]));
|
2019-10-01 18:51:05 -04:00
|
|
|
|
2020-04-18 20:05:13 -04:00
|
|
|
// Convert to Op
|
2019-10-01 18:51:05 -04:00
|
|
|
match result {
|
|
|
|
Ok(JsonOp::Sync(sync_value)) => {
|
|
|
|
assert!(promise_id.is_none());
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Sync(serialize_result(promise_id, Ok(sync_value)))
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
|
|
|
Ok(JsonOp::Async(fut)) => {
|
|
|
|
assert!(promise_id.is_some());
|
2019-11-16 19:17:47 -05:00
|
|
|
let fut2 = fut.then(move |result| {
|
2020-04-18 20:05:13 -04:00
|
|
|
futures::future::ready(serialize_result(promise_id, result))
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Async(fut2.boxed_local())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2020-01-21 12:01:10 -05:00
|
|
|
Ok(JsonOp::AsyncUnref(fut)) => {
|
|
|
|
assert!(promise_id.is_some());
|
|
|
|
let fut2 = fut.then(move |result| {
|
2020-04-18 20:05:13 -04:00
|
|
|
futures::future::ready(serialize_result(promise_id, result))
|
2020-01-21 12:01:10 -05:00
|
|
|
});
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::AsyncUnref(fut2.boxed_local())
|
2020-01-21 12:01:10 -05:00
|
|
|
}
|
2019-10-01 18:51:05 -04:00
|
|
|
Err(sync_err) => {
|
|
|
|
let buf = serialize_result(promise_id, Err(sync_err));
|
|
|
|
if is_sync {
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Sync(buf)
|
2019-10-01 18:51:05 -04:00
|
|
|
} else {
|
2020-04-18 20:05:13 -04:00
|
|
|
Op::Async(futures::future::ready(buf).boxed_local())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:51:29 -05:00
|
|
|
pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, OpError>
|
2019-08-23 01:30:14 -04:00
|
|
|
where
|
2020-02-03 18:08:44 -05:00
|
|
|
F: 'static + Send + FnOnce() -> JsonResult,
|
2019-08-23 01:30:14 -04:00
|
|
|
{
|
|
|
|
if is_sync {
|
|
|
|
Ok(JsonOp::Sync(f()?))
|
|
|
|
} else {
|
2020-02-07 17:54:44 -05:00
|
|
|
let fut = async move { tokio::task::spawn_blocking(f).await.unwrap() };
|
2020-02-03 18:08:44 -05:00
|
|
|
Ok(JsonOp::Async(fut.boxed_local()))
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
}
|