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

[WIP] feat: dispatch_json in core #4434

Closed
wants to merge 1 commit into from

Conversation

afinch7
Copy link
Contributor

@afinch7 afinch7 commented Mar 19, 2020

heavily based on #3692
This should be mostly working other than plugin support(maybe save that for a follow up).

cc @bartlomieju

@afinch7 afinch7 force-pushed the dispatch_json_core branch 2 times, most recently from 1998d2c to 6612fcb Compare March 20, 2020 19:20
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afinch7 could you scale back this PR to just add a separate crate for dispatch_json without changing CLI/plugins? It can be another example in deno_core that uses dispatch json. It will be much easier to iterate on and land

Comment on lines +131 to +69
pub type JsonResult<E> = Result<Value, E>;

pub type AsyncJsonOp<E> = Pin<Box<dyn Future<Output = JsonResult<E>>>>;

pub enum JsonOp<E: JsonError> {
Sync(Value),
Async(AsyncJsonOp<E>),
/// AsyncUnref is the variation of Async, which doesn't block the program
/// exiting.
AsyncUnref(AsyncJsonOp<E>),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type parameter required here? It leads to Result<JsonOp<OpError>, OpError> declarations... How about:

pub type JsonResult = Result<Value, Into<JsonSerializableError>>;

struct JsonSerializableError {
   kind/id: i32,
   message: String
}

It would require users to implement only single trait for custom error type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really simplify anything afaik.

  • Can't implement Into directly for foreign error kinds I.E. impl Into<JsonSerializableError> for url::ParseError
  • Into is a trait therefore it isn't sized(would need to be boxed)
  • From is required for use of the ? operator

Comment on lines 107 to 194
/// Internal representation of an error from inside or outside of json dispatch.
/// All errors get converted to this format before being serialized.
#[derive(Debug, Clone, Serialize)]
struct FinalJsonError {
pub kind: i64,
pub message: String,
}

impl<E: JsonError + Send + Sync> From<E> for FinalJsonError {
fn from(e: E) -> FinalJsonError {
let kind = match e.kind() {
JsonErrorKind::Kind(k) => {
assert!(k != 0, "kind = 0 is reserved for UnKind");
k as i64
}
JsonErrorKind::UnKind => 0,
};
FinalJsonError {
kind,
message: e.msg(),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really complex, PTAL at my comment below, I believe we could get rid of it completely

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is internal only. Not relevant to implementations of dispatch_json. The existing implementation in cli only requires a single trait implementation for OpError:

impl JsonError for OpError {
  fn kind(&self) -> JsonErrorKind {
    (self.kind as u32).into()
  }

  fn msg(&self) -> String {
    self.msg.clone()
  }
}

@afinch7 afinch7 force-pushed the dispatch_json_core branch 3 times, most recently from f0ea28a to 67bdd57 Compare March 31, 2020 18:59
@afinch7 afinch7 force-pushed the dispatch_json_core branch 2 times, most recently from 531b081 to 83b0e00 Compare May 15, 2020 15:30
@afinch7
Copy link
Contributor Author

afinch7 commented May 15, 2020

I still want to land this one. I'm working on some simplifications now.

Comment on lines +18 to +30
pub trait DispatchOpFn:
Fn(&mut dyn Interface, &[u8], Option<ZeroCopyBuf>) -> Op
{
fn box_op(self) -> Box<dyn DispatchOpFn>;
}

impl<D: Fn(&mut dyn Interface, &[u8], Option<ZeroCopyBuf>) -> Op + 'static>
DispatchOpFn for D
{
fn box_op(self) -> Box<dyn DispatchOpFn> {
Box::new(self)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It don't know if this really simplifies things enough to be worth it. It's not much harder to just use Box::new. Users will still have to import the trait to use it anyways.

@bartlomieju bartlomieju added cli related to cli/ dir deno_core Changes in "deno_core" crate are needed labels May 18, 2020
@afinch7
Copy link
Contributor Author

afinch7 commented May 21, 2020

This seems to have created a segfault on windows at exit. It shouldn't really be possible to drop the library as there are still references to it, but for some reason dropping plugin ops at exit causes a segfault only on windows. Edit: It might make sense to land the changes to the plugin api in their own PR.

@ry
Copy link
Member

ry commented Jun 25, 2020

Closed in favor of #6398

@ry ry closed this Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli related to cli/ dir deno_core Changes in "deno_core" crate are needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants