| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.jsonml; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.javascript.jscomp.CheckLevel; |
| 23 |
import com.google.javascript.jscomp.Compiler; |
| 24 |
import com.google.javascript.jscomp.CompilerInput; |
| 25 |
import com.google.javascript.jscomp.CompilerOptions; |
| 26 |
import com.google.javascript.jscomp.CompilerOptions.Reach; |
| 27 |
import com.google.javascript.jscomp.JSError; |
| 28 |
import com.google.javascript.jscomp.JSModule; |
| 29 |
import com.google.javascript.jscomp.Result; |
| 30 |
import com.google.javascript.jscomp.SourceFile; |
| 31 |
import com.google.javascript.jscomp.VariableRenamingPolicy; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class SecureCompiler { |
| 54 |
|
| 55 |
private static final String COMPILATION_UNCOMPLETED_MSG = |
| 56 |
"No compilation has been completed yet."; |
| 57 |
|
| 58 |
private static final String COMPILATION_UNSUCCESSFUL_MSG = |
| 59 |
"The last compilation was not successful."; |
| 60 |
|
| 61 |
private static final String COMPILATION_ALREADY_COMPLETED_MSG = |
| 62 |
"This instance has already compiled one source."; |
| 63 |
|
| 64 |
private Compiler compiler; |
| 65 |
private CompilerOptions options; |
| 66 |
private JsonMLAst sourceAst; |
| 67 |
|
| 68 |
|
| 69 |
private Report report; |
| 70 |
|
| 71 |
public SecureCompiler() { |
| 72 |
compiler = new Compiler(); |
| 73 |
options = getSecureCompilerOptions(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public JsonML getJsonML() { |
| 80 |
Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG); |
| 81 |
Preconditions.checkState(report.success, COMPILATION_UNSUCCESSFUL_MSG); |
| 82 |
return sourceAst.convertToJsonML(); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public String getString() { |
| 89 |
Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG); |
| 90 |
Preconditions.checkState(report.success, COMPILATION_UNSUCCESSFUL_MSG); |
| 91 |
return compiler.toSource(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public Report getReport() { |
| 98 |
Preconditions.checkState(report != null, COMPILATION_UNCOMPLETED_MSG); |
| 99 |
return report; |
| 100 |
} |
| 101 |
|
| 102 |
public void compile(JsonML source) { |
| 103 |
if (report != null) { |
| 104 |
throw new IllegalStateException(COMPILATION_ALREADY_COMPLETED_MSG); |
| 105 |
} |
| 106 |
|
| 107 |
sourceAst = new JsonMLAst(source); |
| 108 |
|
| 109 |
CompilerInput input = new CompilerInput( |
| 110 |
sourceAst, "[[jsonmlsource]]", false); |
| 111 |
|
| 112 |
JSModule module = new JSModule("[[jsonmlmodule]]"); |
| 113 |
module.add(input); |
| 114 |
|
| 115 |
Result result = compiler.compileModules( |
| 116 |
ImmutableList.<SourceFile>of(), |
| 117 |
ImmutableList.of(module), |
| 118 |
options); |
| 119 |
|
| 120 |
report = generateReport(result); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
private CompilerOptions getSecureCompilerOptions() { |
| 130 |
CompilerOptions options = new CompilerOptions(); |
| 131 |
|
| 132 |
options.variableRenaming = VariableRenamingPolicy.OFF; |
| 133 |
options.setInlineVariables(Reach.LOCAL_ONLY); |
| 134 |
options.inlineLocalFunctions = true; |
| 135 |
options.checkGlobalThisLevel = CheckLevel.OFF; |
| 136 |
options.coalesceVariableNames = true; |
| 137 |
options.deadAssignmentElimination = true; |
| 138 |
options.collapseVariableDeclarations = true; |
| 139 |
options.convertToDottedProperties = true; |
| 140 |
options.labelRenaming = true; |
| 141 |
options.removeDeadCode = true; |
| 142 |
options.optimizeArgumentsArray = true; |
| 143 |
options.removeUnusedVars = false; |
| 144 |
options.removeUnusedLocalVars = true; |
| 145 |
|
| 146 |
return options; |
| 147 |
} |
| 148 |
|
| 149 |
public void enableFoldConstant() { |
| 150 |
options.foldConstants = true; |
| 151 |
} |
| 152 |
|
| 153 |
Report generateReport(Result result) { |
| 154 |
|
| 155 |
if (result == null) { |
| 156 |
return null; |
| 157 |
} |
| 158 |
|
| 159 |
ArrayList<JsonMLError> errors = Lists.newArrayList(); |
| 160 |
for (JSError error : result.errors) { |
| 161 |
errors.add(JsonMLError.make(error, sourceAst)); |
| 162 |
} |
| 163 |
|
| 164 |
ArrayList<JsonMLError> warnings = Lists.newArrayList(); |
| 165 |
for (JSError warning : result.warnings) { |
| 166 |
warnings.add(JsonMLError.make(warning, sourceAst)); |
| 167 |
} |
| 168 |
|
| 169 |
return new Report( |
| 170 |
errors.toArray(new JsonMLError[0]), |
| 171 |
warnings.toArray(new JsonMLError[0])); |
| 172 |
} |
| 173 |
|
| 174 |
public class Report { |
| 175 |
private final boolean success; |
| 176 |
private final JsonMLError[] errors; |
| 177 |
private final JsonMLError[] warnings; |
| 178 |
|
| 179 |
private Report(JsonMLError[] errors, JsonMLError[] warnings) { |
| 180 |
this.success = errors.length == 0; |
| 181 |
this.errors = errors; |
| 182 |
this.warnings = warnings; |
| 183 |
} |
| 184 |
|
| 185 |
public boolean isSuccessful() { |
| 186 |
return success; |
| 187 |
} |
| 188 |
|
| 189 |
public JsonMLError[] getErrors() { |
| 190 |
return errors; |
| 191 |
} |
| 192 |
|
| 193 |
public JsonMLError[] getWarnings() { |
| 194 |
return warnings; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
} |