DelSkayn / rquickjs

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

Evaluation of a File fails and returns strange error #154

Closed DraftedDev closed 1 year ago

DraftedDev commented 1 year ago

Hi, I recently stumbled upon this great library, however my code fails with a weird exception. My cargo dependency looks like this: rquickjs = { git = "https://github.com/DelSkayn/rquickjs.git", features = ["smol", "macro", "parallel", "dyn-load", "loader"] }

I have this code:

        let loader = (
            BuiltinLoader::default(),
            ScriptLoader::default(),
            NativeLoader::default(),
            self.modules, // just an empty ModuleLoader::default()
        );

        let mut runtime = Runtime::new().expect("Failed to create runtime");
        runtime.set_loader(
            FileResolver::default()
                .with_native()
                .with_paths(self.paths), // just an empty Vec<String>
            loader);

        // js context with runtime
        let context = Context::builder()
            .build(&runtime)
            .expect("Failed to create context");

        // actually evaluate/run modules and file
        context.with(|ctx| {
            ctx.eval_file(&Path::new("../scripts/script.js")).unwrap_or_else(|e| {
                panic!("{}", e);
            });
        });

When running this, rust compiles, but generates a runtime error that says "Exception generated by quickjs". Seems to have something to do with .eval_file().

NOTE: The ../scripts/script.js file DOES exist in the RIGHT location and contains nothing at all (empty file).

Anyone know what exactly is failing and how to fix it? Also more detailed QuickJS Errors would be cool. Thanks 😄

DelSkayn commented 1 year ago

Hi, looking at your code it seems you build a context without the ability to evaluate code. Context::builder by default does not able any intrisics, use ContextBuilder::with

If you want to run javascript code from source you need to enable at least the intrisic Eval and probably also BaseObjects. Otherwise the context is only able to run code already compiled into bytecode.

DelSkayn commented 1 year ago

The reason the error message is so short is because QuickJS doesn't return an error message in this case and just returns an exception

DraftedDev commented 1 year ago

WOW! That helped!

Thanks 👍 for your help and developing this crate!