pub struct Module {/* private fields */ }Expand description
ECMAScript’sAbstract module record.
Implementations§
Source§implModule
implModule
Sourcepub fnparse<R:ReadChar>( src:Source<'_, R>, realm:Option<Realm>, context: &mutContext,) ->JsResult<Self>
pub fnparse<R:ReadChar>( src:Source<'_, R>, realm:Option<Realm>, context: &mutContext,) ->JsResult<Self>
Abstract operationParseModule ( sourceText, realm, hostDefined ).
Parses the providedsrc as an ECMAScript module, returning an error if parsing fails.
Sourcepub fnsynthetic( export_names: &[JsString], evaluation_steps:SyntheticModuleInitializer, path:Option<PathBuf>, realm:Option<Realm>, context: &mutContext,) -> Self
pub fnsynthetic( export_names: &[JsString], evaluation_steps:SyntheticModuleInitializer, path:Option<PathBuf>, realm:Option<Realm>, context: &mutContext,) -> Self
Abstract operationCreateSyntheticModule ( exportNames, evaluationSteps, realm ).
Creates a new Synthetic Module from its list of exported names, its evaluation steps andoptionally a root realm.
Sourcepub fnfrom_value_as_default(value:JsValue, context: &mutContext) -> Self
pub fnfrom_value_as_default(value:JsValue, context: &mutContext) -> Self
Create aModule from aJsValue, exporting that value as the default export.This will clone the module everytime it is initialized.
Sourcepub fnparse_json(json:JsString, context: &mutContext) ->JsResult<Self>
pub fnparse_json(json:JsString, context: &mutContext) ->JsResult<Self>
Create a module that exports a single JSON value as the default export, from itsJSON string.
§Specification
This is a custom extension to the ECMAScript specification. The current proposalfor JSON modules is being considered inhttps://github.com/tc39/proposal-json-modulesand might differ from this implementation.
This method is provided as a convenience for hosts to create JSON modules.
§Errors
This will return an error if the JSON string is invalid or cannot be converted.
Sourcepub fnhost_defined(&self) -> &HostDefined
pub fnhost_defined(&self) -> &HostDefined
Returns theECMAScript specification defined\[\[HostDefined\]\] field of theModule.
Sourcepub fnload(&self, context: &mutContext) ->JsPromise
pub fnload(&self, context: &mutContext) ->JsPromise
Abstract methodLoadRequestedModules ( [ hostDefined ] ).
Prepares the module for linking by loading all its module dependencies. Returns aJsPromisethat will resolve when the loading process either completes or fails.
Sourcepub fnlink(&self, context: &mutContext) ->JsResult<()>
pub fnlink(&self, context: &mutContext) ->JsResult<()>
Abstract methodLink().
Prepares this module for evaluation by resolving all its module dependencies and initializingits environment.
§Note
This must only be called if theJsPromise returned byModule::load has fulfilled.
Sourcepub fnevaluate(&self, context: &mutContext) ->JsPromise
pub fnevaluate(&self, context: &mutContext) ->JsPromise
Abstract methodEvaluate().
Evaluates this module, returning a promise for the result of the evaluation of this moduleand its dependencies.If the promise is rejected, hosts are expected to handle the promise rejection and rethrowthe evaluation error.
§Note
This must only be called if theModule::link method finished successfully.
Sourcepub fnload_link_evaluate(&self, context: &mutContext) ->JsPromise
pub fnload_link_evaluate(&self, context: &mutContext) ->JsPromise
Loads, links and evaluates this module, returning a promise that will resolve after the modulefinishes its lifecycle.
§Examples
letloader = Rc::new(SimpleModuleLoader::new(Path::new(".")).unwrap());letmutcontext =&mutContext::builder() .module_loader(loader.clone()) .build() .unwrap();letsource = Source::from_bytes("1 + 3");letmodule = Module::parse(source,None, context).unwrap();loader.insert(Path::new("main.mjs").to_path_buf(), module.clone());letpromise = module.load_link_evaluate(context);context.run_jobs().unwrap();assert_eq!( promise.state(), PromiseState::Fulfilled(JsValue::undefined()));Sourcepub fnnamespace(&self, context: &mutContext) ->JsObject
pub fnnamespace(&self, context: &mutContext) ->JsObject
Abstract operationGetModuleNamespace ( module ).
Gets theModule Namespace Object that represents this module’s exports.
Sourcepub fnget_value<K>(&self, name: K, context: &mutContext) ->JsResult<JsValue>where K:Into<PropertyKey>,
pub fnget_value<K>(&self, name: K, context: &mutContext) ->JsResult<JsValue>where K:Into<PropertyKey>,
Get an exported value from the module.
Sourcepub fnget_typed_fn<A, R>( &self, name:JsString, context: &mutContext,) ->JsResult<TypedJsFunction<A, R>>where A:TryIntoJsArguments, R:TryFromJs,
pub fnget_typed_fn<A, R>( &self, name:JsString, context: &mutContext,) ->JsResult<TypedJsFunction<A, R>>where A:TryIntoJsArguments, R:TryFromJs,
Get an exported function, typed, from the module.
Trait Implementations§
Source§implTrace forModule
implTrace forModule
Source§unsafe fntrace_non_roots(&self)
unsafe fntrace_non_roots(&self)
Source§fnrun_finalizer(&self)
fnrun_finalizer(&self)
Finalize::finalize on this object and allcontained subobjects.implEq forModule
Auto Trait Implementations§
implFreeze forModule
impl !RefUnwindSafe forModule
impl !Send forModule
impl !Sync forModule
implUnpin forModule
impl !UnwindSafe forModule
Blanket Implementations§
Source§impl<T>BorrowMut<T> for Twhere T: ?Sized,
impl<T>BorrowMut<T> for Twhere T: ?Sized,
Source§fnborrow_mut(&mut self) ->&mut T
fnborrow_mut(&mut self) ->&mut T
Source§impl<T>CloneToUninit for Twhere T:Clone,
impl<T>CloneToUninit for Twhere T:Clone,
Source§impl<Q, K>Equivalent<K> for Q
impl<Q, K>Equivalent<K> for Q
Source§fnequivalent(&self, key:&K) ->bool
fnequivalent(&self, key:&K) ->bool
key and returntrue if they are equal.Source§impl<Q, K>Equivalent<K> for Q
impl<Q, K>Equivalent<K> for Q
Source§impl<T>IntoEither for T
impl<T>IntoEither for T
Source§fninto_either(self, into_left:bool) ->Either<Self, Self>
fninto_either(self, into_left:bool) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left istrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left(&self) returnstrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§impl<T>Pipe for Twhere T: ?Sized,
impl<T>Pipe for Twhere T: ?Sized,
Source§fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
Source§fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
Source§fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
Source§fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
self, then passesself.as_ref() into the pipe function.Source§fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
self, then passesself.as_mut() into the pipefunction.Source§fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
self, then passesself.deref() into the pipe function.Source§impl<T>Tap for T
impl<T>Tap for T
Source§fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
Borrow<B> of a value.Read moreSource§fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
BorrowMut<B> of a value.Read moreSource§fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
AsRef<R> view of a value.Read moreSource§fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
AsMut<R> view of a value.Read moreSource§fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
Deref::Target of a value.Read moreSource§fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
Deref::Target of a value.Read moreSource§fntap_dbg(self, func: implFnOnce(&Self)) -> Self
fntap_dbg(self, func: implFnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in releasebuilds.