class
vm.SourceTextModule
class SourceTextModule
This feature is only available with the --experimental-vm-modules command flag enabled.
The vm.SourceTextModule class provides the Source Text Module Record as defined in the ECMAScript specification.
- error: any
If the
module.statusis'errored', this property contains the exception thrown by the module during evaluation. If the status is anything else, accessing this property will result in a thrown exception.The value
undefinedcannot be used for cases where there is not a thrown exception due to possible ambiguity withthrow undefined;.Corresponds to the
[[EvaluationError]]field of Cyclic Module Record s in the ECMAScript specification. - readonly moduleRequests: readonly ModuleRequest[]
The requested import dependencies of this module. The returned array is frozen to disallow any changes to it.
For example, given a source text:
import foo from 'foo'; import fooAlias from 'foo'; import bar from './bar.js'; import withAttrs from '../with-attrs.ts' with { arbitraryAttr: 'attr-val' }; import source Module from 'wasm-mod.wasm';The value of the
sourceTextModule.moduleRequestswill be:[ { specifier: 'foo', attributes: {}, phase: 'evaluation', }, { specifier: 'foo', attributes: {}, phase: 'evaluation', }, { specifier: './bar.js', attributes: {}, phase: 'evaluation', }, { specifier: '../with-attrs.ts', attributes: { arbitraryAttr: 'attr-val' }, phase: 'evaluation', }, { specifier: 'wasm-mod.wasm', attributes: {}, phase: 'source', }, ]; - namespace: Object
The namespace object of the module. This is only available after linking (
module.link()) has completed.Corresponds to the GetModuleNamespace abstract operation in the ECMAScript specification.
- status: ModuleStatus
The current status of the module. Will be one of:
'unlinked':module.link()has not yet been called.'linking':module.link()has been called, but not all Promises returned by the linker function have been resolved yet.'linked': The module has been linked successfully, and all of its dependencies are linked, butmodule.evaluate()has not yet been called.'evaluating': The module is being evaluated through amodule.evaluate()on itself or a parent module.'evaluated': The module has been successfully evaluated.'errored': The module has been evaluated, but an exception was thrown.
Other than
'errored', this status string corresponds to the specification's Cyclic Module Record's[[Status]]field.'errored'corresponds to'evaluated'in the specification, but with[[EvaluationError]]set to a value that is notundefined. - ): Promise<void>;
Evaluate the module and its depenendencies. Corresponds to the Evaluate() concrete method field of Cyclic Module Records in the ECMAScript specification.
If the module is a
vm.SourceTextModule,evaluate()must be called after the module has been instantiated; otherwiseevaluate()will return a rejected promise.For a
vm.SourceTextModule, the promise returned byevaluate()may be fulfilled either synchronously or asynchronously:- If the
vm.SourceTextModulehas no top-levelawaitin itself or any of its dependencies, the promise will be fulfilled synchronously after the module and all its dependencies have been evaluated.- If the evaluation succeeds, the promise will be synchronously resolved to
undefined. - If the evaluation results in an exception, the promise will be synchronously rejected with the exception that causes the evaluation to fail, which is the same as
module.error.
- If the evaluation succeeds, the promise will be synchronously resolved to
- If the
vm.SourceTextModulehas top-levelawaitin itself or any of its dependencies, the promise will be fulfilled asynchronously after the module and all its dependencies have been evaluated.- If the evaluation succeeds, the promise will be asynchronously resolved to
undefined. - If the evaluation results in an exception, the promise will be asynchronously rejected with the exception that causes the evaluation to fail.
- If the evaluation succeeds, the promise will be asynchronously resolved to
If the module is a
vm.SyntheticModule,evaluate()always returns a promise that fulfills synchronously, see the specification of Evaluate() of a Synthetic Module Record:- If the
evaluateCallbackpassed to its constructor throws an exception synchronously,evaluate()returns a promise that will be synchronously rejected with that exception. - If the
evaluateCallbackdoes not throw an exception,evaluate()returns a promise that will be synchronously resolved toundefined.
The
evaluateCallbackof avm.SyntheticModuleis executed synchronously within theevaluate()call, and its return value is discarded. This means ifevaluateCallbackis an asynchronous function, the promise returned byevaluate()will not reflect its asynchronous behavior, and any rejections from an asynchronousevaluateCallbackwill be lost.evaluate()could also be called again after the module has already been evaluated, in which case:- If the initial evaluation ended in success (
module.statusis'evaluated'), it will do nothing and return a promise that resolves toundefined. - If the initial evaluation resulted in an exception (
module.statusis'errored'), it will re-reject the exception that the initial evaluation resulted in.
This method cannot be called while the module is being evaluated (
module.statusis'evaluating').@returnsFulfills with
undefinedupon success. - If the
Iterates over the dependency graph and returns
trueif any module in its dependencies or this module itself contains top-levelawaitexpressions, otherwise returnsfalse.The search may be slow if the graph is big enough.
This requires the module to be instantiated first. If the module is not instantiated yet, an error will be thrown.
Returns whether the module itself contains any top-level
awaitexpressions.This corresponds to the field
[[HasTLA]]in Cyclic Module Record in the ECMAScript specification.Instantiate the module with the linked requested modules.
This resolves the imported bindings of the module, including re-exported binding names. When there are any bindings that cannot be resolved, an error would be thrown synchronously.
If the requested modules include cyclic dependencies, the
sourceTextModule.linkRequests(modules)method must be called on all modules in the cycle before calling this method.- link(): Promise<void>;
Link module dependencies. This method must be called before evaluation, and can only be called once per module.
Use
sourceTextModule.linkRequests(modules)andsourceTextModule.instantiate()to link modules either synchronously or asynchronously.The function is expected to return a
Moduleobject or aPromisethat eventually resolves to aModuleobject. The returnedModulemust satisfy the following two invariants:- It must belong to the same context as the parent
Module. - Its
statusmust not be'errored'.
If the returned
Module'sstatusis'unlinked', this method will be recursively called on the returnedModulewith the same providedlinkerfunction.link()returns aPromisethat will either get resolved when all linking instances resolve to a validModule, or rejected if the linker function either throws an exception or returns an invalidModule.The linker function roughly corresponds to the implementation-defined HostResolveImportedModule abstract operation in the ECMAScript specification, with a few key differences:
- The linker function is allowed to be asynchronous while HostResolveImportedModule is synchronous.
The actual HostResolveImportedModule implementation used during module linking is one that returns the modules linked during linking. Since at that point all modules would have been fully linked already, the HostResolveImportedModule implementation is fully synchronous per specification.
Corresponds to the Link() concrete method field of Cyclic Module Record s in the ECMAScript specification.
- It must belong to the same context as the parent
- ): void;
Link module dependencies. This method must be called before evaluation, and can only be called once per module.
The order of the module instances in the
modulesarray should correspond to the order ofsourceTextModule.moduleRequestsbeing resolved. If two module requests have the same specifier and import attributes, they must be resolved with the same module instance or anERR_MODULE_LINK_MISMATCHwould be thrown. For example, when linking requests for this module:import foo from 'foo'; import source Foo from 'foo';The
modulesarray must contain two references to the same instance, because the two module requests are identical but in two phases.If the module has no dependencies, the
modulesarray can be empty.Users can use
sourceTextModule.moduleRequeststo implement the host-defined HostLoadImportedModule abstract operation in the ECMAScript specification, and usingsourceTextModule.linkRequests()to invoke specification defined FinishLoadingImportedModule, on the module with all dependencies in a batch.It's up to the creator of the
SourceTextModuleto determine if the resolution of the dependencies is synchronous or asynchronous.After each module in the
modulesarray is linked, callsourceTextModule.instantiate().@param modulesArray of
vm.Moduleobjects that this module depends on. The order of the modules in the array is the order ofsourceTextModule.moduleRequests.