Skip to content

Commit 07dd395

Browse files
committed
Improve documentation
1 parent 968b06c commit 07dd395

File tree

4 files changed

+15
-120
lines changed

4 files changed

+15
-120
lines changed

src/lib.rs

Lines changed: 10 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! An improved module system for rquickjs
2+
//!
3+
//! This is an extension to [rquickjs](https://github.com/DelSkayn/rquickjs)
4+
//! to allow the ecosystem to create more unified Rust modules.
5+
//!
6+
//! The goal was to create a better version of
7+
//! [`ModuleDef`](rquickjs::module::ModuleDef)
8+
//! that would allow it to have options as input and set global.
9+
110
pub use self::definition::{GlobalsOnly, ModuleDefExt, ModuleImpl};
211
pub use self::loader::{GlobalInitializer, ModuleLoader, ModuleLoaderBuilder, ModuleResolver};
312

@@ -8,15 +17,12 @@ mod wrapper;
817

918
#[cfg(test)]
1019
mod tests {
11-
1220
use rquickjs::{
1321
async_with, class::Trace, context::EvalOptions, AsyncContext, AsyncRuntime, CatchResultExt,
1422
JsLifetime, Object, Result, Value,
1523
};
1624

17-
use crate::{globals_only_module, GlobalsOnly, ModuleImpl, ModuleLoader};
18-
19-
use super::ModuleDefExt;
25+
use super::*;
2026

2127
struct Example;
2228
impl ModuleDefExt for Example {
@@ -34,119 +40,4 @@ mod tests {
3440
// Custom globals initialization code here
3541
Ok(())
3642
});
37-
38-
#[derive(Clone, Trace, JsLifetime)]
39-
#[rquickjs::class(frozen)]
40-
struct Console {
41-
target: String,
42-
newline: bool,
43-
}
44-
45-
impl Console {
46-
pub fn new(target: String, newline: bool) -> Self {
47-
Self { target, newline }
48-
}
49-
}
50-
51-
#[rquickjs::methods]
52-
impl Console {
53-
fn log(&self, value: Value<'_>) {
54-
print!(
55-
"{}: {:?}{}",
56-
self.target,
57-
value,
58-
if self.newline { "\n" } else { "" }
59-
);
60-
}
61-
}
62-
63-
#[derive(JsLifetime, Debug)]
64-
struct ConsoleOptions {
65-
target: String,
66-
newline: bool,
67-
}
68-
69-
struct ConsoleModule {
70-
options: ConsoleOptions,
71-
}
72-
73-
impl ConsoleModule {
74-
pub fn new<T: Into<String>>(target: T, newline: bool) -> Self {
75-
Self {
76-
options: ConsoleOptions {
77-
target: target.into(),
78-
newline,
79-
},
80-
}
81-
}
82-
}
83-
84-
impl ModuleDefExt<ConsoleOptions> for ConsoleModule {
85-
type Implementation = ModuleImpl<ConsoleOptions>;
86-
87-
fn implementation() -> &'static Self::Implementation {
88-
&ModuleImpl {
89-
declare: |decl| {
90-
decl.declare("default")?;
91-
Ok(())
92-
},
93-
evaluate: |ctx, exports, options| {
94-
println!("Options in eval? {:?}", options);
95-
exports.export("default", options.target.clone())?;
96-
Ok(())
97-
},
98-
name: "console",
99-
}
100-
}
101-
102-
fn options(self) -> ConsoleOptions {
103-
self.options
104-
}
105-
106-
fn globals(globals: &Object<'_>, options: &ConsoleOptions) -> Result<()> {
107-
println!("Options in globals? {:?}", options);
108-
Ok(())
109-
}
110-
}
111-
112-
#[tokio::test]
113-
async fn test() {
114-
let rt = AsyncRuntime::new().unwrap();
115-
116-
let mut loader = ModuleLoader::builder();
117-
loader.add_module(ConsoleModule::new("console", true));
118-
119-
let (loader, resolver, initalizer) = loader.build();
120-
121-
// let loader = ModuleLoader::default().with_module(
122-
// "console",
123-
124-
// .as_module(),
125-
// );
126-
127-
rt.set_loader(resolver, loader).await;
128-
129-
let ctx = AsyncContext::full(&rt).await.unwrap();
130-
131-
async_with!(ctx => |ctx| {
132-
133-
if let Err(err) = initalizer.init(&ctx).catch(&ctx){
134-
eprintln!("{:?}", err);
135-
}
136-
137-
let mut opts = EvalOptions::default();
138-
opts.global = false;
139-
140-
if let Err(err) = ctx.eval_with_options::<Value,_>(r#"
141-
142-
import console from "console";
143-
144-
"#, opts).catch(&ctx){
145-
eprintln!("{:?}", err);
146-
}
147-
148-
149-
})
150-
.await;
151-
}
15243
}

src/loader/global.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rquickjs::{Ctx, Result};
22

33
use super::GlobalLoadFn;
44

5+
/// Global initializer that MUST be called before any user code is run.
56
pub struct GlobalInitializer {
67
globals: Vec<GlobalLoadFn>,
78
}

src/loader/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ mod resolver;
1313
type GlobalLoadFn = Box<dyn for<'js> FnOnce(&Ctx<'js>, &Object<'js>) -> Result<()> + Send + Sync>;
1414
type ModuleLoadFn = for<'js> fn(Ctx<'js>, Vec<u8>) -> Result<Module<'js>>;
1515

16-
/// Loader for Rust modules defined using [`crate::ModuleDefExt`].
16+
/// Rquickjs [`Loader`](rquickjs::loader::Loader) for Rust modules
17+
/// defined using [`ModuleDefExt`](crate::ModuleDefExt).
1718
///
1819
/// See [`ModuleLoaderBuilder`] for usage.
1920
pub struct ModuleLoader {

src/loader/resolver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use rquickjs::{
55
Ctx, Result,
66
};
77

8+
/// Rquickjs [`Resolver`](rquickjs::loader::Resolver) for modules
9+
/// defined using [`ModuleDefExt`](crate::ModuleDefExt).
810
pub struct ModuleResolver {
911
inner: BuiltinResolver,
1012
}

0 commit comments

Comments
 (0)