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/BuiltinFunction.java b/src/main/java/com/google/devtools/build/lib/syntax/BuiltinFunction.java
index e3634f1..4324069 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/BuiltinFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/BuiltinFunction.java
@@ -122,9 +122,11 @@
@Override
@Nullable
public Object call(Object[] args,
- @Nullable FuncallExpression ast, @Nullable Environment env)
+ FuncallExpression ast, Environment env)
throws EvalException, InterruptedException {
- final Location loc = (ast == null) ? location : ast.getLocation();
+ Preconditions.checkNotNull(ast);
+ Preconditions.checkNotNull(env);
+ Location loc = ast.getLocation();
// Add extra arguments, if needed
if (extraArgs != null) {
@@ -151,6 +153,7 @@
this.getClass().getName() + "#" + getName());
// Last but not least, actually make an inner call to the function with the resolved arguments.
try {
+ env.enterScope(this, ast, env.getGlobals());
return invokeMethod.invoke(this, args);
} catch (InvocationTargetException x) {
Throwable e = x.getCause();
@@ -191,6 +194,7 @@
throw badCallException(loc, e, args);
} finally {
Profiler.instance().completeTask(ProfilerTask.SKYLARK_BUILTIN_FN);
+ env.exitScope();
}
}