|
1 | 1 | # rquickjs module |
2 | 2 |
|
3 | | -[](https://github.com/rquickjs/rquickjs-module) |
4 | | -[](https://crates.io/crates/rquickjs-module) |
| 3 | +[](https://github.com/rquickjs/rquickjs-module) |
| 4 | +[](https://crates.io/crates/rquickjs-module) |
5 | 5 |
|
6 | 6 | This is an extension to [rquickjs](https://github.com/DelSkayn/rquickjs) to allow the ecosystem to create more unified Rust modules. |
7 | 7 |
|
8 | 8 | The goal was to create a better version of [`ModuleDef`](https://docs.rs/rquickjs/latest/rquickjs/module/trait.ModuleDef.html) that would allow it to have options as input and set global. |
9 | 9 |
|
10 | 10 | For example, a `fetch` module using `ModuleDefExt` could set a global `fetch` function and have as options an allowlist of domains. |
11 | 11 |
|
12 | | -## Usage |
| 12 | +## Using modules |
| 13 | + |
| 14 | +If you are a consumer of modules create using that crate, here is how you can import them in your runtime. |
| 15 | + |
| 16 | +```rust |
| 17 | +use rquickjs::AsyncRuntime; |
| 18 | +use rquickjs_module::ModuleLoader; |
| 19 | + |
| 20 | +#[tokio::main] |
| 21 | +async fn main() { |
| 22 | + let rt = AsyncRuntime::new().unwrap(); |
| 23 | + |
| 24 | + let (loader, resolver, initalizer) = ModuleLoader::builder().with_module(MyModule).build(); |
| 25 | + |
| 26 | + rt.set_loader(resolver, loader).await; |
| 27 | + |
| 28 | + let ctx = AsyncContext::full(&rt).await.unwrap(); |
| 29 | + |
| 30 | + async_with!(ctx => |ctx| { |
| 31 | + if let Err(err) = initalizer.init(&ctx).catch(&ctx){ |
| 32 | + eprintln!("{:?}", err); |
| 33 | + } |
| 34 | + }) |
| 35 | + .await; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## Creating modules |
| 40 | + |
| 41 | +For the base case, replace the `ModuleDef` by an implementation of `ModuleDefExt`. |
| 42 | + |
| 43 | +```rust |
| 44 | +use rquickjs::{Ctx, JsLifetime, Object, Result}; |
| 45 | +use rquickjs_module::{ModuleDefExt, ModuleImpl}; |
| 46 | + |
| 47 | +#[derive(JsLifetime, Debug)] |
| 48 | +struct MyModuleOptions { |
| 49 | + user: String, |
| 50 | +} |
| 51 | + |
| 52 | +struct MyModule { |
| 53 | + options: MyModuleOptions, |
| 54 | +} |
| 55 | + |
| 56 | +impl MyModule { |
| 57 | + pub fn new<T: Into<String>>(user: T) -> Self { |
| 58 | + Self { |
| 59 | + options: MyModuleOptions { |
| 60 | + user: user.into(), |
| 61 | + }, |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl ModuleDefExt<MyModuleOptions> for MyModule { |
| 67 | + // Use `ModuleImpl` when you want a Javascript module. |
| 68 | + // The options generic is not required. |
| 69 | + type Implementation = ModuleImpl<MyModuleOptions>; |
| 70 | + |
| 71 | + fn implementation() -> &'static Self::Implementation { |
| 72 | + // This is the same as the implementation of `ModuleDef` |
| 73 | + &ModuleImpl { |
| 74 | + declare: |decl| { |
| 75 | + decl.declare("user")?; |
| 76 | + Ok(()) |
| 77 | + }, |
| 78 | + evaluate: |ctx, exports, options| { |
| 79 | + exports.export("user", options.user.clone())?; |
| 80 | + Ok(()) |
| 81 | + }, |
| 82 | + name: "my-module", |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn options(self) -> MyModuleOptions { |
| 87 | + self.options |
| 88 | + } |
| 89 | + |
| 90 | + fn globals(globals: &Object<'_>, options: &MyModuleOptions) -> Result<()> { |
| 91 | + // Set your globals here |
| 92 | + globals.set("user", options.user.clone())?; |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Globals only |
| 99 | + |
| 100 | +If you only need to set globals and do **NOT** want an actual module that the Javascript can import, use `GlobalsOnly` for the implementation. |
| 101 | + |
| 102 | +```rust |
| 103 | +use rquickjs::{Object, Result}; |
| 104 | +use rquickjs_module::{ModuleDefExt, GlobalsOnly}; |
| 105 | + |
| 106 | +struct MyModule; |
| 107 | + |
| 108 | +impl ModuleDefExt for MyModule { |
| 109 | + type Implementation = GlobalsOnly; |
| 110 | + |
| 111 | + fn implementation() -> &'static Self::Implementation { |
| 112 | + &GlobalsOnly |
| 113 | + } |
| 114 | + |
| 115 | + fn options(self) {} |
| 116 | + |
| 117 | + fn globals(globals: &Object<'_>, options: &()) -> Result<()> { |
| 118 | + // Set your globals here |
| 119 | + globals.set("hello", "world".to_string())?; |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +You can also use the macro for a simpler experience: |
| 126 | + |
| 127 | +```rust |
| 128 | +use rquickjs_module::globals_only_module; |
| 129 | + |
| 130 | +struct MyModule; |
| 131 | +globals_only_module!(MyModule, |globals| { |
| 132 | + // Custom globals initialization code here |
| 133 | + Ok(()) |
| 134 | +}); |
| 135 | +``` |
0 commit comments