| 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.javascript.jscomp.AnalyzePrototypeProperties.NameInfo; |
| 20 |
import com.google.javascript.jscomp.AnalyzePrototypeProperties.Property; |
| 21 |
import com.google.javascript.jscomp.AnalyzePrototypeProperties.Symbol; |
| 22 |
import com.google.javascript.rhino.IR; |
| 23 |
import com.google.javascript.rhino.Node; |
| 24 |
import java.io.Serializable; |
| 25 |
import java.util.Collection; |
| 26 |
import java.util.Iterator; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class CrossModuleMethodMotion implements CompilerPass { |
| 34 |
|
| 35 |
|
| 36 |
static final DiagnosticType NULL_COMMON_MODULE_ERROR = DiagnosticType.error( |
| 37 |
"JSC_INTERNAL_ERROR_MODULE_DEPEND", |
| 38 |
"null deepest common module"); |
| 39 |
|
| 40 |
private final AbstractCompiler compiler; |
| 41 |
private final IdGenerator idGenerator; |
| 42 |
private final AnalyzePrototypeProperties analyzer; |
| 43 |
private final JSModuleGraph moduleGraph; |
| 44 |
|
| 45 |
static final String STUB_METHOD_NAME = "JSCompiler_stubMethod"; |
| 46 |
static final String UNSTUB_METHOD_NAME = "JSCompiler_unstubMethod"; |
| 47 |
|
| 48 |
|
| 49 |
static final String STUB_DECLARATIONS = |
| 50 |
"var JSCompiler_stubMap = [];" + |
| 51 |
"function JSCompiler_stubMethod(JSCompiler_stubMethod_id) {" + |
| 52 |
" return function() {" + |
| 53 |
" return JSCompiler_stubMap[JSCompiler_stubMethod_id].apply(" + |
| 54 |
" this, arguments);" + |
| 55 |
" };" + |
| 56 |
"}" + |
| 57 |
"function JSCompiler_unstubMethod(" + |
| 58 |
" JSCompiler_unstubMethod_id, JSCompiler_unstubMethod_body) {" + |
| 59 |
" return JSCompiler_stubMap[JSCompiler_unstubMethod_id] = " + |
| 60 |
" JSCompiler_unstubMethod_body;" + |
| 61 |
"}"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
CrossModuleMethodMotion(AbstractCompiler compiler, IdGenerator idGenerator, |
| 71 |
boolean canModifyExterns) { |
| 72 |
this.compiler = compiler; |
| 73 |
this.idGenerator = idGenerator; |
| 74 |
this.moduleGraph = compiler.getModuleGraph(); |
| 75 |
this.analyzer = new AnalyzePrototypeProperties(compiler, moduleGraph, |
| 76 |
canModifyExterns, false); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void process(Node externRoot, Node root) { |
| 81 |
|
| 82 |
|
| 83 |
if (moduleGraph != null && moduleGraph.getModuleCount() > 1) { |
| 84 |
analyzer.process(externRoot, root); |
| 85 |
moveMethods(analyzer.getAllNameInfo()); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
private void moveMethods(Collection<NameInfo> allNameInfo) { |
| 93 |
boolean hasStubDeclaration = idGenerator.hasGeneratedAnyIds(); |
| 94 |
for (NameInfo nameInfo : allNameInfo) { |
| 95 |
if (!nameInfo.isReferenced()) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if (nameInfo.readsClosureVariables()) { |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
JSModule deepestCommonModuleRef = nameInfo.getDeepestCommonModuleRef(); |
| 107 |
if (deepestCommonModuleRef == null) { |
| 108 |
compiler.report(JSError.make(NULL_COMMON_MODULE_ERROR)); |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
Iterator<Symbol> declarations = |
| 113 |
nameInfo.getDeclarations().descendingIterator(); |
| 114 |
while (declarations.hasNext()) { |
| 115 |
Symbol symbol = declarations.next(); |
| 116 |
if (!(symbol instanceof Property)) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
Property prop = (Property) symbol; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if (prop.getRootVar() == null || !prop.getRootVar().isGlobal()) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
Node value = prop.getValue(); |
| 142 |
if (moduleGraph.dependsOn(deepestCommonModuleRef, prop.getModule()) && |
| 143 |
value.isFunction()) { |
| 144 |
Node valueParent = value.getParent(); |
| 145 |
if (valueParent.isGetterDef() |
| 146 |
|| valueParent.isSetterDef()) { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
continue; |
| 151 |
} |
| 152 |
Node proto = prop.getPrototype(); |
| 153 |
int stubId = idGenerator.newId(); |
| 154 |
|
| 155 |
|
| 156 |
Node stubCall = IR.call( |
| 157 |
IR.name(STUB_METHOD_NAME), |
| 158 |
IR.number(stubId)) |
| 159 |
.copyInformationFromForTree(value); |
| 160 |
stubCall.putBooleanProp(Node.FREE_CALL, true); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
valueParent.replaceChild(value, stubCall); |
| 165 |
|
| 166 |
|
| 167 |
Node unstubParent = compiler.getNodeForCodeInsertion( |
| 168 |
deepestCommonModuleRef); |
| 169 |
Node unstubCall = IR.call( |
| 170 |
IR.name(UNSTUB_METHOD_NAME), |
| 171 |
IR.number(stubId), |
| 172 |
value); |
| 173 |
unstubCall.putBooleanProp(Node.FREE_CALL, true); |
| 174 |
unstubParent.addChildToFront( |
| 175 |
|
| 176 |
IR.exprResult( |
| 177 |
IR.assign( |
| 178 |
IR.getprop( |
| 179 |
proto.cloneTree(), |
| 180 |
IR.string(nameInfo.name)), |
| 181 |
unstubCall)) |
| 182 |
.copyInformationFromForTree(value)); |
| 183 |
|
| 184 |
compiler.reportCodeChange(); |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if (!hasStubDeclaration && idGenerator.hasGeneratedAnyIds()) { |
| 190 |
|
| 191 |
Node declarations = compiler.parseSyntheticCode(STUB_DECLARATIONS); |
| 192 |
compiler.getNodeForCodeInsertion(null).addChildrenToFront( |
| 193 |
declarations.removeChildren()); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
static class IdGenerator implements Serializable { |
| 198 |
private static final long serialVersionUID = 0L; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private int currentId = 0; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
boolean hasGeneratedAnyIds() { |
| 210 |
return currentId != 0; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
int newId() { |
| 217 |
return currentId++; |
| 218 |
} |
| 219 |
} |
| 220 |
} |