embed!() { /* proc-macro */ }
Available on crate feature
macro
only.Expand description
A macro for embedding JavaScript code into a binary.
Compiles a JavaScript module to bytecode and then compiles the resulting bytecode into the binary. Each file loaded is turned into its own module. The macro takes a list of paths to files to be compiled into a module with an option name. Module paths are relative to the crate manifest file.
ยงUsage
use rquickjs::{embed, loader::Bundle, CatchResultExt, Context, Module, Runtime};
/// load the `my_module.js` file and name it myModule
static BUNDLE: Bundle = embed! {
"myModule": "my_module.js",
};
fn main() {
let rt = Runtime::new().unwrap();
let ctx = Context::full(&rt).unwrap();
rt.set_loader(BUNDLE, BUNDLE);
ctx.with(|ctx| {
Module::evaluate(
ctx.clone(),
"testModule",
r#"
import { foo } from 'myModule';
if(foo() !== 2){
throw new Error("Function didn't return the correct value");
}
"#,
)
.unwrap()
.finish::<()>()
.catch(&ctx)
.unwrap();
})
}