Move BazelLibrary from syntax/ to packages/
This helps the Skylark interpreter to not depend on Bazel concepts, though it adds a temporary dependency of Skylint on packages/. The fix for that will be to create a Build API interface for BazelLibrary (e.g., "BazelLibraryAPI").
Refactored some GlobalFrame construction logic to be more uniform. Instead of constructing a whole Environment just to get a frame, we build the frame directly, using ImmutableMap.Builder to accumulate bindings. This convention may further change once we convert MethodLibrary and the like to @SkylarkGlobalLibrary, but for now it's more readable.
RELNOTES: None
PiperOrigin-RevId: 194960824
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
index 46253fa..c536a68 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Environment.java
@@ -281,19 +281,36 @@
this.bindings = new LinkedHashMap<>();
}
- public GlobalFrame(Mutability mutability, @Nullable GlobalFrame parent, @Nullable Label label) {
+ public GlobalFrame(
+ Mutability mutability,
+ @Nullable GlobalFrame parent,
+ @Nullable Label label,
+ @Nullable Map<String, Object> bindings) {
this.mutability = Preconditions.checkNotNull(mutability);
this.parent = parent;
this.label = label;
this.bindings = new LinkedHashMap<>();
+ if (bindings != null) {
+ this.bindings.putAll(bindings);
+ }
}
public GlobalFrame(Mutability mutability) {
- this(mutability, null, null);
+ this(mutability, null, null, null);
}
- public GlobalFrame(Mutability mutability, GlobalFrame parent) {
- this(mutability, parent, null);
+ public GlobalFrame(Mutability mutability, @Nullable GlobalFrame parent) {
+ this(mutability, parent, null, null);
+ }
+
+ public GlobalFrame(Mutability mutability, @Nullable GlobalFrame parent, @Nullable Label label) {
+ this(mutability, parent, label, null);
+ }
+
+ /** Constructs a global frame for the given builtin bindings. */
+ public static GlobalFrame createForBuiltins(Map<String, Object> bindings) {
+ Mutability mutability = Mutability.create("<builtins>").freeze();
+ return new GlobalFrame(mutability, null, null, bindings);
}
private void checkInitialized() {
@@ -1217,34 +1234,29 @@
return transitiveHashCode;
}
- /** A read-only {@link Environment.GlobalFrame} with global constants in it only */
+ /** A read-only {@link Environment.GlobalFrame} with False/True/None constants only. */
static final GlobalFrame CONSTANTS_ONLY = createConstantsGlobals();
- /** A read-only {@link Environment.GlobalFrame} with initial globals */
+ /**
+ * A read-only {@link Environment.GlobalFrame} with initial globals as defined in
+ * MethodLibrary.
+ */
public static final GlobalFrame DEFAULT_GLOBALS = createDefaultGlobals();
/** To be removed when all call-sites are updated. */
public static final GlobalFrame SKYLARK = DEFAULT_GLOBALS;
private static Environment.GlobalFrame createConstantsGlobals() {
- try (Mutability mutability = Mutability.create("CONSTANTS")) {
- Environment env = Environment.builder(mutability)
- .useDefaultSemantics()
- .build();
- Runtime.setupConstants(env);
- return env.getGlobals();
- }
+ ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
+ Runtime.addConstantsToBuilder(builder);
+ return GlobalFrame.createForBuiltins(builder.build());
}
private static Environment.GlobalFrame createDefaultGlobals() {
- try (Mutability mutability = Mutability.create("BUILD")) {
- Environment env = Environment.builder(mutability)
- .useDefaultSemantics()
- .build();
- Runtime.setupConstants(env);
- Runtime.setupMethodEnvironment(env, MethodLibrary.defaultGlobalFunctions);
- return env.getGlobals();
- }
+ ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
+ Runtime.addConstantsToBuilder(builder);
+ MethodLibrary.addBindingsToBuilder(builder);
+ return GlobalFrame.createForBuiltins(builder.build());
}
/** An exception thrown by {@link #FAIL_FAST_HANDLER}. */