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+
110pub use self :: definition:: { GlobalsOnly , ModuleDefExt , ModuleImpl } ;
211pub use self :: loader:: { GlobalInitializer , ModuleLoader , ModuleLoaderBuilder , ModuleResolver } ;
312
@@ -8,15 +17,12 @@ mod wrapper;
817
918#[ cfg( test) ]
1019mod 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}
0 commit comments