AstModuleLoader class

Lookup-only module resolver for pre-parsed AST bundles.

AstModuleLoader implements ModuleContext to resolve import directives against a pre-loaded map of modules. It performs no file I/O and no source code parsing — all modules must already exist in the bundle.

Module Resolution Order

When loadModule is called with a URI:

  1. Cache — return if already loaded
  2. dart:* — register the corresponding stdlib and return empty module
  3. Bridged libraries — register native bridges and return empty module
  4. Bundle modules — look up in modules, process AST → environment
  5. Error — throw if URI not found anywhere

Usage

Typically created internally by D4rtRunner.executeBundle — not constructed directly in user code.

final loader = AstModuleLoader(
  modules: bundle.modules,
  globalEnvironment: env,
  runner: runner,
);
Implemented types

Constructors

AstModuleLoader({required Map<String, SCompilationUnit> modules, required Environment globalEnvironment, required D4rtRunner runner, Map<String, String>? sources, Map<String, Environment>? sharedBridgedModuleEnvironments, Environment? sharedModuleEnclosing, void onBridgedModuleEnvBuilt()?})
Creates an AstModuleLoader for resolving imports from a bundle.

Properties

callStack → D4rtCallStack
The interpreted call stack of the run this loader serves, located through the bundle's sources — a bundle built without them yields empty traces.
latefinal
currentLibrary ↔ Uri?
The current library URI being executed.
getter/setter pairoverride
executionContext ↔ D4rtExecutionContext?
The run this loader loads for when the execution has hooks (GitHub #19): its onImport sees every library once, and its own permission set narrows the runner's.
getter/setter pair
globalEnvironment → Environment
The global environment shared across all modules.
final
hashCode → int
The hash code for this object.
no setterinherited
isLoadingModules → bool
Whether a module load is in progress, i.e. names a module declares may not be visible yet to a module of the same import cycle (GitHub #9).
no setteroverride
loadedModuleCount → int
Step #3 (retention) — number of bundle modules currently loaded and cached in _moduleCache (each holds an SCompilationUnit AST). Read-only introspection so D4rtRunner can assert prior runs' ASTs are not accumulated. Bridged-module envs (no user AST) are not counted.
no setter
modules → Map<String, SCompilationUnit>
Pre-parsed modules from the bundle, keyed by URI string.
final
onBridgedModuleEnvBuilt → void Function()?
Invoked once each time a bridged-module env is actually built (cache miss). Lets the runner expose a debug build counter for the reuse unit test.
final
runner → D4rtRunner
The D4rtRunner that owns this loader — used for bridge lookups and permission checks.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
scriptEnvironment → Environment
The scope the entry module's own declarations live in, enclosing globalEnvironment: the entry sees everything registered there (the stdlib, bridges, what its imports bring in), while a loaded module, whose environment encloses globalEnvironment too, does not see the entry (ghissues14: a library that named something it neither declares nor imports found a same-named declaration of the entry).
latefinal
sharedBridgedModuleEnvironments → Map<String, Environment>?
Optional process-global cache of bridged-module environments keyed by module URI, shared across every execution that has the same allowed-set signature (PERF step #2). When supplied, _tryLoadBridgedModule reuses an already-built module env instead of re-registering the ~982-class flutter bridge surface on every run. null for legacy (empty allowed-set) instances, which keep the per-loader _bridgedModuleEnvironments cache.
final
sharedModuleEnclosing → Environment?
Enclosing environment for entries built into sharedBridgedModuleEnvironments. Must be the shared warm parent (not the per-execution global environment), so a cached module env is safe to reuse across executions. null falls back to globalEnvironment.
final
sources → Map<String, String>?
The bundle's source text by module URI, when it was built with it (AstBundle.sources). Read only by diagnostics that quote source (SCE236); execution never needs it.
final

Methods

checkPermission(dynamic operation) → bool
Checks if a permission is granted for the given operation.
override
deferUntilModulesLoaded(void resolve()) → bool
Runs resolve once the outermost loadModule returns, because it needs a name that is not declared yet. Dart has no declaration order across libraries: in an import cycle (intl's intl_helpers.dart and message_lookup_by_library.dart), or through a barrel that has not reached the export declaring it, a class can be visited before its supertype - or a method before its type-parameter bound - exists anywhere. Returns false when no module load is in progress: then the name is genuinely undefined and the caller reports it. resolve reports a name that is still undefined when it runs.
override
loadModule(Uri uri, {Set<String>? showNames, Set<String>? hideNames}) → LoadedModule
Loads (or returns the cached) module for uri.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
releaseLoadedModules() → void
Step #3 (retention) — drops the per-loader parsed-module cache so a finished run's SCompilationUnit graph becomes collectable. Only the per-loader _moduleCache is cleared; the process-global shared bridge caches (sharedBridgedModuleEnvironments et al.) are untouched. A subsequent execute rebuilds the loader and re-populates the cache.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

stdlibModuleNames → Iterable<String>
Known stdlib modules and their registration functions.
no setter

Static Methods

latentStdlibBridgeFor(Type nativeType, {bool structural = false}) → BridgedClass?
The stdlib bridge for nativeType from any stdlib library, imported or not.