| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Maps; |
| 22 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.Reference; |
| 23 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceCollection; |
| 24 |
import com.google.javascript.jscomp.ReferenceCollectingCallback.ReferenceMap; |
| 25 |
import com.google.javascript.jscomp.Scope.Var; |
| 26 |
import com.google.javascript.rhino.InputId; |
| 27 |
import com.google.javascript.rhino.Node; |
| 28 |
import java.util.List; |
| 29 |
import java.util.Map; |
| 30 |
import java.util.Map.Entry; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class GlobalVarReferenceMap implements ReferenceMap { |
| 43 |
|
| 44 |
private Map<String, ReferenceCollection> refMap = null; |
| 45 |
|
| 46 |
private final Map<InputId, Integer> inputOrder; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
GlobalVarReferenceMap(List<CompilerInput> inputs, List<CompilerInput> externs) { |
| 52 |
inputOrder = Maps.newHashMap(); |
| 53 |
int ind = 0; |
| 54 |
for (CompilerInput extern : externs) { |
| 55 |
inputOrder.put(extern.getInputId(), ind); |
| 56 |
ind++; |
| 57 |
} |
| 58 |
for (CompilerInput input : inputs) { |
| 59 |
inputOrder.put(input.getInputId(), ind); |
| 60 |
ind++; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public ReferenceCollection getReferences(Var var) { |
| 66 |
if (!var.isGlobal()) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
return refMap.get(var.getName()); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private void resetGlobalVarReferences( |
| 79 |
Map<Var, ReferenceCollection> globalRefMap) { |
| 80 |
refMap = Maps.newHashMap(); |
| 81 |
for (Entry<Var, ReferenceCollection> entry : globalRefMap.entrySet()) { |
| 82 |
Var var = entry.getKey(); |
| 83 |
if (var.isGlobal()) { |
| 84 |
refMap.put(var.getName(), entry.getValue()); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
void updateGlobalVarReferences(Map<Var, ReferenceCollection> |
| 101 |
refMapPatch, Node root) { |
| 102 |
if (refMap == null || !root.isScript()) { |
| 103 |
resetGlobalVarReferences(refMapPatch); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
InputId inputId = root.getInputId(); |
| 108 |
Preconditions.checkNotNull(inputId); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
removeScriptReferences(inputId); |
| 113 |
for (Entry<Var, ReferenceCollection> entry : refMapPatch.entrySet()) { |
| 114 |
Var var = entry.getKey(); |
| 115 |
if (var.isGlobal()) { |
| 116 |
replaceReferences(var.getName(), inputId, entry.getValue()); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private void removeScriptReferences(InputId inputId) { |
| 122 |
Preconditions.checkNotNull(inputId); |
| 123 |
|
| 124 |
if (!inputOrder.containsKey(inputId)) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
for (ReferenceCollection collection : refMap.values()) { |
| 130 |
if (collection == null) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
List<Reference> oldRefs = collection.references; |
| 134 |
SourceRefRange range = findSourceRefRange(oldRefs, inputId); |
| 135 |
List<Reference> newRefs = Lists.newArrayList(range.refsBefore()); |
| 136 |
newRefs.addAll(range.refsAfter()); |
| 137 |
collection.references = newRefs; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private void replaceReferences(String varName, InputId inputId, |
| 142 |
ReferenceCollection newSourceCollection) { |
| 143 |
ReferenceCollection combined = new ReferenceCollection(); |
| 144 |
List<Reference> combinedRefs = combined.references; |
| 145 |
ReferenceCollection oldCollection = refMap.get(varName); |
| 146 |
refMap.put(varName, combined); |
| 147 |
if (oldCollection == null) { |
| 148 |
combinedRefs.addAll(newSourceCollection.references); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
SourceRefRange range = findSourceRefRange(oldCollection.references, |
| 153 |
inputId); |
| 154 |
combinedRefs.addAll(range.refsBefore()); |
| 155 |
combinedRefs.addAll(newSourceCollection.references); |
| 156 |
combinedRefs.addAll(range.refsAfter()); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private SourceRefRange findSourceRefRange(List<Reference> refList, |
| 165 |
InputId inputId) { |
| 166 |
Preconditions.checkNotNull(inputId); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
int lastBefore = -1; |
| 171 |
int firstAfter = refList.size(); |
| 172 |
int index = 0; |
| 173 |
|
| 174 |
Preconditions.checkState(inputOrder.containsKey(inputId), inputId.getIdName()); |
| 175 |
int sourceInputOrder = inputOrder.get(inputId); |
| 176 |
for (Reference ref : refList) { |
| 177 |
Preconditions.checkNotNull(ref.getInputId()); |
| 178 |
int order = inputOrder.get(ref.getInputId()); |
| 179 |
if (order < sourceInputOrder) { |
| 180 |
lastBefore = index; |
| 181 |
} else if (order > sourceInputOrder) { |
| 182 |
firstAfter = index; |
| 183 |
break; |
| 184 |
} |
| 185 |
index++; |
| 186 |
} |
| 187 |
return new SourceRefRange(refList, lastBefore, firstAfter); |
| 188 |
} |
| 189 |
|
| 190 |
private static class SourceRefRange { |
| 191 |
private final int lastBefore; |
| 192 |
private final int firstAfter; |
| 193 |
private final List<Reference> refList; |
| 194 |
|
| 195 |
SourceRefRange(List<Reference> refList, int lastBefore, |
| 196 |
int firstAfter) { |
| 197 |
this.lastBefore = Math.max(lastBefore, -1); |
| 198 |
this.firstAfter = Math.min(firstAfter, refList.size()); |
| 199 |
this.refList = refList; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
List<Reference> refsBefore() { |
| 204 |
return refList.subList(0, lastBefore + 1); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
List<Reference> refsAfter() { |
| 209 |
return refList.subList(firstAfter, refList.size()); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public void updateReferencesWithGlobalScope(Scope globalScope) { |
| 218 |
for (ReferenceCollection collection : refMap.values()) { |
| 219 |
List<Reference> newRefs = |
| 220 |
Lists.newArrayListWithCapacity(collection.references.size()); |
| 221 |
for (Reference ref : collection.references) { |
| 222 |
if (ref.getScope() != globalScope) { |
| 223 |
newRefs.add(ref.cloneWithNewScope(globalScope)); |
| 224 |
} else { |
| 225 |
newRefs.add(ref); |
| 226 |
} |
| 227 |
} |
| 228 |
collection.references = newRefs; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static class GlobalVarRefCleanupPass implements HotSwapCompilerPass { |
| 240 |
|
| 241 |
private final AbstractCompiler compiler; |
| 242 |
|
| 243 |
public GlobalVarRefCleanupPass(AbstractCompiler compiler) { |
| 244 |
this.compiler = compiler; |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public void hotSwapScript(Node scriptRoot, Node originalRoot) { |
| 249 |
GlobalVarReferenceMap refMap = compiler.getGlobalVarReferences(); |
| 250 |
if (refMap != null) { |
| 251 |
refMap.updateReferencesWithGlobalScope(compiler.getTopScope()); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public void process(Node externs, Node root) { |
| 257 |
|
| 258 |
} |
| 259 |
} |
| 260 |
} |