summaryrefslogtreecommitdiff
path: root/core/ops.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2021-04-30 16:51:54 +0200
committerGitHub <noreply@github.com>2021-04-30 10:51:54 -0400
commitfc9c7de94b08049dd04c4ca6016586cdac0dd2ac (patch)
tree939b1dab406e7ea9e077fed6832806016d879065 /core/ops.rs
parent5ec478b5fa6261e2b2d3c8daed3cba9e780730c7 (diff)
cleanup(core): replace OpResponse with OpResult (#10434)
Drop the Value/Buffer enum since #10432 allows buffers to be serialized rust => v8
Diffstat (limited to 'core/ops.rs')
-rw-r--r--core/ops.rs44
1 files changed, 9 insertions, 35 deletions
diff --git a/core/ops.rs b/core/ops.rs
index 6faffba4b..99bff406c 100644
--- a/core/ops.rs
+++ b/core/ops.rs
@@ -19,7 +19,7 @@ use std::pin::Pin;
use std::rc::Rc;
pub type PromiseId = u64;
-pub type OpAsyncFuture = Pin<Box<dyn Future<Output = (PromiseId, OpResponse)>>>;
+pub type OpAsyncFuture = Pin<Box<dyn Future<Output = (PromiseId, OpResult)>>>;
pub type OpFn =
dyn Fn(Rc<RefCell<OpState>>, OpPayload, Option<ZeroCopyBuf>) -> Op + 'static;
pub type OpId = usize;
@@ -58,13 +58,8 @@ impl<'a, 'b, 'c> OpPayload<'a, 'b, 'c> {
}
}
-pub enum OpResponse {
- Value(OpResult),
- Buffer(Box<[u8]>),
-}
-
pub enum Op {
- Sync(OpResponse),
+ Sync(OpResult),
Async(OpAsyncFuture),
/// AsyncUnref is the variation of Async, which doesn't block the program
/// exiting.
@@ -100,14 +95,14 @@ pub struct OpError {
pub fn serialize_op_result<R: Serialize + 'static>(
result: Result<R, AnyError>,
state: Rc<RefCell<OpState>>,
-) -> OpResponse {
- OpResponse::Value(match result {
+) -> OpResult {
+ match result {
Ok(v) => OpResult::Ok(v.into()),
Err(err) => OpResult::Err(OpError {
class_name: (state.borrow().get_error_class_fn)(&err),
message: err.to_string(),
}),
- })
+ }
}
/// Maintains the resources and ops inside a JS runtime.
@@ -205,35 +200,14 @@ mod tests {
let bar_id;
{
let op_table = &mut state.borrow_mut().op_table;
- foo_id = op_table.register_op("foo", |_, _, _| {
- Op::Sync(OpResponse::Buffer(b"oof!"[..].into()))
- });
+ foo_id = op_table
+ .register_op("foo", |_, _, _| Op::Sync(OpResult::Ok(321.into())));
assert_eq!(foo_id, 1);
- bar_id = op_table.register_op("bar", |_, _, _| {
- Op::Sync(OpResponse::Buffer(b"rab!"[..].into()))
- });
+ bar_id = op_table
+ .register_op("bar", |_, _, _| Op::Sync(OpResult::Ok(123.into())));
assert_eq!(bar_id, 2);
}
- let foo_res = OpTable::route_op(
- foo_id,
- state.clone(),
- OpPayload::empty(),
- Default::default(),
- );
- assert!(
- matches!(foo_res, Op::Sync(OpResponse::Buffer(buf)) if &*buf == b"oof!")
- );
- let bar_res = OpTable::route_op(
- bar_id,
- state.clone(),
- OpPayload::empty(),
- Default::default(),
- );
- assert!(
- matches!(bar_res, Op::Sync(OpResponse::Buffer(buf)) if &*buf == b"rab!")
- );
-
let mut catalog_entries = OpTable::op_entries(state);
catalog_entries.sort_by(|(_, id1), (_, id2)| id1.partial_cmp(id2).unwrap());
assert_eq!(