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

How do I add singleton static classes to global? #321

Open
stevefan1999-personal opened this issue May 27, 2024 · 1 comment
Open

How do I add singleton static classes to global? #321

stevefan1999-personal opened this issue May 27, 2024 · 1 comment

Comments

@stevefan1999-personal
Copy link

stevefan1999-personal commented May 27, 2024

I'm implementing some API from Web standard, and I often use this:

#[rquickjs::module]
pub mod text {
    use rquickjs::{module::Exports, Ctx};

    pub use super::{TextDecoder, TextEncoder};

    #[qjs(evaluate)]
    pub fn evaluate<'js>(ctx: &Ctx<'js>, exports: &mut Exports<'js>) -> rquickjs::Result<()> {
        for (k, v) in exports.iter() {
            ctx.globals().set(k.to_str()?, v)?;
        }

        Ok(())
    }
}

I tried to do this:

#[rquickjs::module]
pub mod text {
    use rquickjs::{class::JsClass, module::Exports, Ctx};

    pub use super::{TextDecoder, TextEncoder};

    #[qjs(evaluate)]
    pub fn evaluate<'js>(ctx: &Ctx<'js>, _exports: &Exports<'js>) -> rquickjs::Result<()> {
        ctx.globals()
            .set("TextDecoder", TextDecoder::prototype(ctx))?;
        ctx.globals()
            .set("TextEncoder", TextEncoder::prototype(ctx))?;

        Ok(())
    }
}

But it doesn't work. What is the proper way to add https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder and https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder into the global context during initialization?

@DelSkayn
Copy link
Owner

DelSkayn commented Jun 4, 2024

If you want to add a new API you must manually add it to the global object after creating a context. Rquickjs currently has no support doing this for you.

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