Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 17 | import com.google.common.base.Joiner; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 21 | import com.google.common.collect.Iterables; |
| 22 | import com.google.common.collect.Lists; |
laurentlb | e5894f0 | 2018-10-25 13:02:00 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 25 | import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.events.Event; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.events.EventHandler; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.events.Location; |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.syntax.Mutability.Freezable; |
| 31 | import com.google.devtools.build.lib.syntax.Mutability.MutabilityException; |
| 32 | import com.google.devtools.build.lib.util.Fingerprint; |
| 33 | import com.google.devtools.build.lib.util.Pair; |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.util.SpellChecker; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 35 | import java.util.ArrayList; |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 36 | import java.util.HashMap; |
Jon Brandvein | ded4fbb | 2017-01-18 22:21:04 +0000 | [diff] [blame] | 37 | import java.util.LinkedHashMap; |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 38 | import java.util.LinkedHashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import java.util.Map; |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 40 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import java.util.Set; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 42 | import java.util.TreeSet; |
Googler | 5910093 | 2019-09-12 07:24:20 -0700 | [diff] [blame] | 43 | import java.util.function.Predicate; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | import javax.annotation.Nullable; |
| 45 | |
| 46 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 47 | * An StarlarkThread represents a Starlark thread. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 48 | * |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 49 | * <p>It holds the stack of active Starlark and built-in function calls. In addition, it may hold |
| 50 | * per-thread application state (see {@link #setThreadLocal}) that passes through Starlark functions |
| 51 | * but does not directly affect them, such as information about the BUILD file being loaded. |
brandjon | 0adb784 | 2017-09-28 10:58:43 -0400 | [diff] [blame] | 52 | * |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 53 | * <p>Every {@code StarlarkThread} has a {@link Mutability} field, and must be used within a |
| 54 | * function that creates and closes this {@link Mutability} with the try-with-resource pattern. This |
| 55 | * {@link Mutability} is also used when initializing mutable objects within that {@code |
| 56 | * StarlarkThread}. When the {@code Mutability} is closed at the end of the computation, it freezes |
| 57 | * the {@code StarlarkThread} along with all of those objects. This pattern enforces the discipline |
| 58 | * that there should be no dangling mutable {@code StarlarkThread}, or concurrency between |
| 59 | * interacting {@code StarlarkThread}s. It is a Skylark-level error to attempt to mutate a frozen |
| 60 | * {@code StarlarkThread} or its objects, but it is a Java-level error to attempt to mutate an |
| 61 | * unfrozen {@code StarlarkThread} or its objects from within a different {@code StarlarkThread}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 62 | * |
Googler | bffb687 | 2019-10-22 11:14:46 -0700 | [diff] [blame] | 63 | * <p>One creates an StarlarkThread using the {@link #builder} function, before evaluating code in |
| 64 | * it with {@link StarlarkFile#eval}, or with {@link StarlarkFile#exec} (where the AST was obtained |
| 65 | * by passing a {@link ValidationEnvironment} constructed from the StarlarkThread to {@link |
| 66 | * StarlarkFile#parse}. When the computation is over, the frozen StarlarkThread can still be queried |
| 67 | * with {@link #lookup}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | */ |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 69 | // TODO(adonovan): further steps for StarlarkThread remediation: |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 70 | // Its API should expose the following concepts, and no more: |
| 71 | // 1) "thread local variables": this holds per-thread application |
| 72 | // state such as the current Label, or BUILD package, for all the |
| 73 | // native.* built-ins. |
| 74 | // This may include any thread-specific behaviour relevant to the |
| 75 | // load and print statements. |
| 76 | // 2) a stack of call frames, each representing an active function call. |
| 77 | // Only clients needing debugger-like powers of reflection should need |
| 78 | // this, such as the debugger itself, and the ill-conceived |
| 79 | // generator_name attribute. The API for call frames should not |
| 80 | // expose an object of class CallFrame, because for efficiency we |
| 81 | // will want to recycle objects in place rather than generate garbage |
| 82 | // on every call. |
| 83 | // So the API will look like getCallerLocation(depth), |
| 84 | // not getCaller(depth).location, with one method per "public" CallFrame |
| 85 | // attribute, such as location. |
| 86 | // We must expose these basic CallFrame attributes, for stack traces and errors: |
| 87 | // - function name |
| 88 | // - PC location |
| 89 | // Advanced clients such as the debugger, and the generator_name rule attribute, also need: |
| 90 | // - the function value (Warning: careless clients can pin closures in memory) |
| 91 | // - Object getLocalValue(Identifier parameter). |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 92 | // 3) Debugging support (thread name, profiling counters, etc). |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 93 | // And that is all. See go.starlark.net for the model. |
| 94 | // |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 95 | // The Frame interface should eliminated. |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 96 | // As best I can tell, all the skyframe serialization |
| 97 | // as it applies to LexicalFrames is redundant, as these are transient |
| 98 | // and should not exist after loading. |
| 99 | // We will remove the FuncallExpression parameter from StarlarkFunction.call. |
| 100 | // Clients should use getCallerLocation instead. |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 101 | // The only place that still needs an AST is Bazel's generator_name. |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 102 | // Once the API is small and sound, we can start to represent all |
| 103 | // the lexical frames within a single function using just an array, |
| 104 | // indexed by a small integer computed during the validation pass. |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 105 | public final class StarlarkThread implements Freezable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 106 | |
Luis Fernando Pino Duque | 3fedf9e | 2016-04-28 15:47:29 +0000 | [diff] [blame] | 107 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 108 | * A mapping of bindings, either mutable or immutable according to an associated {@link |
| 109 | * Mutability}. The order of the bindings within a single {@link Frame} is deterministic but |
| 110 | * unspecified. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 111 | * |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 112 | * <p>Any non-frozen {@link Frame} must have the same {@link Mutability} as the current {@link |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 113 | * StarlarkThread}, to avoid interference from other evaluation contexts. For example, a {@link |
| 114 | * StarlarkFunction} will close over the global frame of the {@link StarlarkThread} in which it |
| 115 | * was defined. When the function is called from other {@link StarlarkThread}s (possibly |
| 116 | * simultaneously), that global frame must already be frozen; a new local {@link Frame} is created |
| 117 | * to represent the lexical scope of the function. |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 118 | * |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 119 | * <p>A {@link Frame} can have an associated "parent" {@link Frame}, which is used in {@link #get} |
| 120 | * and {@link #getTransitiveBindings()} |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 121 | * |
| 122 | * <p>TODO(laurentlb): "parent" should be named "universe" since it contains only the builtins. |
| 123 | * The "get" method shouldn't look at the universe (so that "moduleLookup" works as expected) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 124 | */ |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 125 | interface Frame extends Freezable { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Gets a binding from this {@link Frame} or one of its transitive parents. |
| 128 | * |
| 129 | * <p>In case of conflicts, the binding found in the {@link Frame} closest to the current one is |
| 130 | * used; the remaining bindings are shadowed. |
| 131 | * |
| 132 | * @param varname the name of the variable whose value should be retrieved |
| 133 | * @return the value bound to the variable, or null if no binding is found |
| 134 | */ |
| 135 | @Nullable |
| 136 | Object get(String varname); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 137 | |
brandjon | adda512 | 2017-08-29 18:29:58 +0200 | [diff] [blame] | 138 | /** |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 139 | * Assigns or reassigns a binding in the current {@code Frame}. |
| 140 | * |
| 141 | * <p>If the binding has the same name as one in a transitive parent, the parent binding is |
| 142 | * shadowed (i.e., the parent is unaffected). |
| 143 | * |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 144 | * @param varname the name of the variable to be bound |
| 145 | * @param value the value to bind to the variable |
| 146 | */ |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 147 | void put(String varname, Object value) throws MutabilityException; |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 148 | |
| 149 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 150 | * TODO(laurentlb): Remove this method when possible. It should probably not be part of the |
| 151 | * public interface. |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 152 | */ |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 153 | void remove(String varname) throws MutabilityException; |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 154 | |
| 155 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 156 | * Returns a map containing all bindings of this {@link Frame} and of its transitive parents, |
| 157 | * taking into account shadowing precedence. |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 158 | * |
| 159 | * <p>The bindings are returned in a deterministic order (for a given sequence of initial values |
| 160 | * and updates). |
| 161 | */ |
| 162 | Map<String, Object> getTransitiveBindings(); |
| 163 | } |
| 164 | |
| 165 | interface LexicalFrame extends Frame { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private static final class ImmutableEmptyLexicalFrame implements LexicalFrame { |
| 169 | private static final ImmutableEmptyLexicalFrame INSTANCE = new ImmutableEmptyLexicalFrame(); |
| 170 | |
| 171 | @Override |
| 172 | public Mutability mutability() { |
| 173 | return Mutability.IMMUTABLE; |
| 174 | } |
| 175 | |
| 176 | @Nullable |
| 177 | @Override |
| 178 | public Object get(String varname) { |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | @Override |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 183 | public void put(String varname, Object value) throws MutabilityException { |
| 184 | Mutability.checkMutable(this, mutability()); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 185 | throw new IllegalStateException(); |
| 186 | } |
| 187 | |
| 188 | @Override |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 189 | public void remove(String varname) throws MutabilityException { |
| 190 | Mutability.checkMutable(this, mutability()); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 191 | throw new IllegalStateException(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Map<String, Object> getTransitiveBindings() { |
| 196 | return ImmutableMap.of(); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public String toString() { |
| 201 | return "<ImmutableEmptyLexicalFrame>"; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private static final class MutableLexicalFrame implements LexicalFrame { |
| 206 | private final Mutability mutability; |
| 207 | /** Bindings are maintained in order of creation. */ |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 208 | private final LinkedHashMap<String, Object> bindings; |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 209 | |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 210 | private MutableLexicalFrame(Mutability mutability, int initialCapacity) { |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 211 | this.mutability = mutability; |
laurentlb | e5894f0 | 2018-10-25 13:02:00 -0700 | [diff] [blame] | 212 | this.bindings = Maps.newLinkedHashMapWithExpectedSize(initialCapacity); |
nharmata | 62678a4 | 2018-03-08 16:43:45 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private MutableLexicalFrame(Mutability mutability) { |
| 216 | this.mutability = mutability; |
| 217 | this.bindings = new LinkedHashMap<>(); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Mutability mutability() { |
| 222 | return mutability; |
| 223 | } |
| 224 | |
| 225 | @Nullable |
| 226 | @Override |
| 227 | public Object get(String varname) { |
| 228 | return bindings.get(varname); |
| 229 | } |
| 230 | |
| 231 | @Override |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 232 | public void put(String varname, Object value) throws MutabilityException { |
| 233 | Mutability.checkMutable(this, mutability()); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 234 | bindings.put(varname, value); |
| 235 | } |
| 236 | |
| 237 | @Override |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 238 | public void remove(String varname) throws MutabilityException { |
| 239 | Mutability.checkMutable(this, mutability()); |
nharmata | 68cc06b | 2018-03-01 10:21:57 -0800 | [diff] [blame] | 240 | bindings.remove(varname); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public Map<String, Object> getTransitiveBindings() { |
| 245 | return bindings; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public String toString() { |
| 250 | return String.format("<MutableLexicalFrame%s>", mutability()); |
| 251 | } |
| 252 | } |
| 253 | |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 254 | // The mutability of the StarlarkThread comes from its initial global frame. |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 255 | private final Mutability mutability; |
| 256 | |
| 257 | private final Map<Class<?>, Object> threadLocals = new HashMap<>(); |
| 258 | |
| 259 | /** |
| 260 | * setThreadLocal saves {@code value} as a thread-local variable of this Starlark thread, keyed by |
| 261 | * {@code key}, so that it can later be retrieved by {@code getThreadLocal(key)}. |
| 262 | */ |
| 263 | public <T> void setThreadLocal(Class<T> key, T value) { |
| 264 | // The clazz parameter is redundant, but it makes the API clearer. |
| 265 | threadLocals.put(key, value); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * getThreadLocal returns the value {@code v} supplied to the most recent {@code |
| 270 | * setThreadLocal(key, v)} call, or null if there was no prior call. |
| 271 | */ |
| 272 | public <T> T getThreadLocal(Class<T> key) { |
| 273 | Object v = threadLocals.get(key); |
| 274 | return v == null ? null : key.cast(v); |
| 275 | } |
| 276 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 277 | /** A CallFrame records information about an active function call. */ |
| 278 | // TODO(adonovan): merge LexicalFrame into CallFrame. Every function call should have a frame, |
| 279 | // but only Starlark functions need local variables. |
| 280 | private static final class CallFrame { |
| 281 | final StarlarkCallable fn; // the called function |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 282 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 283 | // Note that the inherited design is off-by-one: |
| 284 | // the following fields are logically facts about the _enclosing_ frame. |
| 285 | // This is a consequence of not representing toplevel statements as a function. |
| 286 | // TODO(adonovan): fix that. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 287 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 288 | final Location callerLoc; // location of the enclosing call (may be Location.BUILTIN) |
| 289 | @Nullable final FuncallExpression call; // syntax of the enclosing call |
| 290 | final Frame savedLexicals; // the saved lexicals of the parent |
| 291 | final Module savedModule; // the saved module of the parent (TODO(adonovan): eliminate) |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 292 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 293 | CallFrame( |
| 294 | StarlarkCallable fn, |
| 295 | Location callerLoc, |
| 296 | @Nullable FuncallExpression call, |
| 297 | Frame savedLexicals, |
| 298 | Module savedModule) { |
| 299 | this.fn = fn; |
| 300 | this.callerLoc = callerLoc; |
| 301 | this.call = call; |
| 302 | this.savedLexicals = savedLexicals; |
| 303 | this.savedModule = savedModule; |
| 304 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 305 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 306 | @Override |
| 307 | public String toString() { |
| 308 | return fn.getName() + "@" + callerLoc; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 312 | /** An Extension to be imported with load() into a BUILD or .bzl file. */ |
| 313 | @Immutable |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 314 | // TODO(janakr,brandjon): Do Extensions actually have to start their own memoization? Or can we |
| 315 | // have a node higher up in the hierarchy inject the mutability? |
shahan | 9012e6f | 2018-04-02 13:26:14 -0700 | [diff] [blame] | 316 | @AutoCodec |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 317 | public static final class Extension { |
| 318 | |
| 319 | private final ImmutableMap<String, Object> bindings; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 320 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 321 | /** |
| 322 | * Cached hash code for the transitive content of this {@code Extension} and its dependencies. |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 323 | * |
| 324 | * <p>Note that "content" refers to the AST content, not the evaluated bindings. |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 325 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 326 | private final String transitiveContentHashCode; |
| 327 | |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 328 | /** Constructs with the given hash code and bindings. */ |
janakr | 9ba46f8 | 2018-03-13 13:07:52 -0700 | [diff] [blame] | 329 | @AutoCodec.Instantiator |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 330 | public Extension(ImmutableMap<String, Object> bindings, String transitiveContentHashCode) { |
| 331 | this.bindings = bindings; |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 332 | this.transitiveContentHashCode = transitiveContentHashCode; |
| 333 | } |
| 334 | |
| 335 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 336 | * Constructs using the bindings from the global definitions of the given {@link |
| 337 | * StarlarkThread}, and that {@code StarlarkThread}'s transitive hash code. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 338 | */ |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 339 | public Extension(StarlarkThread thread) { |
laurentlb | 9d179e1 | 2018-09-27 08:15:42 -0700 | [diff] [blame] | 340 | // Legacy behavior: all symbols from the global Frame are exported (including symbols |
| 341 | // introduced by load). |
| 342 | this( |
laurentlb | cdac07f | 2019-11-07 15:40:58 -0800 | [diff] [blame] | 343 | ImmutableMap.copyOf(thread.globalFrame.getExportedBindings()), |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 344 | thread.getTransitiveContentHashCode()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 347 | private String getTransitiveContentHashCode() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 348 | return transitiveContentHashCode; |
| 349 | } |
| 350 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 351 | /** Retrieves all bindings, in a deterministic order. */ |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 352 | public ImmutableMap<String, Object> getBindings() { |
| 353 | return bindings; |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 354 | } |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 355 | |
| 356 | @Override |
| 357 | public boolean equals(Object obj) { |
| 358 | if (this == obj) { |
| 359 | return true; |
| 360 | } |
| 361 | if (!(obj instanceof Extension)) { |
| 362 | return false; |
| 363 | } |
| 364 | Extension other = (Extension) obj; |
| 365 | return transitiveContentHashCode.equals(other.getTransitiveContentHashCode()) |
| 366 | && bindings.equals(other.getBindings()); |
| 367 | } |
| 368 | |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 369 | private static boolean skylarkObjectsProbablyEqual(Object obj1, Object obj2) { |
| 370 | // TODO(b/76154791): check this more carefully. |
| 371 | return obj1.equals(obj2) |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 372 | || (obj1 instanceof StarlarkValue |
| 373 | && obj2 instanceof StarlarkValue |
Googler | b1e232d | 2019-11-22 15:29:43 -0800 | [diff] [blame] | 374 | && Starlark.repr(obj1).equals(Starlark.repr(obj2))); |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 377 | /** |
| 378 | * Throws {@link IllegalStateException} if this {@link Extension} is not equal to {@code obj}. |
| 379 | * |
| 380 | * <p>The exception explains the reason for the inequality, including all unequal bindings. |
| 381 | */ |
| 382 | public void checkStateEquals(Object obj) { |
| 383 | if (this == obj) { |
| 384 | return; |
| 385 | } |
| 386 | if (!(obj instanceof Extension)) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 387 | throw new IllegalStateException( |
| 388 | String.format( |
| 389 | "Expected an equal Extension, but got a %s instead of an Extension", |
| 390 | obj == null ? "null" : obj.getClass().getName())); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 391 | } |
| 392 | Extension other = (Extension) obj; |
| 393 | ImmutableMap<String, Object> otherBindings = other.getBindings(); |
| 394 | |
| 395 | Set<String> names = bindings.keySet(); |
| 396 | Set<String> otherNames = otherBindings.keySet(); |
| 397 | if (!names.equals(otherNames)) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 398 | throw new IllegalStateException( |
| 399 | String.format( |
| 400 | "Expected Extensions to be equal, but they don't define the same bindings: " |
| 401 | + "in this one but not given one: [%s]; in given one but not this one: [%s]", |
| 402 | Joiner.on(", ").join(Sets.difference(names, otherNames)), |
| 403 | Joiner.on(", ").join(Sets.difference(otherNames, names)))); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | ArrayList<String> badEntries = new ArrayList<>(); |
| 407 | for (String name : names) { |
| 408 | Object value = bindings.get(name); |
| 409 | Object otherValue = otherBindings.get(name); |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 410 | if (value.equals(otherValue)) { |
| 411 | continue; |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 412 | } |
Googler | d21a0d1 | 2019-11-21 13:52:30 -0800 | [diff] [blame] | 413 | if (value instanceof Depset) { |
| 414 | if (otherValue instanceof Depset |
| 415 | && ((Depset) value).toCollection().equals(((Depset) otherValue).toCollection())) { |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 416 | continue; |
| 417 | } |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 418 | } else if (value instanceof Dict) { |
| 419 | if (otherValue instanceof Dict) { |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 420 | @SuppressWarnings("unchecked") |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 421 | Dict<Object, Object> thisDict = (Dict<Object, Object>) value; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 422 | @SuppressWarnings("unchecked") |
Googler | a9c9363 | 2019-11-13 10:48:07 -0800 | [diff] [blame] | 423 | Dict<Object, Object> otherDict = (Dict<Object, Object>) otherValue; |
janakr | 33439dc | 2018-03-22 13:44:05 -0700 | [diff] [blame] | 424 | if (thisDict.size() == otherDict.size() |
| 425 | && thisDict.keySet().equals(otherDict.keySet())) { |
| 426 | boolean foundProblem = false; |
| 427 | for (Object key : thisDict.keySet()) { |
| 428 | if (!skylarkObjectsProbablyEqual( |
| 429 | Preconditions.checkNotNull(thisDict.get(key), key), |
| 430 | Preconditions.checkNotNull(otherDict.get(key), key))) { |
| 431 | foundProblem = true; |
| 432 | } |
| 433 | } |
| 434 | if (!foundProblem) { |
| 435 | continue; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } else if (skylarkObjectsProbablyEqual(value, otherValue)) { |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 440 | continue; |
| 441 | } |
| 442 | badEntries.add( |
| 443 | String.format( |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 444 | "%s: this one has %s (class %s, %s), but given one has %s (class %s, %s)", |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 445 | name, |
Googler | b1e232d | 2019-11-22 15:29:43 -0800 | [diff] [blame] | 446 | Starlark.repr(value), |
janakr | 889f562 | 2018-03-16 17:49:11 -0700 | [diff] [blame] | 447 | value.getClass().getName(), |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 448 | value, |
Googler | b1e232d | 2019-11-22 15:29:43 -0800 | [diff] [blame] | 449 | Starlark.repr(otherValue), |
janakr | cfc3432 | 2018-03-26 11:10:08 -0700 | [diff] [blame] | 450 | otherValue.getClass().getName(), |
| 451 | otherValue)); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 452 | } |
| 453 | if (!badEntries.isEmpty()) { |
| 454 | throw new IllegalStateException( |
| 455 | "Expected Extensions to be equal, but the following bindings are unequal: " |
| 456 | + Joiner.on("; ").join(badEntries)); |
| 457 | } |
| 458 | |
| 459 | if (!transitiveContentHashCode.equals(other.getTransitiveContentHashCode())) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 460 | throw new IllegalStateException( |
| 461 | String.format( |
| 462 | "Expected Extensions to be equal, but transitive content hashes don't match:" |
| 463 | + " %s != %s", |
| 464 | transitiveContentHashCode, other.getTransitiveContentHashCode())); |
brandjon | 2c98fff | 2018-02-06 01:32:46 -0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
brandjon | e821fb9 | 2017-07-18 21:00:22 +0200 | [diff] [blame] | 468 | @Override |
| 469 | public int hashCode() { |
| 470 | return Objects.hash(bindings, transitiveContentHashCode); |
| 471 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 474 | // Local environment of the current active call, |
| 475 | // or an alias for globalFrame if no calls are active. |
| 476 | // TODO(adonovan): redundant with callstack; eliminate once we fix off-by-one problem. |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 477 | private Frame lexicalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 478 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 479 | // Global environment of the current topmost call frame, |
| 480 | // or of the file about to be initialized if no calls are active. |
| 481 | // TODO(adonovan): eliminate once we represent even toplevel statements |
| 482 | // as a StarlarkFunction that closes over its Module. |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 483 | private Module globalFrame; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 484 | |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 485 | /** The semantics options that affect how Skylark code is evaluated. */ |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 486 | private final StarlarkSemantics semantics; |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 487 | |
| 488 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 489 | * An EventHandler for errors and warnings. This is not used in the BUILD language, however it |
| 490 | * might be used in Skylark code called from the BUILD language, so shouldn't be null. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 491 | */ |
| 492 | private final EventHandler eventHandler; |
| 493 | |
| 494 | /** |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 495 | * For each imported extension, a global Skylark frame from which to load() individual bindings. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 496 | */ |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 497 | private final Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 498 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 499 | /** Stack of active function calls. */ |
| 500 | // TODO(adonovan): currently off by one because top-level statements don't have a CallFrame. |
| 501 | private final ArrayList<CallFrame> callstack = new ArrayList<>(); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 502 | |
Googler | ba86854 | 2019-10-09 07:26:27 -0700 | [diff] [blame] | 503 | /** A hook for notifications of assignments at top level. */ |
| 504 | PostAssignHook postAssignHook; |
| 505 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 506 | /** |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 507 | * Pushes a function onto the call stack. |
brendandouglas | fe785e1 | 2018-06-12 09:39:38 -0700 | [diff] [blame] | 508 | * |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 509 | * @param fn the function whose scope to enter |
| 510 | * @param loc the source location of the function call. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 511 | */ |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 512 | void push(StarlarkCallable fn, Location loc, @Nullable FuncallExpression call) { |
| 513 | callstack.add(new CallFrame(fn, loc, call, this.lexicalFrame, this.globalFrame)); |
| 514 | |
| 515 | if (fn instanceof StarlarkFunction) { |
| 516 | StarlarkFunction sfn = (StarlarkFunction) fn; |
| 517 | this.lexicalFrame = |
| 518 | new MutableLexicalFrame( |
| 519 | this.mutability(), /*initialCapacity=*/ sfn.getSignature().numParameters()); |
| 520 | this.globalFrame = sfn.getModule(); |
| 521 | } else { |
| 522 | // built-in function |
| 523 | this.lexicalFrame = DUMMY_LEXICAL_FRAME; |
| 524 | // this.globalFrame is left as is. |
| 525 | // For built-ins, thread.globals() returns the module |
| 526 | // of the file from which the built-in was called. |
| 527 | // Really they have no business knowing about that. |
| 528 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 531 | /** Pops a function off the call stack. */ |
| 532 | void pop() { |
| 533 | int last = callstack.size() - 1; |
| 534 | CallFrame top = callstack.get(last); |
| 535 | callstack.remove(last); // pop |
| 536 | this.lexicalFrame = top.savedLexicals; |
| 537 | this.globalFrame = top.savedModule; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 540 | // Builtins cannot create or modify variable bindings, |
| 541 | // so it's sufficient to use a shared instance. |
| 542 | private static final LexicalFrame DUMMY_LEXICAL_FRAME = ImmutableEmptyLexicalFrame.INSTANCE; |
| 543 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 544 | private final String transitiveHashCode; |
Francois-Rene Rideau | 3a8ddcc | 2015-08-26 22:30:38 +0000 | [diff] [blame] | 545 | |
| 546 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 547 | * Is this a global StarlarkThread? |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 548 | * |
| 549 | * @return true if the current code is being executed at the top-level, as opposed to inside the |
| 550 | * body of a function. |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 551 | */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 552 | boolean isGlobal() { |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 553 | return lexicalFrame instanceof Module; |
Francois-Rene Rideau | 6e7160d | 2015-08-26 17:22:35 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 556 | @Override |
| 557 | public Mutability mutability() { |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 558 | return mutability; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 561 | /** Returns the global variables for the StarlarkThread (not including dynamic bindings). */ |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 562 | public Module getGlobals() { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 563 | return globalFrame; |
| 564 | } |
| 565 | |
| 566 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 567 | * Returns an EventHandler for errors and warnings. The BUILD language doesn't use it directly, |
| 568 | * but can call Skylark code that does use it. |
| 569 | * |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 570 | * @return an EventHandler |
| 571 | */ |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 572 | // TODO(adonovan): turn this into a print handler and break dependency on EventHandler. |
| 573 | // First, we must report scan/parse/validation errors using an exception containing events. |
Francois-Rene Rideau | e6a46b8 | 2015-04-10 18:31:43 +0000 | [diff] [blame] | 574 | public EventHandler getEventHandler() { |
| 575 | return eventHandler; |
| 576 | } |
| 577 | |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 578 | /** |
| 579 | * Returns if calling the supplied function would be a recursive call, or in other words if the |
| 580 | * supplied function is already on the stack. |
| 581 | */ |
Googler | 3fcfbe1 | 2019-08-28 08:10:11 -0700 | [diff] [blame] | 582 | boolean isRecursiveCall(StarlarkFunction function) { |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 583 | for (CallFrame fr : callstack) { |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 584 | // TODO(adonovan): compare code, not closure values, otherwise |
| 585 | // one can defeat this check by writing the Y combinator. |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 586 | if (fr.fn.equals(function)) { |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 587 | return true; |
| 588 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 589 | } |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 590 | return false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 593 | /** Returns the current called function, which must exist. */ |
| 594 | StarlarkCallable getCurrentFunction() { |
| 595 | return Iterables.getLast(callstack).fn; |
michajlo | cbb3347 | 2017-10-23 20:30:18 +0200 | [diff] [blame] | 596 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 597 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 598 | /** Returns the call expression and called function for the outermost call being evaluated. */ |
| 599 | // TODO(adonovan): replace this by an API for walking the call stack, then move to lib.packages. |
| 600 | public Pair<FuncallExpression, StarlarkCallable> getOutermostCall() { |
| 601 | if (callstack.isEmpty()) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 602 | return null; |
| 603 | } |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 604 | CallFrame outermost = callstack.get(0); |
| 605 | return new Pair<>(outermost.call, outermost.fn); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 608 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 609 | * Constructs an StarlarkThread. This is the main, most basic constructor. |
Laurent Le Brun | a31bc4e | 2016-10-27 12:48:22 +0000 | [diff] [blame] | 610 | * |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 611 | * @param globalFrame a frame for the global StarlarkThread |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 612 | * @param eventHandler an EventHandler for warnings, errors, etc |
| 613 | * @param importedExtensions Extension-s from which to import bindings with load() |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 614 | * @param fileContentHashCode a hash for the source file being evaluated, if any |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 615 | */ |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 616 | private StarlarkThread( |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 617 | Module globalFrame, |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 618 | StarlarkSemantics semantics, |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 619 | EventHandler eventHandler, |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 620 | Map<String, Extension> importedExtensions, |
Googler | 38be9dd | 2019-09-23 15:34:57 -0700 | [diff] [blame] | 621 | @Nullable String fileContentHashCode) { |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 622 | this.lexicalFrame = Preconditions.checkNotNull(globalFrame); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 623 | this.globalFrame = Preconditions.checkNotNull(globalFrame); |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 624 | this.mutability = globalFrame.mutability(); |
brandjon | 33b4943 | 2017-05-04 18:58:52 +0200 | [diff] [blame] | 625 | Preconditions.checkArgument(!globalFrame.mutability().isFrozen()); |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 626 | this.semantics = semantics; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 627 | this.eventHandler = eventHandler; |
| 628 | this.importedExtensions = importedExtensions; |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 629 | this.transitiveHashCode = |
| 630 | computeTransitiveContentHashCode(fileContentHashCode, importedExtensions); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 634 | * A Builder class for StarlarkThread. |
brandjon | d9a1a43 | 2017-10-19 23:47:08 +0200 | [diff] [blame] | 635 | * |
| 636 | * <p>The caller must explicitly set the semantics by calling either {@link #setSemantics} or |
| 637 | * {@link #useDefaultSemantics}. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 638 | */ |
| 639 | public static class Builder { |
| 640 | private final Mutability mutability; |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 641 | @Nullable private Module parent; |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 642 | @Nullable private StarlarkSemantics semantics; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 643 | @Nullable private EventHandler eventHandler; |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 644 | @Nullable private Map<String, Extension> importedExtensions; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 645 | @Nullable private String fileContentHashCode; |
| 646 | |
| 647 | Builder(Mutability mutability) { |
| 648 | this.mutability = mutability; |
| 649 | } |
| 650 | |
laurentlb | f635062 | 2018-09-19 10:15:34 -0700 | [diff] [blame] | 651 | /** |
| 652 | * Inherits global bindings from the given parent Frame. |
| 653 | * |
| 654 | * <p>TODO(laurentlb): this should be called setUniverse. |
| 655 | */ |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 656 | public Builder setGlobals(Module parent) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 657 | Preconditions.checkState(this.parent == null); |
nharmata | 6070ba7 | 2018-03-01 13:35:33 -0800 | [diff] [blame] | 658 | this.parent = parent; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 659 | return this; |
| 660 | } |
| 661 | |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 662 | public Builder setSemantics(StarlarkSemantics semantics) { |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 663 | this.semantics = semantics; |
| 664 | return this; |
| 665 | } |
| 666 | |
brandjon | d9a1a43 | 2017-10-19 23:47:08 +0200 | [diff] [blame] | 667 | public Builder useDefaultSemantics() { |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 668 | this.semantics = StarlarkSemantics.DEFAULT_SEMANTICS; |
brandjon | d9a1a43 | 2017-10-19 23:47:08 +0200 | [diff] [blame] | 669 | return this; |
| 670 | } |
| 671 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 672 | /** Sets an EventHandler for errors and warnings. */ |
| 673 | public Builder setEventHandler(EventHandler eventHandler) { |
| 674 | Preconditions.checkState(this.eventHandler == null); |
| 675 | this.eventHandler = eventHandler; |
| 676 | return this; |
| 677 | } |
| 678 | |
| 679 | /** Declares imported extensions for load() statements. */ |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 680 | public Builder setImportedExtensions(Map<String, Extension> importMap) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 681 | Preconditions.checkState(this.importedExtensions == null); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 682 | this.importedExtensions = importMap; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 683 | return this; |
| 684 | } |
| 685 | |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 686 | /** Declares content hash for the source file for this StarlarkThread. */ |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 687 | public Builder setFileContentHashCode(String fileContentHashCode) { |
| 688 | this.fileContentHashCode = fileContentHashCode; |
| 689 | return this; |
| 690 | } |
| 691 | |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 692 | /** Builds the StarlarkThread. */ |
| 693 | public StarlarkThread build() { |
brandjon | 33b4943 | 2017-05-04 18:58:52 +0200 | [diff] [blame] | 694 | Preconditions.checkArgument(!mutability.isFrozen()); |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 695 | if (semantics == null) { |
| 696 | throw new IllegalArgumentException("must call either setSemantics or useDefaultSemantics"); |
| 697 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 698 | if (parent != null) { |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 699 | Preconditions.checkArgument(parent.mutability().isFrozen(), "parent frame must be frozen"); |
laurentlb | f635062 | 2018-09-19 10:15:34 -0700 | [diff] [blame] | 700 | if (parent.universe != null) { // This code path doesn't happen in Bazel. |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 701 | |
| 702 | // Flatten the frame, ensure all builtins are in the same frame. |
| 703 | parent = |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 704 | new Module( |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 705 | parent.mutability(), |
| 706 | null /* parent */, |
| 707 | parent.label, |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 708 | parent.getTransitiveBindings(), |
| 709 | parent.restrictedBindings); |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 710 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 711 | } |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 712 | |
| 713 | // Filter out restricted objects from the universe scope. This cannot be done in-place in |
| 714 | // creation of the input global universe scope, because this environment's semantics may not |
| 715 | // have been available during its creation. Thus, create a new universe scope for this |
| 716 | // environment which is equivalent in every way except that restricted bindings are |
| 717 | // filtered out. |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 718 | parent = Module.filterOutRestrictedBindings(mutability, parent, semantics); |
cparsons | 6622e6f | 2018-10-17 15:00:09 -0700 | [diff] [blame] | 719 | |
Googler | 41c333f | 2019-10-15 12:12:24 -0700 | [diff] [blame] | 720 | Module globalFrame = new Module(mutability, parent); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 721 | if (importedExtensions == null) { |
| 722 | importedExtensions = ImmutableMap.of(); |
| 723 | } |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 724 | return new StarlarkThread( |
| 725 | globalFrame, semantics, eventHandler, importedExtensions, fileContentHashCode); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | public static Builder builder(Mutability mutability) { |
| 730 | return new Builder(mutability); |
| 731 | } |
| 732 | |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 733 | /** Remove variable from local bindings. */ |
| 734 | void removeLocalBinding(String varname) { |
| 735 | try { |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 736 | lexicalFrame.remove(varname); |
laurentlb | 9d5c0a0 | 2017-06-13 23:08:06 +0200 | [diff] [blame] | 737 | } catch (MutabilityException e) { |
| 738 | throw new AssertionError(e); |
| 739 | } |
| 740 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 741 | |
laurentlb | 9d179e1 | 2018-09-27 08:15:42 -0700 | [diff] [blame] | 742 | /** Modifies a binding in the current Frame. If it is the module Frame, also export it. */ |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 743 | StarlarkThread updateAndExport(String varname, Object value) throws EvalException { |
laurentlb | 9d179e1 | 2018-09-27 08:15:42 -0700 | [diff] [blame] | 744 | update(varname, value); |
| 745 | if (isGlobal()) { |
| 746 | globalFrame.exportedBindings.add(varname); |
| 747 | } |
| 748 | return this; |
| 749 | } |
| 750 | |
nharmata | f101348 | 2018-03-07 10:48:31 -0800 | [diff] [blame] | 751 | /** |
Googler | ba86854 | 2019-10-09 07:26:27 -0700 | [diff] [blame] | 752 | * Specifies a hook function to be run after each assignment at top level. |
| 753 | * |
| 754 | * <p>This is a short-term hack to allow us to consolidate all StarlarkFile execution in one place |
| 755 | * even while SkylarkImportLookupFunction implements the old "export" behavior, in which rules, |
| 756 | * aspects and providers are "exported" as soon as they are assigned, not at the end of file |
| 757 | * execution. |
| 758 | */ |
| 759 | public void setPostAssignHook(PostAssignHook postAssignHook) { |
| 760 | this.postAssignHook = postAssignHook; |
| 761 | } |
| 762 | |
| 763 | /** A hook for notifications of assignments at top level. */ |
| 764 | public interface PostAssignHook { |
| 765 | void assign(String name, Object value); |
| 766 | } |
| 767 | |
| 768 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 769 | * Modifies a binding in the current Frame of this StarlarkThread, as would an {@link |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 770 | * AssignmentStatement}. Does not try to modify an inherited binding. This will shadow any |
| 771 | * inherited binding, which may be an error that you want to guard against before calling this |
| 772 | * function. |
| 773 | * |
nharmata | f101348 | 2018-03-07 10:48:31 -0800 | [diff] [blame] | 774 | * @param varname the name of the variable to be bound |
| 775 | * @param value the value to bind to the variable |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 776 | * @return this StarlarkThread, in fluid style |
nharmata | f101348 | 2018-03-07 10:48:31 -0800 | [diff] [blame] | 777 | */ |
Googler | d54a499 | 2019-10-18 12:35:06 -0700 | [diff] [blame] | 778 | // TODO(adonovan): eliminate sole external call from EvaluationTestCase and make private. |
| 779 | public StarlarkThread update(String varname, Object value) { |
laurentlb | 9dc4e20 | 2018-09-06 09:34:13 -0700 | [diff] [blame] | 780 | Preconditions.checkNotNull(value, "trying to assign null to '%s'", varname); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 781 | try { |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 782 | lexicalFrame.put(varname, value); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 783 | } catch (MutabilityException e) { |
| 784 | // Note that since at this time we don't accept the global keyword, and don't have closures, |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 785 | // end users should never be able to mutate a frozen StarlarkThread, and a MutabilityException |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 786 | // is therefore a failed assertion for Bazel. However, it is possible to shadow a binding |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 787 | // imported from a parent StarlarkThread by updating the current StarlarkThread, which will |
| 788 | // not trigger a MutabilityException. |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 789 | throw new AssertionError( |
Googler | b1e232d | 2019-11-22 15:29:43 -0800 | [diff] [blame] | 790 | Starlark.format("Can't update %s to %r in frozen environment", varname, value), e); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 791 | } |
| 792 | return this; |
| 793 | } |
| 794 | |
Stepan Koltsov | c1fe6c5 | 2019-10-02 03:15:36 -0700 | [diff] [blame] | 795 | // Used only for Eval.evalComprehension.. |
| 796 | void updateInternal(String name, @Nullable Object value) { |
| 797 | try { |
| 798 | if (value != null) { |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 799 | lexicalFrame.put(name, value); |
Stepan Koltsov | c1fe6c5 | 2019-10-02 03:15:36 -0700 | [diff] [blame] | 800 | } else { |
Googler | 48f6dda | 2019-10-16 14:10:09 -0700 | [diff] [blame] | 801 | lexicalFrame.remove(name); |
Stepan Koltsov | c1fe6c5 | 2019-10-02 03:15:36 -0700 | [diff] [blame] | 802 | } |
| 803 | } catch (MutabilityException ex) { |
| 804 | throw new IllegalStateException(ex); |
| 805 | } |
| 806 | } |
| 807 | |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 808 | /** |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 809 | * Returns the value of a variable defined in Local scope. Do not search in any parent scope. This |
| 810 | * function should be used once the AST has been analysed and we know which variables are local. |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 811 | */ |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 812 | Object localLookup(String varname) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 813 | return lexicalFrame.get(varname); |
| 814 | } |
| 815 | |
| 816 | /** |
laurentlb | c8e6796 | 2018-08-31 14:20:40 -0700 | [diff] [blame] | 817 | * Returns the value of a variable defined in the Module scope (e.g. global variables, functions). |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 818 | */ |
| 819 | public Object moduleLookup(String varname) { |
laurentlb | f635062 | 2018-09-19 10:15:34 -0700 | [diff] [blame] | 820 | return globalFrame.getDirectBindings(varname); |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /** Returns the value of a variable defined in the Universe scope (builtins). */ |
| 824 | public Object universeLookup(String varname) { |
laurentlb | f635062 | 2018-09-19 10:15:34 -0700 | [diff] [blame] | 825 | // TODO(laurentlb): look only at globalFrame.universe. |
laurentlb | 2b3ad18 | 2018-12-05 05:49:45 -0800 | [diff] [blame] | 826 | return globalFrame.get(varname); |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 827 | } |
| 828 | |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 829 | /** |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 830 | * Returns the value from the environment whose name is "varname" if it exists, otherwise null. |
laurentlb | ebb4071 | 2018-08-29 11:54:27 -0700 | [diff] [blame] | 831 | * |
| 832 | * <p>TODO(laurentlb): Remove this method. Callers should know where the value is defined and use |
| 833 | * the corresponding method (e.g. localLookup or moduleLookup). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 834 | */ |
laurentlb | c3e5851 | 2018-09-04 10:11:32 -0700 | [diff] [blame] | 835 | Object lookup(String varname) { |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 836 | // Lexical frame takes precedence, then globals. |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 837 | Object lexicalValue = lexicalFrame.get(varname); |
| 838 | if (lexicalValue != null) { |
| 839 | return lexicalValue; |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 840 | } |
| 841 | Object globalValue = globalFrame.get(varname); |
laurentlb | 2b3ad18 | 2018-12-05 05:49:45 -0800 | [diff] [blame] | 842 | if (globalValue == null) { |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 843 | return null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 844 | } |
laurentlb | 2b3ad18 | 2018-12-05 05:49:45 -0800 | [diff] [blame] | 845 | return globalValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 846 | } |
| 847 | |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 848 | public StarlarkSemantics getSemantics() { |
brandjon | b712f33 | 2017-04-29 16:03:32 +0200 | [diff] [blame] | 849 | return semantics; |
| 850 | } |
| 851 | |
Googler | 7fa0dd2 | 2019-09-24 20:07:53 -0700 | [diff] [blame] | 852 | void handleEvent(Event event) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 853 | eventHandler.handle(event); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 854 | } |
| 855 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 856 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 857 | * Returns a set of all names of variables that are accessible in this {@code StarlarkThread}, in |
| 858 | * a deterministic order. |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 859 | */ |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 860 | Set<String> getVariableNames() { |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 861 | LinkedHashSet<String> vars = new LinkedHashSet<>(); |
laurentlb | 5d26ef1 | 2018-08-27 12:23:15 -0700 | [diff] [blame] | 862 | vars.addAll(lexicalFrame.getTransitiveBindings().keySet()); |
| 863 | // No-op when globalFrame = lexicalFrame |
brandjon | cc0f6a6 | 2017-05-08 13:19:21 -0400 | [diff] [blame] | 864 | vars.addAll(globalFrame.getTransitiveBindings().keySet()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 865 | return vars; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 866 | } |
| 867 | |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 868 | /** Evaluates a Skylark statement in this thread. (Debugger API) This operation mutates expr. */ |
Googler | a4f8b62 | 2019-09-13 11:08:29 -0700 | [diff] [blame] | 869 | // TODO(adonovan): push this up into the debugger once the eval API is finalized. |
Googler | f0890f0 | 2019-10-01 07:28:48 -0700 | [diff] [blame] | 870 | public Object debugEval(Expression expr) throws EvalException, InterruptedException { |
Googler | 698d77f | 2019-09-17 14:54:26 -0700 | [diff] [blame] | 871 | return Eval.eval(this, expr); |
Googler | a4f8b62 | 2019-09-13 11:08:29 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Googler | 5910093 | 2019-09-12 07:24:20 -0700 | [diff] [blame] | 874 | /** |
| 875 | * Returns the stack frames corresponding of the context's current (paused) state. (Debugger API) |
| 876 | * |
| 877 | * <p>For all stack frames except the innermost, location information is retrieved from the |
| 878 | * current context. The innermost frame's location must be supplied as {@code currentLocation} by |
| 879 | * the caller. |
| 880 | */ |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 881 | public ImmutableList<DebugFrame> listFrames(Location loc) { |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 882 | ImmutableList.Builder<DebugFrame> frameListBuilder = ImmutableList.builder(); |
| 883 | |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 884 | Frame lex = this.lexicalFrame; |
| 885 | for (CallFrame fr : Lists.reverse(callstack)) { |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 886 | frameListBuilder.add( |
| 887 | DebugFrame.builder() |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 888 | .setLexicalFrameBindings(ImmutableMap.copyOf(lex.getTransitiveBindings())) |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 889 | .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings())) |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 890 | .setFunctionName(fr.fn.getName()) |
| 891 | .setLocation(loc) |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 892 | .build()); |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 893 | lex = fr.savedLexicals; |
| 894 | loc = fr.callerLoc; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 895 | } |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 896 | // TODO(adonovan): simplify by fixing the callstack's off-by-one problem. |
| 897 | // We won't need to pass in 'loc' nor add a fake <top level> frame, nor |
| 898 | // suffer a loop-carried dependence. |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 899 | frameListBuilder.add( |
| 900 | DebugFrame.builder() |
| 901 | .setGlobalBindings(ImmutableMap.copyOf(getGlobals().getTransitiveBindings())) |
| 902 | .setFunctionName("<top level>") |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 903 | .setLocation(loc) |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 904 | .build()); |
| 905 | |
| 906 | return frameListBuilder.build(); |
| 907 | } |
| 908 | |
Googler | 5910093 | 2019-09-12 07:24:20 -0700 | [diff] [blame] | 909 | /** |
| 910 | * Given a requested stepping behavior, returns a predicate over the context that tells the |
| 911 | * debugger when to pause. (Debugger API) |
| 912 | * |
| 913 | * <p>The predicate will return true if we are at the next statement where execution should pause, |
| 914 | * and it will return false if we are not yet at that statement. No guarantee is made about the |
| 915 | * predicate's return value after we have reached the desired statement. |
| 916 | * |
| 917 | * <p>A null return value indicates that no further pausing should occur. |
| 918 | */ |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 919 | @Nullable |
| 920 | public ReadyToPause stepControl(Stepping stepping) { |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 921 | final int depth = callstack.size(); |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 922 | switch (stepping) { |
| 923 | case NONE: |
| 924 | return null; |
| 925 | case INTO: |
| 926 | // pause at the very next statement |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 927 | return thread -> true; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 928 | case OVER: |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 929 | return thread -> thread.callstack.size() <= depth; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 930 | case OUT: |
Googler | fe544aa | 2019-12-03 14:05:06 -0800 | [diff] [blame^] | 931 | // if we're at the outermost frame, same as NONE |
| 932 | return depth == 0 ? null : thread -> thread.callstack.size() < depth; |
Googler | 29eafdf | 2018-05-23 12:32:07 -0700 | [diff] [blame] | 933 | } |
| 934 | throw new IllegalArgumentException("Unsupported stepping type: " + stepping); |
| 935 | } |
| 936 | |
Googler | 5910093 | 2019-09-12 07:24:20 -0700 | [diff] [blame] | 937 | /** See stepControl (Debugger API) */ |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 938 | public interface ReadyToPause extends Predicate<StarlarkThread> {} |
Googler | 5910093 | 2019-09-12 07:24:20 -0700 | [diff] [blame] | 939 | |
| 940 | /** |
| 941 | * Describes the stepping behavior that should occur when execution of a thread is continued. |
| 942 | * (Debugger API) |
| 943 | */ |
| 944 | public enum Stepping { |
| 945 | /** Continue execution without stepping. */ |
| 946 | NONE, |
| 947 | /** |
| 948 | * If the thread is paused on a statement that contains a function call, step into that |
| 949 | * function. Otherwise, this is the same as OVER. |
| 950 | */ |
| 951 | INTO, |
| 952 | /** |
| 953 | * Step over the current statement and any functions that it may call, stopping at the next |
| 954 | * statement in the same frame. If no more statements are available in the current frame, same |
| 955 | * as OUT. |
| 956 | */ |
| 957 | OVER, |
| 958 | /** |
| 959 | * Continue execution until the current frame has been exited and then pause. If we are |
| 960 | * currently in the outer-most frame, same as NONE. |
| 961 | */ |
| 962 | OUT, |
| 963 | } |
| 964 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 965 | @Override |
| 966 | public int hashCode() { |
| 967 | throw new UnsupportedOperationException(); // avoid nondeterminism |
| 968 | } |
| 969 | |
| 970 | @Override |
| 971 | public boolean equals(Object that) { |
| 972 | throw new UnsupportedOperationException(); |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public String toString() { |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 977 | return String.format("<StarlarkThread%s>", mutability()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /** |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 981 | * An Exception thrown when an attempt is made to import a symbol from a file that was not |
| 982 | * properly loaded. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 983 | */ |
Googler | 0c8f22e | 2019-08-28 07:10:45 -0700 | [diff] [blame] | 984 | // TODO(adonovan): replace with plain EvalException. |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 985 | static class LoadFailedException extends Exception { |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 986 | LoadFailedException(String importString) { |
laurentlb | caafe30 | 2018-08-28 10:24:31 -0700 | [diff] [blame] | 987 | super( |
| 988 | String.format( |
| 989 | "file '%s' was not correctly loaded. " |
| 990 | + "Make sure the 'load' statement appears in the global scope in your file", |
| 991 | importString)); |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Laurent Le Brun | a2501d5 | 2017-01-02 15:42:19 +0000 | [diff] [blame] | 994 | LoadFailedException(String importString, String symbolString, Iterable<String> allKeys) { |
| 995 | super( |
| 996 | String.format( |
| 997 | "file '%s' does not contain symbol '%s'%s", |
| 998 | importString, symbolString, SpellChecker.didYouMean(symbolString, allKeys))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1002 | void importSymbol(String importString, Identifier symbol, String nameInLoadedFile) |
Vladimir Moskva | 4994cb3 | 2016-08-10 15:44:31 +0000 | [diff] [blame] | 1003 | throws LoadFailedException { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1004 | Preconditions.checkState(isGlobal()); // loading is only allowed at global scope. |
| 1005 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1006 | if (!importedExtensions.containsKey(importString)) { |
| 1007 | throw new LoadFailedException(importString); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1008 | } |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1009 | |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1010 | Extension ext = importedExtensions.get(importString); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1011 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 1012 | Map<String, Object> bindings = ext.getBindings(); |
| 1013 | if (!bindings.containsKey(nameInLoadedFile)) { |
| 1014 | throw new LoadFailedException(importString, nameInLoadedFile, bindings.keySet()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
brandjon | 357ed7a | 2017-05-09 11:37:22 -0400 | [diff] [blame] | 1017 | Object value = bindings.get(nameInLoadedFile); |
Florian Weikert | 9d659ad | 2015-07-23 14:44:36 +0000 | [diff] [blame] | 1018 | |
Googler | d54a499 | 2019-10-18 12:35:06 -0700 | [diff] [blame] | 1019 | update(symbol.getName(), value); |
Florian Weikert | 6a66339 | 2015-09-02 14:04:33 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
brandjon | 73e768e | 2018-02-13 12:26:52 -0800 | [diff] [blame] | 1022 | /** |
| 1023 | * Computes a deterministic hash for the given base hash code and extension map (the map's order |
| 1024 | * does not matter). |
| 1025 | */ |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1026 | private static String computeTransitiveContentHashCode( |
| 1027 | @Nullable String baseHashCode, Map<String, Extension> importedExtensions) { |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1028 | // Calculate a new hash from the hash of the loaded Extension-s. |
| 1029 | Fingerprint fingerprint = new Fingerprint(); |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1030 | if (baseHashCode != null) { |
| 1031 | fingerprint.addString(Preconditions.checkNotNull(baseHashCode)); |
| 1032 | } |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 1033 | TreeSet<String> importStrings = new TreeSet<>(importedExtensions.keySet()); |
| 1034 | for (String importString : importStrings) { |
| 1035 | fingerprint.addString(importedExtensions.get(importString).getTransitiveContentHashCode()); |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1036 | } |
| 1037 | return fingerprint.hexDigestAndReset(); |
Janak Ramakrishnan | b6e33bc | 2015-09-06 21:05:23 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1040 | /** |
Googler | a3421e2 | 2019-09-26 06:48:32 -0700 | [diff] [blame] | 1041 | * Returns a hash code calculated from the hash code of this StarlarkThread and the transitive |
| 1042 | * closure of other StarlarkThreads it loads. |
Janak Ramakrishnan | 9b12f9c | 2016-05-20 16:33:02 +0000 | [diff] [blame] | 1043 | */ |
| 1044 | public String getTransitiveContentHashCode() { |
| 1045 | return transitiveHashCode; |
| 1046 | } |
Francois-Rene Rideau | 89312fb | 2015-09-10 18:53:03 +0000 | [diff] [blame] | 1047 | |
Googler | 6901c79 | 2019-11-07 18:01:49 -0800 | [diff] [blame] | 1048 | // legacy for copybara; to be inlined and deleted in Nov 2019. |
| 1049 | public static final Module SKYLARK = Module.createForBuiltins(Starlark.UNIVERSE); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1050 | } |