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

Allow using ? operator for custom error types in Function callbacks #1606

Closed
webmaster128 opened this issue Sep 10, 2020 · 2 comments
Closed
Assignees

Comments

@webmaster128
Copy link
Contributor

webmaster128 commented Sep 10, 2020

I'm trying to port an import implementation to Wasmer Reborn. What I have so far is:

                "db_read" => Function::new_with_env(&store, &u32_to_u32, env.clone(), |env, args| {
                    let key_ptr = args[0].unwrap_i32() as u32;
                    let ptr = do_read::<S, Q>(env, key_ptr)?;
                    Ok(vec![ptr.into()])
                }),

where do_read returns a core::result::Result<u32, VmError>. Now I implemented

impl Into<wasmer_engine::RuntimeError> for VmError {
    fn into(self) -> wasmer_engine::RuntimeError {
        let msg: String = self.to_string();
        wasmer_engine::RuntimeError::new(msg)
    }
}

From<T> for U implies Into<U> for T but not the other way round.

Unfortunately the ? operator required From to be implemented, not Into. I don't know why this was chosen by Rust but I don't think it will change any time soon.

Would it be possible to require Into<RuntimeError> instead of RuntimeError in the callback like this:

    pub fn new_with_env<F, Err, Env>(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self
    where
        F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, Err> + 'static,
        Err: Into<RuntimeError>,
        Env: Sized + 'static,
    {

Any other ideas how to deal with such use cases? In our old code, Wasmer did not care about the error type in the callback's Result.

@MarkMcCaskey MarkMcCaskey self-assigned this Sep 11, 2020
@webmaster128
Copy link
Contributor Author

webmaster128 commented Sep 15, 2020

Nevermind, I can implement

// VmError is my custom error

impl From<VmError> for wasmer_engine::RuntimeError {
    fn from(original: VmError) -> wasmer_engine::RuntimeError {
        let msg: String = original.to_string();
        wasmer_engine::RuntimeError::new(msg)
    }
}

I was not expecting to be able to implement for a foreign type, but it works. From my side this can be closed, but since you assigned yourself, @MarkMcCaskey, do you have additional thoughts?

@MarkMcCaskey
Copy link
Contributor

@webmaster128 Oh, that's great to hear! If it's solved on your end then I think it's good, I thought there might be another issue here but I just checked and it doesn't seem to be an issue.

I'll close this then. Feel free to reopen it if other issues come up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants