|
| 1 | +use rquickjs::{ |
| 2 | + async_with, AsyncContext, AsyncRuntime, CatchResultExt, Function, Module, Object, Result, |
| 3 | +}; |
| 4 | +use rquickjs_module::{ModuleDefExt, ModuleImpl, ModuleLoader}; |
| 5 | + |
| 6 | +use self::common::{Printer, PrinterOptions}; |
| 7 | + |
| 8 | +mod common; |
| 9 | + |
| 10 | +struct PrinterModule { |
| 11 | + options: PrinterOptions, |
| 12 | +} |
| 13 | + |
| 14 | +impl PrinterModule { |
| 15 | + pub fn new<T: Into<String>>(target: T) -> Self { |
| 16 | + Self { |
| 17 | + options: PrinterOptions { |
| 18 | + target: target.into(), |
| 19 | + }, |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl ModuleDefExt<PrinterOptions> for PrinterModule { |
| 25 | + type Implementation = ModuleImpl<PrinterOptions>; |
| 26 | + |
| 27 | + fn implementation() -> &'static Self::Implementation { |
| 28 | + &ModuleImpl { |
| 29 | + declare: |decl| { |
| 30 | + decl.declare("default")?; |
| 31 | + Ok(()) |
| 32 | + }, |
| 33 | + evaluate: |_ctx, exports, options| { |
| 34 | + exports.export("default", Printer::new(options.target.clone()))?; |
| 35 | + Ok(()) |
| 36 | + }, |
| 37 | + name: "printer", |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fn options(self) -> PrinterOptions { |
| 42 | + self.options |
| 43 | + } |
| 44 | + |
| 45 | + fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> { |
| 46 | + globals.set("global_printer", Printer::new(options.target.clone()))?; |
| 47 | + Ok(()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[tokio::test] |
| 52 | +async fn test_module() { |
| 53 | + let rt = AsyncRuntime::new().unwrap(); |
| 54 | + |
| 55 | + let (loader, resolver, initalizer) = ModuleLoader::builder() |
| 56 | + .with_module(PrinterModule::new("john")) |
| 57 | + .build(); |
| 58 | + |
| 59 | + rt.set_loader(resolver, loader).await; |
| 60 | + |
| 61 | + let ctx = AsyncContext::full(&rt).await.unwrap(); |
| 62 | + |
| 63 | + async_with!(ctx => |ctx| { |
| 64 | + initalizer.init(&ctx).unwrap(); |
| 65 | + |
| 66 | + let (module, module_eval) = Module::declare(ctx.clone(), "test", r#" |
| 67 | + import printer from "printer"; |
| 68 | + export function testFunc() { |
| 69 | + return printer.print(); |
| 70 | + } |
| 71 | + "#).unwrap().eval().unwrap(); |
| 72 | + module_eval.into_future::<()>().await.unwrap(); |
| 73 | + let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap(); |
| 74 | + assert_eq!(result, "hello john"); |
| 75 | + }) |
| 76 | + .await; |
| 77 | +} |
| 78 | + |
| 79 | +#[tokio::test] |
| 80 | +async fn test_module_named() { |
| 81 | + let rt = AsyncRuntime::new().unwrap(); |
| 82 | + |
| 83 | + let (loader, resolver, initalizer) = ModuleLoader::builder() |
| 84 | + .with_module_named(PrinterModule::new("arnold"), "custom_printer") |
| 85 | + .build(); |
| 86 | + |
| 87 | + rt.set_loader(resolver, loader).await; |
| 88 | + |
| 89 | + let ctx = AsyncContext::full(&rt).await.unwrap(); |
| 90 | + |
| 91 | + async_with!(ctx => |ctx| { |
| 92 | + initalizer.init(&ctx).unwrap(); |
| 93 | + |
| 94 | + let (module, module_eval) = Module::declare(ctx.clone(), "test", r#" |
| 95 | + import printer from "custom_printer"; |
| 96 | + export function testFunc() { |
| 97 | + return printer.print(); |
| 98 | + } |
| 99 | + "#).unwrap().eval().unwrap(); |
| 100 | + module_eval.into_future::<()>().await.unwrap(); |
| 101 | + let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap(); |
| 102 | + assert_eq!(result, "hello arnold"); |
| 103 | + }) |
| 104 | + .await; |
| 105 | +} |
| 106 | + |
| 107 | +#[tokio::test] |
| 108 | +async fn test_module_global() { |
| 109 | + let rt = AsyncRuntime::new().unwrap(); |
| 110 | + |
| 111 | + let (loader, resolver, initalizer) = ModuleLoader::builder() |
| 112 | + .with_module(PrinterModule::new("david")) |
| 113 | + .build(); |
| 114 | + |
| 115 | + rt.set_loader(resolver, loader).await; |
| 116 | + |
| 117 | + let ctx = AsyncContext::full(&rt).await.unwrap(); |
| 118 | + |
| 119 | + async_with!(ctx => |ctx| { |
| 120 | + initalizer.init(&ctx).unwrap(); |
| 121 | + |
| 122 | + let result = ctx.eval::<String,_>(r#" |
| 123 | + global_printer.print() |
| 124 | + "#).catch(&ctx).unwrap(); |
| 125 | + assert_eq!(result, "hello david"); |
| 126 | + }) |
| 127 | + .await; |
| 128 | +} |
0 commit comments