Skip to content

Commit

Permalink
Use Mutability for Args class.
Browse files Browse the repository at this point in the history
Make sure we do not keep reference to SkylarkRuleContext in Args object.

RELNOTES: None.
PiperOrigin-RevId: 170346094
  • Loading branch information
dslomov authored and katre committed Sep 29, 2017
1 parent 0adb784 commit b289f11
Showing 1 changed file with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -869,9 +873,8 @@ private static String convertLatin1ToUtf8(String latin1) {
+ "# ]"
+ "</pre>"
)
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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -1114,8 +1117,8 @@ public SkylarkCustomCommandLine build() {
}

@Override
public boolean isImmutable() {
return context.isImmutable();
public Mutability mutability() {
return mutability;
}

@Override
Expand All @@ -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() {
Expand All @@ -1151,4 +1168,8 @@ public void repr(SkylarkPrinter printer) {
void nullify() {
ruleContext = null;
}

static {
SkylarkSignatureProcessor.configureSkylarkFunctions(SkylarkActionFactory.class);
}
}

0 comments on commit b289f11

Please sign in to comment.