-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use rquickjs::{class::Trace, JsLifetime}; | ||
|
||
#[derive(Clone, Trace, JsLifetime)] | ||
#[rquickjs::class(frozen)] | ||
pub struct Printer { | ||
target: String, | ||
} | ||
|
||
impl Printer { | ||
pub fn new(target: String) -> Self { | ||
Self { target } | ||
} | ||
} | ||
|
||
#[rquickjs::methods] | ||
impl Printer { | ||
fn print(&self) -> String { | ||
format!("hello {}", self.target) | ||
} | ||
} | ||
|
||
#[derive(JsLifetime, Debug)] | ||
pub struct PrinterOptions { | ||
pub target: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use rquickjs::{async_with, AsyncContext, AsyncRuntime, CatchResultExt, Object, Result}; | ||
use rquickjs_module::{globals_only_module, GlobalsOnly, ModuleDefExt, ModuleLoader}; | ||
|
||
use self::common::{Printer, PrinterOptions}; | ||
|
||
mod common; | ||
|
||
struct PrinterModule { | ||
options: PrinterOptions, | ||
} | ||
|
||
impl PrinterModule { | ||
pub fn new<T: Into<String>>(target: T) -> Self { | ||
Self { | ||
options: PrinterOptions { | ||
target: target.into(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl ModuleDefExt<PrinterOptions> for PrinterModule { | ||
type Implementation = GlobalsOnly; | ||
|
||
fn implementation() -> &'static Self::Implementation { | ||
&GlobalsOnly | ||
} | ||
|
||
fn options(self) -> PrinterOptions { | ||
self.options | ||
} | ||
|
||
fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> { | ||
globals.set("global_printer", Printer::new(options.target.clone()))?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
struct PrinterModule2; | ||
globals_only_module!(PrinterModule2, |globals| { | ||
globals.set("global_printer", Printer::new("emile".to_string()))?; | ||
Ok(()) | ||
}); | ||
|
||
#[tokio::test] | ||
async fn test_global() { | ||
let rt = AsyncRuntime::new().unwrap(); | ||
|
||
let (loader, resolver, initalizer) = ModuleLoader::builder() | ||
.with_module(PrinterModule::new("world")) | ||
.build(); | ||
|
||
rt.set_loader(resolver, loader).await; | ||
|
||
let ctx = AsyncContext::full(&rt).await.unwrap(); | ||
|
||
async_with!(ctx => |ctx| { | ||
initalizer.init(&ctx).unwrap(); | ||
|
||
let result = ctx.eval::<String,_>(r#" | ||
global_printer.print() | ||
"#).catch(&ctx).unwrap(); | ||
assert_eq!(result, "hello world"); | ||
}) | ||
.await; | ||
} | ||
|
||
// Enable once https://github.com/DelSkayn/rquickjs/pull/395 is merged | ||
// #[tokio::test] | ||
// async fn test_global_macro() { | ||
// let rt = AsyncRuntime::new().unwrap(); | ||
|
||
// let (loader, resolver, initalizer) = | ||
// ModuleLoader::builder().with_module(PrinterModule2).build(); | ||
|
||
// rt.set_loader(resolver, loader).await; | ||
|
||
// let ctx = AsyncContext::full(&rt).await.unwrap(); | ||
|
||
// async_with!(ctx => |ctx| { | ||
// initalizer.init(&ctx).unwrap(); | ||
|
||
// let result = ctx.eval::<String,_>(r#" | ||
// global_printer.print() | ||
// "#).catch(&ctx).unwrap(); | ||
// assert_eq!(result, "hello emile"); | ||
// }) | ||
// .await; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
use rquickjs::{ | ||
async_with, AsyncContext, AsyncRuntime, CatchResultExt, Function, Module, Object, Result, | ||
}; | ||
use rquickjs_module::{ModuleDefExt, ModuleImpl, ModuleLoader}; | ||
|
||
use self::common::{Printer, PrinterOptions}; | ||
|
||
mod common; | ||
|
||
struct PrinterModule { | ||
options: PrinterOptions, | ||
} | ||
|
||
impl PrinterModule { | ||
pub fn new<T: Into<String>>(target: T) -> Self { | ||
Self { | ||
options: PrinterOptions { | ||
target: target.into(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl ModuleDefExt<PrinterOptions> for PrinterModule { | ||
type Implementation = ModuleImpl<PrinterOptions>; | ||
|
||
fn implementation() -> &'static Self::Implementation { | ||
&ModuleImpl { | ||
declare: |decl| { | ||
decl.declare("default")?; | ||
Ok(()) | ||
}, | ||
evaluate: |_ctx, exports, options| { | ||
exports.export("default", Printer::new(options.target.clone()))?; | ||
Ok(()) | ||
}, | ||
name: "printer", | ||
} | ||
} | ||
|
||
fn options(self) -> PrinterOptions { | ||
self.options | ||
} | ||
|
||
fn globals(globals: &Object<'_>, options: &PrinterOptions) -> Result<()> { | ||
globals.set("global_printer", Printer::new(options.target.clone()))?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_module() { | ||
let rt = AsyncRuntime::new().unwrap(); | ||
|
||
let (loader, resolver, initalizer) = ModuleLoader::builder() | ||
.with_module(PrinterModule::new("john")) | ||
.build(); | ||
|
||
rt.set_loader(resolver, loader).await; | ||
|
||
let ctx = AsyncContext::full(&rt).await.unwrap(); | ||
|
||
async_with!(ctx => |ctx| { | ||
initalizer.init(&ctx).unwrap(); | ||
|
||
let (module, module_eval) = Module::declare(ctx.clone(), "test", r#" | ||
import printer from "printer"; | ||
export function testFunc() { | ||
return printer.print(); | ||
} | ||
"#).unwrap().eval().unwrap(); | ||
module_eval.into_future::<()>().await.unwrap(); | ||
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap(); | ||
assert_eq!(result, "hello john"); | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_module_named() { | ||
let rt = AsyncRuntime::new().unwrap(); | ||
|
||
let (loader, resolver, initalizer) = ModuleLoader::builder() | ||
.with_module_named(PrinterModule::new("arnold"), "custom_printer") | ||
.build(); | ||
|
||
rt.set_loader(resolver, loader).await; | ||
|
||
let ctx = AsyncContext::full(&rt).await.unwrap(); | ||
|
||
async_with!(ctx => |ctx| { | ||
initalizer.init(&ctx).unwrap(); | ||
|
||
let (module, module_eval) = Module::declare(ctx.clone(), "test", r#" | ||
import printer from "custom_printer"; | ||
export function testFunc() { | ||
return printer.print(); | ||
} | ||
"#).unwrap().eval().unwrap(); | ||
module_eval.into_future::<()>().await.unwrap(); | ||
let result = module.get::<_, Function>("testFunc").unwrap().call::<_, String>(()).unwrap(); | ||
assert_eq!(result, "hello arnold"); | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_module_global() { | ||
let rt = AsyncRuntime::new().unwrap(); | ||
|
||
let (loader, resolver, initalizer) = ModuleLoader::builder() | ||
.with_module(PrinterModule::new("david")) | ||
.build(); | ||
|
||
rt.set_loader(resolver, loader).await; | ||
|
||
let ctx = AsyncContext::full(&rt).await.unwrap(); | ||
|
||
async_with!(ctx => |ctx| { | ||
initalizer.init(&ctx).unwrap(); | ||
|
||
let result = ctx.eval::<String,_>(r#" | ||
global_printer.print() | ||
"#).catch(&ctx).unwrap(); | ||
assert_eq!(result, "hello david"); | ||
}) | ||
.await; | ||
} |