diff options
author | 2018-05-23 10:01:20 -0700 | |
---|---|---|
committer | 2018-05-23 10:02:24 -0700 | |
commit | cf3e59d30e56d79163a5006e079e04eadb026541 (patch) | |
tree | 2595dd7555337fd6b0a97a9b507d27f15f83ab95 /src/main/java/com/google/devtools/build/lib/syntax | |
parent | a1a990e42bab1bed01a7dff02a8b214c23f3dae4 (diff) |
Create GlobalBootstrap and the Bootstrap interface to add portions of the build API to a global environment.
RELNOTES: None.
PiperOrigin-RevId: 197742427
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/Runtime.java | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Runtime.java b/src/main/java/com/google/devtools/build/lib/syntax/Runtime.java index 0ada911516..9774ec5bb9 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/Runtime.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/Runtime.java @@ -327,7 +327,24 @@ public final class Runtime { } /** - * Registers global (top-level) symbols provided by the given class object. + * Convenience overload of {@link #setupModuleGlobals(ImmutableMap.Builder, Class)} to add + * bindings directly to an {@link Environment}. + * + * @param env the Environment into which to register fields. + * @param moduleClass the Class object containing globals. + */ + public static void setupModuleGlobals(Environment env, Class<?> moduleClass) { + ImmutableMap.Builder<String, Object> envBuilder = ImmutableMap.builder(); + + setupModuleGlobals(envBuilder, moduleClass); + for (Map.Entry<String, Object> envEntry : envBuilder.build().entrySet()) { + env.setup(envEntry.getKey(), envEntry.getValue()); + } + } + + /** + * Adds global (top-level) symbols, provided by the given class object, to the given bindings + * builder. * * <p>Global symbols may be provided by the given class in the following ways: * <ul> @@ -345,14 +362,16 @@ public final class Runtime { * multiple global libraries have functions of the same name, two modules of the same name * are given, or if two subclasses of the same module are given. * - * @param env the Environment into which to register fields. - * @param moduleClass the Class object containing globals. + * @param builder the builder for the "bindings" map, which maps from symbol names to objects, + * and which will be built into a global frame + * @param moduleClass the Class object containing globals */ - public static void setupModuleGlobals(Environment env, Class<?> moduleClass) { + public static void setupModuleGlobals(ImmutableMap.Builder<String, Object> builder, + Class<?> moduleClass) { try { SkylarkModule skylarkModule = SkylarkInterfaceUtils.getSkylarkModule(moduleClass); if (skylarkModule != null) { - env.setup( + builder.put( skylarkModule.name(), moduleClass.getConstructor().newInstance()); } @@ -367,14 +386,14 @@ public final class Runtime { if (!(value instanceof BuiltinFunction.Factory || (value instanceof BaseFunction && !annotation.objectType().equals(Object.class)))) { - env.setup(annotation.name(), value); + builder.put(annotation.name(), value); } } } if (SkylarkInterfaceUtils.hasSkylarkGlobalLibrary(moduleClass)) { Object moduleInstance = moduleClass.getConstructor().newInstance(); for (String methodName : FuncallExpression.getMethodNames(moduleClass)) { - env.setup(methodName, FuncallExpression.getBuiltinCallable(moduleInstance, methodName)); + builder.put(methodName, FuncallExpression.getBuiltinCallable(moduleInstance, methodName)); } } } catch (ReflectiveOperationException e) { |