-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin to factor BazelStarlarkContext into subclasses
BazelStarlarkContext is a hodgepodge of Bazel-specific state needed for or manipulated by various Starlark evaluations. A minor refactoring goal is to break this class up into subclasses, each one representing a different kind of Starlark evaluation that needs different kinds of data. This avoids the proliferation of new @nullable fields that may or may not be set and used for a given evaluation. This CL begins the process by breaking out the case of initializing a .bzl module (i.e. running its top-level code). The other cases should follow a similar pathway, but I don't intend to pursue that in the immediate future. My motivation for tackling .bzl initialization now is that I want to add a field needed for the .bzl visibility without feeling guilty. I also took this opportunity to clarify that BazelStarlarkContext ought not to be shared or reused between multiple StarlarkThreads, and fixed a location where this was violated by StarlarkCallbackHelper. Work towards #11261. PiperOrigin-RevId: 457732549 Change-Id: I3ee869865c47457275f1b84819f0f154290aa22a
- Loading branch information
1 parent
ed886ee
commit de34469
Showing
6 changed files
with
163 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/com/google/devtools/build/lib/packages/BzlInitThreadContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package com.google.devtools.build.lib.packages; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.devtools.build.lib.cmdline.Label; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import javax.annotation.Nullable; | ||
import net.starlark.java.eval.EvalException; | ||
import net.starlark.java.eval.Starlark; | ||
import net.starlark.java.eval.StarlarkThread; | ||
|
||
/** | ||
* Bazel application data for the Starlark thread that evaluates the top-level code in a .bzl module | ||
* (i.e. when evaluating that module's global symbols). | ||
*/ | ||
public final class BzlInitThreadContext extends BazelStarlarkContext { | ||
|
||
// TODO(b/236456122): Are all these arguments needed for .bzl initialization? | ||
public BzlInitThreadContext( | ||
@Nullable RepositoryName toolsRepository, | ||
@Nullable ImmutableMap<String, Class<?>> fragmentNameToClass, | ||
SymbolGenerator<?> symbolGenerator, | ||
@Nullable Label networkAllowlistForTests) { | ||
super( | ||
BazelStarlarkContext.Phase.LOADING, | ||
toolsRepository, | ||
fragmentNameToClass, | ||
symbolGenerator, | ||
/*analysisRuleLabel=*/ null, | ||
networkAllowlistForTests); | ||
} | ||
|
||
/** | ||
* Retrieves this context from a Starlark thread, or throws {@link IllegalStateException} if | ||
* unavailable. | ||
*/ | ||
public static BzlInitThreadContext from(StarlarkThread thread) { | ||
BazelStarlarkContext ctx = thread.getThreadLocal(BazelStarlarkContext.class); | ||
Preconditions.checkState( | ||
ctx instanceof BzlInitThreadContext, | ||
"Expected to be in a .bzl initialization (top-level evaluation) Starlark thread"); | ||
return (BzlInitThreadContext) ctx; | ||
} | ||
|
||
/** | ||
* Retrieves this context from a Starlark thread. If not present, throws {@code EvalException} | ||
* with an error message indicating the failure was in a function named {@code function}. | ||
*/ | ||
public static BzlInitThreadContext fromOrFailFunction(StarlarkThread thread, String function) | ||
throws EvalException { | ||
@Nullable BazelStarlarkContext ctx = thread.getThreadLocal(BazelStarlarkContext.class); | ||
if (!(ctx instanceof BzlInitThreadContext)) { | ||
throw Starlark.errorf( | ||
"'%s' can only be called during .bzl initialization (top-level evaluation)", function); | ||
} | ||
return (BzlInitThreadContext) ctx; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters