pub struct ServiceModuleResolver { /* private fields */ }Expand description
Host-owned resolver for script-visible require.
Resolution is performed on demand with the requested path and the current chunk’s source name. The host must return a stable canonical identity when it labels the resolver deterministic. Because the canonical identity is a resolver result, the resolver is called before every cache lookup; a host with expensive I/O or compilation should keep its own source cache. A cache hit skips module execution and discards the newly resolved program.
A module may return zero or one value. Successful results are cached within the calling function’s module cohort; failures can be retried. Module code running in a guest coroutine may yield and resume before its result is cached. Recursive loading is an error, as is requesting an identity that a different coroutine is still loading.
Ordinary require keeps completed writes when a module fails. An enclosing
Chunk::install still rolls back guest state when
the installation fails; external Rust effects remain outside that rollback.
use rustia::{ModuleSource, RustiaBuilder, ServiceModuleResolver};
let resolver = ServiceModuleResolver::from_fn(|name, _referrer| {
match name {
"answer" => Ok(ModuleSource::new("answer", "return 42")),
_ => Err(rustia::Error::embed(format!("unknown module {name}"))),
}
});
let mut lua = RustiaBuilder::deterministic().module_resolver(resolver).build()?;
assert_eq!(lua.load("return require('answer')").eval::<i64>()?, 42);Implementations§
Source§impl ServiceModuleResolver
impl ServiceModuleResolver
Sourcepub fn from_fn(
resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static,
) -> Self
pub fn from_fn( resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static, ) -> Self
Host-supplied replay-stable resolver. The determinism assertion is trusted, not verified.
Sourcepub fn from_nondeterministic_fn(
resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static,
) -> Self
pub fn from_nondeterministic_fn( resolver: impl FnMut(&str, Option<&str>) -> Result<ModuleSource> + 'static, ) -> Self
Host-supplied live resolver explicitly labeled nondeterministic.
Trait Implementations§
Source§impl Clone for ServiceModuleResolver
impl Clone for ServiceModuleResolver
Source§fn clone(&self) -> ServiceModuleResolver
fn clone(&self) -> ServiceModuleResolver
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more