From b289f1160b0f262da92c94ceffe5a3660fedc382 Mon Sep 17 00:00:00 2001 From: dslomov Date: Thu, 28 Sep 2017 11:19:04 -0400 Subject: [PATCH] Use Mutability for Args class. Make sure we do not keep reference to SkylarkRuleContext in Args object. RELNOTES: None. PiperOrigin-RevId: 170346094 --- .../skylark/SkylarkActionFactory.java | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkActionFactory.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkActionFactory.java index 5befedce89eefc..110e323a5b4591 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkActionFactory.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkActionFactory.java @@ -50,12 +50,15 @@ import com.google.devtools.build.lib.skylarkinterface.SkylarkValue; import com.google.devtools.build.lib.syntax.BaseFunction; import com.google.devtools.build.lib.syntax.BuiltinFunction; +import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; +import com.google.devtools.build.lib.syntax.Mutability; import com.google.devtools.build.lib.syntax.Runtime; import com.google.devtools.build.lib.syntax.Runtime.NoneType; import com.google.devtools.build.lib.syntax.SkylarkDict; import com.google.devtools.build.lib.syntax.SkylarkList; +import com.google.devtools.build.lib.syntax.SkylarkMutable; import com.google.devtools.build.lib.syntax.SkylarkNestedSet; import com.google.devtools.build.lib.syntax.SkylarkSemanticsOptions; import com.google.devtools.build.lib.syntax.SkylarkSignatureProcessor; @@ -65,6 +68,7 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import javax.annotation.Nullable; /** * Provides a Skylark interface for all action creation needs. @@ -869,9 +873,8 @@ private static String convertLatin1ToUtf8(String latin1) { + "# ]" + "" ) - static class Args implements SkylarkValue { - - private final SkylarkRuleContext context; + static class Args extends SkylarkMutable { + private final Mutability mutability; private final SkylarkCustomCommandLine.Builder commandLine; private ParameterFileType parameterFileType = ParameterFileType.SHELL_QUOTED; private String flagFormatString; @@ -1101,11 +1104,11 @@ public void setParamFileFormat(String format) throws EvalException { this.parameterFileType = parameterFileType; } - public Args( - SkylarkRuleContext context, + private Args( + @Nullable Mutability mutability, SkylarkSemanticsOptions skylarkSemantics, EventHandler eventHandler) { - this.context = context; + this.mutability = mutability != null ? mutability : Mutability.IMMUTABLE; this.commandLine = new SkylarkCustomCommandLine.Builder(skylarkSemantics, eventHandler); } @@ -1114,8 +1117,8 @@ public SkylarkCustomCommandLine build() { } @Override - public boolean isImmutable() { - return context.isImmutable(); + public Mutability mutability() { + return mutability; } @Override @@ -1128,14 +1131,28 @@ public void repr(SkylarkPrinter printer) { } } - @SkylarkCallable( + @SkylarkSignature( name = "args", - doc = "returns an Args object that can be used to build memory-efficient command lines." + doc = "returns an Args object that can be used to build memory-efficient command lines.", + objectType = SkylarkActionFactory.class, + returnType = Args.class, + parameters = { + @Param( + name = "self", + type = SkylarkActionFactory.class, + doc = "This 'actions' object." + ) + }, + useEnvironment = true ) - public Args args() { - return new Args( - context, skylarkSemanticsOptions, ruleContext.getAnalysisEnvironment().getEventHandler()); - } + public static final BuiltinFunction args = + new BuiltinFunction("args") { + public Args invoke(SkylarkActionFactory self, Environment env) { + return new Args(env.mutability(), + env.getSemantics(), + self.ruleContext.getAnalysisEnvironment().getEventHandler()); + } + }; @Override public boolean isImmutable() { @@ -1151,4 +1168,8 @@ public void repr(SkylarkPrinter printer) { void nullify() { ruleContext = null; } + + static { + SkylarkSignatureProcessor.configureSkylarkFunctions(SkylarkActionFactory.class); + } }