DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
504 stars 63 forks source link

Custom loaders can't set module meta #177

Closed richarddd closed 1 year ago

richarddd commented 1 year ago

Since loaders now have to return Result<ModuleData> there is no longer a way to set input meta object since we never have access to the Module object before it's evaluated.

I can work around this by implementing RawLoader but that requires all my loaders to implement RawLoader, and i can't use the default ones without creating wrappers for them.

A solution to this could be to have an optional fn on_load(ctx,module) -> Result<()> for the Loader trait

richarddd commented 1 year ago

Solved it by using a wrapper around all my loaders and then setting input meta from there.

struct RawLoaderContainer<T: Loader>(T);

unsafe impl<T: Loader> RawLoader for RawLoaderContainer<T> {
    unsafe fn raw_load<'js>(&mut self, ctx: Ctx<'js>, name: &str) -> Result<Module<'js>> {
        let res = self.0.load(ctx, name)?.unsafe_declare(ctx)?;
        set_import_meta(res, name)?;
        Ok(res)
    }
}
let loader = RawLoaderContainer((ModuleLoader::default(),BuiltinLoader::default()))