-
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
8 changed files
with
173 additions
and
172 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
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
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
use std::collections::HashMap; | ||
|
||
use rquickjs::{loader::Loader, Ctx, Error, Module, Result}; | ||
|
||
use super::ModuleLoadFn; | ||
|
||
/// Rquickjs [`Loader`](rquickjs::loader::Loader) for Rust modules | ||
/// defined using [`ModuleDefExt`](crate::ModuleDefExt). | ||
/// | ||
/// See [`ModuleLoaderBuilder`] for usage. | ||
pub struct ModuleLoader { | ||
modules: HashMap<&'static str, ModuleLoadFn>, | ||
} | ||
|
||
impl ModuleLoader { | ||
pub(crate) fn new(modules: HashMap<&'static str, ModuleLoadFn>) -> Self { | ||
Self { modules } | ||
} | ||
} | ||
|
||
impl Loader for ModuleLoader { | ||
fn load<'js>(&mut self, ctx: &Ctx<'js>, path: &str) -> Result<Module<'js>> { | ||
let load = self | ||
.modules | ||
.remove(path) | ||
.ok_or_else(|| Error::new_loading(path))?; | ||
|
||
(load)(ctx.clone(), Vec::from(path)) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,142 @@ | ||
use std::collections::HashMap; | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use rquickjs::{loader::Loader, Ctx, Error, Module, Object, Result}; | ||
use rquickjs::{ | ||
module::{Module, ModuleDef}, | ||
Ctx, JsLifetime, Object, Result, | ||
}; | ||
|
||
pub use self::builder::ModuleLoaderBuilder; | ||
pub use self::global::GlobalInitializer; | ||
pub use self::loader::ModuleLoader; | ||
pub use self::resolver::ModuleResolver; | ||
use crate::wrapper::{IntoModule, ModuleMeta}; | ||
|
||
mod builder; | ||
mod global; | ||
#[allow(clippy::module_inception)] | ||
mod loader; | ||
mod resolver; | ||
|
||
type GlobalLoadFn = Box<dyn for<'js> FnOnce(&Ctx<'js>, &Object<'js>) -> Result<()> + Send + Sync>; | ||
type ModuleLoadFn = for<'js> fn(Ctx<'js>, Vec<u8>) -> Result<Module<'js>>; | ||
|
||
/// Rquickjs [`Loader`](rquickjs::loader::Loader) for Rust modules | ||
/// defined using [`ModuleDefExt`](crate::ModuleDefExt). | ||
fn load_module_func<D: ModuleDef>(ctx: Ctx<'_>, name: Vec<u8>) -> Result<Module<'_>> { | ||
Module::declare_def::<D, _>(ctx, name) | ||
} | ||
|
||
/// Builder to create a [`ModuleLoader`], [`ModuleResolver`] and [`GlobalInitializer`] | ||
/// | ||
/// # Example | ||
/// ```rust | ||
/// use rquickjs_module::{ModuleLoader, ModuleDefExt, ModuleImpl}; | ||
/// | ||
/// struct MyExtension; | ||
/// | ||
/// impl Extension for MyExtension { | ||
/// type Implementation = ModuleImpl<()>; | ||
/// | ||
/// See [`ModuleLoaderBuilder`] for usage. | ||
pub struct ModuleLoader { | ||
/// fn implementation() -> &'static Self::Implementation { | ||
/// &ModuleImpl { | ||
/// declare: |decl| { | ||
/// decl.declare("hello")?; | ||
/// Ok(()) | ||
/// }, | ||
/// evaluate: |ctx, exports, options| { | ||
/// exports.export("hello", "world".to_string())?; | ||
/// Ok(()) | ||
/// }, | ||
/// name: "my-module", | ||
/// } | ||
/// } | ||
/// | ||
/// fn options(self) -> () {} | ||
/// } | ||
/// | ||
/// ``` | ||
#[derive(Default)] | ||
pub struct ExtensionBuilder { | ||
modules: HashMap<&'static str, ModuleLoadFn>, | ||
globals: Vec<GlobalLoadFn>, | ||
names: HashSet<&'static str>, | ||
} | ||
|
||
impl ModuleLoader { | ||
pub(crate) fn new(modules: HashMap<&'static str, ModuleLoadFn>) -> Self { | ||
Self { modules } | ||
impl ExtensionBuilder { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
pub fn builder() -> ModuleLoaderBuilder { | ||
ModuleLoaderBuilder::default() | ||
#[must_use] | ||
pub fn with_extension<O, M, R>(mut self, extension: M) -> Self | ||
where | ||
for<'js> O: JsLifetime<'js> + Send + Sync + 'static, | ||
R: ModuleDef + ModuleMeta, | ||
M: IntoModule<O, R>, | ||
{ | ||
self.process_extension(extension, None); | ||
self | ||
} | ||
|
||
#[must_use] | ||
pub fn with_extension_named<O, M, R>(mut self, extension: M, name: &'static str) -> Self | ||
where | ||
for<'js> O: JsLifetime<'js> + Send + Sync + 'static, | ||
R: ModuleDef + ModuleMeta, | ||
M: IntoModule<O, R>, | ||
{ | ||
self.process_extension(extension, Some(name)); | ||
self | ||
} | ||
} | ||
|
||
impl Loader for ModuleLoader { | ||
fn load<'js>(&mut self, ctx: &Ctx<'js>, path: &str) -> Result<Module<'js>> { | ||
let load = self | ||
.modules | ||
.remove(path) | ||
.ok_or_else(|| Error::new_loading(path))?; | ||
pub fn add_extension<O, M, R>(&mut self, extension: M) -> &mut Self | ||
where | ||
for<'js> O: JsLifetime<'js> + Send + Sync + 'static, | ||
R: ModuleDef + ModuleMeta, | ||
M: IntoModule<O, R>, | ||
{ | ||
self.process_extension(extension, None) | ||
} | ||
|
||
pub fn add_extension_named<O, M, R>(&mut self, extension: M, name: &'static str) -> &mut Self | ||
where | ||
for<'js> O: JsLifetime<'js> + Send + Sync + 'static, | ||
R: ModuleDef + ModuleMeta, | ||
M: IntoModule<O, R>, | ||
{ | ||
self.process_extension(extension, Some(name)) | ||
} | ||
|
||
fn process_extension<O, M, R>(&mut self, extension: M, name: Option<&'static str>) -> &mut Self | ||
where | ||
for<'js> O: JsLifetime<'js> + Send + Sync + 'static, | ||
R: ModuleDef + ModuleMeta, | ||
M: IntoModule<O, R>, | ||
{ | ||
let o = extension.options(); | ||
|
||
// Create a new closure that explicitly captures 'js lifetime | ||
let globals_fn = move |ctx: &Ctx<'_>, globals: &Object<'_>| { | ||
let globals_fn = M::globals; | ||
globals_fn(globals, &o)?; | ||
let _ = ctx.store_userdata(o); | ||
Ok(()) | ||
}; | ||
|
||
// Box the closure with explicit lifetime bounds | ||
let boxed_globals: GlobalLoadFn = Box::new(globals_fn); | ||
|
||
if R::is_module() { | ||
let name = name.unwrap_or(R::name()); | ||
self.names.insert(name); | ||
self.modules.insert(name, load_module_func::<R>); | ||
} | ||
|
||
self.globals.push(boxed_globals); | ||
self | ||
} | ||
|
||
(load)(ctx.clone(), Vec::from(path)) | ||
pub fn build(self) -> (ModuleLoader, ModuleResolver, GlobalInitializer) { | ||
( | ||
ModuleLoader::new(self.modules), | ||
ModuleResolver::new(self.names), | ||
GlobalInitializer::new(self.globals), | ||
) | ||
} | ||
} |
Oops, something went wrong.