2021-03-20 12:51:08 -04:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
use crate::error::AnyError;
|
2021-03-31 10:37:38 -04:00
|
|
|
use crate::serialize_op_result;
|
2021-03-20 12:51:08 -04:00
|
|
|
use crate::Op;
|
|
|
|
use crate::OpFn;
|
|
|
|
use crate::OpState;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::future::Future;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-08-31 07:08:16 -04:00
|
|
|
/// A helper function that returns a sync NOP OpFn
|
|
|
|
///
|
|
|
|
/// It's mainly intended for embedders who want to disable ops, see ./examples/disable_ops.rs
|
|
|
|
pub fn void_op_sync() -> Box<OpFn> {
|
|
|
|
// TODO(@AaronO): use this simpler implementation after changing serde_v8 to allow all values
|
|
|
|
// to deserialize to the unit type instead of failing with `ExpectedNull`
|
|
|
|
// op_sync(|_, _: (), _: ()| Ok(()))
|
|
|
|
Box::new(move |state, _| -> Op {
|
|
|
|
let op_result = serialize_op_result(Ok(()), state);
|
|
|
|
Op::Sync(op_result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A helper function that returns an async NOP OpFn
|
|
|
|
///
|
|
|
|
/// It's mainly intended for embedders who want to disable ops, see ./examples/disable_ops.rs
|
|
|
|
pub fn void_op_async() -> Box<OpFn> {
|
|
|
|
// TODO(@AaronO): use this simpler implementation after changing serde_v8 to allow all values
|
|
|
|
// to deserialize to the unit type instead of failing with `ExpectedNull`
|
|
|
|
// op_async(|_, _: (), _: ()| futures::future::ok(()))
|
|
|
|
Box::new(move |state, payload| -> Op {
|
|
|
|
let pid = payload.promise_id;
|
|
|
|
let op_result = serialize_op_result(Ok(()), state);
|
|
|
|
Op::Async(Box::pin(futures::future::ready((pid, op_result))))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-20 12:51:08 -04:00
|
|
|
/// Creates an op that passes data synchronously using JSON.
|
|
|
|
///
|
|
|
|
/// The provided function `op_fn` has the following parameters:
|
|
|
|
/// * `&mut OpState`: the op state, can be used to read/write resources in the runtime from an op.
|
|
|
|
/// * `V`: the deserializable value that is passed to the Rust function.
|
|
|
|
/// * `&mut [ZeroCopyBuf]`: raw bytes passed along, usually not needed if the JSON value is used.
|
|
|
|
///
|
|
|
|
/// `op_fn` returns a serializable value, which is directly returned to JavaScript.
|
|
|
|
///
|
|
|
|
/// When registering an op like this...
|
|
|
|
/// ```ignore
|
|
|
|
/// let mut runtime = JsRuntime::new(...);
|
2021-04-12 15:55:05 -04:00
|
|
|
/// runtime.register_op("hello", deno_core::op_sync(Self::hello_op));
|
2021-04-25 16:00:05 -04:00
|
|
|
/// runtime.sync_ops_cache();
|
2021-03-20 12:51:08 -04:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ...it can be invoked from JS using the provided name, for example:
|
|
|
|
/// ```js
|
2021-06-23 15:11:00 -04:00
|
|
|
/// let result = Deno.core.opSync("hello", args);
|
2021-03-20 12:51:08 -04:00
|
|
|
/// ```
|
|
|
|
///
|
2021-04-25 16:00:05 -04:00
|
|
|
/// `runtime.sync_ops_cache()` must be called after registering new ops
|
2021-03-20 12:51:08 -04:00
|
|
|
/// A more complete example is available in the examples directory.
|
2021-05-06 13:32:03 -04:00
|
|
|
pub fn op_sync<F, A, B, R>(op_fn: F) -> Box<OpFn>
|
2021-03-20 12:51:08 -04:00
|
|
|
where
|
2021-05-06 13:32:03 -04:00
|
|
|
F: Fn(&mut OpState, A, B) -> Result<R, AnyError> + 'static,
|
|
|
|
A: DeserializeOwned,
|
|
|
|
B: DeserializeOwned,
|
2021-03-31 10:37:38 -04:00
|
|
|
R: Serialize + 'static,
|
2021-03-20 12:51:08 -04:00
|
|
|
{
|
2021-05-06 13:32:03 -04:00
|
|
|
Box::new(move |state, payload| -> Op {
|
2021-03-31 10:37:38 -04:00
|
|
|
let result = payload
|
|
|
|
.deserialize()
|
2021-05-06 13:32:03 -04:00
|
|
|
.and_then(|(a, b)| op_fn(&mut state.borrow_mut(), a, b));
|
2021-03-31 10:37:38 -04:00
|
|
|
Op::Sync(serialize_op_result(result, state))
|
2021-03-20 12:51:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an op that passes data asynchronously using JSON.
|
|
|
|
///
|
2021-06-25 00:15:35 -04:00
|
|
|
/// When this op is dispatched, the runtime doesn't exit while processing it.
|
|
|
|
/// Use op_async_unref instead if you want to make the runtime exit while processing it.
|
|
|
|
///
|
2021-03-20 12:51:08 -04:00
|
|
|
/// The provided function `op_fn` has the following parameters:
|
|
|
|
/// * `Rc<RefCell<OpState>`: the op state, can be used to read/write resources in the runtime from an op.
|
|
|
|
/// * `V`: the deserializable value that is passed to the Rust function.
|
|
|
|
/// * `BufVec`: raw bytes passed along, usually not needed if the JSON value is used.
|
|
|
|
///
|
|
|
|
/// `op_fn` returns a future, whose output is a serializable value. This value will be asynchronously
|
|
|
|
/// returned to JavaScript.
|
|
|
|
///
|
|
|
|
/// When registering an op like this...
|
|
|
|
/// ```ignore
|
|
|
|
/// let mut runtime = JsRuntime::new(...);
|
2021-04-12 15:55:05 -04:00
|
|
|
/// runtime.register_op("hello", deno_core::op_async(Self::hello_op));
|
2021-04-25 16:00:05 -04:00
|
|
|
/// runtime.sync_ops_cache();
|
2021-03-20 12:51:08 -04:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ...it can be invoked from JS using the provided name, for example:
|
|
|
|
/// ```js
|
2021-06-23 15:11:00 -04:00
|
|
|
/// let future = Deno.core.opAsync("hello", args);
|
2021-03-20 12:51:08 -04:00
|
|
|
/// ```
|
|
|
|
///
|
2021-04-25 16:00:05 -04:00
|
|
|
/// `runtime.sync_ops_cache()` must be called after registering new ops
|
2021-03-20 12:51:08 -04:00
|
|
|
/// A more complete example is available in the examples directory.
|
2021-05-06 13:32:03 -04:00
|
|
|
pub fn op_async<F, A, B, R, RV>(op_fn: F) -> Box<OpFn>
|
2021-03-20 12:51:08 -04:00
|
|
|
where
|
2021-05-06 13:32:03 -04:00
|
|
|
F: Fn(Rc<RefCell<OpState>>, A, B) -> R + 'static,
|
|
|
|
A: DeserializeOwned,
|
|
|
|
B: DeserializeOwned,
|
2021-03-20 12:51:08 -04:00
|
|
|
R: Future<Output = Result<RV, AnyError>> + 'static,
|
2021-03-31 10:37:38 -04:00
|
|
|
RV: Serialize + 'static,
|
2021-03-20 12:51:08 -04:00
|
|
|
{
|
2021-05-06 13:32:03 -04:00
|
|
|
Box::new(move |state, payload| -> Op {
|
2021-04-18 09:29:06 -04:00
|
|
|
let pid = payload.promise_id;
|
|
|
|
// Deserialize args, sync error on failure
|
|
|
|
let args = match payload.deserialize() {
|
|
|
|
Ok(args) => args,
|
|
|
|
Err(err) => {
|
|
|
|
return Op::Sync(serialize_op_result(Err::<(), AnyError>(err), state))
|
|
|
|
}
|
|
|
|
};
|
2021-05-06 13:32:03 -04:00
|
|
|
let (a, b) = args;
|
2021-03-31 10:37:38 -04:00
|
|
|
|
|
|
|
use crate::futures::FutureExt;
|
2021-05-06 13:32:03 -04:00
|
|
|
let fut = op_fn(state.clone(), a, b)
|
2021-04-11 01:05:43 -04:00
|
|
|
.map(move |result| (pid, serialize_op_result(result, state)));
|
2021-04-18 09:29:06 -04:00
|
|
|
Op::Async(Box::pin(fut))
|
|
|
|
})
|
2021-03-20 12:51:08 -04:00
|
|
|
}
|
2021-04-09 11:55:33 -04:00
|
|
|
|
2021-06-25 00:15:35 -04:00
|
|
|
/// Creates an op that passes data asynchronously using JSON.
|
|
|
|
///
|
|
|
|
/// When this op is dispatched, the runtime still can exit while processing it.
|
|
|
|
///
|
|
|
|
/// The other usages are the same as `op_async`.
|
|
|
|
pub fn op_async_unref<F, A, B, R, RV>(op_fn: F) -> Box<OpFn>
|
|
|
|
where
|
|
|
|
F: Fn(Rc<RefCell<OpState>>, A, B) -> R + 'static,
|
|
|
|
A: DeserializeOwned,
|
|
|
|
B: DeserializeOwned,
|
|
|
|
R: Future<Output = Result<RV, AnyError>> + 'static,
|
|
|
|
RV: Serialize + 'static,
|
|
|
|
{
|
|
|
|
Box::new(move |state, payload| -> Op {
|
|
|
|
let pid = payload.promise_id;
|
|
|
|
// Deserialize args, sync error on failure
|
|
|
|
let args = match payload.deserialize() {
|
|
|
|
Ok(args) => args,
|
|
|
|
Err(err) => {
|
|
|
|
return Op::Sync(serialize_op_result(Err::<(), AnyError>(err), state))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let (a, b) = args;
|
|
|
|
|
|
|
|
use crate::futures::FutureExt;
|
|
|
|
let fut = op_fn(state.clone(), a, b)
|
|
|
|
.map(move |result| (pid, serialize_op_result(result, state)));
|
|
|
|
Op::AsyncUnref(Box::pin(fut))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-09 11:55:33 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[tokio::test]
|
2021-04-12 15:55:05 -04:00
|
|
|
async fn op_async_stack_trace() {
|
2021-04-09 11:55:33 -04:00
|
|
|
let mut runtime = crate::JsRuntime::new(Default::default());
|
|
|
|
|
|
|
|
async fn op_throw(
|
|
|
|
_state: Rc<RefCell<OpState>>,
|
|
|
|
msg: Option<String>,
|
2021-05-06 13:32:03 -04:00
|
|
|
_: (),
|
2021-04-09 11:55:33 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
assert_eq!(msg.unwrap(), "hello");
|
|
|
|
Err(crate::error::generic_error("foo"))
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
runtime.register_op("op_throw", op_async(op_throw));
|
2021-04-25 16:00:05 -04:00
|
|
|
runtime.sync_ops_cache();
|
2021-04-09 11:55:33 -04:00
|
|
|
runtime
|
2021-06-21 19:45:41 -04:00
|
|
|
.execute_script(
|
2021-04-09 11:55:33 -04:00
|
|
|
"<init>",
|
|
|
|
r#"
|
|
|
|
async function f1() {
|
2021-04-12 15:55:05 -04:00
|
|
|
await Deno.core.opAsync('op_throw', 'hello');
|
2021-04-09 11:55:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function f2() {
|
|
|
|
await f1();
|
|
|
|
}
|
|
|
|
|
|
|
|
f2();
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-05-26 15:07:12 -04:00
|
|
|
let e = runtime.run_event_loop(false).await.unwrap_err().to_string();
|
2021-04-09 11:55:33 -04:00
|
|
|
println!("{}", e);
|
|
|
|
assert!(e.contains("Error: foo"));
|
|
|
|
assert!(e.contains("at async f1 (<init>:"));
|
|
|
|
assert!(e.contains("at async f2 (<init>:"));
|
|
|
|
}
|
|
|
|
}
|