Refactor Skylark Environment-s
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
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 50ef2546..9230d6b 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
@@ -33,11 +33,11 @@
@SkylarkSignature(name = "True", returnType = Boolean.class,
doc = "Literal for the boolean true.")
- static final Boolean TRUE = true;
+ private static final Boolean TRUE = true;
@SkylarkSignature(name = "False", returnType = Boolean.class,
doc = "Literal for the boolean false.")
- static final Boolean FALSE = false;
+ private static final Boolean FALSE = false;
/**
* There should be only one instance of this type to allow "== None" tests.
@@ -70,11 +70,11 @@
/**
* Set up a given environment for supported class methods.
*/
- static Environment setupConstants(Environment env) throws EvalException {
+ static Environment setupConstants(Environment env) {
// In Python 2.x, True and False are global values and can be redefined by the user.
// In Python 3.x, they are keywords. We implement them as values, for the sake of
// simplicity. We define them as Boolean objects.
- return env.update("False", FALSE).update("True", TRUE).update("None", NONE);
+ return env.setup("False", FALSE).setup("True", TRUE).setup("None", NONE);
}
@@ -128,7 +128,7 @@
public static void registerModuleGlobals(Environment env, Class<?> moduleClass) {
try {
if (moduleClass.isAnnotationPresent(SkylarkModule.class)) {
- env.update(
+ env.setup(
moduleClass.getAnnotation(SkylarkModule.class).name(), moduleClass.newInstance());
}
for (Field field : moduleClass.getDeclaredFields()) {
@@ -142,7 +142,7 @@
if (!(value instanceof BuiltinFunction.Factory
|| (value instanceof BaseFunction
&& !annotation.objectType().equals(Object.class)))) {
- env.update(annotation.name(), value);
+ env.setup(annotation.name(), value);
}
}
}
@@ -168,9 +168,9 @@
}
static void setupMethodEnvironment(
- Environment env, Iterable<BaseFunction> functions) throws EvalException {
+ Environment env, Iterable<BaseFunction> functions) {
for (BaseFunction function : functions) {
- env.update(function.getName(), function);
+ env.setup(function.getName(), function);
}
}
}