Skip to content

Commit

Permalink
Refactor Skylark Environment-s
Browse files Browse the repository at this point in the history
Make Environment-s freezable: Introduce a class Mutability
as a revokable capability to mutate objects in an Environment.
For now, only Environment-s carry this capability.
Make sure that every Mutability is revoked in the same function that create...

This reinstates a change that previously rolled-back because it broke the
serializability of SkylarkLookupValue. Bad news: serializing it succeeds for the
wrong reason, because a SkylarkEnvironment was stored as a result (now an
Environment.Extension) that was Serializable but inherited its bindings from an Environment (now an Environment.BaseExtension) which wasn't Serializable.
Apparently, Java doesn't try to serialize the bindings then (or at least doesn't
error out when it fails), because these bindings map variable names to pretty
arbitrary objects, and a lot of those we find in practice aren't Serializable.
Thus the current code passes the same tests as the previous code, but obviously
the serialization is just as ineffective as it used to be.

--
MOS_MIGRATED_REVID=102776694
  • Loading branch information
fare authored and damienmg committed Sep 11, 2015
1 parent 5831c69 commit 89312fb
Show file tree
Hide file tree
Showing 53 changed files with 1,774 additions and 1,398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
import com.google.devtools.build.lib.packages.RuleClassProvider;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
import com.google.devtools.build.lib.rules.SkylarkModules;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.Environment.Extension;
import com.google.devtools.build.lib.syntax.Label;
import com.google.devtools.build.lib.syntax.SkylarkEnvironment;
import com.google.devtools.build.lib.syntax.Mutability;
import com.google.devtools.build.lib.syntax.SkylarkType;
import com.google.devtools.build.lib.syntax.ValidationEnvironment;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.OptionsClassProvider;

Expand Down Expand Up @@ -310,9 +311,7 @@ public Label load(String from) {

private final PrerequisiteValidator prerequisiteValidator;

private final ImmutableMap<String, SkylarkType> skylarkAccessibleJavaClasses;

private final ValidationEnvironment skylarkValidationEnvironment;
private final Environment.Frame globals;

public ConfiguredRuleClassProvider(
PathFragment preludePath,
Expand All @@ -338,10 +337,7 @@ public ConfiguredRuleClassProvider(
this.configurationFragments = configurationFragments;
this.configurationCollectionFactory = configurationCollectionFactory;
this.prerequisiteValidator = prerequisiteValidator;
this.skylarkAccessibleJavaClasses = skylarkAccessibleJavaClasses;
this.skylarkValidationEnvironment = new ValidationEnvironment(
// TODO(bazel-team): refactor constructors so we don't have those null-s
createSkylarkRuleClassEnvironment(null, null));
this.globals = createGlobals(skylarkAccessibleJavaClasses);
}

public PrerequisiteValidator getPrerequisiteValidator() {
Expand Down Expand Up @@ -425,22 +421,46 @@ public BuildOptions createBuildOptions(OptionsClassProvider optionsProvider) {
return BuildOptions.of(configurationOptions, optionsProvider);
}

@Override
public SkylarkEnvironment createSkylarkRuleClassEnvironment(
EventHandler eventHandler, String astFileContentHashCode) {
SkylarkEnvironment env = SkylarkModules.getNewEnvironment(eventHandler, astFileContentHashCode);
for (Map.Entry<String, SkylarkType> entry : skylarkAccessibleJavaClasses.entrySet()) {
env.update(entry.getKey(), entry.getValue().getType());
private Environment.Frame createGlobals(
ImmutableMap<String, SkylarkType> skylarkAccessibleJavaClasses) {
try (Mutability mutability = Mutability.create("ConfiguredRuleClassProvider globals")) {
Environment env = createSkylarkRuleClassEnvironment(
mutability, SkylarkModules.GLOBALS, null, null, null);
for (Map.Entry<String, SkylarkType> entry : skylarkAccessibleJavaClasses.entrySet()) {
env.setup(entry.getKey(), entry.getValue().getType());
}
return env.getGlobals();
}
env.setLoadingPhase();
}

private Environment createSkylarkRuleClassEnvironment(
Mutability mutability,
Environment.Frame globals,
EventHandler eventHandler,
String astFileContentHashCode,
Map<PathFragment, Extension> importMap) {
Environment env = Environment.builder(mutability)
.setSkylark()
.setGlobals(globals)
.setEventHandler(eventHandler)
.setFileContentHashCode(astFileContentHashCode)
.setImportedExtensions(importMap)
.setLoadingPhase()
.build();
return env;
}

@Override
public ValidationEnvironment getSkylarkValidationEnvironment() {
return skylarkValidationEnvironment;
public Environment createSkylarkRuleClassEnvironment(
Mutability mutability,
EventHandler eventHandler,
String astFileContentHashCode,
Map<PathFragment, Extension> importMap) {
return createSkylarkRuleClassEnvironment(
mutability, globals, eventHandler, astFileContentHashCode, importMap);
}


@Override
public String getDefaultWorkspaceFile() {
return defaultWorkspaceFile;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/google/devtools/build/lib/events/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public LineAndColumn getStartLineAndColumn() {
return null;
}

/**
* Returns a line corresponding to the position denoted by getStartOffset.
* Returns null if this information is not available.
*/
public Integer getStartLine() {
LineAndColumn lac = getStartLineAndColumn();
if (lac == null) {
return null;
}
return lac.getLine();
}

/**
* Returns a (line, column) pair corresponding to the position denoted by
* getEndOffset. Returns null if this information is not available.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.events.StoredEventHandler;
import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException;
import com.google.devtools.build.lib.syntax.Environment;
import com.google.devtools.build.lib.syntax.FuncallExpression;
import com.google.devtools.build.lib.syntax.Label;
import com.google.devtools.build.lib.syntax.Label.SyntaxException;
Expand Down Expand Up @@ -120,7 +121,8 @@ public void addBindRule(
attributes.put("actual", actual);
}
StoredEventHandler handler = new StoredEventHandler();
Rule rule = RuleFactory.createRule(this, bindRuleClass, attributes, handler, null, location);
Rule rule = RuleFactory.createRule(
this, bindRuleClass, attributes, handler, null, location, null);
overwriteRule(rule);
rule.setVisibility(ConstantRuleVisibility.PUBLIC);
}
Expand All @@ -130,11 +132,11 @@ public void addBindRule(
* WORKSPACE files to overwrite "earlier" ones.
*/
public Builder createAndAddRepositoryRule(RuleClass ruleClass, RuleClass bindRuleClass,
Map<String, Object> kwargs, FuncallExpression ast)
Map<String, Object> kwargs, FuncallExpression ast, Environment env)
throws InvalidRuleException, NameConflictException, SyntaxException {
StoredEventHandler eventHandler = new StoredEventHandler();
Rule tempRule = RuleFactory.createRule(this, ruleClass, kwargs, eventHandler, ast,
ast.getLocation());
Rule tempRule = RuleFactory.createRule(
this, ruleClass, kwargs, eventHandler, ast, ast.getLocation(), env);
addEvents(eventHandler.getEvents());
try {
repositoryMap.put(RepositoryName.create("@" + tempRule.getName()), tempRule);
Expand Down
Loading

0 comments on commit 89312fb

Please sign in to comment.