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

export_class alternative for napi runtime? #836

Closed
CumpsD opened this issue Dec 21, 2021 · 2 comments
Closed

export_class alternative for napi runtime? #836

CumpsD opened this issue Dec 21, 2021 · 2 comments

Comments

@CumpsD
Copy link

CumpsD commented Dec 21, 2021

I am trying to create a very small test binding:

use neon::prelude::*;

#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
    cx.export_class::<Blake3Hash>("Hasher");
    Ok(())
}

pub struct Blake3Hash {
    hasher: blake3::Hasher,
}

impl Blake3Hash {}

impl Default for Blake3Hash {
    fn default() -> Self {
        Self { hasher: blake3::Hasher::new() }
    }
}

impl Finalize for Blake3Hash { }

My cargo.toml is created by create neon:

[package]
name = "blakish"
version = "0.1.0"
license = "ISC"
edition = "2018"
exclude = ["index.node"]

[lib]
crate-type = ["cdylib"]

[dependencies]
blake3 = "1.2"

[dependencies.neon]
version = "0.9"
default-features = false
features = ["napi-6"]

However on cx.export_class::<Blake3Hash>("Hasher"); I am getting an error the method is not found because it is apparently only for the legacy-runtime.

    #[cfg(feature = "legacy-runtime")]
    /// Convenience method for exporting a Neon class constructor from a module.
    pub fn export_class<T: Class>(&mut self, key: &str) -> NeonResult<()> {
        let constructor = T::constructor(self)?;
        self.exports.set(self, key, constructor)?;
        Ok(())
    }

Is there still a way to export a class with napi? Or do we have to wrap it all in functions now with JsBox?

@kjvalencik
Copy link
Member

Correct. Classes are only implemented in the legacy runtime. Their design is not compatible with with web workers and were not easily ported. There is proposal to add them (neon-bindings/rfcs#43), but it's not being currently worked on.

In the meantime, JsBox provides everything you need when combined with some JavaScript glue code. The idea is that store your Rust data in a JsBox and create a wrapper class in JavaScript that passes it to functions, either as the first argument or bound to this.

This example demonstrates how: https://github.com/neon-bindings/examples/blob/main/examples/async-sqlite/index.js

https://docs.rs/neon/latest/neon/types/struct.JsBox.html

@CumpsD
Copy link
Author

CumpsD commented Dec 21, 2021

Ok I will have to have a look. I am getting this with the legacy runtime: connor4312/blake3#38

Trying to move to the new one, but it needs to expose that class, will have to refactor I guess

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