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

Redeclaration of Modules? #344

Open
Gentle opened this issue Jul 30, 2024 · 3 comments
Open

Redeclaration of Modules? #344

Gentle opened this issue Jul 30, 2024 · 3 comments

Comments

@Gentle
Copy link

Gentle commented Jul 30, 2024

I would like to be able to redeclare modules, but the observed behavior is that the first declaration is always taken

Example Code that doesn't do what I would have expected:

    #[test]
    fn test_redeclare() {
        let rt = rquickjs::Runtime::new().unwrap();
        let context = rquickjs::Context::full(&rt).unwrap();
        context.with(|ctx| {
            Module::evaluate(
                ctx.clone(),
                "a",
                r#"export default function() { return 42 }"#,
            )
            .catch(&ctx)
            .unwrap();
            while ctx.execute_pending_job() {}
            Module::evaluate(
                ctx.clone(),
                "a",
                r#"export default function() { return 10 }"#,
            )
            .catch(&ctx)
            .unwrap();
            while ctx.execute_pending_job() {}
            let (module, _) = Module::declare(
                ctx.clone(),
                "b",
                r#"
                import blah from 'a'
                export default function() { return blah() }
                "#,
            )
            .catch(&ctx)
            .unwrap()
            .eval()
            .catch(&ctx)
            .unwrap();
            while ctx.execute_pending_job() {}
            let func = module
                .namespace()
                .unwrap()
                .get::<_, Function>("default")
                .unwrap();
            let x: i32 = func.call(()).catch(&ctx).unwrap();
            assert_eq!(x, 10);
        });
    }
@DelSkayn
Copy link
Owner

DelSkayn commented Aug 1, 2024

This is the standard QuickJS behavior and I think rquickjs should stick to to how QuickJS does things.

@DelSkayn
Copy link
Owner

DelSkayn commented Aug 5, 2024

Too add I don't think there is a good way to support this without some extensive patching of QuickJS. So I don't think there is a way to do this.

@Gentle
Copy link
Author

Gentle commented Aug 8, 2024

thanks for your response, I assumed as much

I'm trying to get some form of hot module reloading to work, I've now switched to declaring the modules and keeping the returned namespace in a registry, so that the registry can handle when a module has been replaced with a new version

If there's a better way I'm interested :)

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