A Module represents a Starlark module, a container of global variables populated by executing a Starlark file. Each top-level assignment updates a global variable in the module. Each module references its "predeclared" environment, which is often shared among many modules. These are the
| 50 | * filter what predeclared bindings are available via {@link GuardedValue}. |
| 51 | */ |
| 52 | public final class Module implements Resolver.Module { |
| 53 | |
| 54 | // The module's predeclared environment. Excludes UNIVERSE bindings. Values that are conditionally |
| 55 | // present are stored as GuardedValues regardless of whether they are actually enabled. |
| 56 | private final ImmutableMap<String, Object> predeclared; |
| 57 | |
| 58 | // The module's global variables, in order of creation. |
| 59 | private final LinkedHashMap<String, Integer> globalIndex = new LinkedHashMap<>(); |
| 60 | private Object[] globals = new Object[8]; |
| 61 | |
| 62 | // An optional piece of application-specific metadata associated with the module/file. |
| 63 | // Its toString appears to Starlark in str(function): "<function f from ...>". |
| 64 | @Nullable private final Object clientData; |
| 65 | |
| 66 | private final StarlarkSemantics semantics; |
| 67 | |
| 68 | // An optional doc string for the module. Set after construction when evaluating a .bzl file. |
| 69 | @Nullable private String documentation; |
| 70 | |
| 71 | private Module( |
| 72 | ImmutableMap<String, Object> predeclared, |
| 73 | @Nullable Object clientData, |
| 74 | StarlarkSemantics semantics) { |
| 75 | this.predeclared = predeclared; |
| 76 | this.clientData = clientData; |
| 77 | this.semantics = semantics; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Constructs a Module with the specified predeclared bindings (filtered by the semantics), in * |
| 82 | * addition to the standard environment, {@link Starlark#UNIVERSE}. No client data is set. |
| 83 | */ |
| 84 | public static Module withPredeclared( |
| 85 | StarlarkSemantics semantics, Map<String, Object> predeclared) { |
| 86 | return withPredeclaredAndData(semantics, predeclared, null); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Constructs a Module as above, but with the specified client data -- an arbitrary |
| 91 | * application-specific value to be associated with this Module. Client data may also affect the |
| 92 | * filtering of predeclareds alongside the semantics. |
| 93 | */ |
| 94 | public static Module withPredeclaredAndData( |
| 95 | StarlarkSemantics semantics, Map<String, Object> predeclared, @Nullable Object clientData) { |
| 96 | return new Module(ImmutableMap.copyOf(predeclared), clientData, semantics); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Creates a module with no predeclared bindings other than the standard environment, {@link |
| 101 | * Starlark#UNIVERSE}, and with no client data. |
| 102 | */ |
| 103 | public static Module create() { |
| 104 | return new Module( |
| 105 | /* predeclared= */ ImmutableMap.of(), /* clientData= */ null, StarlarkSemantics.DEFAULT); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the module (file) of the {@code depth}-th innermost enclosing Starlark function on the |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…